check_tcp.c 21 KB

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