check_smtp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. printf (_("SMTP %s - %.3f sec. response time%s%s|%s\n"),
  193. state_text (result), elapsed_time,
  194. verbose?", ":"", verbose?buffer:"",
  195. fperfdata ("time", elapsed_time, "s",
  196. (int)check_warning_time, warning_time,
  197. (int)check_critical_time, critical_time,
  198. TRUE, 0, FALSE, 0));
  199. return result;
  200. }
  201. /* process command-line arguments */
  202. int
  203. process_arguments (int argc, char **argv)
  204. {
  205. int c;
  206. int option = 0;
  207. static struct option longopts[] = {
  208. {"hostname", required_argument, 0, 'H'},
  209. {"expect", required_argument, 0, 'e'},
  210. {"critical", required_argument, 0, 'c'},
  211. {"warning", required_argument, 0, 'w'},
  212. {"timeout", required_argument, 0, 't'},
  213. {"port", required_argument, 0, 'p'},
  214. {"from", required_argument, 0, 'f'},
  215. {"command", required_argument, 0, 'C'},
  216. {"response", required_argument, 0, 'R'},
  217. {"nocommand", required_argument, 0, 'n'},
  218. {"verbose", no_argument, 0, 'v'},
  219. {"version", no_argument, 0, 'V'},
  220. {"use-ipv4", no_argument, 0, '4'},
  221. {"use-ipv6", no_argument, 0, '6'},
  222. {"help", no_argument, 0, 'h'},
  223. {0, 0, 0, 0}
  224. };
  225. if (argc < 2)
  226. return ERROR;
  227. for (c = 1; c < argc; c++) {
  228. if (strcmp ("-to", argv[c]) == 0)
  229. strcpy (argv[c], "-t");
  230. else if (strcmp ("-wt", argv[c]) == 0)
  231. strcpy (argv[c], "-w");
  232. else if (strcmp ("-ct", argv[c]) == 0)
  233. strcpy (argv[c], "-c");
  234. }
  235. while (1) {
  236. c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:",
  237. longopts, &option);
  238. if (c == -1 || c == EOF)
  239. break;
  240. switch (c) {
  241. case 'H': /* hostname */
  242. if (is_host (optarg)) {
  243. server_address = optarg;
  244. }
  245. else {
  246. usage2 (_("Invalid hostname/address"), optarg);
  247. }
  248. break;
  249. case 'p': /* port */
  250. if (is_intpos (optarg))
  251. server_port = atoi (optarg);
  252. else
  253. usage (_("Port must be a positive integer\n"));
  254. break;
  255. case 'f': /* from argument */
  256. from_arg = optarg;
  257. smtp_use_dummycmd = 1;
  258. break;
  259. case 'e': /* server expect string on 220 */
  260. server_expect = optarg;
  261. break;
  262. case 'C': /* commands */
  263. if (ncommands >= command_size) {
  264. commands = realloc (commands, command_size+8);
  265. if (commands == NULL)
  266. die (STATE_UNKNOWN,
  267. _("Could not realloc() units [%d]\n"), ncommands);
  268. }
  269. commands[ncommands] = optarg;
  270. ncommands++;
  271. break;
  272. case 'R': /* server responses */
  273. if (nresponses >= response_size) {
  274. responses = realloc (responses, response_size+8);
  275. if (responses == NULL)
  276. die (STATE_UNKNOWN,
  277. _("Could not realloc() units [%d]\n"), nresponses);
  278. }
  279. responses[nresponses] = optarg;
  280. nresponses++;
  281. break;
  282. case 'c': /* critical time threshold */
  283. if (is_intnonneg (optarg)) {
  284. critical_time = atoi (optarg);
  285. check_critical_time = TRUE;
  286. }
  287. else {
  288. usage (_("Critical time must be a positive integer\n"));
  289. }
  290. break;
  291. case 'w': /* warning time threshold */
  292. if (is_intnonneg (optarg)) {
  293. warning_time = atoi (optarg);
  294. check_warning_time = TRUE;
  295. }
  296. else {
  297. usage (_("Warning time must be a positive integer\n"));
  298. }
  299. break;
  300. case 'v': /* verbose */
  301. verbose++;
  302. break;
  303. case 't': /* timeout */
  304. if (is_intnonneg (optarg)) {
  305. socket_timeout = atoi (optarg);
  306. }
  307. else {
  308. usage (_("Time interval must be a positive integer\n"));
  309. }
  310. break;
  311. case '4':
  312. address_family = AF_INET;
  313. break;
  314. case '6':
  315. #ifdef USE_IPV6
  316. address_family = AF_INET6;
  317. #else
  318. usage (_("IPv6 support not available\n"));
  319. #endif
  320. break;
  321. case 'V': /* version */
  322. print_revision (progname, revision);
  323. exit (STATE_OK);
  324. case 'h': /* help */
  325. print_help ();
  326. exit (STATE_OK);
  327. case '?': /* help */
  328. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  329. print_usage ();
  330. exit (STATE_UNKNOWN);
  331. }
  332. }
  333. c = optind;
  334. if (server_address == NULL) {
  335. if (argv[c]) {
  336. if (is_host (argv[c]))
  337. server_address = argv[c];
  338. else
  339. usage2 (_("Invalid hostname/address"), argv[c]);
  340. }
  341. else {
  342. asprintf (&server_address, "127.0.0.1");
  343. }
  344. }
  345. if (server_expect == NULL)
  346. server_expect = strdup (SMTP_EXPECT);
  347. if (mail_command == NULL)
  348. mail_command = strdup("MAIL ");
  349. if (from_arg==NULL)
  350. from_arg = strdup(" ");
  351. return validate_arguments ();
  352. }
  353. int
  354. validate_arguments (void)
  355. {
  356. return OK;
  357. }
  358. void
  359. print_help (void)
  360. {
  361. char *myport;
  362. asprintf (&myport, "%d", SMTP_PORT);
  363. print_revision (progname, revision);
  364. printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n");
  365. printf (COPYRIGHT, copyright, email);
  366. printf(_("This plugin will attempt to open an SMTP connection with the host.\n\n"));
  367. print_usage ();
  368. printf (_(UT_HELP_VRSN));
  369. printf (_(UT_HOST_PORT), 'p', myport);
  370. printf (_(UT_IPv46));
  371. printf (_("\
  372. -e, --expect=STRING\n\
  373. String to expect in first line of server response (default: '%s')\n\
  374. -n, nocommand\n\
  375. Suppress SMTP command\n\
  376. -C, --command=STRING\n\
  377. SMTP command (may be used repeatedly)\n\
  378. -R, --command=STRING\n\
  379. Expected response to command (may be used repeatedly)\n\
  380. -f, --from=STRING\n\
  381. FROM-address to include in MAIL command, required by Exchange 2000\n"),
  382. SMTP_EXPECT);
  383. printf (_(UT_WARN_CRIT));
  384. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  385. printf (_(UT_VERBOSE));
  386. printf(_("\n\
  387. Successul connects return STATE_OK, refusals and timeouts return\n\
  388. STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
  389. connects, but incorrect reponse messages from the host result in\n\
  390. STATE_WARNING return values.\n"));
  391. printf (_(UT_SUPPORT));
  392. }
  393. void
  394. print_usage (void)
  395. {
  396. printf ("\
  397. Usage: %s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
  398. [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]\n", progname);
  399. }