check_tcp.c 21 KB

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