check_smtp.c 9.8 KB

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