check_smtp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. printf (_("SMTP %s - %.3f sec. response time%s%s|%s\n"),
  133. state_text (result), elapsed_time,
  134. verbose?", ":"", verbose?buffer:"",
  135. perfdata ("time", microsec, "us",
  136. check_warning_time, (long)(1000000*warning_time),
  137. check_critical_time, (long)(1000000*critical_time),
  138. TRUE, 0, FALSE, 0));
  139. return result;
  140. }
  141. /* process command-line arguments */
  142. int
  143. process_arguments (int argc, char **argv)
  144. {
  145. int c;
  146. int option = 0;
  147. static struct option longopts[] = {
  148. {"hostname", required_argument, 0, 'H'},
  149. {"expect", required_argument, 0, 'e'},
  150. {"critical", required_argument, 0, 'c'},
  151. {"warning", required_argument, 0, 'w'},
  152. {"timeout", required_argument, 0, 't'},
  153. {"port", required_argument, 0, 'p'},
  154. {"from", required_argument, 0, 'f'},
  155. {"command", required_argument, 0, 'C'},
  156. {"nocommand", required_argument, 0, 'n'},
  157. {"verbose", no_argument, 0, 'v'},
  158. {"version", no_argument, 0, 'V'},
  159. {"use-ipv4", no_argument, 0, '4'},
  160. {"use-ipv6", no_argument, 0, '6'},
  161. {"help", no_argument, 0, 'h'},
  162. {0, 0, 0, 0}
  163. };
  164. if (argc < 2)
  165. return ERROR;
  166. for (c = 1; c < argc; c++) {
  167. if (strcmp ("-to", argv[c]) == 0)
  168. strcpy (argv[c], "-t");
  169. else if (strcmp ("-wt", argv[c]) == 0)
  170. strcpy (argv[c], "-w");
  171. else if (strcmp ("-ct", argv[c]) == 0)
  172. strcpy (argv[c], "-c");
  173. }
  174. while (1) {
  175. c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:",
  176. longopts, &option);
  177. if (c == -1 || c == EOF)
  178. break;
  179. switch (c) {
  180. case 'H': /* hostname */
  181. if (is_host (optarg)) {
  182. server_address = optarg;
  183. }
  184. else {
  185. usage (_("Invalid host name\n"));
  186. }
  187. break;
  188. case 'p': /* port */
  189. if (is_intpos (optarg))
  190. server_port = atoi (optarg);
  191. else
  192. usage (_("Server port must be a positive integer\n"));
  193. break;
  194. case 'f': /* from argument */
  195. from_arg = optarg;
  196. break;
  197. case 'e': /* server expect string on 220 */
  198. server_expect = optarg;
  199. break;
  200. case 'C': /* server expect string on 220 */
  201. mail_command = optarg;
  202. smtp_use_dummycmd = 1;
  203. break;
  204. case 'n': /* server expect string on 220 */
  205. smtp_use_dummycmd = 0;
  206. break;
  207. case 'c': /* critical time threshold */
  208. if (is_intnonneg (optarg)) {
  209. critical_time = atoi (optarg);
  210. check_critical_time = TRUE;
  211. }
  212. else {
  213. usage (_("Critical time must be a nonnegative integer\n"));
  214. }
  215. break;
  216. case 'w': /* warning time threshold */
  217. if (is_intnonneg (optarg)) {
  218. warning_time = atoi (optarg);
  219. check_warning_time = TRUE;
  220. }
  221. else {
  222. usage (_("Warning time must be a nonnegative integer\n"));
  223. }
  224. break;
  225. case 'v': /* verbose */
  226. verbose++;
  227. break;
  228. case 't': /* timeout */
  229. if (is_intnonneg (optarg)) {
  230. socket_timeout = atoi (optarg);
  231. }
  232. else {
  233. usage (_("Time interval must be a nonnegative integer\n"));
  234. }
  235. break;
  236. case '4':
  237. address_family = AF_INET;
  238. break;
  239. case '6':
  240. #ifdef USE_IPV6
  241. address_family = AF_INET6;
  242. #else
  243. usage (_("IPv6 support not available\n"));
  244. #endif
  245. break;
  246. case 'V': /* version */
  247. print_revision (progname, revision);
  248. exit (STATE_OK);
  249. case 'h': /* help */
  250. print_help ();
  251. exit (STATE_OK);
  252. case '?': /* help */
  253. usage (_("Invalid argument\n"));
  254. }
  255. }
  256. c = optind;
  257. if (server_address == NULL) {
  258. if (argv[c]) {
  259. if (is_host (argv[c]))
  260. server_address = argv[c];
  261. else
  262. usage (_("Invalid host name"));
  263. }
  264. else {
  265. asprintf (&server_address, "127.0.0.1");
  266. }
  267. }
  268. if (server_expect == NULL)
  269. server_expect = strdup (SMTP_EXPECT);
  270. if (mail_command == NULL)
  271. mail_command = strdup("MAIL ");
  272. if (from_arg==NULL)
  273. from_arg = strdup(" ");
  274. return validate_arguments ();
  275. }
  276. int
  277. validate_arguments (void)
  278. {
  279. return OK;
  280. }
  281. void
  282. print_help (void)
  283. {
  284. char *myport;
  285. asprintf (&myport, "%d", SMTP_PORT);
  286. print_revision (progname, revision);
  287. printf (_("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"));
  288. printf (_(COPYRIGHT), copyright, email);
  289. printf(_("\
  290. This plugin will attempt to open an SMTP connection with the host.\n\n"));
  291. print_usage ();
  292. printf (_(UT_HELP_VRSN));
  293. printf (_(UT_HOST_PORT), 'p', myport);
  294. printf (_(UT_IPv46));
  295. printf (_("\
  296. -e, --expect=STRING\n\
  297. String to expect in first line of server response (default: '%s')\n\
  298. -n, nocommand\n\
  299. Suppress SMTP command\n\
  300. -C, --command=STRING\n\
  301. SMTP command (default: '%s')\n\
  302. -f, --from=STRING\n\
  303. FROM-address to include in MAIL command, required by Exchange 2000\n\
  304. (default: '%s')\n"), SMTP_EXPECT, mail_command, from_arg);
  305. printf (_(UT_WARN_CRIT));
  306. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  307. printf (_(UT_VERBOSE));
  308. printf(_("\n\
  309. Successul connects return STATE_OK, refusals and timeouts return\n\
  310. STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
  311. connects, but incorrect reponse messages from the host result in\n\
  312. STATE_WARNING return values.\n"));
  313. printf (_(UT_SUPPORT));
  314. }
  315. void
  316. print_usage (void)
  317. {
  318. printf ("\
  319. Usage: %s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
  320. [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]\n", progname);
  321. printf (_(UT_HLP_VRS), progname, progname);
  322. }