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;
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 long microsec;
63 int result = STATE_UNKNOWN;
64 char buffer[MAX_INPUT_BUFFER];
65 char *from_str = NULL;
66 char *helocmd = NULL;
67 struct timeval tv;
69 setlocale (LC_ALL, "");
70 bindtextdomain (PACKAGE, LOCALEDIR);
71 textdomain (PACKAGE);
73 if (process_arguments (argc, argv) != OK)
74 usage (_("Invalid command arguments supplied\n"));
76 /* initialize the HELO command with the localhostname */
77 #ifndef HOST_MAX_BYTES
78 #define HOST_MAX_BYTES 255
79 #endif
80 helocmd = malloc (HOST_MAX_BYTES);
81 gethostname(helocmd, HOST_MAX_BYTES);
82 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
84 /* initialize the MAIL command with optional FROM command */
85 asprintf (&from_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
87 if (verbose)
88 printf ("FROMCMD: %s\n", from_str);
90 /* initialize alarm signal handling */
91 (void) signal (SIGALRM, socket_timeout_alarm_handler);
93 /* set socket timeout */
94 (void) alarm (socket_timeout);
96 /* start timer */
97 gettimeofday (&tv, NULL);
99 /* try to connect to the host at the given port number */
100 result = my_tcp_connect (server_address, server_port, &sd);
102 /* we connected, so close connection before exiting */
103 if (result == STATE_OK) {
105 /* watch for the SMTP connection string and */
106 /* return a WARNING status if we couldn't read any data */
107 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
108 printf (_("recv() failed\n"));
109 result = STATE_WARNING;
110 }
111 else {
112 /* strip the buffer of carriage returns */
113 strip (buffer);
114 /* make sure we find the response we are looking for */
115 if (!strstr (buffer, server_expect)) {
116 if (server_port == SMTP_PORT)
117 printf (_("Invalid SMTP response received from host\n"));
118 else
119 printf (_("Invalid SMTP response received from host on port %d\n"),
120 server_port);
121 result = STATE_WARNING;
122 }
123 }
125 /* send the HELO command */
126 send(sd, helocmd, strlen(helocmd), 0);
128 /* allow for response to helo command to reach us */
129 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
131 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
132 * to do something useful. This can be prevented by giving a command
133 * even if syntax is illegal (MAIL requires a FROM:<...> argument)
134 *
135 * According to rfc821 you can include a null reversepath in the from command
136 * - but a log message is generated on the smtp server.
137 *
138 * You can disable sending mail_command with '--nocommand'
139 * Use the -f option to provide a FROM address
140 */
141 if (smtp_use_dummycmd) {
143 send(sd, from_str, strlen(from_str), 0);
145 /* allow for response to mail_command to reach us */
146 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
148 if (verbose)
149 printf(_("DUMMYCMD: %s\n%s\n"),from_str,buffer);
151 } /* smtp_use_dummycmd */
153 /* tell the server we're done */
154 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
156 /* finally close the connection */
157 close (sd);
158 }
160 /* reset the alarm */
161 alarm (0);
163 microsec = deltime (tv);
164 elapsed_time = (double)microsec / 1.0e6;
166 if (check_critical_time && elapsed_time > (double) critical_time)
167 result = STATE_CRITICAL;
168 else if (check_warning_time && elapsed_time > (double) warning_time)
169 result = STATE_WARNING;
171 if (verbose)
172 printf (_("SMTP %s - %.3f sec. response time, %s|time=%ldus\n"),
173 state_text (result), elapsed_time, buffer, microsec);
174 else
175 printf (_("SMTP %s - %.3f second response time|time=%ldus\n"),
176 state_text (result), elapsed_time, microsec);
178 return result;
179 }
185 \f
186 /* process command-line arguments */
187 int
188 process_arguments (int argc, char **argv)
189 {
190 int c;
192 int option = 0;
193 static struct option longopts[] = {
194 {"hostname", required_argument, 0, 'H'},
195 {"expect", required_argument, 0, 'e'},
196 {"critical", required_argument, 0, 'c'},
197 {"warning", required_argument, 0, 'w'},
198 {"timeout", required_argument, 0, 't'},
199 {"port", required_argument, 0, 'p'},
200 {"from", required_argument, 0, 'f'},
201 {"command", required_argument, 0, 'C'},
202 {"nocommand", required_argument, 0, 'n'},
203 {"verbose", no_argument, 0, 'v'},
204 {"version", no_argument, 0, 'V'},
205 {"use-ipv4", no_argument, 0, '4'},
206 {"use-ipv6", no_argument, 0, '6'},
207 {"help", no_argument, 0, 'h'},
208 {0, 0, 0, 0}
209 };
211 if (argc < 2)
212 return ERROR;
214 for (c = 1; c < argc; c++) {
215 if (strcmp ("-to", argv[c]) == 0)
216 strcpy (argv[c], "-t");
217 else if (strcmp ("-wt", argv[c]) == 0)
218 strcpy (argv[c], "-w");
219 else if (strcmp ("-ct", argv[c]) == 0)
220 strcpy (argv[c], "-c");
221 }
223 while (1) {
224 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:",
225 longopts, &option);
227 if (c == -1 || c == EOF)
228 break;
230 switch (c) {
231 case 'H': /* hostname */
232 if (is_host (optarg)) {
233 server_address = optarg;
234 }
235 else {
236 usage (_("Invalid host name\n"));
237 }
238 break;
239 case 'p': /* port */
240 if (is_intpos (optarg))
241 server_port = atoi (optarg);
242 else
243 usage (_("Server port must be a positive integer\n"));
244 break;
245 case 'f': /* from argument */
246 from_arg = optarg;
247 break;
248 case 'e': /* server expect string on 220 */
249 server_expect = optarg;
250 break;
251 case 'C': /* server expect string on 220 */
252 mail_command = optarg;
253 smtp_use_dummycmd = 1;
254 break;
255 case 'n': /* server expect string on 220 */
256 smtp_use_dummycmd = 0;
257 break;
258 case 'c': /* critical time threshold */
259 if (is_intnonneg (optarg)) {
260 critical_time = atoi (optarg);
261 check_critical_time = TRUE;
262 }
263 else {
264 usage (_("Critical time must be a nonnegative integer\n"));
265 }
266 break;
267 case 'w': /* warning time threshold */
268 if (is_intnonneg (optarg)) {
269 warning_time = atoi (optarg);
270 check_warning_time = TRUE;
271 }
272 else {
273 usage (_("Warning time must be a nonnegative integer\n"));
274 }
275 break;
276 case 'v': /* verbose */
277 verbose++;
278 break;
279 case 't': /* timeout */
280 if (is_intnonneg (optarg)) {
281 socket_timeout = atoi (optarg);
282 }
283 else {
284 usage (_("Time interval must be a nonnegative integer\n"));
285 }
286 break;
287 case '4':
288 address_family = AF_INET;
289 break;
290 case '6':
291 #ifdef USE_IPV6
292 address_family = AF_INET6;
293 #else
294 usage (_("IPv6 support not available\n"));
295 #endif
296 break;
297 case 'V': /* version */
298 print_revision (progname, revision);
299 exit (STATE_OK);
300 case 'h': /* help */
301 print_help ();
302 exit (STATE_OK);
303 case '?': /* help */
304 usage (_("Invalid argument\n"));
305 }
306 }
308 c = optind;
309 if (server_address == NULL) {
310 if (argv[c]) {
311 if (is_host (argv[c]))
312 server_address = argv[c];
313 else
314 usage (_("Invalid host name"));
315 }
316 else {
317 asprintf (&server_address, "127.0.0.1");
318 }
319 }
321 if (server_expect == NULL)
322 server_expect = strdup (SMTP_EXPECT);
324 if (mail_command == NULL)
325 mail_command = strdup("MAIL ");
327 if (from_arg==NULL)
328 from_arg = strdup(" ");
330 return validate_arguments ();
331 }
337 int
338 validate_arguments (void)
339 {
340 return OK;
341 }
347 \f
348 void
349 print_help (void)
350 {
351 char *myport;
352 asprintf (&myport, "%d", SMTP_PORT);
354 print_revision (progname, revision);
356 printf (_("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"));
357 printf (_(COPYRIGHT), copyright, email);
359 printf(_("\
360 This plugin will attempt to open an SMTP connection with the host.\n\n"));
362 print_usage ();
364 printf (_(UT_HELP_VRSN));
366 printf (_(UT_HOST_PORT), 'p', myport);
368 printf (_(UT_IPv46));
370 printf (_("\
371 -e, --expect=STRING\n\
372 String to expect in first line of server response (default: '%s')\n\
373 -n, nocommand\n\
374 Suppress SMTP command\n\
375 -C, --command=STRING\n\
376 SMTP command (default: '%s')\n\
377 -f, --from=STRING\n\
378 FROM-address to include in MAIL command, required by Exchange 2000\n\
379 (default: '%s')\n"), SMTP_EXPECT, mail_command, from_arg);
381 printf (_(UT_WARN_CRIT));
383 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
385 printf (_(UT_VERBOSE));
387 printf(_("\n\
388 Successul connects return STATE_OK, refusals and timeouts return\n\
389 STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
390 connects, but incorrect reponse messages from the host result in\n\
391 STATE_WARNING return values.\n"));
393 printf (_(UT_SUPPORT));
394 }
400 void
401 print_usage (void)
402 {
403 printf ("\
404 Usage: %s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
405 [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]\n", progname);
406 printf (_(UT_HLP_VRS), progname, progname);
407 }