check_smtp.c 12 KB

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