1 /******************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 ******************************************************************************/
19 const char *progname = "check_smtp";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "netutils.h"
26 #include "utils.h"
28 enum {
29 SMTP_PORT = 25
30 };
31 const char *SMTP_EXPECT = "220";
32 const char *SMTP_HELO = "HELO ";
33 const char *SMTP_QUIT = "QUIT\r\n";
35 int process_arguments (int, char **);
36 int validate_arguments (void);
37 void print_help (void);
38 void print_usage (void);
40 int server_port = SMTP_PORT;
41 char *server_address = NULL;
42 char *server_expect = NULL;
43 int smtp_use_dummycmd = 1;
44 char *mail_command = "MAIL ";
45 char *from_arg = " ";
46 int warning_time = 0;
47 int check_warning_time = FALSE;
48 int critical_time = 0;
49 int check_critical_time = FALSE;
50 int verbose = 0;
56 \f
57 int
58 main (int argc, char **argv)
59 {
60 int sd;
61 double elapsed_time;
62 int result = STATE_UNKNOWN;
63 char buffer[MAX_INPUT_BUFFER] = "";
64 char *from_str = NULL;
65 char *helocmd = NULL;
66 struct timeval tv;
68 if (process_arguments (argc, argv) != OK)
69 usage (_("Invalid command arguments supplied\n"));
71 /* initialize the HELO command with the localhostname */
72 #ifndef HOST_MAX_BYTES
73 #define HOST_MAX_BYTES 255
74 #endif
75 helocmd = malloc (HOST_MAX_BYTES);
76 gethostname(helocmd, HOST_MAX_BYTES);
77 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
79 /* initialize the MAIL command with optional FROM command */
80 asprintf (&from_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
82 if (verbose)
83 printf ("FROMCMD: %s\n", from_str);
85 /* initialize alarm signal handling */
86 (void) signal (SIGALRM, socket_timeout_alarm_handler);
88 /* set socket timeout */
89 (void) alarm (socket_timeout);
91 /* start timer */
92 gettimeofday (&tv, NULL);
94 /* try to connect to the host at the given port number */
95 result = my_tcp_connect (server_address, server_port, &sd);
97 /* we connected, so close connection before exiting */
98 if (result == STATE_OK) {
100 /* watch for the SMTP connection string and */
101 /* return a WARNING status if we couldn't read any data */
102 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
103 printf (_("recv() failed\n"));
104 result = STATE_WARNING;
105 }
106 else {
107 /* strip the buffer of carriage returns */
108 strip (buffer);
109 /* make sure we find the response we are looking for */
110 if (!strstr (buffer, server_expect)) {
111 if (server_port == SMTP_PORT)
112 printf (_("Invalid SMTP response received from host\n"));
113 else
114 printf (_("Invalid SMTP response received from host on port %d\n"),
115 server_port);
116 result = STATE_WARNING;
117 }
118 }
120 /* send the HELO command */
121 send(sd, helocmd, strlen(helocmd), 0);
123 /* allow for response to helo command to reach us */
124 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
126 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
127 * to do something useful. This can be prevented by giving a command
128 * even if syntax is illegal (MAIL requires a FROM:<...> argument)
129 *
130 * According to rfc821 you can include a null reversepath in the from command
131 * - but a log message is generated on the smtp server.
132 *
133 * You can disable sending mail_command with '--nocommand'
134 * Use the -f option to provide a FROM address
135 */
136 if (smtp_use_dummycmd) {
138 send(sd, from_str, strlen(from_str), 0);
140 /* allow for response to mail_command to reach us */
141 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
143 if (verbose)
144 printf(_("DUMMYCMD: %s\n%s\n"),from_str,buffer);
146 } /* smtp_use_dummycmd */
148 /* tell the server we're done */
149 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
151 /* finally close the connection */
152 close (sd);
153 }
155 /* reset the alarm */
156 alarm (0);
158 elapsed_time = delta_time (tv);
160 if (check_critical_time && elapsed_time > (double) critical_time)
161 result = STATE_CRITICAL;
162 else if (check_warning_time && elapsed_time > (double) warning_time)
163 result = STATE_WARNING;
165 if (verbose)
166 printf (_("SMTP %s - %.3f sec. response time, %s|time=%.3f\n"),
167 state_text (result), elapsed_time, buffer, elapsed_time);
168 else
169 printf (_("SMTP %s - %.3f second response time|time=%.3f\n"),
170 state_text (result), elapsed_time, elapsed_time);
172 return result;
173 }
179 \f
180 /* process command-line arguments */
181 int
182 process_arguments (int argc, char **argv)
183 {
184 int c;
186 int option_index = 0;
187 static struct option long_options[] = {
188 {"hostname", required_argument, 0, 'H'},
189 {"expect", required_argument, 0, 'e'},
190 {"critical", required_argument, 0, 'c'},
191 {"warning", required_argument, 0, 'w'},
192 {"timeout", required_argument, 0, 't'},
193 {"port", required_argument, 0, 'p'},
194 {"from", required_argument, 0, 'f'},
195 {"command", required_argument, 0, 'C'},
196 {"nocommand", required_argument, 0, 'n'},
197 {"verbose", no_argument, 0, 'v'},
198 {"version", no_argument, 0, 'V'},
199 {"use-ipv4", no_argument, 0, '4'},
200 {"use-ipv6", no_argument, 0, '6'},
201 {"help", no_argument, 0, 'h'},
202 {0, 0, 0, 0}
203 };
205 if (argc < 2)
206 return ERROR;
208 for (c = 1; c < argc; c++) {
209 if (strcmp ("-to", argv[c]) == 0)
210 strcpy (argv[c], "-t");
211 else if (strcmp ("-wt", argv[c]) == 0)
212 strcpy (argv[c], "-w");
213 else if (strcmp ("-ct", argv[c]) == 0)
214 strcpy (argv[c], "-c");
215 }
217 while (1) {
218 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:",
219 long_options, &option_index);
221 if (c == -1 || c == EOF)
222 break;
224 switch (c) {
225 case 'H': /* hostname */
226 if (is_host (optarg)) {
227 server_address = optarg;
228 }
229 else {
230 usage (_("Invalid host name\n"));
231 }
232 break;
233 case 'p': /* port */
234 if (is_intpos (optarg))
235 server_port = atoi (optarg);
236 else
237 usage (_("Server port must be a positive integer\n"));
238 break;
239 case 'f': /* from argument */
240 from_arg = optarg;
241 break;
242 case 'e': /* server expect string on 220 */
243 server_expect = optarg;
244 break;
245 case 'C': /* server expect string on 220 */
246 mail_command = optarg;
247 smtp_use_dummycmd = 1;
248 break;
249 case 'n': /* server expect string on 220 */
250 smtp_use_dummycmd = 0;
251 break;
252 case 'c': /* critical time threshold */
253 if (is_intnonneg (optarg)) {
254 critical_time = atoi (optarg);
255 check_critical_time = TRUE;
256 }
257 else {
258 usage (_("Critical time must be a nonnegative integer\n"));
259 }
260 break;
261 case 'w': /* warning time threshold */
262 if (is_intnonneg (optarg)) {
263 warning_time = atoi (optarg);
264 check_warning_time = TRUE;
265 }
266 else {
267 usage (_("Warning time must be a nonnegative integer\n"));
268 }
269 break;
270 case 'v': /* verbose */
271 verbose++;
272 break;
273 case 't': /* timeout */
274 if (is_intnonneg (optarg)) {
275 socket_timeout = atoi (optarg);
276 }
277 else {
278 usage (_("Time interval must be a nonnegative integer\n"));
279 }
280 break;
281 case '4':
282 address_family = AF_INET;
283 break;
284 case '6':
285 #ifdef USE_IPV6
286 address_family = AF_INET6;
287 #else
288 usage (_("IPv6 support not available\n"));
289 #endif
290 break;
291 case 'V': /* version */
292 print_revision (progname, revision);
293 exit (STATE_OK);
294 case 'h': /* help */
295 print_help ();
296 exit (STATE_OK);
297 case '?': /* help */
298 usage (_("Invalid argument\n"));
299 }
300 }
302 c = optind;
303 if (server_address == NULL) {
304 if (argv[c]) {
305 if (is_host (argv[c]))
306 server_address = argv[c];
307 else
308 usage (_("Invalid host name"));
309 }
310 else {
311 asprintf (&server_address, "127.0.0.1");
312 }
313 }
315 if (server_expect == NULL)
316 asprintf (&server_expect, SMTP_EXPECT);
318 return validate_arguments ();
319 }
325 int
326 validate_arguments (void)
327 {
328 return OK;
329 }
335 \f
336 void
337 print_help (void)
338 {
339 char *myport;
340 asprintf (&myport, "%d", SMTP_PORT);
342 print_revision (progname, revision);
344 printf (_("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"));
345 printf (_(COPYRIGHT), copyright, email);
347 printf(_("\
348 This plugin will attempt to open an SMTP connection with the host.\n\n"));
350 print_usage ();
352 printf (_(UT_HELP_VRSN));
354 printf (_(UT_HOST_PORT), 'p', myport);
356 printf (_(UT_IPv46));
358 printf (_("\
359 -e, --expect=STRING\n\
360 String to expect in first line of server response (default: '%s')\n\
361 -n, nocommand\n\
362 Suppress SMTP command\n\
363 -C, --command=STRING\n\
364 SMTP command (default: '%s')\n\
365 -f, --from=STRING\n\
366 FROM-address to include in MAIL command, required by Exchange 2000\n\
367 (default: '%s')\n"), SMTP_EXPECT, mail_command, from_arg);
369 printf (_(UT_WARN_CRIT));
371 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
373 printf (_(UT_VERBOSE));
375 printf(_("\n\
376 Successul connects return STATE_OK, refusals and timeouts return\n\
377 STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
378 connects, but incorrect reponse messages from the host result in\n\
379 STATE_WARNING return values.\n"));
381 printf (_(UT_SUPPORT));
382 }
388 void
389 print_usage (void)
390 {
391 printf ("\
392 Usage: %s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
393 [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]\n", progname);
394 printf (_(UT_HLP_VRS), progname, progname);
395 }