09f8f143eece6f42d3d8eb60c2c5ec59ea5224a2
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 = 0;
44 char *mail_command = NULL;
45 char *from_arg = NULL;
46 int ncommands=0;
47 int command_size=0;
48 int nresponses=0;
49 int response_size=0;
50 char **commands = NULL;
51 char **responses = NULL;
52 int warning_time = 0;
53 int check_warning_time = FALSE;
54 int critical_time = 0;
55 int check_critical_time = FALSE;
56 int verbose = 0;
62 \f
63 int
64 main (int argc, char **argv)
65 {
66 int sd;
67 int n = 0;
68 double elapsed_time;
69 long microsec;
70 int result = STATE_UNKNOWN;
71 char buffer[MAX_INPUT_BUFFER];
72 char *cmd_str = NULL;
73 char *helocmd = NULL;
74 struct timeval tv;
76 setlocale (LC_ALL, "");
77 bindtextdomain (PACKAGE, LOCALEDIR);
78 textdomain (PACKAGE);
80 if (process_arguments (argc, argv) != OK)
81 usage (_("Invalid command arguments supplied\n"));
83 /* initialize the HELO command with the localhostname */
84 #ifndef HOST_MAX_BYTES
85 #define HOST_MAX_BYTES 255
86 #endif
87 helocmd = malloc (HOST_MAX_BYTES);
88 gethostname(helocmd, HOST_MAX_BYTES);
89 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
91 /* initialize the MAIL command with optional FROM command */
92 asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
94 if (verbose && smtp_use_dummycmd)
95 printf ("FROM CMD: %s", cmd_str);
97 /* initialize alarm signal handling */
98 (void) signal (SIGALRM, socket_timeout_alarm_handler);
100 /* set socket timeout */
101 (void) alarm (socket_timeout);
103 /* start timer */
104 gettimeofday (&tv, NULL);
106 /* try to connect to the host at the given port number */
107 result = my_tcp_connect (server_address, server_port, &sd);
109 if (result == STATE_OK) { /* we connected */
111 /* watch for the SMTP connection string and */
112 /* return a WARNING status if we couldn't read any data */
113 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
114 printf (_("recv() failed\n"));
115 result = STATE_WARNING;
116 }
117 else {
118 if (verbose)
119 printf ("%s", buffer);
120 /* strip the buffer of carriage returns */
121 strip (buffer);
122 /* make sure we find the response we are looking for */
123 if (!strstr (buffer, server_expect)) {
124 if (server_port == SMTP_PORT)
125 printf (_("Invalid SMTP response received from host\n"));
126 else
127 printf (_("Invalid SMTP response received from host on port %d\n"),
128 server_port);
129 result = STATE_WARNING;
130 }
131 }
133 /* send the HELO command */
134 send(sd, helocmd, strlen(helocmd), 0);
136 /* allow for response to helo command to reach us */
137 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
139 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
140 * to do something useful. This can be prevented by giving a command
141 * even if syntax is illegal (MAIL requires a FROM:<...> argument)
142 *
143 * According to rfc821 you can include a null reversepath in the from command
144 * - but a log message is generated on the smtp server.
145 *
146 * You can disable sending mail_command with '--nocommand'
147 * Use the -f option to provide a FROM address
148 */
149 if (smtp_use_dummycmd) {
150 send(sd, cmd_str, strlen(cmd_str), 0);
151 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
152 if (verbose)
153 printf("%s", buffer);
154 }
156 while (n < ncommands) {
157 asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
158 send(sd, cmd_str, strlen(cmd_str), 0);
159 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
160 if (verbose)
161 printf("%s", buffer);
162 strip (buffer);
163 if (n < nresponses && strstr(buffer, responses[n])!=buffer) {
164 result = STATE_WARNING;
165 printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
166 }
167 n++;
168 }
170 /* tell the server we're done */
171 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
173 /* finally close the connection */
174 close (sd);
175 }
177 /* reset the alarm */
178 alarm (0);
180 microsec = deltime (tv);
181 elapsed_time = (double)microsec / 1.0e6;
183 if (check_critical_time && elapsed_time > (double) critical_time)
184 result = STATE_CRITICAL;
185 else if (check_warning_time && elapsed_time > (double) warning_time)
186 result = STATE_WARNING;
188 printf (_("SMTP %s - %.3f sec. response time%s%s|%s\n"),
189 state_text (result), elapsed_time,
190 verbose?", ":"", verbose?buffer:"",
191 perfdata ("time", microsec, "us",
192 check_warning_time, (long)(1000000*warning_time),
193 check_critical_time, (long)(1000000*critical_time),
194 TRUE, 0, FALSE, 0));
196 return result;
197 }
203 \f
204 /* process command-line arguments */
205 int
206 process_arguments (int argc, char **argv)
207 {
208 int c;
210 int option = 0;
211 static struct option longopts[] = {
212 {"hostname", required_argument, 0, 'H'},
213 {"expect", required_argument, 0, 'e'},
214 {"critical", required_argument, 0, 'c'},
215 {"warning", required_argument, 0, 'w'},
216 {"timeout", required_argument, 0, 't'},
217 {"port", required_argument, 0, 'p'},
218 {"from", required_argument, 0, 'f'},
219 {"command", required_argument, 0, 'C'},
220 {"response", required_argument, 0, 'R'},
221 {"nocommand", required_argument, 0, 'n'},
222 {"verbose", no_argument, 0, 'v'},
223 {"version", no_argument, 0, 'V'},
224 {"use-ipv4", no_argument, 0, '4'},
225 {"use-ipv6", no_argument, 0, '6'},
226 {"help", no_argument, 0, 'h'},
227 {0, 0, 0, 0}
228 };
230 if (argc < 2)
231 return ERROR;
233 for (c = 1; c < argc; c++) {
234 if (strcmp ("-to", argv[c]) == 0)
235 strcpy (argv[c], "-t");
236 else if (strcmp ("-wt", argv[c]) == 0)
237 strcpy (argv[c], "-w");
238 else if (strcmp ("-ct", argv[c]) == 0)
239 strcpy (argv[c], "-c");
240 }
242 while (1) {
243 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:",
244 longopts, &option);
246 if (c == -1 || c == EOF)
247 break;
249 switch (c) {
250 case 'H': /* hostname */
251 if (is_host (optarg)) {
252 server_address = optarg;
253 }
254 else {
255 usage (_("Invalid host name\n"));
256 }
257 break;
258 case 'p': /* port */
259 if (is_intpos (optarg))
260 server_port = atoi (optarg);
261 else
262 usage (_("Server port must be a positive integer\n"));
263 break;
264 case 'f': /* from argument */
265 from_arg = optarg;
266 smtp_use_dummycmd = 1;
267 break;
268 case 'e': /* server expect string on 220 */
269 server_expect = optarg;
270 break;
271 case 'C': /* commands */
272 if (ncommands >= command_size) {
273 commands = realloc (commands, command_size+8);
274 if (commands == NULL)
275 die (STATE_UNKNOWN,
276 _("Could not realloc() units [%d]\n"), ncommands);
277 }
278 commands[ncommands] = optarg;
279 ncommands++;
280 break;
281 case 'R': /* server responses */
282 if (nresponses >= response_size) {
283 responses = realloc (responses, response_size+8);
284 if (responses == NULL)
285 die (STATE_UNKNOWN,
286 _("Could not realloc() units [%d]\n"), nresponses);
287 }
288 responses[nresponses] = optarg;
289 nresponses++;
290 break;
291 case 'c': /* critical time threshold */
292 if (is_intnonneg (optarg)) {
293 critical_time = atoi (optarg);
294 check_critical_time = TRUE;
295 }
296 else {
297 usage (_("Critical time must be a nonnegative integer\n"));
298 }
299 break;
300 case 'w': /* warning time threshold */
301 if (is_intnonneg (optarg)) {
302 warning_time = atoi (optarg);
303 check_warning_time = TRUE;
304 }
305 else {
306 usage (_("Warning time must be a nonnegative integer\n"));
307 }
308 break;
309 case 'v': /* verbose */
310 verbose++;
311 break;
312 case 't': /* timeout */
313 if (is_intnonneg (optarg)) {
314 socket_timeout = atoi (optarg);
315 }
316 else {
317 usage (_("Time interval must be a nonnegative integer\n"));
318 }
319 break;
320 case '4':
321 address_family = AF_INET;
322 break;
323 case '6':
324 #ifdef USE_IPV6
325 address_family = AF_INET6;
326 #else
327 usage (_("IPv6 support not available\n"));
328 #endif
329 break;
330 case 'V': /* version */
331 print_revision (progname, revision);
332 exit (STATE_OK);
333 case 'h': /* help */
334 print_help ();
335 exit (STATE_OK);
336 case '?': /* help */
337 usage (_("Invalid argument\n"));
338 }
339 }
341 c = optind;
342 if (server_address == NULL) {
343 if (argv[c]) {
344 if (is_host (argv[c]))
345 server_address = argv[c];
346 else
347 usage (_("Invalid host name"));
348 }
349 else {
350 asprintf (&server_address, "127.0.0.1");
351 }
352 }
354 if (server_expect == NULL)
355 server_expect = strdup (SMTP_EXPECT);
357 if (mail_command == NULL)
358 mail_command = strdup("MAIL ");
360 if (from_arg==NULL)
361 from_arg = strdup(" ");
363 return validate_arguments ();
364 }
370 int
371 validate_arguments (void)
372 {
373 return OK;
374 }
380 \f
381 void
382 print_help (void)
383 {
384 char *myport;
385 asprintf (&myport, "%d", SMTP_PORT);
387 print_revision (progname, revision);
389 printf (_("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"));
390 printf (_(COPYRIGHT), copyright, email);
392 printf(_("\
393 This plugin will attempt to open an SMTP connection with the host.\n\n"));
395 print_usage ();
397 printf (_(UT_HELP_VRSN));
399 printf (_(UT_HOST_PORT), 'p', myport);
401 printf (_(UT_IPv46));
403 printf (_("\
404 -e, --expect=STRING\n\
405 String to expect in first line of server response (default: '%s')\n\
406 -n, nocommand\n\
407 Suppress SMTP command\n\
408 -C, --command=STRING\n\
409 SMTP command (may be used repeatedly)\n\
410 -R, --command=STRING\n\
411 Expected response to command (may be used repeatedly)\n\
412 -f, --from=STRING\n\
413 FROM-address to include in MAIL command, required by Exchange 2000\n"),
414 SMTP_EXPECT);
416 printf (_(UT_WARN_CRIT));
418 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
420 printf (_(UT_VERBOSE));
422 printf(_("\n\
423 Successul connects return STATE_OK, refusals and timeouts return\n\
424 STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
425 connects, but incorrect reponse messages from the host result in\n\
426 STATE_WARNING return values.\n"));
428 printf (_(UT_SUPPORT));
429 }
435 void
436 print_usage (void)
437 {
438 printf ("\
439 Usage: %s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
440 [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]\n", progname);
441 printf (_(UT_HLP_VRS), progname, progname);
442 }