check_http.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. $Id$
  14. ******************************************************************************/
  15. /* splint -I. -I../../plugins -I../../lib/ -I/usr/kerberos/include/ ../../plugins/check_http.c */
  16. const char *progname = "check_http";
  17. const char *revision = "$Revision$";
  18. const char *copyright = "1999-2004";
  19. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  20. #include "common.h"
  21. #include "netutils.h"
  22. #include "utils.h"
  23. #define INPUT_DELIMITER ";"
  24. #define HTTP_EXPECT "HTTP/1."
  25. enum {
  26. MAX_IPV4_HOSTLENGTH = 255,
  27. HTTP_PORT = 80,
  28. HTTPS_PORT = 443
  29. };
  30. #ifdef HAVE_SSL_H
  31. #include <rsa.h>
  32. #include <crypto.h>
  33. #include <x509.h>
  34. #include <pem.h>
  35. #include <ssl.h>
  36. #include <err.h>
  37. #include <rand.h>
  38. #else
  39. # ifdef HAVE_OPENSSL_SSL_H
  40. # include <openssl/rsa.h>
  41. # include <openssl/crypto.h>
  42. # include <openssl/x509.h>
  43. # include <openssl/pem.h>
  44. # include <openssl/ssl.h>
  45. # include <openssl/err.h>
  46. # include <openssl/rand.h>
  47. # endif
  48. #endif
  49. #ifdef HAVE_SSL
  50. int check_cert = FALSE;
  51. int days_till_exp;
  52. char *randbuff;
  53. SSL_CTX *ctx;
  54. SSL *ssl;
  55. X509 *server_cert;
  56. int connect_SSL (void);
  57. int check_certificate (X509 **);
  58. #endif
  59. int no_body = FALSE;
  60. int maximum_age = -1;
  61. #ifdef HAVE_REGEX_H
  62. enum {
  63. REGS = 2,
  64. MAX_RE_SIZE = 256
  65. };
  66. #include <regex.h>
  67. regex_t preg;
  68. regmatch_t pmatch[REGS];
  69. char regexp[MAX_RE_SIZE];
  70. char errbuf[MAX_INPUT_BUFFER];
  71. int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE;
  72. int errcode;
  73. #endif
  74. struct timeval tv;
  75. #define HTTP_URL "/"
  76. #define CRLF "\r\n"
  77. char timestamp[17] = "";
  78. int specify_port = FALSE;
  79. int server_port = HTTP_PORT;
  80. char server_port_text[6] = "";
  81. char server_type[6] = "http";
  82. char *server_address;
  83. char *host_name;
  84. char *server_url;
  85. char *user_agent;
  86. int server_url_length;
  87. int server_expect_yn = 0;
  88. char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT;
  89. char string_expect[MAX_INPUT_BUFFER] = "";
  90. double warning_time = 0;
  91. int check_warning_time = FALSE;
  92. double critical_time = 0;
  93. int check_critical_time = FALSE;
  94. char user_auth[MAX_INPUT_BUFFER] = "";
  95. int display_html = FALSE;
  96. char *http_opt_headers;
  97. int onredirect = STATE_OK;
  98. int use_ssl = FALSE;
  99. int verbose = FALSE;
  100. int sd;
  101. int min_page_len = 0;
  102. int redir_depth = 0;
  103. int max_depth = 15;
  104. char *http_method;
  105. char *http_post_data;
  106. char *http_content_type;
  107. char buffer[MAX_INPUT_BUFFER];
  108. int process_arguments (int, char **);
  109. static char *base64 (const char *bin, size_t len);
  110. int check_http (void);
  111. void redir (char *pos, char *status_line);
  112. int server_type_check(const char *type);
  113. int server_port_check(int ssl_flag);
  114. char *perfd_time (double microsec);
  115. char *perfd_size (int page_len);
  116. int my_recv (void);
  117. int my_close (void);
  118. void print_help (void);
  119. void print_usage (void);
  120. int
  121. main (int argc, char **argv)
  122. {
  123. int result = STATE_UNKNOWN;
  124. /* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */
  125. server_url = strdup(HTTP_URL);
  126. server_url_length = strlen(server_url);
  127. asprintf (&user_agent, "User-Agent: check_http/%s (nagios-plugins %s)",
  128. clean_revstring (revision), VERSION);
  129. if (process_arguments (argc, argv) == ERROR)
  130. usage4 (_("Could not parse arguments"));
  131. if (strstr (timestamp, ":")) {
  132. if (strstr (server_url, "?"))
  133. asprintf (&server_url, "%s&%s", server_url, timestamp);
  134. else
  135. asprintf (&server_url, "%s?%s", server_url, timestamp);
  136. }
  137. if (display_html == TRUE)
  138. printf ("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">",
  139. use_ssl ? "https" : "http", host_name,
  140. server_port, server_url);
  141. /* initialize alarm signal handling, set socket timeout, start timer */
  142. (void) signal (SIGALRM, socket_timeout_alarm_handler);
  143. (void) alarm (socket_timeout);
  144. gettimeofday (&tv, NULL);
  145. #ifdef HAVE_SSL
  146. if (use_ssl && check_cert == TRUE) {
  147. if (connect_SSL () != OK)
  148. die (STATE_CRITICAL, _("HTTP CRITICAL - Could not make SSL connection\n"));
  149. if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) {
  150. result = check_certificate (&server_cert);
  151. X509_free (server_cert);
  152. }
  153. else {
  154. printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
  155. result = STATE_CRITICAL;
  156. }
  157. SSL_shutdown (ssl);
  158. SSL_free (ssl);
  159. SSL_CTX_free (ctx);
  160. close (sd);
  161. }
  162. else {
  163. result = check_http ();
  164. }
  165. #else
  166. result = check_http ();
  167. #endif
  168. return result;
  169. }
  170. /* process command-line arguments */
  171. int
  172. process_arguments (int argc, char **argv)
  173. {
  174. int c = 1;
  175. int option = 0;
  176. static struct option longopts[] = {
  177. STD_LONG_OPTS,
  178. {"file",required_argument,0,'F'},
  179. {"link", no_argument, 0, 'L'},
  180. {"nohtml", no_argument, 0, 'n'},
  181. {"ssl", no_argument, 0, 'S'},
  182. {"verbose", no_argument, 0, 'v'},
  183. {"post", required_argument, 0, 'P'},
  184. {"IP-address", required_argument, 0, 'I'},
  185. {"url", required_argument, 0, 'u'},
  186. {"string", required_argument, 0, 's'},
  187. {"regex", required_argument, 0, 'r'},
  188. {"ereg", required_argument, 0, 'r'},
  189. {"eregi", required_argument, 0, 'R'},
  190. {"linespan", no_argument, 0, 'l'},
  191. {"onredirect", required_argument, 0, 'f'},
  192. {"certificate", required_argument, 0, 'C'},
  193. {"useragent", required_argument, 0, 'A'},
  194. {"header", required_argument, 0, 'k'},
  195. {"no-body", no_argument, 0, 'N'},
  196. {"max-age", required_argument, 0, 'M'},
  197. {"content-type", required_argument, 0, 'T'},
  198. {"min", required_argument, 0, 'm'},
  199. {"use-ipv4", no_argument, 0, '4'},
  200. {"use-ipv6", no_argument, 0, '6'},
  201. {0, 0, 0, 0}
  202. };
  203. if (argc < 2)
  204. return ERROR;
  205. for (c = 1; c < argc; c++) {
  206. if (strcmp ("-to", argv[c]) == 0)
  207. strcpy (argv[c], "-t");
  208. if (strcmp ("-hn", argv[c]) == 0)
  209. strcpy (argv[c], "-H");
  210. if (strcmp ("-wt", argv[c]) == 0)
  211. strcpy (argv[c], "-w");
  212. if (strcmp ("-ct", argv[c]) == 0)
  213. strcpy (argv[c], "-c");
  214. if (strcmp ("-nohtml", argv[c]) == 0)
  215. strcpy (argv[c], "-n");
  216. }
  217. while (1) {
  218. c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:T:I:a:e:p:s:R:r:u:f:C:nlLSm:M:N", longopts, &option);
  219. if (c == -1 || c == EOF)
  220. break;
  221. switch (c) {
  222. case '?': /* usage */
  223. usage2 (_("Unknown argument"), optarg);
  224. break;
  225. case 'h': /* help */
  226. print_help ();
  227. exit (STATE_OK);
  228. break;
  229. case 'V': /* version */
  230. print_revision (progname, revision);
  231. exit (STATE_OK);
  232. break;
  233. case 't': /* timeout period */
  234. if (!is_intnonneg (optarg))
  235. usage2 (_("Timeout interval must be a positive integer"), optarg);
  236. else
  237. socket_timeout = atoi (optarg);
  238. break;
  239. case 'c': /* critical time threshold */
  240. if (!is_nonnegative (optarg))
  241. usage2 (_("Critical threshold must be integer"), optarg);
  242. else {
  243. critical_time = strtod (optarg, NULL);
  244. check_critical_time = TRUE;
  245. }
  246. break;
  247. case 'w': /* warning time threshold */
  248. if (!is_nonnegative (optarg))
  249. usage2 (_("Warning threshold must be integer"), optarg);
  250. else {
  251. warning_time = strtod (optarg, NULL);
  252. check_warning_time = TRUE;
  253. }
  254. break;
  255. case 'A': /* User Agent String */
  256. asprintf (&user_agent, "User-Agent: %s", optarg);
  257. break;
  258. case 'k': /* Additional headers */
  259. asprintf (&http_opt_headers, "%s", optarg);
  260. break;
  261. case 'L': /* show html link */
  262. display_html = TRUE;
  263. break;
  264. case 'n': /* do not show html link */
  265. display_html = FALSE;
  266. break;
  267. case 'S': /* use SSL */
  268. #ifndef HAVE_SSL
  269. usage4 (_("Invalid option - SSL is not available"));
  270. #endif
  271. use_ssl = TRUE;
  272. if (specify_port == FALSE)
  273. server_port = HTTPS_PORT;
  274. break;
  275. case 'C': /* Check SSL cert validity */
  276. #ifdef HAVE_SSL
  277. if (!is_intnonneg (optarg))
  278. usage2 (_("Invalid certificate expiration period"), optarg);
  279. else {
  280. days_till_exp = atoi (optarg);
  281. check_cert = TRUE;
  282. }
  283. #else
  284. usage4 (_("Invalid option - SSL is not available"));
  285. #endif
  286. break;
  287. case 'f': /* onredirect */
  288. if (!strcmp (optarg, "follow"))
  289. onredirect = STATE_DEPENDENT;
  290. if (!strcmp (optarg, "unknown"))
  291. onredirect = STATE_UNKNOWN;
  292. if (!strcmp (optarg, "ok"))
  293. onredirect = STATE_OK;
  294. if (!strcmp (optarg, "warning"))
  295. onredirect = STATE_WARNING;
  296. if (!strcmp (optarg, "critical"))
  297. onredirect = STATE_CRITICAL;
  298. if (verbose)
  299. printf(_("option f:%d \n"), onredirect);
  300. break;
  301. /* Note: H, I, and u must be malloc'd or will fail on redirects */
  302. case 'H': /* Host Name (virtual host) */
  303. host_name = strdup (optarg);
  304. if (strstr (optarg, ":"))
  305. sscanf (optarg, "%*[^:]:%d", &server_port);
  306. break;
  307. case 'I': /* Server IP-address */
  308. server_address = strdup (optarg);
  309. break;
  310. case 'u': /* URL path */
  311. server_url = strdup (optarg);
  312. server_url_length = strlen (server_url);
  313. break;
  314. case 'p': /* Server port */
  315. if (!is_intnonneg (optarg))
  316. usage2 (_("Invalid port number"), optarg);
  317. else {
  318. server_port = atoi (optarg);
  319. specify_port = TRUE;
  320. }
  321. break;
  322. case 'a': /* authorization info */
  323. strncpy (user_auth, optarg, MAX_INPUT_BUFFER - 1);
  324. user_auth[MAX_INPUT_BUFFER - 1] = 0;
  325. break;
  326. case 'P': /* HTTP POST data in URL encoded format */
  327. if (http_method || http_post_data) break;
  328. http_method = strdup("POST");
  329. http_post_data = strdup (optarg);
  330. break;
  331. case 's': /* string or substring */
  332. strncpy (string_expect, optarg, MAX_INPUT_BUFFER - 1);
  333. string_expect[MAX_INPUT_BUFFER - 1] = 0;
  334. break;
  335. case 'e': /* string or substring */
  336. strncpy (server_expect, optarg, MAX_INPUT_BUFFER - 1);
  337. server_expect[MAX_INPUT_BUFFER - 1] = 0;
  338. server_expect_yn = 1;
  339. break;
  340. case 'T': /* Content-type */
  341. asprintf (&http_content_type, "%s", optarg);
  342. break;
  343. #ifndef HAVE_REGEX_H
  344. case 'l': /* linespan */
  345. case 'r': /* linespan */
  346. case 'R': /* linespan */
  347. usage4 (_("Call for regex which was not a compiled option"));
  348. break;
  349. #else
  350. case 'l': /* linespan */
  351. cflags &= ~REG_NEWLINE;
  352. break;
  353. case 'R': /* regex */
  354. cflags |= REG_ICASE;
  355. case 'r': /* regex */
  356. strncpy (regexp, optarg, MAX_RE_SIZE - 1);
  357. regexp[MAX_RE_SIZE - 1] = 0;
  358. errcode = regcomp (&preg, regexp, cflags);
  359. if (errcode != 0) {
  360. (void) regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  361. printf (_("Could Not Compile Regular Expression: %s"), errbuf);
  362. return ERROR;
  363. }
  364. break;
  365. #endif
  366. case '4':
  367. address_family = AF_INET;
  368. break;
  369. case '6':
  370. #ifdef USE_IPV6
  371. address_family = AF_INET6;
  372. #else
  373. usage4 (_("IPv6 support not available"));
  374. #endif
  375. break;
  376. case 'v': /* verbose */
  377. verbose = TRUE;
  378. break;
  379. case 'm': /* min_page_length */
  380. min_page_len = atoi (optarg);
  381. break;
  382. case 'N': /* no-body */
  383. no_body = TRUE;
  384. break;
  385. case 'M': /* max-age */
  386. {
  387. int L = strlen(optarg);
  388. if (L && optarg[L-1] == 'm')
  389. maximum_age = atoi (optarg) * 60;
  390. else if (L && optarg[L-1] == 'h')
  391. maximum_age = atoi (optarg) * 60 * 60;
  392. else if (L && optarg[L-1] == 'd')
  393. maximum_age = atoi (optarg) * 60 * 60 * 24;
  394. else if (L && (optarg[L-1] == 's' ||
  395. isdigit (optarg[L-1])))
  396. maximum_age = atoi (optarg);
  397. else {
  398. fprintf (stderr, "unparsable max-age: %s\n", optarg);
  399. exit (1);
  400. }
  401. }
  402. break;
  403. }
  404. }
  405. c = optind;
  406. if (server_address == NULL && c < argc)
  407. server_address = strdup (argv[c++]);
  408. if (host_name == NULL && c < argc)
  409. host_name = strdup (argv[c++]);
  410. if (server_address == NULL) {
  411. if (host_name == NULL)
  412. usage4 (_("You must specify a server address or host name"));
  413. else
  414. server_address = strdup (host_name);
  415. }
  416. if (check_critical_time && critical_time>(double)socket_timeout)
  417. socket_timeout = (int)critical_time + 1;
  418. if (http_method == NULL)
  419. http_method = strdup ("GET");
  420. return TRUE;
  421. }
  422. /* written by lauri alanko */
  423. static char *
  424. base64 (const char *bin, size_t len)
  425. {
  426. char *buf = (char *) malloc ((len + 2) / 3 * 4 + 1);
  427. size_t i = 0, j = 0;
  428. char BASE64_END = '=';
  429. char base64_table[64];
  430. strncpy (base64_table, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 64);
  431. while (j < len - 2) {
  432. buf[i++] = base64_table[bin[j] >> 2];
  433. buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
  434. buf[i++] = base64_table[((bin[j + 1] & 15) << 2) | (bin[j + 2] >> 6)];
  435. buf[i++] = base64_table[bin[j + 2] & 63];
  436. j += 3;
  437. }
  438. switch (len - j) {
  439. case 1:
  440. buf[i++] = base64_table[bin[j] >> 2];
  441. buf[i++] = base64_table[(bin[j] & 3) << 4];
  442. buf[i++] = BASE64_END;
  443. buf[i++] = BASE64_END;
  444. break;
  445. case 2:
  446. buf[i++] = base64_table[bin[j] >> 2];
  447. buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
  448. buf[i++] = base64_table[(bin[j + 1] & 15) << 2];
  449. buf[i++] = BASE64_END;
  450. break;
  451. case 0:
  452. break;
  453. }
  454. buf[i] = '\0';
  455. return buf;
  456. }
  457. /* Returns 1 if we're done processing the document body; 0 to keep going */
  458. static int
  459. document_headers_done (char *full_page)
  460. {
  461. const char *body;
  462. for (body = full_page; *body; body++) {
  463. if (!strncmp (body, "\n\n", 2) || !strncmp (body, "\n\r\n", 3))
  464. break;
  465. }
  466. if (!*body)
  467. return 0; /* haven't read end of headers yet */
  468. full_page[body - full_page] = 0;
  469. return 1;
  470. }
  471. static time_t
  472. parse_time_string (const char *string)
  473. {
  474. struct tm tm;
  475. time_t t;
  476. memset (&tm, 0, sizeof(tm));
  477. /* Like this: Tue, 25 Dec 2001 02:59:03 GMT */
  478. if (isupper (string[0]) && /* Tue */
  479. islower (string[1]) &&
  480. islower (string[2]) &&
  481. ',' == string[3] &&
  482. ' ' == string[4] &&
  483. (isdigit(string[5]) || string[5] == ' ') && /* 25 */
  484. isdigit (string[6]) &&
  485. ' ' == string[7] &&
  486. isupper (string[8]) && /* Dec */
  487. islower (string[9]) &&
  488. islower (string[10]) &&
  489. ' ' == string[11] &&
  490. isdigit (string[12]) && /* 2001 */
  491. isdigit (string[13]) &&
  492. isdigit (string[14]) &&
  493. isdigit (string[15]) &&
  494. ' ' == string[16] &&
  495. isdigit (string[17]) && /* 02: */
  496. isdigit (string[18]) &&
  497. ':' == string[19] &&
  498. isdigit (string[20]) && /* 59: */
  499. isdigit (string[21]) &&
  500. ':' == string[22] &&
  501. isdigit (string[23]) && /* 03 */
  502. isdigit (string[24]) &&
  503. ' ' == string[25] &&
  504. 'G' == string[26] && /* GMT */
  505. 'M' == string[27] && /* GMT */
  506. 'T' == string[28]) {
  507. tm.tm_sec = 10 * (string[23]-'0') + (string[24]-'0');
  508. tm.tm_min = 10 * (string[20]-'0') + (string[21]-'0');
  509. tm.tm_hour = 10 * (string[17]-'0') + (string[18]-'0');
  510. tm.tm_mday = 10 * (string[5] == ' ' ? 0 : string[5]-'0') + (string[6]-'0');
  511. tm.tm_mon = (!strncmp (string+8, "Jan", 3) ? 0 :
  512. !strncmp (string+8, "Feb", 3) ? 1 :
  513. !strncmp (string+8, "Mar", 3) ? 2 :
  514. !strncmp (string+8, "Apr", 3) ? 3 :
  515. !strncmp (string+8, "May", 3) ? 4 :
  516. !strncmp (string+8, "Jun", 3) ? 5 :
  517. !strncmp (string+8, "Jul", 3) ? 6 :
  518. !strncmp (string+8, "Aug", 3) ? 7 :
  519. !strncmp (string+8, "Sep", 3) ? 8 :
  520. !strncmp (string+8, "Oct", 3) ? 9 :
  521. !strncmp (string+8, "Nov", 3) ? 10 :
  522. !strncmp (string+8, "Dec", 3) ? 11 :
  523. -1);
  524. tm.tm_year = ((1000 * (string[12]-'0') +
  525. 100 * (string[13]-'0') +
  526. 10 * (string[14]-'0') +
  527. (string[15]-'0'))
  528. - 1900);
  529. tm.tm_isdst = 0; /* GMT is never in DST, right? */
  530. if (tm.tm_mon < 0 || tm.tm_mday < 1 || tm.tm_mday > 31)
  531. return 0;
  532. /*
  533. This is actually wrong: we need to subtract the local timezone
  534. offset from GMT from this value. But, that's ok in this usage,
  535. because we only comparing these two GMT dates against each other,
  536. so it doesn't matter what time zone we parse them in.
  537. */
  538. t = mktime (&tm);
  539. if (t == (time_t) -1) t = 0;
  540. if (verbose) {
  541. const char *s = string;
  542. while (*s && *s != '\r' && *s != '\n')
  543. fputc (*s++, stdout);
  544. printf (" ==> %lu\n", (unsigned long) t);
  545. }
  546. return t;
  547. } else {
  548. return 0;
  549. }
  550. }
  551. static void
  552. check_document_dates (const char *headers)
  553. {
  554. const char *s;
  555. char *server_date = 0;
  556. char *document_date = 0;
  557. s = headers;
  558. while (*s) {
  559. const char *field = s;
  560. const char *value = 0;
  561. /* Find the end of the header field */
  562. while (*s && !isspace(*s) && *s != ':')
  563. s++;
  564. /* Remember the header value, if any. */
  565. if (*s == ':')
  566. value = ++s;
  567. /* Skip to the end of the header, including continuation lines. */
  568. while (*s && !(*s == '\n' && (s[1] != ' ' && s[1] != '\t')))
  569. s++;
  570. s++;
  571. /* Process this header. */
  572. if (value && value > field+2) {
  573. char *ff = (char *) malloc (value-field);
  574. char *ss = ff;
  575. while (field < value-1)
  576. *ss++ = tolower(*field++);
  577. *ss++ = 0;
  578. if (!strcmp (ff, "date") || !strcmp (ff, "last-modified")) {
  579. const char *e;
  580. while (*value && isspace (*value))
  581. value++;
  582. for (e = value; *e && *e != '\r' && *e != '\n'; e++)
  583. ;
  584. ss = (char *) malloc (e - value + 1);
  585. strncpy (ss, value, e - value);
  586. ss[e - value] = 0;
  587. if (!strcmp (ff, "date")) {
  588. if (server_date) free (server_date);
  589. server_date = ss;
  590. } else {
  591. if (document_date) free (document_date);
  592. document_date = ss;
  593. }
  594. }
  595. free (ff);
  596. }
  597. }
  598. /* Done parsing the body. Now check the dates we (hopefully) parsed. */
  599. if (!server_date || !*server_date) {
  600. die (STATE_UNKNOWN, _("Server date unknown\n"));
  601. } else if (!document_date || !*document_date) {
  602. die (STATE_CRITICAL, _("Document modification date unknown\n"));
  603. } else {
  604. time_t srv_data = parse_time_string (server_date);
  605. time_t doc_data = parse_time_string (document_date);
  606. if (srv_data <= 0) {
  607. die (STATE_CRITICAL, _("CRITICAL - Server date \"%100s\" unparsable"), server_date);
  608. } else if (doc_data <= 0) {
  609. die (STATE_CRITICAL, _("CRITICAL - Document date \"%100s\" unparsable"), document_date);
  610. } else if (doc_data > srv_data + 30) {
  611. die (STATE_CRITICAL, _("CRITICAL - Document is %d seconds in the future\n"), (int)doc_data - (int)srv_data);
  612. } else if (doc_data < srv_data - maximum_age) {
  613. int n = (srv_data - doc_data);
  614. if (n > (60 * 60 * 24 * 2))
  615. die (STATE_CRITICAL,
  616. _("CRITICAL - Last modified %.1f days ago\n"),
  617. ((float) n) / (60 * 60 * 24));
  618. else
  619. die (STATE_CRITICAL,
  620. _("CRITICAL - Last modified %d:%02d:%02d ago\n"),
  621. n / (60 * 60), (n / 60) % 60, n % 60);
  622. }
  623. free (server_date);
  624. free (document_date);
  625. }
  626. }
  627. int
  628. check_http (void)
  629. {
  630. char *msg;
  631. char *status_line;
  632. char *status_code;
  633. char *header;
  634. char *page;
  635. char *auth;
  636. int http_status;
  637. int i = 0;
  638. size_t pagesize = 0;
  639. char *full_page;
  640. char *buf;
  641. char *pos;
  642. long microsec;
  643. double elapsed_time;
  644. int page_len = 0;
  645. #ifdef HAVE_SSL
  646. int sslerr;
  647. #endif
  648. /* try to connect to the host at the given port number */
  649. #ifdef HAVE_SSL
  650. if (use_ssl == TRUE) {
  651. if (connect_SSL () != OK) {
  652. die (STATE_CRITICAL, _("Unable to open TCP socket\n"));
  653. }
  654. if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) {
  655. X509_free (server_cert);
  656. }
  657. else {
  658. printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
  659. return STATE_CRITICAL;
  660. }
  661. }
  662. else {
  663. #endif
  664. if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
  665. die (STATE_CRITICAL, _("Unable to open TCP socket\n"));
  666. #ifdef HAVE_SSL
  667. }
  668. #endif
  669. asprintf (&buf, "%s %s HTTP/1.0\r\n%s\r\n", http_method, server_url, user_agent);
  670. /* optionally send the host header info */
  671. if (host_name)
  672. asprintf (&buf, "%sHost: %s\r\n", buf, host_name);
  673. /* optionally send any other header tag */
  674. if (http_opt_headers) {
  675. for ((pos = strtok(http_opt_headers, INPUT_DELIMITER)); pos; (pos = strtok(NULL, INPUT_DELIMITER)))
  676. asprintf (&buf, "%s%s\r\n", buf, pos);
  677. }
  678. /* optionally send the authentication info */
  679. if (strlen(user_auth)) {
  680. auth = base64 (user_auth, strlen (user_auth));
  681. asprintf (&buf, "%sAuthorization: Basic %s\r\n", buf, auth);
  682. }
  683. /* either send http POST data */
  684. if (http_post_data) {
  685. if (http_content_type) {
  686. asprintf (&buf, "%sContent-Type: %s\r\n", buf, http_content_type);
  687. } else {
  688. asprintf (&buf, "%sContent-Type: application/x-www-form-urlencoded\r\n", buf);
  689. }
  690. asprintf (&buf, "%sContent-Length: %i\r\n\r\n", buf, strlen (http_post_data));
  691. asprintf (&buf, "%s%s%s", buf, http_post_data, CRLF);
  692. }
  693. else {
  694. /* or just a newline so the server knows we're done with the request */
  695. asprintf (&buf, "%s%s", buf, CRLF);
  696. }
  697. if (verbose)
  698. printf ("%s\n", buf);
  699. #ifdef HAVE_SSL
  700. if (use_ssl == TRUE) {
  701. if (SSL_write (ssl, buf, (int)strlen(buf)) == -1) {
  702. ERR_print_errors_fp (stderr);
  703. return STATE_CRITICAL;
  704. }
  705. }
  706. else {
  707. #endif
  708. send (sd, buf, strlen (buf), 0);
  709. #ifdef HAVE_SSL
  710. }
  711. #endif
  712. /* fetch the page */
  713. full_page = strdup("");
  714. while ((i = my_recv ()) > 0) {
  715. buffer[i] = '\0';
  716. asprintf (&full_page, "%s%s", full_page, buffer);
  717. pagesize += i;
  718. if (no_body && document_headers_done (full_page)) {
  719. i = 0;
  720. break;
  721. }
  722. }
  723. if (i < 0 && errno != ECONNRESET) {
  724. #ifdef HAVE_SSL
  725. if (use_ssl) {
  726. sslerr=SSL_get_error(ssl, i);
  727. if ( sslerr == SSL_ERROR_SSL ) {
  728. die (STATE_WARNING, _("Client Certificate Required\n"));
  729. } else {
  730. die (STATE_CRITICAL, _("Error on receive\n"));
  731. }
  732. }
  733. else {
  734. #endif
  735. die (STATE_CRITICAL, _("Error on receive\n"));
  736. #ifdef HAVE_SSL
  737. }
  738. #endif
  739. }
  740. /* return a CRITICAL status if we couldn't read any data */
  741. if (pagesize == (size_t) 0)
  742. die (STATE_CRITICAL, _("No data received %s\n"), timestamp);
  743. /* close the connection */
  744. my_close ();
  745. /* reset the alarm */
  746. alarm (0);
  747. /* leave full_page untouched so we can free it later */
  748. page = full_page;
  749. if (verbose)
  750. printf ("%s://%s:%d%s is %d characters\n",
  751. use_ssl ? "https" : "http", server_address,
  752. server_port, server_url, pagesize);
  753. /* find status line and null-terminate it */
  754. status_line = page;
  755. page += (size_t) strcspn (page, "\r\n");
  756. pos = page;
  757. page += (size_t) strspn (page, "\r\n");
  758. status_line[strcspn(status_line, "\r\n")] = 0;
  759. strip (status_line);
  760. if (verbose)
  761. printf ("STATUS: %s\n", status_line);
  762. /* find header info and null-terminate it */
  763. header = page;
  764. while (strcspn (page, "\r\n") > 0) {
  765. page += (size_t) strcspn (page, "\r\n");
  766. pos = page;
  767. if ((strspn (page, "\r") == 1 && strspn (page, "\r\n") >= 2) ||
  768. (strspn (page, "\n") == 1 && strspn (page, "\r\n") >= 2))
  769. page += (size_t) 2;
  770. else
  771. page += (size_t) 1;
  772. }
  773. page += (size_t) strspn (page, "\r\n");
  774. header[pos - header] = 0;
  775. if (verbose)
  776. printf ("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header,
  777. (no_body ? " [[ skipped ]]" : page));
  778. /* make sure the status line matches the response we are looking for */
  779. if (!strstr (status_line, server_expect)) {
  780. if (server_port == HTTP_PORT)
  781. asprintf (&msg,
  782. _("Invalid HTTP response received from host\n"));
  783. else
  784. asprintf (&msg,
  785. _("Invalid HTTP response received from host on port %d\n"),
  786. server_port);
  787. die (STATE_CRITICAL, "%s", msg);
  788. }
  789. /* Exit here if server_expect was set by user and not default */
  790. if ( server_expect_yn ) {
  791. asprintf (&msg,
  792. _("HTTP OK: Status line output matched \"%s\"\n"),
  793. server_expect);
  794. if (verbose)
  795. printf ("%s\n",msg);
  796. }
  797. else {
  798. /* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */
  799. /* HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT */
  800. /* Status-Code = 3 DIGITS */
  801. status_code = strchr (status_line, ' ') + sizeof (char);
  802. if (strspn (status_code, "1234567890") != 3)
  803. die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status Line (%s)\n"), status_line);
  804. http_status = atoi (status_code);
  805. /* check the return code */
  806. if (http_status >= 600 || http_status < 100)
  807. die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%s)\n"), status_line);
  808. /* server errors result in a critical state */
  809. else if (http_status >= 500)
  810. die (STATE_CRITICAL, _("HTTP CRITICAL: %s\n"), status_line);
  811. /* client errors result in a warning state */
  812. else if (http_status >= 400)
  813. die (STATE_WARNING, _("HTTP WARNING: %s\n"), status_line);
  814. /* check redirected page if specified */
  815. else if (http_status >= 300) {
  816. if (onredirect == STATE_DEPENDENT)
  817. redir (header, status_line);
  818. else if (onredirect == STATE_UNKNOWN)
  819. printf (_("UNKNOWN"));
  820. else if (onredirect == STATE_OK)
  821. printf (_("OK"));
  822. else if (onredirect == STATE_WARNING)
  823. printf (_("WARNING"));
  824. else if (onredirect == STATE_CRITICAL)
  825. printf (_("CRITICAL"));
  826. microsec = deltime (tv);
  827. elapsed_time = (double)microsec / 1.0e6;
  828. die (onredirect,
  829. _(" - %s - %.3f second response time %s%s|%s %s\n"),
  830. status_line, elapsed_time, timestamp,
  831. (display_html ? "</A>" : ""),
  832. perfd_time (elapsed_time), perfd_size (pagesize));
  833. } /* end if (http_status >= 300) */
  834. } /* end else (server_expect_yn) */
  835. if (maximum_age >= 0) {
  836. check_document_dates (header);
  837. }
  838. /* check elapsed time */
  839. microsec = deltime (tv);
  840. elapsed_time = (double)microsec / 1.0e6;
  841. asprintf (&msg,
  842. _("HTTP WARNING: %s - %.3f second response time %s%s|%s %s\n"),
  843. status_line, elapsed_time, timestamp,
  844. (display_html ? "</A>" : ""),
  845. perfd_time (elapsed_time), perfd_size (pagesize));
  846. if (check_critical_time == TRUE && elapsed_time > critical_time)
  847. die (STATE_CRITICAL, "%s", msg);
  848. if (check_warning_time == TRUE && elapsed_time > warning_time)
  849. die (STATE_WARNING, "%s", msg);
  850. /* Page and Header content checks go here */
  851. /* these checks should be last */
  852. if (strlen (string_expect)) {
  853. if (strstr (page, string_expect)) {
  854. printf (_("HTTP OK %s - %.3f second response time %s%s|%s %s\n"),
  855. status_line, elapsed_time,
  856. timestamp, (display_html ? "</A>" : ""),
  857. perfd_time (elapsed_time), perfd_size (pagesize));
  858. exit (STATE_OK);
  859. }
  860. else {
  861. printf (_("CRITICAL - string not found%s|%s %s\n"),
  862. (display_html ? "</A>" : ""),
  863. perfd_time (elapsed_time), perfd_size (pagesize));
  864. exit (STATE_CRITICAL);
  865. }
  866. }
  867. #ifdef HAVE_REGEX_H
  868. if (strlen (regexp)) {
  869. errcode = regexec (&preg, page, REGS, pmatch, 0);
  870. if (errcode == 0) {
  871. printf (_("HTTP OK %s - %.3f second response time %s%s|%s %s\n"),
  872. status_line, elapsed_time,
  873. timestamp, (display_html ? "</A>" : ""),
  874. perfd_time (elapsed_time), perfd_size (pagesize));
  875. exit (STATE_OK);
  876. }
  877. else {
  878. if (errcode == REG_NOMATCH) {
  879. printf (_("CRITICAL - pattern not found%s|%s %s\n"),
  880. (display_html ? "</A>" : ""),
  881. perfd_time (elapsed_time), perfd_size (pagesize));
  882. exit (STATE_CRITICAL);
  883. }
  884. else {
  885. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  886. printf (_("CRITICAL - Execute Error: %s\n"), errbuf);
  887. exit (STATE_CRITICAL);
  888. }
  889. }
  890. }
  891. #endif
  892. /* make sure the page is of an appropriate size */
  893. page_len = strlen (page);
  894. if ((min_page_len > 0) && (page_len < min_page_len)) {
  895. printf (_("HTTP WARNING: page size %d too small%s|%s\n"),
  896. page_len, (display_html ? "</A>" : ""), perfd_size (page_len) );
  897. exit (STATE_WARNING);
  898. }
  899. /* We only get here if all tests have been passed */
  900. asprintf (&msg, _("HTTP OK %s - %d bytes in %.3f seconds %s%s|%s %s\n"),
  901. status_line, page_len, elapsed_time,
  902. timestamp, (display_html ? "</A>" : ""),
  903. perfd_time (elapsed_time), perfd_size (page_len));
  904. die (STATE_OK, "%s", msg);
  905. return STATE_UNKNOWN;
  906. }
  907. /* per RFC 2396 */
  908. #define HDR_LOCATION "%*[Ll]%*[Oo]%*[Cc]%*[Aa]%*[Tt]%*[Ii]%*[Oo]%*[Nn]: "
  909. #define URI_HTTP "%[HTPShtps]://"
  910. #define URI_HOST "%[-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"
  911. #define URI_PORT ":%[0123456789]"
  912. #define URI_PATH "%[-_.!~*'();/?:@&=+$,%#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"
  913. #define HD1 URI_HTTP URI_HOST URI_PORT URI_PATH
  914. #define HD2 URI_HTTP URI_HOST URI_PATH
  915. #define HD3 URI_HTTP URI_HOST URI_PORT
  916. #define HD4 URI_HTTP URI_HOST
  917. #define HD5 URI_PATH
  918. void
  919. redir (char *pos, char *status_line)
  920. {
  921. int i = 0;
  922. char *x;
  923. char xx[2];
  924. char type[6];
  925. char *addr;
  926. char port[6];
  927. char *url;
  928. addr = malloc (MAX_IPV4_HOSTLENGTH + 1);
  929. if (addr == NULL)
  930. die (STATE_UNKNOWN, _("Could not allocate addr\n"));
  931. url = malloc (strcspn (pos, "\r\n"));
  932. if (url == NULL)
  933. die (STATE_UNKNOWN, _("Could not allocate url\n"));
  934. while (pos) {
  935. if (sscanf (pos, "%[Ll]%*[Oo]%*[Cc]%*[Aa]%*[Tt]%*[Ii]%*[Oo]%*[Nn]:%n", xx, &i) < 1) {
  936. pos += (size_t) strcspn (pos, "\r\n");
  937. pos += (size_t) strspn (pos, "\r\n");
  938. if (strlen(pos) == 0)
  939. die (STATE_UNKNOWN,
  940. _("UNKNOWN - Could not find redirect location - %s%s\n"),
  941. status_line, (display_html ? "</A>" : ""));
  942. continue;
  943. }
  944. pos += i;
  945. pos += strspn (pos, " \t\r\n");
  946. url = realloc (url, strcspn (pos, "\r\n"));
  947. if (url == NULL)
  948. die (STATE_UNKNOWN, _("could not allocate url\n"));
  949. /* URI_HTTP, URI_HOST, URI_PORT, URI_PATH */
  950. if (sscanf (pos, HD1, type, addr, port, url) == 4) {
  951. use_ssl = server_type_check (type);
  952. i = atoi (port);
  953. }
  954. /* URI_HTTP URI_HOST URI_PATH */
  955. else if (sscanf (pos, HD2, type, addr, url) == 3 ) {
  956. use_ssl = server_type_check (type);
  957. i = server_port_check (use_ssl);
  958. }
  959. /* URI_HTTP URI_HOST URI_PORT */
  960. else if(sscanf (pos, HD3, type, addr, port) == 3) {
  961. strcpy (url, HTTP_URL);
  962. use_ssl = server_type_check (type);
  963. i = atoi (port);
  964. }
  965. /* URI_HTTP URI_HOST */
  966. else if(sscanf (pos, HD4, type, addr) == 2) {
  967. strcpy (url, HTTP_URL);
  968. use_ssl = server_type_check (type);
  969. i = server_port_check (use_ssl);
  970. }
  971. /* URI_PATH */
  972. else if (sscanf (pos, HD5, url) == 1) {
  973. /* relative url */
  974. if ((url[0] != '/')) {
  975. if ((x = strrchr(server_url, '/')))
  976. *x = '\0';
  977. asprintf (&url, "%s/%s", server_url, url);
  978. }
  979. i = server_port;
  980. strcpy (type, server_type);
  981. strcpy (addr, host_name);
  982. }
  983. else {
  984. die (STATE_UNKNOWN,
  985. _("UNKNOWN - Could not parse redirect location - %s%s\n"),
  986. pos, (display_html ? "</A>" : ""));
  987. }
  988. break;
  989. } /* end while (pos) */
  990. if (++redir_depth > max_depth)
  991. die (STATE_WARNING,
  992. _("WARNING - maximum redirection depth %d exceeded - %s://%s:%d%s%s\n"),
  993. max_depth, type, addr, i, url, (display_html ? "</A>" : ""));
  994. if (server_port==i &&
  995. !strcmp(server_address, addr) &&
  996. (host_name && !strcmp(host_name, addr)) &&
  997. !strcmp(server_url, url))
  998. die (STATE_WARNING,
  999. _("WARNING - redirection creates an infinite loop - %s://%s:%d%s%s\n"),
  1000. type, addr, i, url, (display_html ? "</A>" : ""));
  1001. server_port = i;
  1002. strcpy (server_type, type);
  1003. free (host_name);
  1004. host_name = strdup (addr);
  1005. free (server_address);
  1006. server_address = strdup (addr);
  1007. free (server_url);
  1008. server_url = strdup (url);
  1009. check_http ();
  1010. }
  1011. int
  1012. server_type_check (const char *type)
  1013. {
  1014. if (strcmp (type, "https"))
  1015. return FALSE;
  1016. else
  1017. return TRUE;
  1018. }
  1019. int
  1020. server_port_check (int ssl_flag)
  1021. {
  1022. if (ssl_flag)
  1023. return HTTPS_PORT;
  1024. else
  1025. return HTTP_PORT;
  1026. }
  1027. #ifdef HAVE_SSL
  1028. int connect_SSL (void)
  1029. {
  1030. SSL_METHOD *meth;
  1031. asprintf (&randbuff, "%s", "qwertyuiopasdfghjklqwertyuiopasdfghjkl");
  1032. RAND_seed (randbuff, (int)strlen(randbuff));
  1033. if (verbose)
  1034. printf(_("SSL seeding: %s\n"), (RAND_status()==1 ? _("OK") : _("Failed")) );
  1035. /* Initialize SSL context */
  1036. SSLeay_add_ssl_algorithms ();
  1037. meth = SSLv23_client_method ();
  1038. SSL_load_error_strings ();
  1039. if ((ctx = SSL_CTX_new (meth)) == NULL) {
  1040. printf (_("CRITICAL - Cannot create SSL context.\n"));
  1041. return STATE_CRITICAL;
  1042. }
  1043. /* Initialize alarm signal handling */
  1044. signal (SIGALRM, socket_timeout_alarm_handler);
  1045. /* Set socket timeout */
  1046. alarm (socket_timeout);
  1047. /* Save start time */
  1048. gettimeofday (&tv, NULL);
  1049. /* Make TCP connection */
  1050. if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK) {
  1051. /* Do the SSL handshake */
  1052. if ((ssl = SSL_new (ctx)) != NULL) {
  1053. SSL_set_cipher_list(ssl, "ALL");
  1054. SSL_set_fd (ssl, sd);
  1055. if (SSL_connect (ssl) != -1)
  1056. return OK;
  1057. ERR_print_errors_fp (stderr);
  1058. }
  1059. else {
  1060. printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
  1061. }
  1062. SSL_free (ssl);
  1063. }
  1064. SSL_CTX_free (ctx);
  1065. close (sd);
  1066. return STATE_CRITICAL;
  1067. }
  1068. #endif
  1069. #ifdef HAVE_SSL
  1070. int
  1071. check_certificate (X509 ** certificate)
  1072. {
  1073. ASN1_STRING *tm;
  1074. int offset;
  1075. struct tm stamp;
  1076. int days_left;
  1077. /* Retrieve timestamp of certificate */
  1078. tm = X509_get_notAfter (*certificate);
  1079. /* Generate tm structure to process timestamp */
  1080. if (tm->type == V_ASN1_UTCTIME) {
  1081. if (tm->length < 10) {
  1082. printf (_("CRITICAL - Wrong time format in certificate.\n"));
  1083. return STATE_CRITICAL;
  1084. }
  1085. else {
  1086. stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
  1087. if (stamp.tm_year < 50)
  1088. stamp.tm_year += 100;
  1089. offset = 0;
  1090. }
  1091. }
  1092. else {
  1093. if (tm->length < 12) {
  1094. printf (_("CRITICAL - Wrong time format in certificate.\n"));
  1095. return STATE_CRITICAL;
  1096. }
  1097. else {
  1098. stamp.tm_year =
  1099. (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
  1100. (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
  1101. stamp.tm_year -= 1900;
  1102. offset = 2;
  1103. }
  1104. }
  1105. stamp.tm_mon =
  1106. (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
  1107. stamp.tm_mday =
  1108. (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
  1109. stamp.tm_hour =
  1110. (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
  1111. stamp.tm_min =
  1112. (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
  1113. stamp.tm_sec = 0;
  1114. stamp.tm_isdst = -1;
  1115. days_left = (mktime (&stamp) - time (NULL)) / 86400;
  1116. snprintf
  1117. (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
  1118. stamp.tm_mon + 1,
  1119. stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
  1120. if (days_left > 0 && days_left <= days_till_exp) {
  1121. printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
  1122. return STATE_WARNING;
  1123. }
  1124. if (days_left < 0) {
  1125. printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
  1126. return STATE_CRITICAL;
  1127. }
  1128. if (days_left == 0) {
  1129. printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
  1130. return STATE_WARNING;
  1131. }
  1132. printf (_("OK - Certificate will expire on %s.\n"), timestamp);
  1133. return STATE_OK;
  1134. }
  1135. #endif
  1136. char *perfd_time (double elapsed_time)
  1137. {
  1138. return fperfdata ("time", elapsed_time, "s",
  1139. check_warning_time, warning_time,
  1140. check_critical_time, critical_time,
  1141. TRUE, 0, FALSE, 0);
  1142. }
  1143. char *perfd_size (int page_len)
  1144. {
  1145. return perfdata ("size", page_len, "B",
  1146. (min_page_len>0?TRUE:FALSE), min_page_len,
  1147. (min_page_len>0?TRUE:FALSE), 0,
  1148. TRUE, 0, FALSE, 0);
  1149. }
  1150. int
  1151. my_recv (void)
  1152. {
  1153. int i;
  1154. #ifdef HAVE_SSL
  1155. if (use_ssl) {
  1156. i = SSL_read (ssl, buffer, MAX_INPUT_BUFFER - 1);
  1157. }
  1158. else {
  1159. i = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  1160. }
  1161. #else
  1162. i = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  1163. #endif
  1164. return i;
  1165. }
  1166. int
  1167. my_close (void)
  1168. {
  1169. #ifdef HAVE_SSL
  1170. if (use_ssl == TRUE) {
  1171. SSL_shutdown (ssl);
  1172. SSL_free (ssl);
  1173. SSL_CTX_free (ctx);
  1174. return 0;
  1175. }
  1176. else {
  1177. #endif
  1178. return close (sd);
  1179. #ifdef HAVE_SSL
  1180. }
  1181. #endif
  1182. }
  1183. void
  1184. print_help (void)
  1185. {
  1186. print_revision (progname, revision);
  1187. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  1188. printf (COPYRIGHT, copyright, email);
  1189. printf (_("\
  1190. This plugin tests the HTTP service on the specified host. It can test\n\
  1191. normal (http) and secure (https) servers, follow redirects, search for\n\
  1192. strings and regular expressions, check connection times, and report on\n\
  1193. certificate expiration times.\n\n"));
  1194. print_usage ();
  1195. printf (_("NOTE: One or both of -H and -I must be specified\n"));
  1196. printf (_(UT_HELP_VRSN));
  1197. printf (_("\
  1198. -H, --hostname=ADDRESS\n\
  1199. Host name argument for servers using host headers (virtual host)\n\
  1200. Append a port to include it in the header (eg: example.com:5000)\n\
  1201. -I, --IP-address=ADDRESS\n\
  1202. IP address or name (use numeric address if possible to bypass DNS lookup).\n\
  1203. -p, --port=INTEGER\n\
  1204. Port number (default: %d)\n"), HTTP_PORT);
  1205. printf (_(UT_IPv46));
  1206. #ifdef HAVE_SSL
  1207. printf (_("\
  1208. -S, --ssl\n\
  1209. Connect via SSL\n\
  1210. -C, --certificate=INTEGER\n\
  1211. Minimum number of days a certificate has to be valid.\n\
  1212. (when this option is used the url is not checked.)\n"));
  1213. #endif
  1214. printf (_("\
  1215. -e, --expect=STRING\n\
  1216. String to expect in first (status) line of server response (default: %s)\n\
  1217. If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)\n\
  1218. -s, --string=STRING\n\
  1219. String to expect in the content\n\
  1220. -u, --url=PATH\n\
  1221. URL to GET or POST (default: /)\n\
  1222. -P, --post=STRING\n\
  1223. URL encoded http POST data\n\
  1224. -N, --no-body\n\
  1225. Don't wait for document body: stop reading after headers.\n\
  1226. (Note that this still does an HTTP GET or POST, not a HEAD.)\n\
  1227. -M, --max-age=SECONDS\n\
  1228. Warn if document is more than SECONDS old. the number can also be of \n\
  1229. the form \"10m\" for minutes, \"10h\" for hours, or \"10d\" for days.\n\
  1230. -T, --content-type=STRING\n\
  1231. specify Content-Type header media type when POSTing\n"), HTTP_EXPECT);
  1232. #ifdef HAVE_REGEX_H
  1233. printf (_("\
  1234. -l, --linespan\n\
  1235. Allow regex to span newlines (must precede -r or -R)\n\
  1236. -r, --regex, --ereg=STRING\n\
  1237. Search page for regex STRING\n\
  1238. -R, --eregi=STRING\n\
  1239. Search page for case-insensitive regex STRING\n"));
  1240. #endif
  1241. printf (_("\
  1242. -a, --authorization=AUTH_PAIR\n\
  1243. Username:password on sites with basic authentication\n\
  1244. -A, --useragent=STRING\n\
  1245. String to be sent in http header as \"User Agent\"\n\
  1246. -k, --header=STRING\n\
  1247. Any other tags to be sent in http header, separated by semicolon\n\
  1248. -L, --link=URL\n\
  1249. Wrap output in HTML link (obsoleted by urlize)\n\
  1250. -f, --onredirect=<ok|warning|critical|follow>\n\
  1251. How to handle redirected pages\n\
  1252. -m, --min=INTEGER\n\
  1253. Minimum page size required (bytes)\n"));
  1254. printf (_(UT_WARN_CRIT));
  1255. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  1256. printf (_(UT_VERBOSE));
  1257. printf (_("\
  1258. This plugin will attempt to open an HTTP connection with the host. Successful\n\
  1259. connects return STATE_OK, refusals and timeouts return STATE_CRITICAL, other\n\
  1260. errors return STATE_UNKNOWN. Successful connects, but incorrect reponse\n\
  1261. messages from the host result in STATE_WARNING return values. If you are\n\
  1262. checking a virtual server that uses 'host headers' you must supply the FQDN\n\
  1263. (fully qualified domain name) as the [host_name] argument.\n"));
  1264. #ifdef HAVE_SSL
  1265. printf (_("\n\
  1266. This plugin can also check whether an SSL enabled web server is able to\n\
  1267. serve content (optionally within a specified time) or whether the X509 \n\
  1268. certificate is still valid for the specified number of days.\n"));
  1269. printf (_("\n\
  1270. CHECK CONTENT: check_http -w 5 -c 10 --ssl www.verisign.com\n\n\
  1271. When the 'www.verisign.com' server returns its content within 5 seconds, a\n\
  1272. STATE_OK will be returned. When the server returns its content but exceeds\n\
  1273. the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,\n\
  1274. a STATE_CRITICAL will be returned.\n\n"));
  1275. printf (_("\
  1276. CHECK CERTIFICATE: check_http www.verisign.com -C 14\n\n\
  1277. When the certificate of 'www.verisign.com' is valid for more than 14 days, a\n\
  1278. STATE_OK is returned. When the certificate is still valid, but for less than\n\
  1279. 14 days, a STATE_WARNING is returned. A STATE_CRITICAL will be returned when\n\
  1280. the certificate is expired.\n"));
  1281. #endif
  1282. printf (_(UT_SUPPORT));
  1283. }
  1284. void
  1285. print_usage (void)
  1286. {
  1287. printf ("\
  1288. Usage: %s -H <vhost> | -I <IP-address>) [-u <uri>] [-p <port>]\n\
  1289. [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L]\n\
  1290. [-a auth] [-f <ok | warn | critcal | follow>] [-e <expect>]\n\
  1291. [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n\
  1292. [-P string] [-m min_pg_size] [-4|-6] [-N] [-M <age>]\n\
  1293. [-A string] [-k string]\n", progname);
  1294. }