summaryrefslogtreecommitdiffstats
path: root/plugins/check_smtp.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_smtp.c')
-rw-r--r--plugins/check_smtp.c179
1 files changed, 98 insertions, 81 deletions
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c
index 5e980bc..04df7e6 100644
--- a/plugins/check_smtp.c
+++ b/plugins/check_smtp.c
@@ -1,23 +1,5 @@
1/****************************************************************************** 1/******************************************************************************
2* 2*
3* CHECK_SMTP.C
4*
5* Program: SMTP plugin for Nagios
6* License: GPL
7* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8*
9* $Id$
10*
11* Description:
12*
13* This plugin will attempt to open an SMTP connection with the host.
14* Successul connects return STATE_OK, refusals and timeouts return
15* STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful
16* connects, but incorrect reponse messages from the host result in
17* STATE_WARNING return values.
18*
19* License Information:
20*
21* This program is free software; you can redistribute it and/or modify 3* This program is free software; you can redistribute it and/or modify
22* it under the terms of the GNU General Public License as published by 4* it under the terms of the GNU General Public License as published by
23* the Free Software Foundation; either version 2 of the License, or 5* the Free Software Foundation; either version 2 of the License, or
@@ -34,32 +16,64 @@
34* 16*
35*****************************************************************************/ 17*****************************************************************************/
36 18
19const char *progname = "check_smtp";
20const char *revision = "$Revision$";
21const char *copyright = "1999-2003";
22const char *authors = "Nagios Plugin Development Team";
23const char *email = "nagiosplug-devel@lists.sourceforge.net";
24
25const char *summary = "\
26This plugin will attempt to open an SMTP connection with the host.\n";
27
28const char *description = "\
29Successul connects return STATE_OK, refusals and timeouts return\n\
30STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
31connects, but incorrect reponse messages from the host result in\n\
32STATE_WARNING return values.\n";
33
34const char *option_summary = "\
35-H host [-p port] [-e expect] [-C command] [-f from addr]
36 [-w warn] [-c crit] [-t timeout] [-n] [-v]";
37
38const char *options = "\
39 -H, --hostname=STRING or IPADDRESS\n\
40 Check server on the indicated host\n\
41 -p, --port=INTEGER\n\
42 Make connection on the indicated port (default: %d)\n\
43 -e, --expect=STRING\n\
44 String to expect in first line of server response (default: '%s')\n\
45 -n, nocommand\n\
46 Suppress SMTP command\n\
47 -C, --command=STRING\n\
48 SMTP command (default: '%s')\n\
49 -f, --from=STRING\n\
50 FROM-address to include in MAIL command, required by Exchange 2000\n\
51 (default: '%s')\n\
52 -w, --warning=INTEGER\n\
53 Seconds necessary to result in a warning status\n\
54 -c, --critical=INTEGER\n\
55 Seconds necessary to result in a critical status\n\
56 -t, --timeout=INTEGER\n\
57 Seconds before connection attempt times out (default: %d)\n\
58 -v, --verbose\n\
59 Print extra information (command-line use only)\n\
60 -h, --help\n\
61 Print detailed help screen\n\
62 -V, --version\n\
63 Print version information\n\n";
64
65enum {
66 SMTP_PORT = 25
67};
68const char *SMTP_EXPECT = "220";
69const char *SMTP_HELO = "HELO ";
70const char *SMTP_QUIT = "QUIT\r\n";
71
37#include "config.h" 72#include "config.h"
38#include "common.h" 73#include "common.h"
39#include "netutils.h" 74#include "netutils.h"
40#include "utils.h" 75#include "utils.h"
41 76
42const char *progname = "check_smtp";
43
44#define SMTP_PORT 25
45#define SMTP_EXPECT "220"
46#define SMTP_HELO "HELO "
47
48/* sendmail will syslog a "NOQUEUE" error if session does not attempt
49 * to do something useful. This can be prevented by giving a command
50 * even if syntax is illegal (MAIL requires a FROM:<...> argument)
51 * You can disable sending DUMMYCMD by undefining SMTP_USE_DUMMYCMD.
52 *
53 * According to rfc821 you can include a null reversepath in the from command
54 * - but a log message is generated on the smtp server.
55 *
56 * Use the -f option to provide a FROM address
57 */
58
59#define SMTP_DUMMYCMD "MAIL "
60#define SMTP_USE_DUMMYCMD 1
61#define SMTP_QUIT "QUIT\r\n"
62
63int process_arguments (int, char **); 77int process_arguments (int, char **);
64int validate_arguments (void); 78int validate_arguments (void);
65void print_help (void); 79void print_help (void);
@@ -68,6 +82,8 @@ void print_usage (void);
68int server_port = SMTP_PORT; 82int server_port = SMTP_PORT;
69char *server_address = NULL; 83char *server_address = NULL;
70char *server_expect = NULL; 84char *server_expect = NULL;
85int smtp_use_dummycmd = 1;
86char *mail_command = "MAIL ";
71char *from_arg = " "; 87char *from_arg = " ";
72int warning_time = 0; 88int warning_time = 0;
73int check_warning_time = FALSE; 89int check_warning_time = FALSE;
@@ -98,7 +114,7 @@ main (int argc, char **argv)
98 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n"); 114 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
99 115
100 /* initialize the MAIL command with optional FROM command */ 116 /* initialize the MAIL command with optional FROM command */
101 asprintf (&from_str, "%sFROM: %s%s", SMTP_DUMMYCMD, from_arg, "\r\n"); 117 asprintf (&from_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
102 118
103 if (verbose) 119 if (verbose)
104 printf ("FROMCMD: %s\n", from_str); 120 printf ("FROMCMD: %s\n", from_str);
@@ -144,15 +160,27 @@ main (int argc, char **argv)
144 /* allow for response to helo command to reach us */ 160 /* allow for response to helo command to reach us */
145 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0); 161 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
146 162
147#ifdef SMTP_USE_DUMMYCMD 163 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
148 send(sd, from_str, strlen(from_str), 0); 164 * to do something useful. This can be prevented by giving a command
165 * even if syntax is illegal (MAIL requires a FROM:<...> argument)
166 *
167 * According to rfc821 you can include a null reversepath in the from command
168 * - but a log message is generated on the smtp server.
169 *
170 * You can disable sending mail_command with '--nocommand'
171 * Use the -f option to provide a FROM address
172 */
173 if (smtp_use_dummycmd) {
149 174
150 /* allow for response to DUMMYCMD to reach us */ 175 send(sd, from_str, strlen(from_str), 0);
151 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
152 176
153 if (verbose) 177 /* allow for response to mail_command to reach us */
154 printf("DUMMYCMD: %s\n%s\n",from_str,buffer); 178 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
155#endif /* SMTP_USE_DUMMYCMD */ 179
180 if (verbose)
181 printf("DUMMYCMD: %s\n%s\n",from_str,buffer);
182
183 } /* smtp_use_dummycmd */
156 184
157 /* tell the server we're done */ 185 /* tell the server we're done */
158 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0); 186 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
@@ -198,8 +226,11 @@ process_arguments (int argc, char **argv)
198 {"expect", required_argument, 0, 'e'}, 226 {"expect", required_argument, 0, 'e'},
199 {"critical", required_argument, 0, 'c'}, 227 {"critical", required_argument, 0, 'c'},
200 {"warning", required_argument, 0, 'w'}, 228 {"warning", required_argument, 0, 'w'},
229 {"timeout", required_argument, 0, 't'},
201 {"port", required_argument, 0, 'p'}, 230 {"port", required_argument, 0, 'p'},
202 {"from", required_argument, 0, 'f'}, 231 {"from", required_argument, 0, 'f'},
232 {"command", required_argument, 0, 'C'},
233 {"nocommand", required_argument, 0, 'n'},
203 {"verbose", no_argument, 0, 'v'}, 234 {"verbose", no_argument, 0, 'v'},
204 {"version", no_argument, 0, 'V'}, 235 {"version", no_argument, 0, 'V'},
205 {"help", no_argument, 0, 'h'}, 236 {"help", no_argument, 0, 'h'},
@@ -219,8 +250,8 @@ process_arguments (int argc, char **argv)
219 } 250 }
220 251
221 while (1) { 252 while (1) {
222 c = getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:", long_options, 253 c = getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:C:",
223 &option_index); 254 long_options, &option_index);
224 255
225 if (c == -1 || c == EOF) 256 if (c == -1 || c == EOF)
226 break; 257 break;
@@ -248,6 +279,13 @@ process_arguments (int argc, char **argv)
248 case 'e': /* server expect string on 220 */ 279 case 'e': /* server expect string on 220 */
249 server_expect = optarg; 280 server_expect = optarg;
250 break; 281 break;
282 case 'C': /* server expect string on 220 */
283 mail_command = optarg;
284 smtp_use_dummycmd = 1;
285 break;
286 case 'n': /* server expect string on 220 */
287 smtp_use_dummycmd = 0;
288 break;
251 case 'c': /* critical time threshold */ 289 case 'c': /* critical time threshold */
252 if (is_intnonneg (optarg)) { 290 if (is_intnonneg (optarg)) {
253 critical_time = atoi (optarg); 291 critical_time = atoi (optarg);
@@ -278,7 +316,7 @@ process_arguments (int argc, char **argv)
278 } 316 }
279 break; 317 break;
280 case 'V': /* version */ 318 case 'V': /* version */
281 print_revision (progname, "$Revision$"); 319 print_revision (progname, revision);
282 exit (STATE_OK); 320 exit (STATE_OK);
283 case 'h': /* help */ 321 case 'h': /* help */
284 print_help (); 322 print_help ();
@@ -324,34 +362,13 @@ validate_arguments (void)
324void 362void
325print_help (void) 363print_help (void)
326{ 364{
327 print_revision (progname, "$Revision$"); 365 print_revision (progname, revision);
328 printf 366 printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
329 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n" 367 copyright, authors, email, summary);
330 "This plugin test the SMTP service on the specified host.\n\n");
331 print_usage (); 368 print_usage ();
332 printf 369 printf ("\nOptions:\n");
333 ("\nOptions:\n" 370 printf (options, SMTP_PORT, SMTP_EXPECT, mail_command, from_arg,
334 " -H, --hostname=STRING or IPADDRESS\n" 371 DEFAULT_SOCKET_TIMEOUT);
335 " Check server on the indicated host\n"
336 " -p, --port=INTEGER\n"
337 " Make connection on the indicated port (default: %d)\n"
338 " -e, --expect=STRING\n"
339 " String to expect in first line of server response (default: %s)\n"
340 " -f, --from=STRING\n"
341 " from address to include in MAIL command (default NULL, Exchange2000 requires one)\n"
342 " -w, --warning=INTEGER\n"
343 " Seconds necessary to result in a warning status\n"
344 " -c, --critical=INTEGER\n"
345 " Seconds necessary to result in a critical status\n"
346 " -t, --timeout=INTEGER\n"
347 " Seconds before connection attempt times out (default: %d)\n"
348 " -v, --verbose\n"
349 " Print extra information (command-line use only)\n"
350 " -h, --help\n"
351 " Print detailed help screen\n"
352 " -V, --version\n"
353 " Print version information\n\n",
354 SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
355 support (); 372 support ();
356} 373}
357 374
@@ -362,8 +379,8 @@ print_help (void)
362void 379void
363print_usage (void) 380print_usage (void)
364{ 381{
365 printf 382 printf ("Usage: %s %s\n"
366 ("Usage: %s -H host [-e expect] [-p port] [-f from addr] [-w warn] [-c crit] [-t timeout] [-v]\n" 383 " %s --help\n"
367 " %s --help\n" 384 " %s --version\n",
368 " %s --version\n", progname, progname, progname); 385 progname, option_summary, progname, progname);
369} 386}