check_tcp.c 21 KB

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