1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
|
/****************************************************************************
*
* Program: HTTP plugin for Nagios
* License: GPL
*
* License Information:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****************************************************************************/
/****************************************************************************
*
* check_http is derived from the original check_http provided by
* Ethan Galstad/Karl DeBisschop
*
* This provides some additional functionality including:
* - check server certificate against supplied hostname (Host: header) if any
* - check server certificate against local CA certificates (as browsers do)
* - authenticate with client certificate (and optional passphrase)
* - specify HTTP returncodes to return a status of WARNING or OK instead of
* CRITICAL (only global for 3xx or 4xx errors)
* - check only against HTTP status line and exit immediately if not matched
*
*****************************************************************************/
const char *progname = "check_http";
#define REVISION "$Revision: 1.1 $"
#define CVSREVISION "1.24"
#define COPYRIGHT "2003"
#define AUTHORS "Fabian Pehla"
#define EMAIL "fabian@pehla.de"
#include "config.h"
#include "common.h"
#include "netutils.h"
#include "utils.h"
#define HELP_TXT_SUMMARY "\
This plugin tests the HTTP service on the specified host. It can test\n\
normal (http) and secure (https) servers, follow redirects, search for\n\
strings and regular expressions, check connection times, and report on\n\
certificate expiration times.\n"
#define HELP_TXT_OPTIONS "\
-H <virtual host> -I <ip address> [-p <port>] [-u <uri>]\n\
[-w <warn time>] [-c <critical time>] [-t <timeout>]\n\
[-S] [-C <days>] [-a <basic auth>] [-A <certificate file>]\n\
[-Z <ca certificate file>] [-e <expect>] [-E <expect only>]\n\
[-s <string>] [-r <regex>] [-R <regex case insensitive>]\n\
[-f (ok|warn|critical|follow)] [-g (ok|warn|critical)]\n"
#define HELP_TXT_LONGOPTIONS "\
-H, --hostname=<virtual host>\n\
FQDN host name argument for use in HTTP Host:-Header (virtual host)\n\
If used together wich the -S option, the server certificate will\n\
be checked against this hostname\n\
-I, --ip-address=<address>\n\
IP address or hostname for TCP connect (use IP to avoid DNS lookup)\n\
-p, --port=<port>\n\
Port number (default: %d)\n\
-u, --url-path=<uri>\n\
URL to request from host (default: %s)\n\
-S, --ssl\n\
Use SSL (default port: %d)\n\
-C, --server-certificate-days=<days>\n\
Minimum number of days a server certificate must be valid\n\
No other check can be combined with this option\n\
-a, --basic-auth=<username:password>\n\
Colon separated username and password for basic authentication\n\
-A, --client-certificate=<certificate file>\n\
File containing X509 client certificate and key\n\
-K, --passphrase=<passphrase>\n\
Passphrase for the client certificate key\n\
This option can only be used in combination with the -A option\n\
-Z, --ca-certificate=<certificate file>\n\
File containing certificates of trusted CAs\n\
The server certificate will be checked against these CAs\n\
-e, --http-expect=<expect string>\n\
String to expect in HTTP response line (Default: %s)\n\
-E, --http-expect-only=<expect only string>\n\
String to expect in HTTP response line\n\
No other checks are made, this either matches the response\n\
or exits immediately\n\
-s, --content-string=<string>\n\
String to expect in content\n\
-r, --content-ereg=<regex>\n\
Regular expression to expect in content\n\
-R, --content-eregi=<regex case insensitive>\n\
Case insensitive regular expression to expect in content\n\
-f, --onredirect=(ok|warning|critical|follow)\n\
Follow a redirect (3xx) or return with a user defined state\n\
Default: OK\n\
-g, --onerror=(ok|warning|critical)\n\
Status to return on a client error (4xx)\n\
-m, --min=INTEGER\n\
Minimum page size required (bytes)\n\
-t, --timeout=<timeout>\n\
Seconds before connection times out (default: %d)\n\
-c, --critical=<critical time>\n\
Response time to result in critical status (seconds)\n\
-w, --warning=<warn time>\n\
Response time to result in warning status (seconds)\n\
-V, --version\n\
Print version information\n\
-v, --verbose\n\
Show details for command-line debugging (do not use with nagios server)\n\
-h, --help\n\
Print detailed help screen\n"
#define HTTP_PORT 80
#define DEFAULT_HTTP_URL_PATH "/"
#define DEFAULT_HTTP_EXPECT "HTTP/1."
#define DEFAULT_HTTP_METHOD "GET"
#define DEFAULT_HTTP_REDIRECT_STATE STATE_OK
#define DEFAULT_HTTP_CLIENT_ERROR_STATE STATE_WARNING
#define HTTP_TEMPLATE_REQUEST "%s%s %s HTTP/1.0\r\n"
#define HTTP_TEMPLATE_HEADER_USERAGENT "%sUser-Agent: %s/%s (nagios-plugins %s)\r\n"
#define HTTP_TEMPLATE_HEADER_HOST "%sHost: %s\r\n"
#define HTTP_TEMPLATE_HEADER_AUTH "%sAuthorization: Basic %s\r\n"
/* fill in printf with protocol_text(use_ssl), state_text(state), page->status, elapsed_time */
#define RESULT_TEMPLATE_STATUS_RESPONSE_TIME "%s %s: %s - %7.3f seconds response time|time=%7.3f\n"
#define RESULT_TEMPLATE_RESPONSE_TIME "%s %s: %7.3f seconds response time|time=%7.3f\n"
#ifdef HAVE_SSL
#ifdef HAVE_SSL_H
#include <rsa.h>
#include <crypto.h>
#include <x509.h>
#include <pem.h>
#include <ssl.h>
#include <err.h>
#include <rand.h>
#endif
#ifdef HAVE_OPENSSL_SSL_H
#include <openssl/rsa.h>
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#endif
#define HTTPS_PORT 443
#endif
#ifdef HAVE_REGEX_H
#include <regex.h>
#define REGEX_REGS 2
#define MAX_REGEX_SIZE 256
#endif
#define chk_protocol(protocol) ( strstr( protocol, "https" ) ? TRUE : FALSE );
#define protocol_std_port(use_ssl) ( use_ssl ? HTTPS_PORT : HTTP_PORT );
#define protocol_text(use_ssl) ( use_ssl ? "HTTPS" : "HTTP" )
#define MAX_IPV4_HOSTLENGTH 64
#define HTTP_HEADER_LOCATION_MATCH "%*[Ll]%*[Oo]%*[Cc]%*[Aa]%*[Tt]%*[Ii]%*[Oo]%*[Nn]: "
#define HTTP_HEADER_PROTOCOL_MATCH "%[HTPShtps]://"
#define HTTP_HEADER_HOSTNAME_MATCH "%[a-zA-Z0-9.-]"
#define HTTP_HEADER_PORT_MATCH ":%[0-9]"
#define HTTP_HEADER_URL_PATH_MATCH "%[/a-zA-Z0-9._-=@,]"
/*
************************************************************************
* GLOBAL VARIABLE/POINTER DEFINITIONS *
************************************************************************
*/
/* misc variables */
int verbose = FALSE;
/* time thresholds to determine exit code */
int use_warning_interval = FALSE;
double warning_interval = 0;
int use_critical_interval = FALSE;
double critical_interval = 0;
double elapsed_time = 0;
struct timeval start_tv;
/* variables concerning the server host */
int use_server_hostname = FALSE;
char *server_hostname = ""; // hostname for use in HTTPs Host: header
char *server_host = ""; // hostname or ip address for tcp connect
int use_server_port = FALSE;
int server_port = HTTP_PORT;
int use_basic_auth = FALSE;
char basic_auth[MAX_INPUT_BUFFER] = "";
/* variables concerning server responce */
struct pageref {
char *content;
size_t size;
char *status;
char *header;
char *body;
};
/* variables concerning ssl connections */
int use_ssl = FALSE;
#ifdef HAVE_SSL
int server_certificate_min_days_valid = 0;
int check_server_certificate = FALSE;
X509 *server_certificate; // structure containing server certificate
int use_client_certificate = FALSE;
char *client_certificate_file = NULL;
int use_client_certificate_passphrase = FALSE;
char *client_certificate_passphrase = NULL;
int use_ca_certificate = FALSE;
char *ca_certificate_file = NULL;
BIO *bio_err = 0; // error write context
#endif
/* variables concerning check behaviour */
char *http_method = DEFAULT_HTTP_METHOD;
char *http_url_path = "";
int use_http_post_data = FALSE;
char *http_post_data = "";
int use_min_content_length = FALSE;
int min_content_length = 0;
int use_http_expect_only = FALSE;
char http_expect[MAX_INPUT_BUFFER] = DEFAULT_HTTP_EXPECT;
int check_content_string = FALSE;
char content_string[MAX_INPUT_BUFFER] = "";
int http_redirect_state = DEFAULT_HTTP_REDIRECT_STATE;
int http_client_error_state = DEFAULT_HTTP_CLIENT_ERROR_STATE;
#ifdef HAVE_REGEX_H
regex_t regex_preg;
regmatch_t regex_pmatch[REGEX_REGS];
int check_content_regex = FALSE;
char content_regex[MAX_REGEX_SIZE] = "";
int regex_cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE;
int regex_error = 0;
char regex_error_buffer[MAX_INPUT_BUFFER] = "";
#endif
/*
************************************************************************
* FUNCTION PROTOTYPES *
************************************************************************
*/
void print_usage( void );
void print_help( void );
int process_arguments (int, char **);
int http_request( int sock, struct pageref *page);
int parse_http_response( struct pageref *page );
int check_http_response( struct pageref *page );
int check_http_content( struct pageref *page );
int prepare_follow_redirect( struct pageref *page );
static char *base64 (char *bin, int len);
#ifdef HAVE_SSL
int ssl_terminate( int state, char *string );
static int passwd_cb( char *buf, int num, int rwflag, void *userdata );
static void sigpipe_handle( int x );
SSL_CTX * initialize_ssl_ctx( void );
void destroy_ssl_ctx( SSL_CTX *ctx );
int fetch_server_certificate( SSL *ssl );
int check_server_certificate_chain( SSL *ssl );
int check_server_certificate_hostname( void );
int check_server_certificate_expires( void );
int https_request( SSL_CTX *ctx, SSL *ssl, struct pageref *page );
#endif
/*
************************************************************************
* IMPLEMENTATION *
************************************************************************
*/
/*
* main()
*
* PSEUDOCODE OF HOW MAIN IS SUPPOSED TO WORK
*
* process command line arguments including sanity check
* initialize alarm signal handling
* if use_ssl
* build ssl context
* LOOP:
* make tcp connection
* if use_ssl
* make ssl connection
* if use_server_hostname
* check if certificate matches hostname
* if check_server_certificate
* check expiration date of server certificate
* return STATUS
* else
* request http page
* handle ssl rehandshake
* close ssl connection
* else
* request http page
* close tcp connection
* analyze http page
* if follow on redirect
* repeat LOOP
* end of LOOP
* destroy ssl context
*/
int
main (int argc, char **argv)
{
int result = STATE_UNKNOWN;
int sock;
struct pageref page;
#ifdef HAVE_SSL
SSL_CTX *ctx;
SSL *ssl;
BIO *sbio;
#endif
if ( process_arguments(argc, argv) == ERROR )
usage( "check_http: could not parse arguments\n" );
#ifdef HAVE_SSL
/* build SSL context if required:
* a) either we use ssl from the beginning OR
* b) or we follor redirects wich may lead os to a ssl page
*/
if ( use_ssl || ( http_redirect_state == STATE_DEPENDENT ) )
ctx=initialize_ssl_ctx();
#endif
/* Loop around 3xx onredirect=follow */
do {
/*
* initialize alarm signal handling, set socket timeout, start timer
* socket_timeout and socket_timeout_alarm_handler are defined in
* netutils.c
*/
(void) signal( SIGALRM, socket_timeout_alarm_handler );
(void) alarm( socket_timeout );
gettimeofday( &start_tv, NULL );
/* make a tcp connection */
result = my_tcp_connect( server_host, server_port, &sock );
/* result of tcp connect */
if ( result == STATE_OK )
{
#ifdef HAVE_SSL
/* make a ssl connection */
if ( use_ssl ) {
ssl=SSL_new( ctx );
sbio=BIO_new_socket( sock, BIO_NOCLOSE );
SSL_set_bio( ssl, sbio, sbio);
if ( SSL_connect( ssl ) <= 0 )
ssl_terminate( STATE_CRITICAL, "check_http: SSL connect error" );
/* fetch server certificate */
result = fetch_server_certificate( ssl );
/* verify server certificate against CAs */
if ( ( result == STATE_OK ) && use_ca_certificate ) {
result = check_server_certificate_chain( ssl );
}
/* check if certificate matches hostname */
if ( ( result == STATE_OK ) && use_server_hostname ) {
result = check_server_certificate_hostname();
}
if ( result == STATE_OK ) {
/* check server certificate expire date */
if ( check_server_certificate ) {
result = check_server_certificate_expires();
/* OR: perform http request */
} else {
result = https_request( ctx, ssl, (struct pageref *) &page );
}
}
SSL_shutdown( ssl );
SSL_free( ssl );
} else {
#endif
/* HTTP implementation */
result = http_request( sock, (struct pageref *) &page );
#ifdef HAVE_SSL
}
#endif
/* stop timer and calculate elapsed_time */
elapsed_time = delta_time( start_tv );
/* close the tcp connection */
close( sock );
/* reset the alarm */
alarm( 0 );
/* analyze http page */
/* TO DO */
if ( result == STATE_OK )
result = parse_http_response( (struct pageref *) &page );
if ( result == STATE_OK )
result = check_http_response( (struct pageref *) &page );
switch ( result ) {
case STATE_OK:
/* weiter geht's */
result = check_http_content( (struct pageref *) &page );
break;
case STATE_DEPENDENT:
/* try to determine redirect parameters */
result = prepare_follow_redirect( (struct pageref *) &page );
break;
}
} else {
/* some error occured while trying to make a tcp connect */
exit( result );
}
} while ( result == STATE_DEPENDENT ); // end of onredirect loop
/* destroy SSL context */
#ifdef HAVE_SSL
if ( use_ssl || ( http_redirect_state == STATE_DEPENDENT ) )
destroy_ssl_ctx( ctx );
#endif
/* if we ever get to this point, everything went fine */
printf( RESULT_TEMPLATE_STATUS_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( result ),
page.status,
elapsed_time,
elapsed_time );
return result;
}
void
print_help( void )
{
print_revision( progname, REVISION );
printf
( "Copyright (c) %s %s <%s>\n\n%s\n",
COPYRIGHT, AUTHORS, EMAIL, HELP_TXT_SUMMARY );
print_usage();
printf( "NOTE: One or both of -H and -I must be specified\n" );
printf( "\nOptions:\n" HELP_TXT_LONGOPTIONS "\n",
HTTP_PORT, DEFAULT_HTTP_URL_PATH, HTTPS_PORT,
DEFAULT_HTTP_EXPECT, DEFAULT_SOCKET_TIMEOUT );
#ifdef HAVE_SSL
//printf( SSLDESCRIPTION );
#endif
}
void
print_usage( void )
{
printf( "Usage:\n" " %s %s\n"
#ifdef HAVE_GETOPT_H
" %s (-h | --help) for detailed help\n"
" %s (-V | --version) for version information\n",
#else
" %s -h for detailed help\n"
" %s -V for version information\n",
#endif
progname, HELP_TXT_OPTIONS, progname, progname );
}
/*
* process_arguments()
*
* process command line arguments either using getopt_long or getopt
* (parsing long argumants manually)
*/
int
process_arguments( int argc, char **argv )
{
int c, i = 1;
extern char *optarg;
#ifdef HAVE_GETOPT_H
int option_index = 0;
static struct option long_options[] = {
STD_LONG_OPTS,
{"file", required_argument, 0, 'F'},
{"ip-address", required_argument, 0, 'I'},
{"port", required_argument, 0, 'p'},
{"url-path", required_argument, 0, 'u'},
{"post-data", required_argument, 0, 'P'},
{"ssl", no_argument, 0, 'S'},
{"server-certificate-days", required_argument, 0, 'C'},
{"basic-auth", required_argument, 0, 'a'},
{"client-certificate", required_argument, 0, 'A'},
{"passphrase", required_argument, 0, 'K'},
{"ca-certificate", required_argument, 0, 'Z'},
{"http-expect", required_argument, 0, 'e'},
{"http-expect-only", required_argument, 0, 'E'},
{"content-string", required_argument, 0, 's'},
{"content-ereg-linespan", required_argument, 0, 'l'},
{"content-ereg", required_argument, 0, 'r'},
{"content-eregi", required_argument, 0, 'R'},
{"onredirect", required_argument, 0, 'f'},
{"onerror", required_argument, 0, 'g'},
{"min", required_argument, 0, 'm'},
{0, 0, 0, 0}
};
#endif
/* convert commonly used arguments to their equivalent standard options */
for (c = 1; c < argc; c++) {
if ( strcmp( "-to", argv[c]) == 0 )
strcpy( argv[c], "-t" );
if ( strcmp( "-hn", argv[c]) == 0 )
strcpy( argv[c], "-H" );
if ( strcmp( "-wt", argv[c]) == 0 )
strcpy( argv[c], "-w" );
if ( strcmp( "-ct", argv[c]) == 0 )
strcpy( argv[c], "-c" );
}
#define OPTCHARS "Vvht:c:w:H:F:I:p:u:P:SC:a:A:K:Z:e:E:s:r:R:f:g:lm:"
while (1) {
#ifdef HAVE_GETOPT_H
c = getopt_long( argc, argv, OPTCHARS, long_options, &option_index );
#else
c = getopt( argc, argv, OPTCHARS );
#endif
if ( ( c == -1 ) || ( c == EOF ) ) {
break;
}
switch (c) {
case '?': /* usage */
usage2( "unknown argument", optarg );
break;
/* Standard options */
case 'h': /* help */
print_help();
exit( STATE_OK );
break;
case 'V': /* version */
print_revision( progname, REVISION );
exit( STATE_OK );
break;
case 'v': /* verbose */
verbose = TRUE;
break;
case 't': /* timeout period */
if ( !is_intnonneg( optarg ) )
usage2( "timeout interval must be a non-negative integer", optarg );
/* socket_timeout is defined in netutils.h and defaults to
* DEFAULT_SOCKET_TIMEOUT from common.h
*/
socket_timeout = atoi( optarg );
break;
case 'c': /* critical time threshold */
if ( !is_nonnegative( optarg ) )
usage2( "invalid critical threshold", optarg );
critical_interval = strtod( optarg, NULL );
use_critical_interval = TRUE;
break;
case 'w': /* warning time threshold */
if ( !is_nonnegative( optarg ) )
usage2( "invalid warning threshold", optarg );
warning_interval = strtod( optarg, NULL );
use_warning_interval = TRUE;
break;
case 'H': /* Host Name (virtual host) */
/* this rejects FQDNs, so we leave it for now...
*if ( !is_hostname( optarg ) )
* usage2( "invalid hostname", optarg );
*/
asprintf( &server_hostname, "%s", optarg );
use_server_hostname = TRUE;
break;
case 'F': /* File (dummy) */
break;
/* End of standard options */
case 'I': /* Server IP-address or Hostname */
/* this rejects FQDNs, so we leave it for now...
*if ( !is_host( optarg ) )
* usage2( "invalid ip address or hostname", optarg )
*/
asprintf( &server_host, "%s", optarg );
break;
case 'p': /* Server port */
if ( !is_intnonneg( optarg ) )
usage2( "invalid port number", optarg );
server_port = atoi( optarg );
use_server_port = TRUE;
break;
case 'S': /* use SSL */
#ifdef HAVE_SSL
use_ssl = TRUE;
if ( use_server_port == FALSE )
server_port = HTTPS_PORT;
#else
usage( "check_http: invalid option - SSL is not available\n" );
#endif
break;
case 'C': /* Server certificate warning time threshold */
#ifdef HAVE_SSL
if ( !is_intnonneg( optarg ) )
usage2( "invalid certificate expiration period", optarg );
server_certificate_min_days_valid = atoi( optarg );
check_server_certificate = TRUE;
#else
usage( "check_http: invalid option - SSL is not available\n" );
#endif
break;
case 'a': /* basic authorization info */
strncpy( basic_auth, optarg, MAX_INPUT_BUFFER - 1 );
basic_auth[MAX_INPUT_BUFFER - 1] = 0;
use_basic_auth = TRUE;
break;
case 'A': /* client certificate */
#ifdef HAVE_SSL
asprintf( &client_certificate_file, "%s", optarg );
use_client_certificate = TRUE;
#else
usage( "check_http: invalid option - SSL is not available\n" );
#endif
break;
case 'K': /* client certificate passphrase */
#ifdef HAVE_SSL
asprintf( &client_certificate_passphrase, "%s", optarg );
use_client_certificate_passphrase = TRUE;
#else
usage( "check_http: invalid option - SSL is not available\n" );
#endif
case 'Z': /* valid CA certificates */
#ifdef HAVE_SSL
asprintf( &ca_certificate_file, "%s", optarg );
use_ca_certificate = TRUE;
#else
usage( "check_http: invalid option - SSL is not available\n" );
#endif
break;
case 'u': /* URL PATH */
asprintf( &http_url_path, "%s", optarg );
break;
case 'P': /* POST DATA */
asprintf( &http_post_data, "%s", optarg );
use_http_post_data = TRUE;
asprintf( &http_method, "%s", "POST" );
break;
case 'e': /* expected string in first line of HTTP response */
strncpy( http_expect , optarg, MAX_INPUT_BUFFER - 1 );
http_expect[MAX_INPUT_BUFFER - 1] = 0;
break;
case 'E': /* expected string in first line of HTTP response and process no other check*/
strncpy( http_expect , optarg, MAX_INPUT_BUFFER - 1 );
http_expect[MAX_INPUT_BUFFER - 1] = 0;
use_http_expect_only = TRUE;
break;
case 's': /* expected (sub-)string in content */
strncpy( content_string , optarg, MAX_INPUT_BUFFER - 1 );
content_string[MAX_INPUT_BUFFER - 1] = 0;
check_content_string = TRUE;
break;
case 'l': /* regex linespan */
#ifdef HAVE_REGEX_H
regex_cflags &= ~REG_NEWLINE;
#else
usage( "check_http: call for regex which was not a compiled option\n" );
#endif
break;
case 'R': /* expected case insensitive regular expression in content */
#ifdef HAVE_REGEX_H
regex_cflags |= REG_ICASE;
#else
usage( "check_http: call for regex which was not a compiled option\n" );
#endif
case 'r': /* expected regular expression in content */
#ifdef HAVE_REGEX_H
strncpy( content_regex , optarg, MAX_REGEX_SIZE - 1 );
content_regex[MAX_REGEX_SIZE - 1] = 0;
check_content_regex = TRUE;
regex_error = regcomp( ®ex_preg, content_regex, regex_cflags );
if ( regex_error != 0 ) {
regerror( regex_error, ®ex_preg, regex_error_buffer, MAX_INPUT_BUFFER );
printf( "Could Not Compile Regular Expression: %s", regex_error_buffer );
return ERROR;
}
#else
usage( "check_http: call for regex which was not a compiled option\n" );
#endif
break;
case 'f': /* onredirect (3xx errors) */
if ( !strcmp( optarg, "follow" ) )
http_redirect_state = STATE_DEPENDENT;
if ( !strcmp( optarg, "unknown" ) )
http_redirect_state = STATE_UNKNOWN;
if ( !strcmp( optarg, "ok" ) )
http_redirect_state = STATE_OK;
if ( !strcmp( optarg, "warning" ) )
http_redirect_state = STATE_WARNING;
if ( !strcmp( optarg, "critical" ) )
http_redirect_state = STATE_CRITICAL;
break;
case 'g': /* onerror (4xx errors) */
if ( !strcmp( optarg, "unknown" ) )
http_client_error_state = STATE_UNKNOWN;
if ( !strcmp( optarg, "ok" ) )
http_client_error_state = STATE_OK;
if ( !strcmp( optarg, "warning" ) )
http_client_error_state = STATE_WARNING;
if ( !strcmp( optarg, "critical" ) )
http_client_error_state = STATE_CRITICAL;
break;
case 'm': /* min */
if ( !is_intnonneg( optarg ) )
usage2( "invalid page size", optarg );
min_content_length = atoi( optarg );
use_min_content_length = TRUE;
break;
} // end switch
} // end while(1)
c = optind;
/* Sanity checks on supplied command line arguments */
/* 1. if both host and hostname are not defined, try to
* fetch one more argument which is possibly supplied
* without an option
*/
if ( ( strcmp( server_host, "" ) ) && (c < argc) ) {
asprintf( &server_host, "%s", argv[c++] );
}
/* 2. check if another artument is supplied
*/
if ( ( strcmp( server_hostname, "" ) == 0 ) && (c < argc) ) {
asprintf( &server_hostname, "%s", argv[c++] );
}
/* 3. if host is still not defined, just copy hostname,
* which is then guaranteed to be defined by now
*/
if ( strcmp( server_host, "") == 0 ) {
if ( strcmp( server_hostname, "" ) == 0 ) {
usage ("check_http: you must specify a server address or host name\n");
} else {
asprintf( &server_host, "%s", server_hostname );
}
}
/* 4. check if content checks for a string and a regex
* are requested for only one of both is possible at
* a time
*/
if ( check_content_string && check_content_regex )
usage( "check_http: you can only check for string OR regex at a time\n" );
/* 5. check for options which require use_ssl */
if ( check_server_certificate && !use_ssl )
usage( "check_http: you must use -S to check server certificate\n" );
if ( use_client_certificate && !use_ssl )
usage( "check_http: you must use -S to authenticate with a client certificate\n" );
if ( use_ca_certificate && !use_ssl )
usage( "check_http: you must use -S to check server certificate against CA certificates\n" );
/* 6. check for passphrase without client certificate */
if ( use_client_certificate_passphrase && !use_client_certificate )
usage( "check_http: you must supply a client certificate to use a passphrase\n" );
/* Finally set some default values if necessary */
if ( strcmp( http_method, "" ) == 0 )
asprintf( &http_method, "%s", DEFAULT_HTTP_METHOD );
if ( strcmp( http_url_path, "" ) == 0 ) {
asprintf( &http_url_path, "%s", DEFAULT_HTTP_URL_PATH );
}
return TRUE;
}
int
http_request( int sock, struct pageref *page )
{
char *buffer = "";
char recvbuff[MAX_INPUT_BUFFER] = "";
int buffer_len = 0;
int content_len = 0;
size_t sendsize = 0;
size_t recvsize = 0;
char *content = "";
size_t size = 0;
char *basic_auth_encoded = NULL;
asprintf( &buffer, HTTP_TEMPLATE_REQUEST, buffer, http_method, http_url_path );
asprintf( &buffer, HTTP_TEMPLATE_HEADER_USERAGENT, buffer, progname, REVISION, PACKAGE_VERSION );
if ( use_server_hostname ) {
asprintf( &buffer, HTTP_TEMPLATE_HEADER_HOST, buffer, server_hostname );
}
if ( use_basic_auth ) {
basic_auth_encoded = base64( basic_auth, strlen( basic_auth ) );
asprintf( &buffer, HTTP_TEMPLATE_HEADER_AUTH, buffer, basic_auth_encoded );
}
/* either send http POST data */
if ( use_http_post_data ) {
/* based on code written by Chris Henesy <lurker@shadowtech.org> */
asprintf( &buffer, "Content-Type: application/x-www-form-urlencoded\r\n" );
asprintf( &buffer, "Content-Length: %i\r\n\r\n", buffer, content_len );
asprintf( &buffer, "%s%s%s", buffer, http_post_data, "\r\n" );
sendsize = send( sock, buffer, strlen( buffer ), 0 );
if ( sendsize < strlen( buffer ) ) {
printf( "ERROR: Incomplete write\n" );
return STATE_CRITICAL;
}
/* or just a newline */
} else {
asprintf( &buffer, "%s%s", buffer, "\r\n" );
sendsize = send( sock, buffer, strlen( buffer ) , 0 );
if ( sendsize < strlen( buffer ) ) {
printf( "ERROR: Incomplete write\n" );
return STATE_CRITICAL;
}
}
/* read server's response */
do {
recvsize = recv( sock, recvbuff, MAX_INPUT_BUFFER - 1, 0 );
if ( recvsize > (size_t) 0 ) {
recvbuff[recvsize] = '\0';
asprintf( &content, "%s%s", content, recvbuff );
size += recvsize;
}
} while ( recvsize > (size_t) 0 );
asprintf( &page->content, "%s", content );
page->size = size;
/* return a CRITICAL status if we couldn't read any data */
if ( size == (size_t) 0)
ssl_terminate( STATE_CRITICAL, "No data received" );
return STATE_OK;
}
int
parse_http_response( struct pageref *page )
{
char *content = ""; //local copy of struct member
char *status = ""; //local copy of struct member
char *header = ""; //local copy of struct member
size_t len = 0; //temporary used
char *pos = ""; //temporary used
asprintf( &content, "%s", page->content );
/* find status line and null-terminate it */
// copy content to status
status = content;
// find end of status line and copy pointer to pos
content += (size_t) strcspn( content, "\r\n" );
pos = content;
// advance content pointer behind the newline of status line
content += (size_t) strspn( content, "\r\n" );
// null-terminate status line at pos
status[strcspn( status, "\r\n")] = 0;
strip( status );
// copy final status to struct member
page->status = status;
/* find header and null-terminate it */
// copy remaining content to header
header = content;
// loop until line containing only newline is found (end of header)
while ( strcspn( content, "\r\n" ) > 0 ) {
//find end of line and copy pointer to pos
content += (size_t) strcspn( content, "\r\n" );
pos = content;
if ( ( strspn( content, "\r" ) == 1 && strspn( content, "\r\n" ) >= 2 ) ||
( strspn( content, "\n" ) == 1 && strspn( content, "\r\n" ) >= 2 ) )
content += (size_t) 2;
else
content += (size_t) 1;
}
// advance content pointer behind the newline
content += (size_t) strspn( content, "\r\n" );
// null-terminate header at pos
header[pos - header] = 0;
// copy final header to struct member
page->header = header;
// copy remaining content to body
page->body = content;
if ( verbose ) {
printf( "STATUS: %s\n", page->status );
printf( "HEADER: \n%s\n", page->header );
printf( "BODY: \n%s\n", page->body );
}
return STATE_OK;
}
int
check_http_response( struct pageref *page )
{
char *msg = "";
/* check response time befor anything else */
if ( use_critical_interval && ( elapsed_time > critical_interval ) ) {
asprintf( &msg, RESULT_TEMPLATE_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( STATE_CRITICAL ),
elapsed_time,
elapsed_time );
terminate( STATE_CRITICAL, msg );
}
if ( use_warning_interval && ( elapsed_time > warning_interval ) ) {
asprintf( &msg, RESULT_TEMPLATE_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( STATE_WARNING ),
elapsed_time,
elapsed_time );
terminate( STATE_WARNING, msg );
}
/* make sure the status line matches the response we are looking for */
if ( strstr( page->status, http_expect ) ) {
/* The result is only checked against the expected HTTP status line,
so exit immediately after this check */
if ( use_http_expect_only ) {
if ( ( server_port == HTTP_PORT )
#ifdef HAVE_SSL
|| ( server_port == HTTPS_PORT ) )
#else
)
#endif
asprintf( &msg, "Expected HTTP response received from host\n" );
else
asprintf( &msg, "Expected HTTP response received from host on port %d\n", server_port );
terminate( STATE_OK, msg );
}
} else {
if ( ( server_port == HTTP_PORT )
#ifdef HAVE_SSL
|| ( server_port == HTTPS_PORT ) )
#else
)
#endif
asprintf( &msg, "Invalid HTTP response received from host\n" );
else
asprintf( &msg, "Invalid HTTP response received from host on port %d\n", server_port );
terminate( STATE_CRITICAL, msg );
}
/* check the return code */
/* server errors result in a critical state */
if ( strstr( page->status, "500" ) ||
strstr( page->status, "501" ) ||
strstr( page->status, "502" ) ||
strstr( page->status, "503" ) ||
strstr( page->status, "504" ) ||
strstr( page->status, "505" )) {
asprintf( &msg, RESULT_TEMPLATE_STATUS_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( http_client_error_state ),
page->status,
elapsed_time,
elapsed_time );
terminate( STATE_CRITICAL, msg );
}
/* client errors result in a warning state */
if ( strstr( page->status, "400" ) ||
strstr( page->status, "401" ) ||
strstr( page->status, "402" ) ||
strstr( page->status, "403" ) ||
strstr( page->status, "404" ) ||
strstr( page->status, "405" ) ||
strstr( page->status, "406" ) ||
strstr( page->status, "407" ) ||
strstr( page->status, "408" ) ||
strstr( page->status, "409" ) ||
strstr( page->status, "410" ) ||
strstr( page->status, "411" ) ||
strstr( page->status, "412" ) ||
strstr( page->status, "413" ) ||
strstr( page->status, "414" ) ||
strstr( page->status, "415" ) ||
strstr( page->status, "416" ) ||
strstr( page->status, "417" ) ) {
asprintf( &msg, RESULT_TEMPLATE_STATUS_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( http_client_error_state ),
page->status,
elapsed_time,
elapsed_time );
terminate( http_client_error_state, msg );
}
/* check redirected page if specified */
if (strstr( page->status, "300" ) ||
strstr( page->status, "301" ) ||
strstr( page->status, "302" ) ||
strstr( page->status, "303" ) ||
strstr( page->status, "304" ) ||
strstr( page->status, "305" ) ||
strstr( page->status, "306" ) ||
strstr( page->status, "307" ) ) {
if ( http_redirect_state == STATE_DEPENDENT ) {
/* returning STATE_DEPENDENT means follow redirect */
return STATE_DEPENDENT;
} else {
asprintf( &msg, RESULT_TEMPLATE_STATUS_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( http_redirect_state ),
page->status,
elapsed_time,
elapsed_time );
terminate( http_redirect_state, msg );
}
}
return STATE_OK;
}
int
check_http_content( struct pageref *page )
{
char *msg = "";
/* check for string in content */
if ( check_content_string ) {
if ( strstr( page->content, content_string ) ) {
asprintf( &msg, RESULT_TEMPLATE_STATUS_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( STATE_OK ),
page->status,
elapsed_time,
elapsed_time );
terminate( STATE_OK, msg );
} else {
asprintf( &msg, RESULT_TEMPLATE_STATUS_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( STATE_CRITICAL ),
page->status,
elapsed_time,
elapsed_time );
terminate( STATE_CRITICAL, msg );
}
}
#ifdef HAVE_REGEX_H
/* check for regex in content */
if ( check_content_regex ) {
regex_error = regexec( ®ex_preg, page->content, REGEX_REGS, regex_pmatch, 0);
if ( regex_error == 0 ) {
asprintf( &msg, RESULT_TEMPLATE_STATUS_RESPONSE_TIME,
protocol_text( use_ssl ),
state_text( STATE_OK ),
page->status,
elapsed_time,
elapsed_time );
terminate( STATE_OK, msg );
} else {
if ( regex_error == REG_NOMATCH ) {
asprintf( &msg, "%s, %s: regex pattern not found\n",
protocol_text( use_ssl) ,
state_text( STATE_CRITICAL ) );
terminate( STATE_CRITICAL, msg );
} else {
regerror( regex_error, ®ex_preg, regex_error_buffer, MAX_INPUT_BUFFER);
asprintf( &msg, "%s %s: Regex execute Error: %s\n",
protocol_text( use_ssl) ,
state_text( STATE_CRITICAL ),
regex_error_buffer );
terminate( STATE_CRITICAL, msg );
}
}
}
#endif
return STATE_OK;
}
int
prepare_follow_redirect( struct pageref *page )
{
char *header = NULL;
char *msg = "";
char protocol[6];
char hostname[MAX_IPV4_HOSTLENGTH];
char port[6];
char *url_path = NULL;
char *orig_url_path = NULL;
char *orig_url_dirname = NULL;
size_t len = 0;
asprintf( &header, "%s", page->header );
/* restore some default values */
use_http_post_data = FALSE;
asprintf( &http_method, "%s", DEFAULT_HTTP_METHOD );
/* copy url of original request, maybe we need it to compose
absolute url from relative Location: header */
asprintf( &orig_url_path, "%s", http_url_path );
while ( strcspn( header, "\r\n" ) > (size_t) 0 ) {
url_path = realloc( url_path, (size_t) strcspn( header, "\r\n" ) );
if ( url_path == NULL )
terminate( STATE_UNKNOWN, "HTTP UNKNOWN: could not reallocate url_path" );
/* Try to find a Location header combination of METHOD HOSTNAME PORT and PATH */
/* 1. scan for Location: http[s]://hostname:port/path */
if ( sscanf ( header, HTTP_HEADER_LOCATION_MATCH HTTP_HEADER_PROTOCOL_MATCH HTTP_HEADER_HOSTNAME_MATCH HTTP_HEADER_PORT_MATCH HTTP_HEADER_URL_PATH_MATCH, &protocol, &hostname, &port, url_path ) == 4 ) {
asprintf( &server_hostname, "%s", hostname );
asprintf( &server_host, "%s", hostname );
use_ssl = chk_protocol(protocol);
server_port = atoi( port );
asprintf( &http_url_path, "%s", url_path );
return STATE_DEPENDENT;
}
else if ( sscanf ( header, HTTP_HEADER_LOCATION_MATCH HTTP_HEADER_PROTOCOL_MATCH HTTP_HEADER_HOSTNAME_MATCH HTTP_HEADER_URL_PATH_MATCH, &protocol, &hostname, url_path ) == 3) {
asprintf( &server_hostname, "%s", hostname );
asprintf( &server_host, "%s", hostname );
use_ssl = chk_protocol(protocol);
server_port = protocol_std_port(use_ssl);
asprintf( &http_url_path, "%s", url_path );
return STATE_DEPENDENT;
}
else if ( sscanf ( header, HTTP_HEADER_LOCATION_MATCH HTTP_HEADER_PROTOCOL_MATCH HTTP_HEADER_HOSTNAME_MATCH HTTP_HEADER_PORT_MATCH, &protocol, &hostname, &port ) == 3) {
asprintf( &server_hostname, "%s", hostname );
asprintf( &server_host, "%s", hostname );
use_ssl = chk_protocol(protocol);
server_port = atoi( port );
asprintf( &http_url_path, "%s", DEFAULT_HTTP_URL_PATH );
return STATE_DEPENDENT;
}
else if ( sscanf ( header, HTTP_HEADER_LOCATION_MATCH HTTP_HEADER_PROTOCOL_MATCH HTTP_HEADER_HOSTNAME_MATCH, protocol, hostname ) == 2 ) {
asprintf( &server_hostname, "%s", hostname );
asprintf( &server_host, "%s", hostname );
use_ssl = chk_protocol(protocol);
server_port = protocol_std_port(use_ssl);
asprintf( &http_url_path, "%s", DEFAULT_HTTP_URL_PATH );
}
else if ( sscanf ( header, HTTP_HEADER_LOCATION_MATCH HTTP_HEADER_URL_PATH_MATCH, url_path ) == 1 ) {
/* check for relative url and prepend path if necessary */
if ( ( url_path[0] != '/' ) && ( orig_url_dirname = strrchr( orig_url_path, '/' ) ) ) {
*orig_url_dirname = '\0';
asprintf( &http_url_path, "%s%s", orig_url_path, url_path );
} else {
asprintf( &http_url_path, "%s", url_path );
}
return STATE_DEPENDENT;
}
header += (size_t) strcspn( header, "\r\n" );
header += (size_t) strspn( header, "\r\n" );
} /* end while (header) */
/* default return value is STATE_DEPENDENT to continue looping in main() */
asprintf( &msg, "% %: % - Could not find redirect Location",
protocol_text( use_ssl ),
state_text( STATE_UNKNOWN ),
page->status );
terminate( STATE_UNKNOWN, msg );
}
#ifdef HAVE_SSL
int
https_request( SSL_CTX *ctx, SSL *ssl, struct pageref *page )
{
char *buffer = "";
char recvbuff[MAX_INPUT_BUFFER] = "";
int buffer_len = 0;
int content_len = 0;
size_t sendsize = 0;
size_t recvsize = 0;
char *content = "";
size_t size = 0;
char *basic_auth_encoded = NULL;
asprintf( &buffer, HTTP_TEMPLATE_REQUEST, buffer, http_method, http_url_path );
asprintf( &buffer, HTTP_TEMPLATE_HEADER_USERAGENT, buffer, progname, REVISION, PACKAGE_VERSION );
if ( use_server_hostname ) {
asprintf( &buffer, HTTP_TEMPLATE_HEADER_HOST, buffer, server_hostname );
}
if ( use_basic_auth ) {
basic_auth_encoded = base64( basic_auth, strlen( basic_auth ) );
asprintf( &buffer, HTTP_TEMPLATE_HEADER_AUTH, buffer, basic_auth_encoded );
}
/* either send http POST data */
if ( use_http_post_data ) {
asprintf( &buffer, "%sContent-Type: application/x-www-form-urlencoded\r\n", buffer );
asprintf( &buffer, "%sContent-Length: %i\r\n\r\n", buffer, content_len );
asprintf( &buffer, "%s%s%s", buffer, http_post_data, "\r\n" );
sendsize = SSL_write( ssl, buffer, strlen( buffer ) );
switch ( SSL_get_error( ssl, sendsize ) ) {
case SSL_ERROR_NONE:
if ( sendsize < strlen( buffer ) )
ssl_terminate( STATE_CRITICAL, "SSL ERROR: Incomplete write.\n" );
break;
default:
ssl_terminate( STATE_CRITICAL, "SSL ERROR: Write problem.\n" );
break;
}
/* or just a newline */
} else {
asprintf( &buffer, "%s\r\n", buffer );
sendsize = SSL_write( ssl, buffer, strlen( buffer ) );
switch ( SSL_get_error( ssl, sendsize ) ) {
case SSL_ERROR_NONE:
if ( sendsize < strlen( buffer ) )
ssl_terminate( STATE_CRITICAL, "SSL ERROR: Incomplete write.\n" );
break;
default:
ssl_terminate( STATE_CRITICAL, "SSL ERROR: Write problem.\n" );
break;
}
}
/* read server's response */
do {
recvsize = SSL_read( ssl, recvbuff, MAX_INPUT_BUFFER - 1 );
switch ( SSL_get_error( ssl, recvsize ) ) {
case SSL_ERROR_NONE:
if ( recvsize > (size_t) 0 ) {
recvbuff[recvsize] = '\0';
asprintf( &content, "%s%s", content, recvbuff );
size += recvsize;
}
break;
case SSL_ERROR_WANT_READ:
if ( use_client_certificate ) {
continue;
} else {
// workaround while we don't have anonymous client certificates: return OK
//ssl_terminate( STATE_WARNING, "HTTPS WARNING - Client Certificate required.\n" );
ssl_terminate( STATE_OK, "HTTPS WARNING - Client Certificate required.\n" );
}
break;
case SSL_ERROR_ZERO_RETURN:
break;
case SSL_ERROR_SYSCALL:
ssl_terminate( STATE_CRITICAL, "SSL ERROR: Premature close.\n" );
break;
default:
ssl_terminate( STATE_CRITICAL, "SSL ERROR: Read problem.\n" );
break;
}
} while ( recvsize > (size_t) 0 );
asprintf( &page->content, "%s", content );
page->size = size;
/* return a CRITICAL status if we couldn't read any data */
if ( size == (size_t) 0)
ssl_terminate( STATE_CRITICAL, "No data received" );
return STATE_OK;
}
#endif
#ifdef HAVE_SSL
int
ssl_terminate(int state, char *string ) {
ERR_print_errors( bio_err );
terminate( state, string );
}
#endif
#ifdef HAVE_SSL
static int
password_cb( char *buf, int num, int rwflag, void *userdata )
{
if ( num < strlen( client_certificate_passphrase ) + 1 )
return( 0 );
strcpy( buf, client_certificate_passphrase );
return( strlen( client_certificate_passphrase ) );
}
#endif
#ifdef HAVE_SSL
static void
sigpipe_handle( int x ) {
}
#endif
#ifdef HAVE_SSL
SSL_CTX *
initialize_ssl_ctx( void )
{
SSL_METHOD *meth;
SSL_CTX *ctx;
if ( !bio_err ) {
/* Global system initialization */
SSL_library_init();
SSL_load_error_strings();
/* An error write context */
bio_err=BIO_new_fp( stderr, BIO_NOCLOSE );
}
/* set up as SIGPIPE handler */
signal( SIGPIPE, sigpipe_handle );
/* create our context */
meth=SSLv3_method();
ctx=SSL_CTX_new( meth );
/* load client certificate and key */
if ( use_client_certificate ) {
if ( !(SSL_CTX_use_certificate_chain_file( ctx, client_certificate_file )) )
ssl_terminate( STATE_CRITICAL, "check_http: can't read client certificate file" );
/* set client certificate key passphrase */
if ( use_client_certificate_passphrase ) {
SSL_CTX_set_default_passwd_cb( ctx, password_cb );
}
if ( !(SSL_CTX_use_PrivateKey_file( ctx, client_certificate_file, SSL_FILETYPE_PEM )) )
ssl_terminate( STATE_CRITICAL, "check_http: can't read client certificate key file" );
}
/* load the CAs we trust */
if ( use_ca_certificate ) {
if ( !(SSL_CTX_load_verify_locations( ctx, ca_certificate_file, 0 )) )
ssl_terminate( STATE_CRITICAL, "check_http: can't read CA certificate file" );
#if (OPENSSL_VERSION_NUMBER < 0x00905100L)
SSL_CTX_set_verify_depth( ctx, 1 );
#endif
}
return ctx;
}
#endif
#ifdef HAVE_SSL
void destroy_ssl_ctx( SSL_CTX *ctx )
{
SSL_CTX_free( ctx );
}
#endif
#ifdef HAVE_SSL
int
fetch_server_certificate( SSL *ssl )
{
server_certificate = SSL_get_peer_certificate( ssl );
if ( server_certificate == NULL )
ssl_terminate( STATE_CRITICAL, "SSL ERROR: Cannot retrieve server certificate.\n" );
return STATE_OK;
}
#endif
#ifdef HAVE_SSL
int
check_server_certificate_chain( SSL *ssl )
{
if ( SSL_get_verify_result( ssl ) != X509_V_OK )
ssl_terminate( STATE_CRITICAL, "SSL ERROR: Cannot verify server certificate chain.\n" );
return STATE_OK;
}
#endif
#ifdef HAVE_SSL
int
check_server_certificate_hostname( )
{
char server_CN[256];
char *msg = NULL;
X509_NAME_get_text_by_NID( X509_get_subject_name( server_certificate ), NID_commonName, server_CN, 256 );
if ( strcasecmp( server_CN, server_hostname ) ) {
asprintf( &msg, "SSL ERROR: Server Certificate does not match Hostname %s.\n", server_hostname );
ssl_terminate( STATE_WARNING, msg );
}
return STATE_OK;
}
#endif
#ifdef HAVE_SSL
int
check_server_certificate_expires( )
{
ASN1_STRING *tm;
int offset;
struct tm stamp;
int days_left;
char timestamp[17] = "";
char *msg = NULL;
/* Retrieve timestamp of certificate */
tm = X509_get_notAfter( server_certificate );
/* Generate tm structure to process timestamp */
if ( tm->type == V_ASN1_UTCTIME ) {
if ( tm->length < 10 ) {
ssl_terminate( STATE_CRITICAL, "ERROR: Wrong time format in certificate.\n" );
} else {
stamp.tm_year = ( tm->data[0] - '0' ) * 10 + ( tm->data[1] - '0' );
if ( stamp.tm_year < 50 )
stamp.tm_year += 100;
offset = 0;
}
} else {
if ( tm->length < 12 ) {
ssl_terminate( STATE_CRITICAL, "ERROR: Wrong time format in certificate.\n" );
} else {
stamp.tm_year =
( tm->data[0] - '0' ) * 1000 + ( tm->data[1] - '0' ) * 100 +
( tm->data[2] - '0' ) * 10 + ( tm->data[3] - '0' );
stamp.tm_year -= 1900;
offset = 2;
}
}
stamp.tm_mon =
( tm->data[2 + offset] - '0' ) * 10 + ( tm->data[3 + offset] - '0' ) - 1;
stamp.tm_mday =
( tm->data[4 + offset] - '0' ) * 10 + ( tm->data[5 + offset] - '0' );
stamp.tm_hour =
( tm->data[6 + offset] - '0' ) * 10 + ( tm->data[7 + offset] - '0' );
stamp.tm_min =
( tm->data[8 + offset] - '0' ) * 10 + ( tm->data[9 + offset] - '0' );
stamp.tm_sec = 0;
stamp.tm_isdst = -1;
days_left = ( mktime( &stamp ) - time( NULL ) ) / 86400;
snprintf
( timestamp, 17, "%02d.%02d.%04d %02d:%02d",
stamp.tm_mday, stamp.tm_mon +1, stamp.tm_year + 1900,
stamp.tm_hour, stamp.tm_min );
if ( ( days_left > 0 ) && ( days_left <= server_certificate_min_days_valid ) ) {
asprintf( &msg, "Certificate expires in %d day(s) (%s).\n", days_left, timestamp );
ssl_terminate( STATE_WARNING, msg );
}
if ( days_left < 0 ) {
asprintf( &msg, "Certificate expired on %s.\n", timestamp );
ssl_terminate( STATE_CRITICAL, msg );
}
if (days_left == 0) {
asprintf( &msg, "Certificate expires today (%s).\n", timestamp );
ssl_terminate( STATE_WARNING, msg );
}
asprintf( &msg, "Certificate will expire on %s.\n", timestamp );
ssl_terminate( STATE_OK, msg );
}
#endif
/* written by lauri alanko */
static char *
base64 (char *bin, int len)
{
char *buf = (char *) malloc ((len + 2) / 3 * 4 + 1);
int i = 0, j = 0;
char BASE64_END = '=';
char base64_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
while (j < len - 2) {
buf[i++] = base64_table[bin[j] >> 2];
buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
buf[i++] = base64_table[((bin[j + 1] & 15) << 2) | (bin[j + 2] >> 6)];
buf[i++] = base64_table[bin[j + 2] & 63];
j += 3;
}
switch (len - j) {
case 1:
buf[i++] = base64_table[bin[j] >> 2];
buf[i++] = base64_table[(bin[j] & 3) << 4];
buf[i++] = BASE64_END;
buf[i++] = BASE64_END;
break;
case 2:
buf[i++] = base64_table[bin[j] >> 2];
buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
buf[i++] = base64_table[(bin[j + 1] & 15) << 2];
buf[i++] = BASE64_END;
break;
case 0:
break;
}
buf[i] = '\0';
return buf;
}
|