check_smtp.c 11 KB

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