check_tcp.c 20 KB

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