check_smtp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. const char *progname = "check_smtp";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2000-2004";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "netutils.h"
  21. #include "utils.h"
  22. enum {
  23. SMTP_PORT = 25
  24. };
  25. const char *SMTP_EXPECT = "220";
  26. const char *SMTP_HELO = "HELO ";
  27. const char *SMTP_QUIT = "QUIT\r\n";
  28. int process_arguments (int, char **);
  29. int validate_arguments (void);
  30. void print_help (void);
  31. void print_usage (void);
  32. #ifdef HAVE_REGEX_H
  33. #include <regex.h>
  34. char regex_expect[MAX_INPUT_BUFFER] = "";
  35. regex_t preg;
  36. regmatch_t pmatch[10];
  37. char timestamp[10] = "";
  38. char errbuf[MAX_INPUT_BUFFER];
  39. int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  40. int eflags = 0;
  41. int errcode, excode;
  42. #endif
  43. int server_port = SMTP_PORT;
  44. char *server_address = NULL;
  45. char *server_expect = NULL;
  46. int smtp_use_dummycmd = 0;
  47. char *mail_command = NULL;
  48. char *from_arg = NULL;
  49. int ncommands=0;
  50. int command_size=0;
  51. int nresponses=0;
  52. int response_size=0;
  53. char **commands = NULL;
  54. char **responses = NULL;
  55. int warning_time = 0;
  56. int check_warning_time = FALSE;
  57. int critical_time = 0;
  58. int check_critical_time = FALSE;
  59. int verbose = 0;
  60. int
  61. main (int argc, char **argv)
  62. {
  63. int sd;
  64. int n = 0;
  65. double elapsed_time;
  66. long microsec;
  67. int result = STATE_UNKNOWN;
  68. char buffer[MAX_INPUT_BUFFER];
  69. char *cmd_str = NULL;
  70. char *helocmd = NULL;
  71. struct timeval tv;
  72. setlocale (LC_ALL, "");
  73. bindtextdomain (PACKAGE, LOCALEDIR);
  74. textdomain (PACKAGE);
  75. if (process_arguments (argc, argv) == ERROR)
  76. usage4 (_("Could not parse arguments"));
  77. /* initialize the HELO command with the localhostname */
  78. #ifndef HOST_MAX_BYTES
  79. #define HOST_MAX_BYTES 255
  80. #endif
  81. helocmd = malloc (HOST_MAX_BYTES);
  82. gethostname(helocmd, HOST_MAX_BYTES);
  83. asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
  84. /* initialize the MAIL command with optional FROM command */
  85. asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
  86. if (verbose && smtp_use_dummycmd)
  87. printf ("FROM CMD: %s", cmd_str);
  88. /* initialize alarm signal handling */
  89. (void) signal (SIGALRM, socket_timeout_alarm_handler);
  90. /* set socket timeout */
  91. (void) alarm (socket_timeout);
  92. /* start timer */
  93. gettimeofday (&tv, NULL);
  94. /* try to connect to the host at the given port number */
  95. result = my_tcp_connect (server_address, server_port, &sd);
  96. if (result == STATE_OK) { /* we connected */
  97. /* watch for the SMTP connection string and */
  98. /* return a WARNING status if we couldn't read any data */
  99. if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
  100. printf (_("recv() failed\n"));
  101. result = STATE_WARNING;
  102. }
  103. else {
  104. if (verbose)
  105. printf ("%s", buffer);
  106. /* strip the buffer of carriage returns */
  107. strip (buffer);
  108. /* make sure we find the response we are looking for */
  109. if (!strstr (buffer, server_expect)) {
  110. if (server_port == SMTP_PORT)
  111. printf (_("Invalid SMTP response received from host\n"));
  112. else
  113. printf (_("Invalid SMTP response received from host on port %d\n"),
  114. server_port);
  115. result = STATE_WARNING;
  116. }
  117. }
  118. /* send the HELO command */
  119. send(sd, helocmd, strlen(helocmd), 0);
  120. /* allow for response to helo command to reach us */
  121. recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
  122. /* sendmail will syslog a "NOQUEUE" error if session does not attempt
  123. * to do something useful. This can be prevented by giving a command
  124. * even if syntax is illegal (MAIL requires a FROM:<...> argument)
  125. *
  126. * According to rfc821 you can include a null reversepath in the from command
  127. * - but a log message is generated on the smtp server.
  128. *
  129. * You can disable sending mail_command with '--nocommand'
  130. * Use the -f option to provide a FROM address
  131. */
  132. if (smtp_use_dummycmd) {
  133. send(sd, cmd_str, strlen(cmd_str), 0);
  134. recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
  135. if (verbose)
  136. printf("%s", buffer);
  137. }
  138. while (n < ncommands) {
  139. asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
  140. send(sd, cmd_str, strlen(cmd_str), 0);
  141. recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
  142. if (verbose)
  143. printf("%s", buffer);
  144. strip (buffer);
  145. if (n < nresponses) {
  146. #ifdef HAVE_REGEX_H
  147. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  148. //strncpy (regex_expect, responses[n], sizeof (regex_expect) - 1);
  149. //regex_expect[sizeof (regex_expect) - 1] = '\0';
  150. errcode = regcomp (&preg, responses[n], cflags);
  151. if (errcode != 0) {
  152. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  153. printf (_("Could Not Compile Regular Expression"));
  154. return ERROR;
  155. }
  156. excode = regexec (&preg, buffer, 10, pmatch, eflags);
  157. if (excode == 0) {
  158. result = STATE_OK;
  159. }
  160. else if (excode == REG_NOMATCH) {
  161. result = STATE_WARNING;
  162. printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
  163. }
  164. else {
  165. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  166. printf (_("Execute Error: %s\n"), errbuf);
  167. result = STATE_UNKNOWN;
  168. }
  169. #else
  170. if (strstr(buffer, responses[n])!=buffer) {
  171. result = STATE_WARNING;
  172. printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
  173. }
  174. #endif
  175. }
  176. n++;
  177. }
  178. /* tell the server we're done */
  179. send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
  180. /* finally close the connection */
  181. close (sd);
  182. }
  183. /* reset the alarm */
  184. alarm (0);
  185. microsec = deltime (tv);
  186. elapsed_time = (double)microsec / 1.0e6;
  187. if (result == STATE_OK) {
  188. if (check_critical_time && elapsed_time > (double) critical_time)
  189. result = STATE_CRITICAL;
  190. else if (check_warning_time && elapsed_time > (double) warning_time)
  191. result = STATE_WARNING;
  192. }
  193. printf (_("SMTP %s - %.3f sec. response time%s%s|%s\n"),
  194. state_text (result), elapsed_time,
  195. verbose?", ":"", verbose?buffer:"",
  196. fperfdata ("time", elapsed_time, "s",
  197. (int)check_warning_time, warning_time,
  198. (int)check_critical_time, critical_time,
  199. TRUE, 0, FALSE, 0));
  200. return result;
  201. }
  202. /* process command-line arguments */
  203. int
  204. process_arguments (int argc, char **argv)
  205. {
  206. int c;
  207. int option = 0;
  208. static struct option longopts[] = {
  209. {"hostname", required_argument, 0, 'H'},
  210. {"expect", required_argument, 0, 'e'},
  211. {"critical", required_argument, 0, 'c'},
  212. {"warning", required_argument, 0, 'w'},
  213. {"timeout", required_argument, 0, 't'},
  214. {"port", required_argument, 0, 'p'},
  215. {"from", required_argument, 0, 'f'},
  216. {"command", required_argument, 0, 'C'},
  217. {"response", required_argument, 0, 'R'},
  218. {"nocommand", required_argument, 0, 'n'},
  219. {"verbose", no_argument, 0, 'v'},
  220. {"version", no_argument, 0, 'V'},
  221. {"use-ipv4", no_argument, 0, '4'},
  222. {"use-ipv6", no_argument, 0, '6'},
  223. {"help", no_argument, 0, 'h'},
  224. {0, 0, 0, 0}
  225. };
  226. if (argc < 2)
  227. return ERROR;
  228. for (c = 1; c < argc; c++) {
  229. if (strcmp ("-to", argv[c]) == 0)
  230. strcpy (argv[c], "-t");
  231. else if (strcmp ("-wt", argv[c]) == 0)
  232. strcpy (argv[c], "-w");
  233. else if (strcmp ("-ct", argv[c]) == 0)
  234. strcpy (argv[c], "-c");
  235. }
  236. while (1) {
  237. c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:",
  238. longopts, &option);
  239. if (c == -1 || c == EOF)
  240. break;
  241. switch (c) {
  242. case 'H': /* hostname */
  243. if (is_host (optarg)) {
  244. server_address = optarg;
  245. }
  246. else {
  247. usage2 (_("Invalid hostname/address"), optarg);
  248. }
  249. break;
  250. case 'p': /* port */
  251. if (is_intpos (optarg))
  252. server_port = atoi (optarg);
  253. else
  254. usage4 (_("Port must be a positive integer"));
  255. break;
  256. case 'f': /* from argument */
  257. from_arg = optarg;
  258. smtp_use_dummycmd = 1;
  259. break;
  260. case 'e': /* server expect string on 220 */
  261. server_expect = optarg;
  262. break;
  263. case 'C': /* commands */
  264. if (ncommands >= command_size) {
  265. commands = realloc (commands, command_size+8);
  266. if (commands == NULL)
  267. die (STATE_UNKNOWN,
  268. _("Could not realloc() units [%d]\n"), ncommands);
  269. }
  270. commands[ncommands] = optarg;
  271. ncommands++;
  272. break;
  273. case 'R': /* server responses */
  274. if (nresponses >= response_size) {
  275. responses = realloc (responses, response_size+8);
  276. if (responses == NULL)
  277. die (STATE_UNKNOWN,
  278. _("Could not realloc() units [%d]\n"), nresponses);
  279. }
  280. responses[nresponses] = optarg;
  281. nresponses++;
  282. break;
  283. case 'c': /* critical time threshold */
  284. if (is_intnonneg (optarg)) {
  285. critical_time = atoi (optarg);
  286. check_critical_time = TRUE;
  287. }
  288. else {
  289. usage4 (_("Critical time must be a positive integer"));
  290. }
  291. break;
  292. case 'w': /* warning time threshold */
  293. if (is_intnonneg (optarg)) {
  294. warning_time = atoi (optarg);
  295. check_warning_time = TRUE;
  296. }
  297. else {
  298. usage4 (_("Warning time must be a positive integer"));
  299. }
  300. break;
  301. case 'v': /* verbose */
  302. verbose++;
  303. break;
  304. case 't': /* timeout */
  305. if (is_intnonneg (optarg)) {
  306. socket_timeout = atoi (optarg);
  307. }
  308. else {
  309. usage4 (_("Time interval must be a positive integer"));
  310. }
  311. break;
  312. case '4':
  313. address_family = AF_INET;
  314. break;
  315. case '6':
  316. #ifdef USE_IPV6
  317. address_family = AF_INET6;
  318. #else
  319. usage4 (_("IPv6 support not available"));
  320. #endif
  321. break;
  322. case 'V': /* version */
  323. print_revision (progname, revision);
  324. exit (STATE_OK);
  325. case 'h': /* help */
  326. print_help ();
  327. exit (STATE_OK);
  328. case '?': /* help */
  329. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  330. print_usage ();
  331. exit (STATE_UNKNOWN);
  332. }
  333. }
  334. c = optind;
  335. if (server_address == NULL) {
  336. if (argv[c]) {
  337. if (is_host (argv[c]))
  338. server_address = argv[c];
  339. else
  340. usage2 (_("Invalid hostname/address"), argv[c]);
  341. }
  342. else {
  343. asprintf (&server_address, "127.0.0.1");
  344. }
  345. }
  346. if (server_expect == NULL)
  347. server_expect = strdup (SMTP_EXPECT);
  348. if (mail_command == NULL)
  349. mail_command = strdup("MAIL ");
  350. if (from_arg==NULL)
  351. from_arg = strdup(" ");
  352. return validate_arguments ();
  353. }
  354. int
  355. validate_arguments (void)
  356. {
  357. return OK;
  358. }
  359. void
  360. print_help (void)
  361. {
  362. char *myport;
  363. asprintf (&myport, "%d", SMTP_PORT);
  364. print_revision (progname, revision);
  365. printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n");
  366. printf (COPYRIGHT, copyright, email);
  367. printf(_("This plugin will attempt to open an SMTP connection with the host.\n\n"));
  368. print_usage ();
  369. printf (_(UT_HELP_VRSN));
  370. printf (_(UT_HOST_PORT), 'p', myport);
  371. printf (_(UT_IPv46));
  372. printf (_("\
  373. -e, --expect=STRING\n\
  374. String to expect in first line of server response (default: '%s')\n\
  375. -n, nocommand\n\
  376. Suppress SMTP command\n\
  377. -C, --command=STRING\n\
  378. SMTP command (may be used repeatedly)\n\
  379. -R, --command=STRING\n\
  380. Expected response to command (may be used repeatedly)\n\
  381. -f, --from=STRING\n\
  382. FROM-address to include in MAIL command, required by Exchange 2000\n"),
  383. SMTP_EXPECT);
  384. printf (_(UT_WARN_CRIT));
  385. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  386. printf (_(UT_VERBOSE));
  387. printf(_("\n\
  388. Successul connects return STATE_OK, refusals and timeouts return\n\
  389. STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
  390. connects, but incorrect reponse messages from the host result in\n\
  391. STATE_WARNING return values.\n"));
  392. printf (_(UT_SUPPORT));
  393. }
  394. void
  395. print_usage (void)
  396. {
  397. printf ("\
  398. Usage: %s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
  399. [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]\n", progname);
  400. }