check_tcp.c 21 KB

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