check_smtp.c 9.8 KB

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