check_tcp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. /* progname "check_tcp" changes depending on symlink called */
  16. char *progname;
  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. #ifdef HAVE_SSL_H
  24. # include <rsa.h>
  25. # include <crypto.h>
  26. # include <x509.h>
  27. # include <pem.h>
  28. # include <ssl.h>
  29. # include <err.h>
  30. #else
  31. # ifdef HAVE_OPENSSL_SSL_H
  32. # include <openssl/rsa.h>
  33. # include <openssl/crypto.h>
  34. # include <openssl/x509.h>
  35. # include <openssl/pem.h>
  36. # include <openssl/ssl.h>
  37. # include <openssl/err.h>
  38. # endif
  39. #endif
  40. #ifdef HAVE_SSL
  41. int check_cert = FALSE;
  42. int days_till_exp;
  43. char *randbuff = "";
  44. SSL_CTX *ctx;
  45. SSL *ssl;
  46. X509 *server_cert;
  47. int connect_SSL (void);
  48. int check_certificate (X509 **);
  49. #endif
  50. enum {
  51. TCP_PROTOCOL = 1,
  52. UDP_PROTOCOL = 2,
  53. MAXBUF = 1024
  54. };
  55. int process_arguments (int, char **);
  56. int my_recv (void);
  57. void print_help (void);
  58. void print_usage (void);
  59. char *SERVICE = NULL;
  60. char *SEND = NULL;
  61. char *EXPECT = NULL;
  62. char *QUIT = NULL;
  63. int PROTOCOL = 0;
  64. int PORT = 0;
  65. char timestamp[17] = "";
  66. int server_port = 0;
  67. char *server_address = NULL;
  68. char *server_send = NULL;
  69. char *server_quit = NULL;
  70. char **server_expect = NULL;
  71. size_t server_expect_count = 0;
  72. int maxbytes = 0;
  73. char **warn_codes = NULL;
  74. size_t warn_codes_count = 0;
  75. char **crit_codes = NULL;
  76. size_t crit_codes_count = 0;
  77. unsigned int delay = 0;
  78. double warning_time = 0;
  79. int check_warning_time = FALSE;
  80. double critical_time = 0;
  81. int check_critical_time = FALSE;
  82. int hide_output = FALSE;
  83. double elapsed_time = 0;
  84. long microsec;
  85. int verbose = FALSE;
  86. int use_ssl = FALSE;
  87. int sd = 0;
  88. char *buffer;
  89. int
  90. main (int argc, char **argv)
  91. {
  92. int result = STATE_UNKNOWN;
  93. int i;
  94. char *status;
  95. struct timeval tv;
  96. setlocale (LC_ALL, "");
  97. bindtextdomain (PACKAGE, LOCALEDIR);
  98. textdomain (PACKAGE);
  99. if (strstr (argv[0], "check_udp")) {
  100. progname = strdup ("check_udp");
  101. SERVICE = strdup ("UDP");
  102. SEND = NULL;
  103. EXPECT = NULL;
  104. QUIT = NULL;
  105. PROTOCOL = UDP_PROTOCOL;
  106. PORT = 0;
  107. }
  108. else if (strstr (argv[0], "check_tcp")) {
  109. progname = strdup ("check_tcp");
  110. SERVICE = strdup ("TCP");
  111. SEND = NULL;
  112. EXPECT = NULL;
  113. QUIT = NULL;
  114. PROTOCOL = TCP_PROTOCOL;
  115. PORT = 0;
  116. }
  117. else if (strstr (argv[0], "check_ftp")) {
  118. progname = strdup ("check_ftp");
  119. SERVICE = strdup ("FTP");
  120. SEND = NULL;
  121. EXPECT = strdup ("220");
  122. QUIT = strdup ("QUIT\r\n");
  123. PROTOCOL = TCP_PROTOCOL;
  124. PORT = 21;
  125. }
  126. else if (strstr (argv[0], "check_smtp")) {
  127. progname = strdup ("check_smtp");
  128. SERVICE = strdup ("SMTP");
  129. SEND = NULL;
  130. EXPECT = strdup ("220");
  131. QUIT = strdup ("QUIT\r\n");
  132. PROTOCOL = TCP_PROTOCOL;
  133. PORT = 25;
  134. }
  135. else if (strstr (argv[0], "check_pop")) {
  136. progname = strdup ("check_pop");
  137. SERVICE = strdup ("POP");
  138. SEND = NULL;
  139. EXPECT = strdup ("+OK");
  140. QUIT = strdup ("QUIT\r\n");
  141. PROTOCOL = TCP_PROTOCOL;
  142. PORT = 110;
  143. }
  144. else if (strstr (argv[0], "check_imap")) {
  145. progname = strdup ("check_imap");
  146. SERVICE = strdup ("IMAP");
  147. SEND = NULL;
  148. EXPECT = strdup ("* OK");
  149. QUIT = strdup ("a1 LOGOUT\r\n");
  150. PROTOCOL = TCP_PROTOCOL;
  151. PORT = 143;
  152. }
  153. #ifdef HAVE_SSL
  154. else if (strstr(argv[0],"check_simap")) {
  155. progname = strdup ("check_simap");
  156. SERVICE = strdup ("SIMAP");
  157. SEND=NULL;
  158. EXPECT = strdup ("* OK");
  159. QUIT = strdup ("a1 LOGOUT\r\n");
  160. PROTOCOL=TCP_PROTOCOL;
  161. use_ssl=TRUE;
  162. PORT=993;
  163. }
  164. else if (strstr(argv[0],"check_spop")) {
  165. progname = strdup ("check_spop");
  166. SERVICE = strdup ("SPOP");
  167. SEND=NULL;
  168. EXPECT = strdup ("+OK");
  169. QUIT = strdup ("QUIT\r\n");
  170. PROTOCOL=TCP_PROTOCOL;
  171. use_ssl=TRUE;
  172. PORT=995;
  173. }
  174. else if (strstr(argv[0],"check_jabber")) {
  175. progname = strdup("check_jabber");
  176. SERVICE = strdup("JABBER");
  177. SEND = strdup("<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n");
  178. EXPECT = strdup("<?xml version=\'1.0\'?><stream:stream xmlns:stream=\'http://etherx.jabber.org/streams\'");
  179. QUIT = strdup("</stream:stream>\n");
  180. PROTOCOL=TCP_PROTOCOL;
  181. use_ssl=TRUE;
  182. PORT = 5222;
  183. }
  184. else if (strstr (argv[0], "check_nntps")) {
  185. progname = strdup("check_nntps");
  186. SERVICE = strdup("NNTPS");
  187. SEND = NULL;
  188. EXPECT = NULL;
  189. server_expect = realloc (server_expect, ++server_expect_count);
  190. asprintf (&server_expect[server_expect_count - 1], "200");
  191. server_expect = realloc (server_expect, ++server_expect_count);
  192. asprintf (&server_expect[server_expect_count - 1], "201");
  193. QUIT = strdup("QUIT\r\n");
  194. PROTOCOL = TCP_PROTOCOL;
  195. use_ssl=TRUE;
  196. PORT = 563;
  197. }
  198. #endif
  199. else if (strstr (argv[0], "check_nntp")) {
  200. progname = strdup ("check_nntp");
  201. SERVICE = strdup ("NNTP");
  202. SEND = NULL;
  203. EXPECT = NULL;
  204. server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
  205. asprintf (&server_expect[server_expect_count - 1], "200");
  206. server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
  207. asprintf (&server_expect[server_expect_count - 1], "201");
  208. asprintf (&QUIT, "QUIT\r\n");
  209. PROTOCOL = TCP_PROTOCOL;
  210. PORT = 119;
  211. }
  212. else {
  213. progname = strdup ("check_tcp");
  214. usage (_("CRITICAL - Generic check_tcp called with unknown service\n"));
  215. }
  216. server_address = strdup ("127.0.0.1");
  217. server_port = PORT;
  218. server_send = SEND;
  219. server_quit = QUIT;
  220. status = strdup ("");
  221. if (process_arguments (argc, argv) != TRUE)
  222. usage4 (_("Could not parse arguments"));
  223. /* use default expect if none listed in process_arguments() */
  224. if (EXPECT && server_expect_count == 0) {
  225. server_expect = malloc (sizeof (char *) * (++server_expect_count));
  226. server_expect[server_expect_count - 1] = EXPECT;
  227. }
  228. /* initialize alarm signal handling */
  229. signal (SIGALRM, socket_timeout_alarm_handler);
  230. /* set socket timeout */
  231. alarm (socket_timeout);
  232. /* try to connect to the host at the given port number */
  233. gettimeofday (&tv, NULL);
  234. #ifdef HAVE_SSL
  235. if (use_ssl && check_cert == TRUE) {
  236. if (connect_SSL () != OK)
  237. die (STATE_CRITICAL,"CRITICAL - Could not make SSL connection\n");
  238. if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) {
  239. result = check_certificate (&server_cert);
  240. X509_free(server_cert);
  241. }
  242. else {
  243. printf("CRITICAL - Cannot retrieve server certificate.\n");
  244. result = STATE_CRITICAL;
  245. }
  246. SSL_shutdown (ssl);
  247. SSL_free (ssl);
  248. SSL_CTX_free (ctx);
  249. close (sd);
  250. return result;
  251. }
  252. else if (use_ssl)
  253. result = connect_SSL ();
  254. else
  255. #endif
  256. {
  257. if (PROTOCOL == UDP_PROTOCOL)
  258. result = my_udp_connect (server_address, server_port, &sd);
  259. else
  260. /* default is TCP */
  261. result = my_tcp_connect (server_address, server_port, &sd);
  262. }
  263. if (result == STATE_CRITICAL)
  264. return STATE_CRITICAL;
  265. if (server_send != NULL) { /* Something to send? */
  266. asprintf (&server_send, "%s\r\n", server_send);
  267. #ifdef HAVE_SSL
  268. if (use_ssl)
  269. SSL_write(ssl, server_send, (int)strlen(server_send));
  270. else
  271. #endif
  272. send (sd, server_send, strlen(server_send), 0);
  273. }
  274. if (delay > 0) {
  275. tv.tv_sec += delay;
  276. sleep (delay);
  277. }
  278. if (server_send || server_expect_count > 0) {
  279. buffer = malloc (MAXBUF);
  280. memset (buffer, '\0', MAXBUF);
  281. /* watch for the expect string */
  282. while ((i = my_recv ()) > 0) {
  283. buffer[i] = '\0';
  284. asprintf (&status, "%s%s", status, buffer);
  285. if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
  286. break;
  287. if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
  288. break;
  289. }
  290. /* return a CRITICAL status if we couldn't read any data */
  291. if (strlen(status) == 0)
  292. die (STATE_CRITICAL, _("No data received from host\n"));
  293. strip (status);
  294. if (status && verbose)
  295. printf ("%s\n", status);
  296. if (server_expect_count > 0) {
  297. for (i = 0;; i++) {
  298. if (verbose)
  299. printf ("%d %d\n", i, (int)server_expect_count);
  300. if (i >= (int)server_expect_count)
  301. die (STATE_WARNING, _("Invalid response from host\n"));
  302. if (strstr (status, server_expect[i]))
  303. break;
  304. }
  305. }
  306. }
  307. if (server_quit != NULL) {
  308. #ifdef HAVE_SSL
  309. if (use_ssl) {
  310. SSL_write (ssl, server_quit, (int)strlen(server_quit));
  311. SSL_shutdown (ssl);
  312. SSL_free (ssl);
  313. SSL_CTX_free (ctx);
  314. }
  315. else {
  316. #endif
  317. send (sd, server_quit, strlen (server_quit), 0);
  318. #ifdef HAVE_SSL
  319. }
  320. #endif
  321. }
  322. /* close the connection */
  323. if (sd)
  324. close (sd);
  325. microsec = deltime (tv);
  326. elapsed_time = (double)microsec / 1.0e6;
  327. if (check_critical_time == TRUE && elapsed_time > critical_time)
  328. result = STATE_CRITICAL;
  329. else if (check_warning_time == TRUE && elapsed_time > warning_time)
  330. result = STATE_WARNING;
  331. /* reset the alarm */
  332. alarm (0);
  333. printf
  334. (_("%s %s%s - %.3f second response time on port %d"),
  335. SERVICE,
  336. state_text (result),
  337. (was_refused) ? " (refused)" : "",
  338. elapsed_time, server_port);
  339. if (hide_output == FALSE && status && strlen(status) > 0)
  340. printf (" [%s]", status);
  341. printf (" |%s\n", fperfdata ("time", elapsed_time, "s",
  342. TRUE, warning_time,
  343. TRUE, critical_time,
  344. TRUE, 0,
  345. TRUE, socket_timeout));
  346. return result;
  347. }
  348. /* process command-line arguments */
  349. int
  350. process_arguments (int argc, char **argv)
  351. {
  352. int c;
  353. int option = 0;
  354. static struct option longopts[] = {
  355. {"hostname", required_argument, 0, 'H'},
  356. {"critical-time", required_argument, 0, 'c'},
  357. {"warning-time", required_argument, 0, 'w'},
  358. {"critical-codes", required_argument, 0, 'C'},
  359. {"warning-codes", required_argument, 0, 'W'},
  360. {"timeout", required_argument, 0, 't'},
  361. {"protocol", required_argument, 0, 'P'},
  362. {"port", required_argument, 0, 'p'},
  363. {"send", required_argument, 0, 's'},
  364. {"expect", required_argument, 0, 'e'},
  365. {"maxbytes", required_argument, 0, 'm'},
  366. {"quit", required_argument, 0, 'q'},
  367. {"jail", required_argument, 0, 'j'},
  368. {"delay", required_argument, 0, 'd'},
  369. {"refuse", required_argument, 0, 'r'},
  370. {"use-ipv4", no_argument, 0, '4'},
  371. {"use-ipv6", no_argument, 0, '6'},
  372. {"verbose", no_argument, 0, 'v'},
  373. {"version", no_argument, 0, 'V'},
  374. {"help", no_argument, 0, 'h'},
  375. #ifdef HAVE_SSL
  376. {"ssl", no_argument, 0, 'S'},
  377. {"certificate", required_argument, 0, 'D'},
  378. #endif
  379. {0, 0, 0, 0}
  380. };
  381. if (argc < 2)
  382. usage ("No arguments found\n");
  383. /* backwards compatibility */
  384. for (c = 1; c < argc; c++) {
  385. if (strcmp ("-to", argv[c]) == 0)
  386. strcpy (argv[c], "-t");
  387. else if (strcmp ("-wt", argv[c]) == 0)
  388. strcpy (argv[c], "-w");
  389. else if (strcmp ("-ct", argv[c]) == 0)
  390. strcpy (argv[c], "-c");
  391. }
  392. if (!is_option (argv[1])) {
  393. server_address = argv[1];
  394. argv[1] = argv[0];
  395. argv = &argv[1];
  396. argc--;
  397. }
  398. while (1) {
  399. c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:j:D",
  400. longopts, &option);
  401. if (c == -1 || c == EOF || c == 1)
  402. break;
  403. switch (c) {
  404. case '?': /* print short usage statement if args not parsable */
  405. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  406. print_usage ();
  407. exit (STATE_UNKNOWN);
  408. case 'h': /* help */
  409. print_help ();
  410. exit (STATE_OK);
  411. case 'V': /* version */
  412. print_revision (progname, "$Revision$");
  413. exit (STATE_OK);
  414. case 'v': /* verbose mode */
  415. verbose = TRUE;
  416. break;
  417. case '4':
  418. address_family = AF_INET;
  419. break;
  420. case '6':
  421. #ifdef USE_IPV6
  422. address_family = AF_INET6;
  423. #else
  424. usage (_("IPv6 support not available\n"));
  425. #endif
  426. break;
  427. case 'H': /* hostname */
  428. if (is_host (optarg) == FALSE)
  429. usage2 (_("invalid hostname/address"), optarg);
  430. server_address = optarg;
  431. break;
  432. case 'c': /* critical */
  433. if (!is_intnonneg (optarg))
  434. usage (_("Critical threshold must be a positive integer\n"));
  435. else
  436. critical_time = strtod (optarg, NULL);
  437. check_critical_time = TRUE;
  438. break;
  439. case 'j': /* hide output */
  440. hide_output = TRUE;
  441. break;
  442. case 'w': /* warning */
  443. if (!is_intnonneg (optarg))
  444. usage (_("Warning threshold must be a positive integer\n"));
  445. else
  446. warning_time = strtod (optarg, NULL);
  447. check_warning_time = TRUE;
  448. break;
  449. case 'C':
  450. crit_codes = realloc (crit_codes, ++crit_codes_count);
  451. crit_codes[crit_codes_count - 1] = optarg;
  452. break;
  453. case 'W':
  454. warn_codes = realloc (warn_codes, ++warn_codes_count);
  455. warn_codes[warn_codes_count - 1] = optarg;
  456. break;
  457. case 't': /* timeout */
  458. if (!is_intpos (optarg))
  459. usage (_("Timeout interval must be a positive integer\n"));
  460. else
  461. socket_timeout = atoi (optarg);
  462. break;
  463. case 'p': /* port */
  464. if (!is_intpos (optarg))
  465. usage (_("Port must be a positive integer\n"));
  466. else
  467. server_port = atoi (optarg);
  468. break;
  469. case 's':
  470. server_send = optarg;
  471. break;
  472. case 'e': /* expect string (may be repeated) */
  473. EXPECT = NULL;
  474. if (server_expect_count == 0)
  475. server_expect = malloc (sizeof (char *) * (++server_expect_count));
  476. else
  477. server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
  478. server_expect[server_expect_count - 1] = optarg;
  479. break;
  480. case 'm':
  481. if (!is_intpos (optarg))
  482. usage (_("Maxbytes must be a positive integer\n"));
  483. else
  484. maxbytes = atoi (optarg);
  485. case 'q':
  486. asprintf(&server_quit, "%s\r\n", optarg);
  487. break;
  488. case 'r':
  489. if (!strncmp(optarg,"ok",2))
  490. econn_refuse_state = STATE_OK;
  491. else if (!strncmp(optarg,"warn",4))
  492. econn_refuse_state = STATE_WARNING;
  493. else if (!strncmp(optarg,"crit",4))
  494. econn_refuse_state = STATE_CRITICAL;
  495. else
  496. usage (_("Refuse must be one of ok, warn, crit\n"));
  497. break;
  498. case 'd':
  499. if (is_intpos (optarg))
  500. delay = atoi (optarg);
  501. else
  502. usage (_("Delay must be a positive integer\n"));
  503. break;
  504. case 'D': /* Check SSL cert validity - days 'til certificate expiration */
  505. #ifdef HAVE_SSL
  506. if (!is_intnonneg (optarg))
  507. usage2 ("invalid certificate expiration period", optarg);
  508. days_till_exp = atoi (optarg);
  509. check_cert = TRUE;
  510. use_ssl = TRUE;
  511. break;
  512. case 'S':
  513. use_ssl = TRUE;
  514. #else
  515. die (STATE_UNKNOWN, "SSL support not available. Install OpenSSL and recompile.");
  516. #endif
  517. break;
  518. }
  519. }
  520. if (server_address == NULL)
  521. usage (_("You must provide a server address\n"));
  522. return OK;
  523. }
  524. #ifdef HAVE_SSL
  525. int
  526. connect_SSL (void)
  527. {
  528. SSL_METHOD *meth;
  529. /* Initialize SSL context */
  530. SSLeay_add_ssl_algorithms ();
  531. meth = SSLv23_client_method ();
  532. SSL_load_error_strings ();
  533. OpenSSL_add_all_algorithms();
  534. if ((ctx = SSL_CTX_new (meth)) == NULL)
  535. {
  536. printf (_("CRITICAL - Cannot create SSL context.\n"));
  537. return STATE_CRITICAL;
  538. }
  539. /* Initialize alarm signal handling */
  540. signal (SIGALRM, socket_timeout_alarm_handler);
  541. /* Set socket timeout */
  542. alarm (socket_timeout);
  543. /* Save start time */
  544. time (&start_time);
  545. /* Make TCP connection */
  546. if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
  547. {
  548. /* Do the SSL handshake */
  549. if ((ssl = SSL_new (ctx)) != NULL)
  550. {
  551. SSL_set_fd (ssl, sd);
  552. if (SSL_connect(ssl) == 1)
  553. return OK;
  554. /* ERR_print_errors_fp (stderr); */
  555. printf (_("CRITICAL - Cannot make SSL connection "));
  556. ERR_print_errors_fp (stdout);
  557. /* printf("\n"); */
  558. }
  559. else
  560. {
  561. printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
  562. }
  563. SSL_free (ssl);
  564. }
  565. SSL_CTX_free (ctx);
  566. close (sd);
  567. return STATE_CRITICAL;
  568. }
  569. #endif
  570. #ifdef HAVE_SSL
  571. int
  572. check_certificate (X509 ** certificate)
  573. {
  574. ASN1_STRING *tm;
  575. int offset;
  576. struct tm stamp;
  577. int days_left;
  578. /* Retrieve timestamp of certificate */
  579. tm = X509_get_notAfter (*certificate);
  580. /* Generate tm structure to process timestamp */
  581. if (tm->type == V_ASN1_UTCTIME) {
  582. if (tm->length < 10) {
  583. printf ("CRITICAL - Wrong time format in certificate.\n");
  584. return STATE_CRITICAL;
  585. }
  586. else {
  587. stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
  588. if (stamp.tm_year < 50)
  589. stamp.tm_year += 100;
  590. offset = 0;
  591. }
  592. }
  593. else {
  594. if (tm->length < 12) {
  595. printf ("CRITICAL - Wrong time format in certificate.\n");
  596. return STATE_CRITICAL;
  597. }
  598. else {
  599. stamp.tm_year =
  600. (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
  601. (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
  602. stamp.tm_year -= 1900;
  603. offset = 2;
  604. }
  605. }
  606. stamp.tm_mon =
  607. (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
  608. stamp.tm_mday =
  609. (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
  610. stamp.tm_hour =
  611. (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
  612. stamp.tm_min =
  613. (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
  614. stamp.tm_sec = 0;
  615. stamp.tm_isdst = -1;
  616. days_left = (mktime (&stamp) - time (NULL)) / 86400;
  617. snprintf
  618. (timestamp, 16, "%02d/%02d/%04d %02d:%02d",
  619. stamp.tm_mon + 1,
  620. stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
  621. if (days_left > 0 && days_left <= days_till_exp) {
  622. printf ("Certificate expires in %d day(s) (%s).\n", days_left, timestamp);
  623. return STATE_WARNING;
  624. }
  625. if (days_left < 0) {
  626. printf ("Certificate expired on %s.\n", timestamp);
  627. return STATE_CRITICAL;
  628. }
  629. if (days_left == 0) {
  630. printf ("Certificate expires today (%s).\n", timestamp);
  631. return STATE_WARNING;
  632. }
  633. printf ("Certificate will expire on %s.\n", timestamp);
  634. return STATE_OK;
  635. }
  636. #endif
  637. int
  638. my_recv (void)
  639. {
  640. int i;
  641. #ifdef HAVE_SSL
  642. if (use_ssl) {
  643. i = SSL_read (ssl, buffer, MAXBUF - 1);
  644. }
  645. else {
  646. #endif
  647. i = read (sd, buffer, MAXBUF - 1);
  648. #ifdef HAVE_SSL
  649. }
  650. #endif
  651. return i;
  652. }
  653. void
  654. print_help (void)
  655. {
  656. print_revision (progname, revision);
  657. printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
  658. printf (_(COPYRIGHT), copyright, email);
  659. printf (_("This plugin tests %s connections with the specified host.\n\n"),
  660. SERVICE);
  661. print_usage ();
  662. printf (_(UT_HELP_VRSN));
  663. printf (_(UT_HOST_PORT), 'p', "none");
  664. printf (_(UT_IPv46));
  665. printf (_("\
  666. -s, --send=STRING\n\
  667. String to send to the server\n\
  668. -e, --expect=STRING\n\
  669. String to expect in server response\n\
  670. -q, --quit=STRING\n\
  671. String to send server to initiate a clean close of the connection\n"));
  672. printf (_("\
  673. -r, --refuse=ok|warn|crit\n\
  674. Accept tcp refusals with states ok, warn, crit (default: crit)\n\
  675. -j, --jail\n\
  676. Hide output from TCP socket\n\
  677. -m, --maxbytes=INTEGER\n\
  678. Close connection once more than this number of bytes are received\n\
  679. -d, --delay=INTEGER\n\
  680. Seconds to wait between sending string and polling for response\n"));
  681. #ifdef HAVE_SSL
  682. printf (_("\
  683. -D, --certificate=INTEGER\n\
  684. Minimum number of days a certificate has to be valid.\n\
  685. -S, --ssl\n\
  686. Use SSL for the connection.\n"));
  687. #endif
  688. printf (_(UT_WARN_CRIT));
  689. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  690. printf (_(UT_VERBOSE));
  691. printf (_(UT_SUPPORT));
  692. }
  693. void
  694. print_usage (void)
  695. {
  696. printf (_("\
  697. Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
  698. [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
  699. [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
  700. [-r <refuse state>] [-v] [-4|-6] [-j] [-D <days to cert expiry>]\n\
  701. [-S <use SSL>]\n"), progname);
  702. printf (" %s (-h|--help)\n", progname);
  703. printf (" %s (-V|--version)\n", progname);
  704. }