314f59b10154a39e83709a6bf7f6c184af8fcaf5
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 #ifdef HAVE_REGEX_H
41 #include <regex.h>
42 char regex_expect[MAX_INPUT_BUFFER] = "";
43 regex_t preg;
44 regmatch_t pmatch[10];
45 char timestamp[10] = "";
46 char errbuf[MAX_INPUT_BUFFER];
47 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
48 int eflags = 0;
49 int errcode, excode;
50 #endif
52 int server_port = SMTP_PORT;
53 char *server_address = NULL;
54 char *server_expect = NULL;
55 int smtp_use_dummycmd = 0;
56 char *mail_command = NULL;
57 char *from_arg = NULL;
58 int ncommands=0;
59 int command_size=0;
60 int nresponses=0;
61 int response_size=0;
62 char **commands = NULL;
63 char **responses = NULL;
64 int warning_time = 0;
65 int check_warning_time = FALSE;
66 int critical_time = 0;
67 int check_critical_time = FALSE;
68 int verbose = 0;
74 \f
75 int
76 main (int argc, char **argv)
77 {
78 int sd;
79 int n = 0;
80 double elapsed_time;
81 long microsec;
82 int result = STATE_UNKNOWN;
83 char buffer[MAX_INPUT_BUFFER];
84 char *cmd_str = NULL;
85 char *helocmd = NULL;
86 struct timeval tv;
88 setlocale (LC_ALL, "");
89 bindtextdomain (PACKAGE, LOCALEDIR);
90 textdomain (PACKAGE);
92 if (process_arguments (argc, argv) != OK)
93 usage (_("Invalid command arguments supplied\n"));
95 /* initialize the HELO command with the localhostname */
96 #ifndef HOST_MAX_BYTES
97 #define HOST_MAX_BYTES 255
98 #endif
99 helocmd = malloc (HOST_MAX_BYTES);
100 gethostname(helocmd, HOST_MAX_BYTES);
101 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
103 /* initialize the MAIL command with optional FROM command */
104 asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
106 if (verbose && smtp_use_dummycmd)
107 printf ("FROM CMD: %s", cmd_str);
109 /* initialize alarm signal handling */
110 (void) signal (SIGALRM, socket_timeout_alarm_handler);
112 /* set socket timeout */
113 (void) alarm (socket_timeout);
115 /* start timer */
116 gettimeofday (&tv, NULL);
118 /* try to connect to the host at the given port number */
119 result = my_tcp_connect (server_address, server_port, &sd);
121 if (result == STATE_OK) { /* we connected */
123 /* watch for the SMTP connection string and */
124 /* return a WARNING status if we couldn't read any data */
125 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
126 printf (_("recv() failed\n"));
127 result = STATE_WARNING;
128 }
129 else {
130 if (verbose)
131 printf ("%s", buffer);
132 /* strip the buffer of carriage returns */
133 strip (buffer);
134 /* make sure we find the response we are looking for */
135 if (!strstr (buffer, server_expect)) {
136 if (server_port == SMTP_PORT)
137 printf (_("Invalid SMTP response received from host\n"));
138 else
139 printf (_("Invalid SMTP response received from host on port %d\n"),
140 server_port);
141 result = STATE_WARNING;
142 }
143 }
145 /* send the HELO command */
146 send(sd, helocmd, strlen(helocmd), 0);
148 /* allow for response to helo command to reach us */
149 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
151 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
152 * to do something useful. This can be prevented by giving a command
153 * even if syntax is illegal (MAIL requires a FROM:<...> argument)
154 *
155 * According to rfc821 you can include a null reversepath in the from command
156 * - but a log message is generated on the smtp server.
157 *
158 * You can disable sending mail_command with '--nocommand'
159 * Use the -f option to provide a FROM address
160 */
161 if (smtp_use_dummycmd) {
162 send(sd, cmd_str, strlen(cmd_str), 0);
163 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
164 if (verbose)
165 printf("%s", buffer);
166 }
168 while (n < ncommands) {
169 asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
170 send(sd, cmd_str, strlen(cmd_str), 0);
171 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
172 if (verbose)
173 printf("%s", buffer);
174 strip (buffer);
175 if (n < nresponses) {
176 #ifdef HAVE_REGEX_H
177 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
178 //strncpy (regex_expect, responses[n], sizeof (regex_expect) - 1);
179 //regex_expect[sizeof (regex_expect) - 1] = '\0';
180 errcode = regcomp (&preg, responses[n], cflags);
181 if (errcode != 0) {
182 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
183 printf (_("Could Not Compile Regular Expression"));
184 return ERROR;
185 }
186 excode = regexec (&preg, buffer, 10, pmatch, eflags);
187 if (excode == 0) {
188 result = STATE_OK;
189 }
190 else if (excode == REG_NOMATCH) {
191 result = STATE_WARNING;
192 printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
193 }
194 else {
195 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
196 printf (_("Execute Error: %s\n"), errbuf);
197 result = STATE_UNKNOWN;
198 }
199 #else
200 if (strstr(buffer, responses[n])!=buffer) {
201 result = STATE_WARNING;
202 printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
203 }
204 #endif
205 }
206 n++;
207 }
209 /* tell the server we're done */
210 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
212 /* finally close the connection */
213 close (sd);
214 }
216 /* reset the alarm */
217 alarm (0);
219 microsec = deltime (tv);
220 elapsed_time = (double)microsec / 1.0e6;
222 if (check_critical_time && elapsed_time > (double) critical_time)
223 result = STATE_CRITICAL;
224 else if (check_warning_time && elapsed_time > (double) warning_time)
225 result = STATE_WARNING;
227 printf (_("SMTP %s - %.3f sec. response time%s%s|%s\n"),
228 state_text (result), elapsed_time,
229 verbose?", ":"", verbose?buffer:"",
230 perfdata ("time", microsec, "us",
231 (int)check_warning_time, warning_time,
232 (int)check_critical_time, critical_time,
233 TRUE, 0, FALSE, 0));
235 return result;
236 }
242 \f
243 /* process command-line arguments */
244 int
245 process_arguments (int argc, char **argv)
246 {
247 int c;
249 int option = 0;
250 static struct option longopts[] = {
251 {"hostname", required_argument, 0, 'H'},
252 {"expect", required_argument, 0, 'e'},
253 {"critical", required_argument, 0, 'c'},
254 {"warning", required_argument, 0, 'w'},
255 {"timeout", required_argument, 0, 't'},
256 {"port", required_argument, 0, 'p'},
257 {"from", required_argument, 0, 'f'},
258 {"command", required_argument, 0, 'C'},
259 {"response", required_argument, 0, 'R'},
260 {"nocommand", required_argument, 0, 'n'},
261 {"verbose", no_argument, 0, 'v'},
262 {"version", no_argument, 0, 'V'},
263 {"use-ipv4", no_argument, 0, '4'},
264 {"use-ipv6", no_argument, 0, '6'},
265 {"help", no_argument, 0, 'h'},
266 {0, 0, 0, 0}
267 };
269 if (argc < 2)
270 return ERROR;
272 for (c = 1; c < argc; c++) {
273 if (strcmp ("-to", argv[c]) == 0)
274 strcpy (argv[c], "-t");
275 else if (strcmp ("-wt", argv[c]) == 0)
276 strcpy (argv[c], "-w");
277 else if (strcmp ("-ct", argv[c]) == 0)
278 strcpy (argv[c], "-c");
279 }
281 while (1) {
282 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:",
283 longopts, &option);
285 if (c == -1 || c == EOF)
286 break;
288 switch (c) {
289 case 'H': /* hostname */
290 if (is_host (optarg)) {
291 server_address = optarg;
292 }
293 else {
294 usage (_("Invalid host name\n"));
295 }
296 break;
297 case 'p': /* port */
298 if (is_intpos (optarg))
299 server_port = atoi (optarg);
300 else
301 usage (_("Server port must be a positive integer\n"));
302 break;
303 case 'f': /* from argument */
304 from_arg = optarg;
305 smtp_use_dummycmd = 1;
306 break;
307 case 'e': /* server expect string on 220 */
308 server_expect = optarg;
309 break;
310 case 'C': /* commands */
311 if (ncommands >= command_size) {
312 commands = realloc (commands, command_size+8);
313 if (commands == NULL)
314 die (STATE_UNKNOWN,
315 _("Could not realloc() units [%d]\n"), ncommands);
316 }
317 commands[ncommands] = optarg;
318 ncommands++;
319 break;
320 case 'R': /* server responses */
321 if (nresponses >= response_size) {
322 responses = realloc (responses, response_size+8);
323 if (responses == NULL)
324 die (STATE_UNKNOWN,
325 _("Could not realloc() units [%d]\n"), nresponses);
326 }
327 responses[nresponses] = optarg;
328 nresponses++;
329 break;
330 case 'c': /* critical time threshold */
331 if (is_intnonneg (optarg)) {
332 critical_time = atoi (optarg);
333 check_critical_time = TRUE;
334 }
335 else {
336 usage (_("Critical time must be a nonnegative integer\n"));
337 }
338 break;
339 case 'w': /* warning time threshold */
340 if (is_intnonneg (optarg)) {
341 warning_time = atoi (optarg);
342 check_warning_time = TRUE;
343 }
344 else {
345 usage (_("Warning time must be a nonnegative integer\n"));
346 }
347 break;
348 case 'v': /* verbose */
349 verbose++;
350 break;
351 case 't': /* timeout */
352 if (is_intnonneg (optarg)) {
353 socket_timeout = atoi (optarg);
354 }
355 else {
356 usage (_("Time interval must be a nonnegative integer\n"));
357 }
358 break;
359 case '4':
360 address_family = AF_INET;
361 break;
362 case '6':
363 #ifdef USE_IPV6
364 address_family = AF_INET6;
365 #else
366 usage (_("IPv6 support not available\n"));
367 #endif
368 break;
369 case 'V': /* version */
370 print_revision (progname, revision);
371 exit (STATE_OK);
372 case 'h': /* help */
373 print_help ();
374 exit (STATE_OK);
375 case '?': /* help */
376 usage (_("Invalid argument\n"));
377 }
378 }
380 c = optind;
381 if (server_address == NULL) {
382 if (argv[c]) {
383 if (is_host (argv[c]))
384 server_address = argv[c];
385 else
386 usage (_("Invalid host name"));
387 }
388 else {
389 asprintf (&server_address, "127.0.0.1");
390 }
391 }
393 if (server_expect == NULL)
394 server_expect = strdup (SMTP_EXPECT);
396 if (mail_command == NULL)
397 mail_command = strdup("MAIL ");
399 if (from_arg==NULL)
400 from_arg = strdup(" ");
402 return validate_arguments ();
403 }
409 int
410 validate_arguments (void)
411 {
412 return OK;
413 }
419 \f
420 void
421 print_help (void)
422 {
423 char *myport;
424 asprintf (&myport, "%d", SMTP_PORT);
426 print_revision (progname, revision);
428 printf (_("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"));
429 printf (_(COPYRIGHT), copyright, email);
431 printf(_("\
432 This plugin will attempt to open an SMTP connection with the host.\n\n"));
434 print_usage ();
436 printf (_(UT_HELP_VRSN));
438 printf (_(UT_HOST_PORT), 'p', myport);
440 printf (_(UT_IPv46));
442 printf (_("\
443 -e, --expect=STRING\n\
444 String to expect in first line of server response (default: '%s')\n\
445 -n, nocommand\n\
446 Suppress SMTP command\n\
447 -C, --command=STRING\n\
448 SMTP command (may be used repeatedly)\n\
449 -R, --command=STRING\n\
450 Expected response to command (may be used repeatedly)\n\
451 -f, --from=STRING\n\
452 FROM-address to include in MAIL command, required by Exchange 2000\n"),
453 SMTP_EXPECT);
455 printf (_(UT_WARN_CRIT));
457 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
459 printf (_(UT_VERBOSE));
461 printf(_("\n\
462 Successul connects return STATE_OK, refusals and timeouts return\n\
463 STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
464 connects, but incorrect reponse messages from the host result in\n\
465 STATE_WARNING return values.\n"));
467 printf (_(UT_SUPPORT));
468 }
474 void
475 print_usage (void)
476 {
477 printf ("\
478 Usage: %s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
479 [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]\n", progname);
480 printf (_(UT_HLP_VRS), progname, progname);
481 }