check_tcp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*****************************************************************************
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *
  17. *****************************************************************************/
  18. #include "config.h"
  19. #include "common.h"
  20. #include "netutils.h"
  21. #include "utils.h"
  22. #ifdef HAVE_SSL_H
  23. # include <rsa.h>
  24. # include <crypto.h>
  25. # include <x509.h>
  26. # include <pem.h>
  27. # include <ssl.h>
  28. # include <err.h>
  29. #else
  30. # ifdef HAVE_OPENSSL_SSL_H
  31. # include <openssl/rsa.h>
  32. # include <openssl/crypto.h>
  33. # include <openssl/x509.h>
  34. # include <openssl/pem.h>
  35. # include <openssl/ssl.h>
  36. # include <openssl/err.h>
  37. # endif
  38. #endif
  39. #ifdef HAVE_SSL
  40. SSL_CTX *ctx;
  41. SSL *ssl;
  42. int connect_SSL (void);
  43. #endif
  44. enum {
  45. TCP_PROTOCOL = 1,
  46. UDP_PROTOCOL = 2,
  47. MAXBUF = 1024
  48. };
  49. int process_arguments (int, char **);
  50. void print_usage (void);
  51. void print_help (void);
  52. int my_recv (void);
  53. char *SERVICE = NULL;
  54. char *SEND = NULL;
  55. char *EXPECT = NULL;
  56. char *QUIT = NULL;
  57. int PROTOCOL = 0;
  58. int PORT = 0;
  59. int server_port = 0;
  60. char *server_address = NULL;
  61. char *server_send = NULL;
  62. char *server_quit = NULL;
  63. char **server_expect = NULL;
  64. int server_expect_count = 0;
  65. int maxbytes = 0;
  66. char **warn_codes = NULL;
  67. int warn_codes_count = 0;
  68. char **crit_codes = NULL;
  69. int crit_codes_count = 0;
  70. int delay = 0;
  71. double warning_time = 0;
  72. int check_warning_time = FALSE;
  73. double critical_time = 0;
  74. int check_critical_time = FALSE;
  75. double elapsed_time = 0;
  76. int verbose = FALSE;
  77. int use_ssl = FALSE;
  78. int sd = 0;
  79. char *buffer = "";
  80. /* progname changes depending on symlink called */
  81. char *progname = "check_tcp";
  82. const char *revision = "$Revision$";
  83. const char *copyright = "2002-2003";
  84. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  85. void
  86. print_usage (void)
  87. {
  88. printf (_("\
  89. Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
  90. [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
  91. [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
  92. [-r <refuse state>] [-v] [-4|-6]\n"), progname);
  93. printf (" %s (-h|--help)\n", progname);
  94. printf (" %s (-V|--version)\n", progname);
  95. }
  96. void
  97. print_help (void)
  98. {
  99. print_revision (progname, revision);
  100. printf (_(COPYRIGHT), copyright, email);
  101. printf (_("This plugin tests %s connections with the specified host.\n\n"),
  102. SERVICE);
  103. print_usage ();
  104. printf (_(HELP_VRSN));
  105. printf (_(HOST_PORT), 'p', "none");
  106. printf (_(IPv46));
  107. printf (_("\
  108. -s, --send=STRING\n\
  109. String to send to the server\n\
  110. -e, --expect=STRING\n\
  111. String to expect in server response\n\
  112. -q, --quit=STRING\n\
  113. String to send server to initiate a clean close of the connection\n"));
  114. printf (_("\
  115. -r, --refuse=ok|warn|crit\n\
  116. Accept tcp refusals with states ok, warn, crit (default: crit)\n\
  117. -m, --maxbytes=INTEGER\n\
  118. Close connection once more than this number of bytes are received\n\
  119. -d, --delay=INTEGER\n\
  120. Seconds to wait between sending string and polling for response\n"));
  121. printf (_(WARN_CRIT_TO), DEFAULT_SOCKET_TIMEOUT);
  122. printf (_(VRBS));
  123. support ();
  124. }
  125. int
  126. main (int argc, char **argv)
  127. {
  128. int result;
  129. int i;
  130. char *status = "";
  131. struct timeval tv;
  132. setlocale (LC_ALL, "");
  133. bindtextdomain (PACKAGE, LOCALEDIR);
  134. textdomain (PACKAGE);
  135. if (strstr (argv[0], "check_udp")) {
  136. progname = strdup ("check_udp");
  137. SERVICE = strdup ("UDP");
  138. SEND = NULL;
  139. EXPECT = NULL;
  140. QUIT = NULL;
  141. PROTOCOL = UDP_PROTOCOL;
  142. PORT = 0;
  143. }
  144. else if (strstr (argv[0], "check_tcp")) {
  145. progname = strdup ("check_tcp");
  146. SERVICE = strdup ("TCP");
  147. SEND = NULL;
  148. EXPECT = NULL;
  149. QUIT = NULL;
  150. PROTOCOL = TCP_PROTOCOL;
  151. PORT = 0;
  152. }
  153. else if (strstr (argv[0], "check_ftp")) {
  154. progname = strdup ("check_ftp");
  155. SERVICE = strdup ("FTP");
  156. SEND = NULL;
  157. EXPECT = strdup ("220");
  158. QUIT = strdup ("QUIT\r\n");
  159. PROTOCOL = TCP_PROTOCOL;
  160. PORT = 21;
  161. }
  162. else if (strstr (argv[0], "check_smtp")) {
  163. progname = strdup ("check_smtp");
  164. SERVICE = strdup ("SMTP");
  165. SEND = NULL;
  166. EXPECT = strdup ("220");
  167. QUIT = strdup ("QUIT\r\n");
  168. PROTOCOL = TCP_PROTOCOL;
  169. PORT = 25;
  170. }
  171. else if (strstr (argv[0], "check_pop")) {
  172. progname = strdup ("check_pop");
  173. SERVICE = strdup ("POP");
  174. SEND = NULL;
  175. EXPECT = strdup ("+OK");
  176. QUIT = strdup ("QUIT\r\n");
  177. PROTOCOL = TCP_PROTOCOL;
  178. PORT = 110;
  179. }
  180. else if (strstr (argv[0], "check_imap")) {
  181. progname = strdup ("check_imap");
  182. SERVICE = strdup ("IMAP");
  183. SEND = NULL;
  184. EXPECT = strdup ("* OK");
  185. QUIT = strdup ("a1 LOGOUT\r\n");
  186. PROTOCOL = TCP_PROTOCOL;
  187. PORT = 143;
  188. }
  189. #ifdef HAVE_SSL
  190. else if (strstr(argv[0],"check_simap")) {
  191. progname = strdup ("check_simap");
  192. SERVICE = strdup ("SIMAP");
  193. SEND=NULL;
  194. EXPECT = strdup ("* OK");
  195. QUIT = strdup ("a1 LOGOUT\r\n");
  196. PROTOCOL=TCP_PROTOCOL;
  197. use_ssl=TRUE;
  198. PORT=993;
  199. }
  200. else if (strstr(argv[0],"check_spop")) {
  201. progname = strdup ("check_spop");
  202. SERVICE = strdup ("SPOP");
  203. SEND=NULL;
  204. EXPECT = strdup ("+OK");
  205. QUIT = strdup ("QUIT\r\n");
  206. PROTOCOL=TCP_PROTOCOL;
  207. use_ssl=TRUE;
  208. PORT=995;
  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, ++server_expect_count);
  217. asprintf (&server_expect[server_expect_count - 1], "200");
  218. server_expect = realloc (server_expect, ++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. usage (_("ERROR: Generic check_tcp called with unknown service\n"));
  226. }
  227. server_address = strdup ("127.0.0.1");
  228. server_port = PORT;
  229. server_send = SEND;
  230. server_quit = QUIT;
  231. if (process_arguments (argc, argv) == ERROR)
  232. usage (_("Could not parse arguments\n"));
  233. /* use default expect if none listed in process_arguments() */
  234. if (EXPECT && server_expect_count == 0) {
  235. server_expect = malloc (++server_expect_count);
  236. server_expect[server_expect_count - 1] = EXPECT;
  237. }
  238. /* initialize alarm signal handling */
  239. signal (SIGALRM, socket_timeout_alarm_handler);
  240. /* set socket timeout */
  241. alarm (socket_timeout);
  242. /* try to connect to the host at the given port number */
  243. gettimeofday (&tv, NULL);
  244. #ifdef HAVE_SSL
  245. if (use_ssl)
  246. result = connect_SSL ();
  247. else
  248. #endif
  249. {
  250. if (PROTOCOL == UDP_PROTOCOL)
  251. result = my_udp_connect (server_address, server_port, &sd);
  252. else
  253. /* default is TCP */
  254. result = my_tcp_connect (server_address, server_port, &sd);
  255. }
  256. if (result == STATE_CRITICAL)
  257. return STATE_CRITICAL;
  258. if (server_send != NULL) { /* Something to send? */
  259. asprintf (&server_send, "%s\r\n", server_send);
  260. #ifdef HAVE_SSL
  261. if (use_ssl)
  262. SSL_write(ssl, server_send, strlen (server_send));
  263. else
  264. #endif
  265. send (sd, server_send, strlen (server_send), 0);
  266. }
  267. if (delay > 0) {
  268. tv.tv_sec += delay;
  269. sleep (delay);
  270. }
  271. if (server_send || server_expect_count > 0) {
  272. buffer = malloc (MAXBUF);
  273. memset (buffer, '\0', MAXBUF);
  274. /* watch for the expect string */
  275. while ((i = my_recv ()) > 0) {
  276. buffer[i] = '\0';
  277. asprintf (&status, "%s%s", status, buffer);
  278. if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
  279. break;
  280. if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
  281. break;
  282. }
  283. /* return a CRITICAL status if we couldn't read any data */
  284. if (status == NULL)
  285. terminate (STATE_CRITICAL, _("No data received from host\n"));
  286. strip (status);
  287. if (status && verbose)
  288. printf ("%s\n", status);
  289. if (server_expect_count > 0) {
  290. for (i = 0;; i++) {
  291. if (verbose)
  292. printf ("%d %d\n", i, server_expect_count);
  293. if (i >= server_expect_count)
  294. terminate (STATE_WARNING, _("Invalid response from host\n"));
  295. if (strstr (status, server_expect[i]))
  296. break;
  297. }
  298. }
  299. }
  300. if (server_quit != NULL) {
  301. #ifdef HAVE_SSL
  302. if (use_ssl) {
  303. SSL_write (ssl, server_quit, strlen (server_quit));
  304. SSL_shutdown (ssl);
  305. SSL_free (ssl);
  306. SSL_CTX_free (ctx);
  307. }
  308. else {
  309. #endif
  310. send (sd, server_quit, strlen (server_quit), 0);
  311. #ifdef HAVE_SSL
  312. }
  313. #endif
  314. }
  315. /* close the connection */
  316. if (sd)
  317. close (sd);
  318. elapsed_time = delta_time (tv);
  319. if (check_critical_time == TRUE && elapsed_time > critical_time)
  320. result = STATE_CRITICAL;
  321. else if (check_warning_time == TRUE && elapsed_time > warning_time)
  322. result = STATE_WARNING;
  323. /* reset the alarm */
  324. alarm (0);
  325. printf
  326. (_("%s %s%s - %.3f second response time on port %d"),
  327. SERVICE,
  328. state_text (result),
  329. (was_refused) ? " (refused)" : "",
  330. elapsed_time, server_port);
  331. if (status && strlen(status) > 0)
  332. printf (" [%s]", status);
  333. printf ("|time=%.3f\n", elapsed_time);
  334. return result;
  335. }
  336. /* process command-line arguments */
  337. int
  338. process_arguments (int argc, char **argv)
  339. {
  340. int c;
  341. int option_index = 0;
  342. static struct option long_options[] = {
  343. {"hostname", required_argument, 0, 'H'},
  344. {"critical-time", required_argument, 0, 'c'},
  345. {"warning-time", required_argument, 0, 'w'},
  346. {"critical-codes", required_argument, 0, 'C'},
  347. {"warning-codes", required_argument, 0, 'W'},
  348. {"timeout", required_argument, 0, 't'},
  349. {"protocol", required_argument, 0, 'P'},
  350. {"port", required_argument, 0, 'p'},
  351. {"send", required_argument, 0, 's'},
  352. {"expect", required_argument, 0, 'e'},
  353. {"maxbytes", required_argument, 0, 'm'},
  354. {"quit", required_argument, 0, 'q'},
  355. {"delay", required_argument, 0, 'd'},
  356. {"refuse", required_argument, 0, 'r'},
  357. {"use-ipv4", no_argument, 0, '4'},
  358. {"use-ipv6", no_argument, 0, '6'},
  359. {"verbose", no_argument, 0, 'v'},
  360. {"version", no_argument, 0, 'V'},
  361. {"help", no_argument, 0, 'h'},
  362. {0, 0, 0, 0}
  363. };
  364. if (argc < 2)
  365. usage ("No arguments found\n");
  366. /* backwards compatibility */
  367. for (c = 1; c < argc; c++) {
  368. if (strcmp ("-to", argv[c]) == 0)
  369. strcpy (argv[c], "-t");
  370. else if (strcmp ("-wt", argv[c]) == 0)
  371. strcpy (argv[c], "-w");
  372. else if (strcmp ("-ct", argv[c]) == 0)
  373. strcpy (argv[c], "-c");
  374. }
  375. if (!is_option (argv[1])) {
  376. server_address = argv[1];
  377. argv[1] = argv[0];
  378. argv = &argv[1];
  379. argc--;
  380. }
  381. while (1) {
  382. c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:",
  383. long_options, &option_index);
  384. if (c == -1 || c == EOF || c == 1)
  385. break;
  386. switch (c) {
  387. case '?': /* print short usage statement if args not parsable */
  388. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  389. print_usage ();
  390. exit (STATE_UNKNOWN);
  391. case 'h': /* help */
  392. print_help ();
  393. exit (STATE_OK);
  394. case 'V': /* version */
  395. print_revision (progname, "$Revision$");
  396. exit (STATE_OK);
  397. case 'v': /* verbose mode */
  398. verbose = TRUE;
  399. break;
  400. case '4':
  401. address_family = AF_INET;
  402. break;
  403. case '6':
  404. #ifdef USE_IPV6
  405. address_family = AF_INET6;
  406. #else
  407. usage (_("IPv6 support not available\n"));
  408. #endif
  409. break;
  410. case 'H': /* hostname */
  411. if (is_host (optarg) == FALSE)
  412. usage2 (_("invalid host name or address"), optarg);
  413. server_address = optarg;
  414. break;
  415. case 'c': /* critical */
  416. if (!is_intnonneg (optarg))
  417. usage (_("Critical threshold must be a nonnegative integer\n"));
  418. critical_time = strtod (optarg, NULL);
  419. check_critical_time = TRUE;
  420. break;
  421. case 'w': /* warning */
  422. if (!is_intnonneg (optarg))
  423. usage (_("Warning threshold must be a nonnegative integer\n"));
  424. warning_time = strtod (optarg, NULL);
  425. check_warning_time = TRUE;
  426. break;
  427. case 'C':
  428. crit_codes = realloc (crit_codes, ++crit_codes_count);
  429. crit_codes[crit_codes_count - 1] = optarg;
  430. break;
  431. case 'W':
  432. warn_codes = realloc (warn_codes, ++warn_codes_count);
  433. warn_codes[warn_codes_count - 1] = optarg;
  434. break;
  435. case 't': /* timeout */
  436. if (!is_intpos (optarg))
  437. usage (_("Timeout interval must be a positive integer\n"));
  438. socket_timeout = atoi (optarg);
  439. break;
  440. case 'p': /* port */
  441. if (!is_intpos (optarg))
  442. usage (_("Server port must be a positive integer\n"));
  443. server_port = atoi (optarg);
  444. break;
  445. case 's':
  446. server_send = optarg;
  447. break;
  448. case 'e': /* expect string (may be repeated) */
  449. EXPECT = NULL;
  450. if (server_expect_count == 0)
  451. server_expect = malloc (++server_expect_count);
  452. else
  453. server_expect = realloc (server_expect, ++server_expect_count);
  454. server_expect[server_expect_count - 1] = optarg;
  455. break;
  456. case 'm':
  457. if (!is_intpos (optarg))
  458. usage (_("Maxbytes must be a positive integer\n"));
  459. maxbytes = atoi (optarg);
  460. case 'q':
  461. server_quit = optarg;
  462. break;
  463. case 'r':
  464. if (!strncmp(optarg,"ok",2))
  465. econn_refuse_state = STATE_OK;
  466. else if (!strncmp(optarg,"warn",4))
  467. econn_refuse_state = STATE_WARNING;
  468. else if (!strncmp(optarg,"crit",4))
  469. econn_refuse_state = STATE_CRITICAL;
  470. else
  471. usage (_("Refuse mut be one of ok, warn, crit\n"));
  472. break;
  473. case 'd':
  474. if (is_intpos (optarg))
  475. delay = atoi (optarg);
  476. else
  477. usage (_("Delay must be a positive integer\n"));
  478. break;
  479. case 'S':
  480. #ifndef HAVE_SSL
  481. terminate (STATE_UNKNOWN,
  482. _("SSL support not available. Install OpenSSL and recompile."));
  483. #endif
  484. use_ssl = TRUE;
  485. break;
  486. }
  487. }
  488. if (server_address == NULL)
  489. usage (_("You must provide a server address\n"));
  490. return OK;
  491. }
  492. #ifdef HAVE_SSL
  493. int
  494. connect_SSL (void)
  495. {
  496. SSL_METHOD *meth;
  497. /* Initialize SSL context */
  498. SSLeay_add_ssl_algorithms ();
  499. meth = SSLv2_client_method ();
  500. SSL_load_error_strings ();
  501. if ((ctx = SSL_CTX_new (meth)) == NULL)
  502. {
  503. printf (_("ERROR: Cannot create SSL context.\n"));
  504. return STATE_CRITICAL;
  505. }
  506. /* Initialize alarm signal handling */
  507. signal (SIGALRM, socket_timeout_alarm_handler);
  508. /* Set socket timeout */
  509. alarm (socket_timeout);
  510. /* Save start time */
  511. time (&start_time);
  512. /* Make TCP connection */
  513. if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
  514. {
  515. /* Do the SSL handshake */
  516. if ((ssl = SSL_new (ctx)) != NULL)
  517. {
  518. SSL_set_fd (ssl, sd);
  519. if (SSL_connect (ssl) != -1)
  520. return OK;
  521. ERR_print_errors_fp (stderr);
  522. }
  523. else
  524. {
  525. printf (_("ERROR: Cannot initiate SSL handshake.\n"));
  526. }
  527. SSL_free (ssl);
  528. }
  529. SSL_CTX_free (ctx);
  530. close (sd);
  531. return STATE_CRITICAL;
  532. }
  533. #endif
  534. int
  535. my_recv (void)
  536. {
  537. int i;
  538. #ifdef HAVE_SSL
  539. if (use_ssl) {
  540. i = SSL_read (ssl, buffer, MAXBUF - 1);
  541. }
  542. else {
  543. #endif
  544. i = read (sd, buffer, MAXBUF - 1);
  545. #ifdef HAVE_SSL
  546. }
  547. #endif
  548. return i;
  549. }