check_smtp.c 9.7 KB

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