check_tcp.c 21 KB

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