check_smtp.c 12 KB

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