4
0

check_tcp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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_46), 'p', "none");
  106. printf (_("\
  107. -s, --send=STRING\n\
  108. String to send to the server\n\
  109. -e, --expect=STRING\n\
  110. String to expect in server response\n\
  111. -q, --quit=STRING\n\
  112. String to send server to initiate a clean close of the connection\n"));
  113. printf (_("\
  114. -r, --refuse=ok|warn|crit\n\
  115. Accept tcp refusals with states ok, warn, crit (default: crit)\n\
  116. -m, --maxbytes=INTEGER\n\
  117. Close connection once more than this number of bytes are received\n\
  118. -d, --delay=INTEGER\n\
  119. Seconds to wait between sending string and polling for response\n"));
  120. printf (_(WARN_CRIT_TO), DEFAULT_SOCKET_TIMEOUT);
  121. printf (_(VRBS));
  122. support ();
  123. }
  124. int
  125. main (int argc, char **argv)
  126. {
  127. int result;
  128. int i;
  129. char *status = "";
  130. struct timeval tv;
  131. setlocale (LC_ALL, "");
  132. bindtextdomain (PACKAGE, LOCALEDIR);
  133. textdomain (PACKAGE);
  134. if (strstr (argv[0], "check_udp")) {
  135. progname = strdup ("check_udp");
  136. SERVICE = strdup ("UDP");
  137. SEND = NULL;
  138. EXPECT = NULL;
  139. QUIT = NULL;
  140. PROTOCOL = UDP_PROTOCOL;
  141. PORT = 0;
  142. }
  143. else if (strstr (argv[0], "check_tcp")) {
  144. progname = strdup ("check_tcp");
  145. SERVICE = strdup ("TCP");
  146. SEND = NULL;
  147. EXPECT = NULL;
  148. QUIT = NULL;
  149. PROTOCOL = TCP_PROTOCOL;
  150. PORT = 0;
  151. }
  152. else if (strstr (argv[0], "check_ftp")) {
  153. progname = strdup ("check_ftp");
  154. SERVICE = strdup ("FTP");
  155. SEND = NULL;
  156. EXPECT = strdup ("220");
  157. QUIT = strdup ("QUIT\r\n");
  158. PROTOCOL = TCP_PROTOCOL;
  159. PORT = 21;
  160. }
  161. else if (strstr (argv[0], "check_smtp")) {
  162. progname = strdup ("check_smtp");
  163. SERVICE = strdup ("SMTP");
  164. SEND = NULL;
  165. EXPECT = strdup ("220");
  166. QUIT = strdup ("QUIT\r\n");
  167. PROTOCOL = TCP_PROTOCOL;
  168. PORT = 25;
  169. }
  170. else if (strstr (argv[0], "check_pop")) {
  171. progname = strdup ("check_pop");
  172. SERVICE = strdup ("POP");
  173. SEND = NULL;
  174. EXPECT = strdup ("+OK");
  175. QUIT = strdup ("QUIT\r\n");
  176. PROTOCOL = TCP_PROTOCOL;
  177. PORT = 110;
  178. }
  179. else if (strstr (argv[0], "check_imap")) {
  180. progname = strdup ("check_imap");
  181. SERVICE = strdup ("IMAP");
  182. SEND = NULL;
  183. EXPECT = strdup ("* OK");
  184. QUIT = strdup ("a1 LOGOUT\r\n");
  185. PROTOCOL = TCP_PROTOCOL;
  186. PORT = 143;
  187. }
  188. #ifdef HAVE_SSL
  189. else if (strstr(argv[0],"check_simap")) {
  190. progname = strdup ("check_simap");
  191. SERVICE = strdup ("SIMAP");
  192. SEND=NULL;
  193. EXPECT = strdup ("* OK");
  194. QUIT = strdup ("a1 LOGOUT\r\n");
  195. PROTOCOL=TCP_PROTOCOL;
  196. use_ssl=TRUE;
  197. PORT=993;
  198. }
  199. else if (strstr(argv[0],"check_spop")) {
  200. progname = strdup ("check_spop");
  201. SERVICE = strdup ("SPOP");
  202. SEND=NULL;
  203. EXPECT = strdup ("+OK");
  204. QUIT = strdup ("QUIT\r\n");
  205. PROTOCOL=TCP_PROTOCOL;
  206. use_ssl=TRUE;
  207. PORT=995;
  208. }
  209. #endif
  210. else if (strstr (argv[0], "check_nntp")) {
  211. progname = strdup ("check_nntp");
  212. SERVICE = strdup ("NNTP");
  213. SEND = NULL;
  214. EXPECT = NULL;
  215. server_expect = realloc (server_expect, ++server_expect_count);
  216. asprintf (&server_expect[server_expect_count - 1], "200");
  217. server_expect = realloc (server_expect, ++server_expect_count);
  218. asprintf (&server_expect[server_expect_count - 1], "201");
  219. asprintf (&QUIT, "QUIT\r\n");
  220. PROTOCOL = TCP_PROTOCOL;
  221. PORT = 119;
  222. }
  223. else {
  224. usage (_("ERROR: Generic check_tcp called with unknown service\n"));
  225. }
  226. server_address = strdup ("127.0.0.1");
  227. server_port = PORT;
  228. server_send = SEND;
  229. server_quit = QUIT;
  230. if (process_arguments (argc, argv) == ERROR)
  231. usage (_("Could not parse arguments\n"));
  232. /* use default expect if none listed in process_arguments() */
  233. if (EXPECT && server_expect_count == 0) {
  234. server_expect = malloc (++server_expect_count);
  235. server_expect[server_expect_count - 1] = EXPECT;
  236. }
  237. /* initialize alarm signal handling */
  238. signal (SIGALRM, socket_timeout_alarm_handler);
  239. /* set socket timeout */
  240. alarm (socket_timeout);
  241. /* try to connect to the host at the given port number */
  242. gettimeofday (&tv, NULL);
  243. #ifdef HAVE_SSL
  244. if (use_ssl)
  245. result = connect_SSL ();
  246. else
  247. #endif
  248. {
  249. if (PROTOCOL == UDP_PROTOCOL)
  250. result = my_udp_connect (server_address, server_port, &sd);
  251. else
  252. /* default is TCP */
  253. result = my_tcp_connect (server_address, server_port, &sd);
  254. }
  255. if (result == STATE_CRITICAL)
  256. return STATE_CRITICAL;
  257. if (server_send != NULL) { /* Something to send? */
  258. asprintf (&server_send, "%s\r\n", server_send);
  259. #ifdef HAVE_SSL
  260. if (use_ssl)
  261. SSL_write(ssl, server_send, strlen (server_send));
  262. else
  263. #endif
  264. send (sd, server_send, strlen (server_send), 0);
  265. }
  266. if (delay > 0) {
  267. tv.tv_sec += delay;
  268. sleep (delay);
  269. }
  270. if (server_send || server_expect_count > 0) {
  271. buffer = malloc (MAXBUF);
  272. memset (buffer, '\0', MAXBUF);
  273. /* watch for the expect string */
  274. while ((i = my_recv ()) > 0) {
  275. buffer[i] = '\0';
  276. asprintf (&status, "%s%s", status, buffer);
  277. if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
  278. break;
  279. if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
  280. break;
  281. }
  282. /* return a CRITICAL status if we couldn't read any data */
  283. if (status == NULL)
  284. terminate (STATE_CRITICAL, _("No data received from host\n"));
  285. strip (status);
  286. if (status && verbose)
  287. printf ("%s\n", status);
  288. if (server_expect_count > 0) {
  289. for (i = 0;; i++) {
  290. if (verbose)
  291. printf ("%d %d\n", i, server_expect_count);
  292. if (i >= server_expect_count)
  293. terminate (STATE_WARNING, _("Invalid response from host\n"));
  294. if (strstr (status, server_expect[i]))
  295. break;
  296. }
  297. }
  298. }
  299. if (server_quit != NULL) {
  300. #ifdef HAVE_SSL
  301. if (use_ssl) {
  302. SSL_write (ssl, server_quit, strlen (server_quit));
  303. SSL_shutdown (ssl);
  304. SSL_free (ssl);
  305. SSL_CTX_free (ctx);
  306. }
  307. else {
  308. #endif
  309. send (sd, server_quit, strlen (server_quit), 0);
  310. #ifdef HAVE_SSL
  311. }
  312. #endif
  313. }
  314. /* close the connection */
  315. if (sd)
  316. close (sd);
  317. elapsed_time = delta_time (tv);
  318. if (check_critical_time == TRUE && elapsed_time > critical_time)
  319. result = STATE_CRITICAL;
  320. else if (check_warning_time == TRUE && elapsed_time > warning_time)
  321. result = STATE_WARNING;
  322. /* reset the alarm */
  323. alarm (0);
  324. printf
  325. (_("%s %s%s - %.3f second response time on port %d"),
  326. SERVICE,
  327. state_text (result),
  328. (was_refused) ? " (refused)" : "",
  329. elapsed_time, server_port);
  330. if (status && strlen(status) > 0)
  331. printf (" [%s]", status);
  332. printf ("|time=%.3f\n", elapsed_time);
  333. return result;
  334. }
  335. /* process command-line arguments */
  336. int
  337. process_arguments (int argc, char **argv)
  338. {
  339. int c;
  340. int option_index = 0;
  341. static struct option long_options[] = {
  342. {"hostname", required_argument, 0, 'H'},
  343. {"critical-time", required_argument, 0, 'c'},
  344. {"warning-time", required_argument, 0, 'w'},
  345. {"critical-codes", required_argument, 0, 'C'},
  346. {"warning-codes", required_argument, 0, 'W'},
  347. {"timeout", required_argument, 0, 't'},
  348. {"protocol", required_argument, 0, 'P'},
  349. {"port", required_argument, 0, 'p'},
  350. {"send", required_argument, 0, 's'},
  351. {"expect", required_argument, 0, 'e'},
  352. {"maxbytes", required_argument, 0, 'm'},
  353. {"quit", required_argument, 0, 'q'},
  354. {"delay", required_argument, 0, 'd'},
  355. {"refuse", required_argument, 0, 'r'},
  356. {"use-ipv4", no_argument, 0, '4'},
  357. {"use-ipv6", no_argument, 0, '6'},
  358. {"verbose", no_argument, 0, 'v'},
  359. {"version", no_argument, 0, 'V'},
  360. {"help", no_argument, 0, 'h'},
  361. {0, 0, 0, 0}
  362. };
  363. if (argc < 2)
  364. usage ("No arguments found\n");
  365. /* backwards compatibility */
  366. for (c = 1; c < argc; c++) {
  367. if (strcmp ("-to", argv[c]) == 0)
  368. strcpy (argv[c], "-t");
  369. else if (strcmp ("-wt", argv[c]) == 0)
  370. strcpy (argv[c], "-w");
  371. else if (strcmp ("-ct", argv[c]) == 0)
  372. strcpy (argv[c], "-c");
  373. }
  374. if (!is_option (argv[1])) {
  375. server_address = argv[1];
  376. argv[1] = argv[0];
  377. argv = &argv[1];
  378. argc--;
  379. }
  380. while (1) {
  381. c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:",
  382. long_options, &option_index);
  383. if (c == -1 || c == EOF || c == 1)
  384. break;
  385. switch (c) {
  386. case '?': /* print short usage statement if args not parsable */
  387. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  388. print_usage ();
  389. exit (STATE_UNKNOWN);
  390. case 'h': /* help */
  391. print_help ();
  392. exit (STATE_OK);
  393. case 'V': /* version */
  394. print_revision (progname, "$Revision$");
  395. exit (STATE_OK);
  396. case 'v': /* verbose mode */
  397. verbose = TRUE;
  398. break;
  399. case '4':
  400. address_family = AF_INET;
  401. break;
  402. case '6':
  403. #ifdef USE_IPV6
  404. address_family = AF_INET6;
  405. #else
  406. usage (_("IPv6 support not available\n"));
  407. #endif
  408. break;
  409. case 'H': /* hostname */
  410. if (is_host (optarg) == FALSE)
  411. usage2 (_("invalid host name or address"), optarg);
  412. server_address = optarg;
  413. break;
  414. case 'c': /* critical */
  415. if (!is_intnonneg (optarg))
  416. usage (_("Critical threshold must be a nonnegative integer\n"));
  417. critical_time = strtod (optarg, NULL);
  418. check_critical_time = TRUE;
  419. break;
  420. case 'w': /* warning */
  421. if (!is_intnonneg (optarg))
  422. usage (_("Warning threshold must be a nonnegative integer\n"));
  423. warning_time = strtod (optarg, NULL);
  424. check_warning_time = TRUE;
  425. break;
  426. case 'C':
  427. crit_codes = realloc (crit_codes, ++crit_codes_count);
  428. crit_codes[crit_codes_count - 1] = optarg;
  429. break;
  430. case 'W':
  431. warn_codes = realloc (warn_codes, ++warn_codes_count);
  432. warn_codes[warn_codes_count - 1] = optarg;
  433. break;
  434. case 't': /* timeout */
  435. if (!is_intpos (optarg))
  436. usage (_("Timeout interval must be a positive integer\n"));
  437. socket_timeout = atoi (optarg);
  438. break;
  439. case 'p': /* port */
  440. if (!is_intpos (optarg))
  441. usage (_("Server port must be a positive integer\n"));
  442. server_port = atoi (optarg);
  443. break;
  444. case 's':
  445. server_send = optarg;
  446. break;
  447. case 'e': /* expect string (may be repeated) */
  448. EXPECT = NULL;
  449. if (server_expect_count == 0)
  450. server_expect = malloc (++server_expect_count);
  451. else
  452. server_expect = realloc (server_expect, ++server_expect_count);
  453. server_expect[server_expect_count - 1] = optarg;
  454. break;
  455. case 'm':
  456. if (!is_intpos (optarg))
  457. usage (_("Maxbytes must be a positive integer\n"));
  458. maxbytes = atoi (optarg);
  459. case 'q':
  460. server_quit = optarg;
  461. break;
  462. case 'r':
  463. if (!strncmp(optarg,"ok",2))
  464. econn_refuse_state = STATE_OK;
  465. else if (!strncmp(optarg,"warn",4))
  466. econn_refuse_state = STATE_WARNING;
  467. else if (!strncmp(optarg,"crit",4))
  468. econn_refuse_state = STATE_CRITICAL;
  469. else
  470. usage (_("Refuse mut be one of ok, warn, crit\n"));
  471. break;
  472. case 'd':
  473. if (is_intpos (optarg))
  474. delay = atoi (optarg);
  475. else
  476. usage (_("Delay must be a positive integer\n"));
  477. break;
  478. case 'S':
  479. #ifndef HAVE_SSL
  480. terminate (STATE_UNKNOWN,
  481. _("SSL support not available. Install OpenSSL and recompile."));
  482. #endif
  483. use_ssl = TRUE;
  484. break;
  485. }
  486. }
  487. if (server_address == NULL)
  488. usage (_("You must provide a server address\n"));
  489. return OK;
  490. }
  491. #ifdef HAVE_SSL
  492. int
  493. connect_SSL (void)
  494. {
  495. SSL_METHOD *meth;
  496. /* Initialize SSL context */
  497. SSLeay_add_ssl_algorithms ();
  498. meth = SSLv2_client_method ();
  499. SSL_load_error_strings ();
  500. if ((ctx = SSL_CTX_new (meth)) == NULL)
  501. {
  502. printf (_("ERROR: Cannot create SSL context.\n"));
  503. return STATE_CRITICAL;
  504. }
  505. /* Initialize alarm signal handling */
  506. signal (SIGALRM, socket_timeout_alarm_handler);
  507. /* Set socket timeout */
  508. alarm (socket_timeout);
  509. /* Save start time */
  510. time (&start_time);
  511. /* Make TCP connection */
  512. if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
  513. {
  514. /* Do the SSL handshake */
  515. if ((ssl = SSL_new (ctx)) != NULL)
  516. {
  517. SSL_set_fd (ssl, sd);
  518. if (SSL_connect (ssl) != -1)
  519. return OK;
  520. ERR_print_errors_fp (stderr);
  521. }
  522. else
  523. {
  524. printf (_("ERROR: Cannot initiate SSL handshake.\n"));
  525. }
  526. SSL_free (ssl);
  527. }
  528. SSL_CTX_free (ctx);
  529. close (sd);
  530. return STATE_CRITICAL;
  531. }
  532. #endif
  533. int
  534. my_recv (void)
  535. {
  536. int i;
  537. #ifdef HAVE_SSL
  538. if (use_ssl) {
  539. i = SSL_read (ssl, buffer, MAXBUF - 1);
  540. }
  541. else {
  542. #endif
  543. i = read (sd, buffer, MAXBUF - 1);
  544. #ifdef HAVE_SSL
  545. }
  546. #endif
  547. return i;
  548. }