Code

debian builds not actively maintained, so suppress error in setup script
[nagiosplug.git] / plugins / check_smtp.c
1 /******************************************************************************
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
22 * 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
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 *
35 *****************************************************************************/
37 #include "config.h"
38 #include "common.h"
39 #include "netutils.h"
40 #include "utils.h"
42 #define PROGNAME "check_smtp"
44 #define SMTP_PORT       25
45 #define SMTP_EXPECT     "220"
46 #define SMTP_HELO "HELO "
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"
63 int process_arguments (int, char **);
64 int call_getopt (int, char **);
65 int validate_arguments (void);
66 int check_disk (int usp, int free_disk);
67 void print_help (void);
68 void print_usage (void);
70 int server_port = SMTP_PORT;
71 char *server_address = NULL;
72 char *server_expect = NULL;
73 char *from_arg = " ";
74 int warning_time = 0;
75 int check_warning_time = FALSE;
76 int critical_time = 0;
77 int check_critical_time = FALSE;
78 int verbose = FALSE;
80 int
81 main (int argc, char **argv)
82 {
83         int sd;
84         int result;
85         char buffer[MAX_INPUT_BUFFER] = "";
86         char helocmd[255] = SMTP_HELO ;
87         char from_str[255] = SMTP_DUMMYCMD ;
88         char myhostname[248];
89         
91         if (process_arguments (argc, argv) != OK)
92                 usage ("Invalid command arguments supplied\n");
94         /* initialize the HELO command with the localhostname */
95         gethostname(myhostname, sizeof(myhostname));
96         strcat(helocmd, myhostname);
97         strcat(helocmd, "\r\n");
99         /* initialize the MAIL command with optional FROM command  */
100         if (from_arg) {
101                 strcat(from_str, "FROM: ");
102                 strcat(from_str, from_arg);
103         }
104         /* terminate line with a CRLF */
105         strcat(from_str, "\r\n");
106                 
107         if (verbose == TRUE){
108                 printf ("FROMCMD: %s\n", from_str);
109         }
111         
112         
113         /* initialize alarm signal handling */
114         signal (SIGALRM, socket_timeout_alarm_handler);
116         /* set socket timeout */
117         alarm (socket_timeout);
119         /* try to connect to the host at the given port number */
120         time (&start_time);
121         result = my_tcp_connect (server_address, server_port, &sd);
123         /* we connected, so close connection before exiting */
124         if (result == STATE_OK) {
126                 /* watch for the SMTP connection string */
127                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
129                 /* strip the buffer of carriage returns */
130                 strip (buffer);
132                 /* return a WARNING status if we couldn't read any data */
133                 if (result == -1) {
134                         printf ("recv() failed\n");
135                         result = STATE_WARNING;
136                 }
138                 else {
140                         /* make sure we find the response we are looking for */
141                         if (!strstr (buffer, server_expect)) {
142                                 if (server_port == SMTP_PORT)
143                                         printf ("Invalid SMTP response received from host\n");
144                                 else
145                                         printf ("Invalid SMTP response received from host on port %d\n",
146                                                                         server_port);
147                                 result = STATE_WARNING;
148                         }
150                         else {
152                                 time (&end_time);
154                                 result = STATE_OK;
156                                 if (check_critical_time == TRUE
157                                                 && (end_time - start_time) > critical_time) result =
158                                                 STATE_CRITICAL;
159                                 else if (check_warning_time == TRUE
160                                                                  && (end_time - start_time) > warning_time) result =
161                                                 STATE_WARNING;
163                                 if (verbose == TRUE)
164                                         printf ("SMTP %s - %d sec. response time, %s\n",
165                                                                         state_text (result), (int) (end_time - start_time), buffer);
166                                 else
167                                         printf ("SMTP %s - %d second response time\n", state_text (result),
168                                                                         (int) (end_time - start_time));
169                         }
170                 }
172                 /* close the connection */
173                 /* first send the HELO command */
174                 send(sd,helocmd,strlen(helocmd),0);
175                 /* allow for response to helo command to reach us */
176                 recv(sd,buffer,MAX_INPUT_BUFFER-1,0);
177                                 
178 #ifdef SMTP_USE_DUMMYCMD
179                 send(sd,from_str,strlen(from_str),0);
180                 /* allow for response to DUMMYCMD to reach us */
181                 recv(sd,buffer,MAX_INPUT_BUFFER-1,0);
182                 if (verbose == TRUE) 
183                         printf("DUMMYCMD: %s\n%s\n",from_str,buffer);           
184 #endif /* SMTP_USE_DUMMYCMD */
186                 /* finally close the connection */
187                 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
188                 close (sd);
189         }
191         /* reset the alarm */
192         alarm (0);
194         return result;
202 /* process command-line arguments */
203 int
204 process_arguments (int argc, char **argv)
206         int c;
208         if (argc < 2)
209                 return ERROR;
211         for (c = 1; c < argc; c++) {
212                 if (strcmp ("-to", argv[c]) == 0)
213                         strcpy (argv[c], "-t");
214                 else if (strcmp ("-wt", argv[c]) == 0)
215                         strcpy (argv[c], "-w");
216                 else if (strcmp ("-ct", argv[c]) == 0)
217                         strcpy (argv[c], "-c");
218         }
222         c = 0;
223         while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
225                 if (is_option (argv[c]))
226                         continue;
228                 if (server_address == NULL) {
229                         if (is_host (argv[c])) {
230                                 server_address = argv[c];
231                         }
232                         else {
233                                 usage ("Invalid host name");
234                         }
235                 }
236         }
238         if (server_address == NULL)
239                 server_address = strscpy (NULL, "127.0.0.1");
241         if (server_expect == NULL)
242                 server_expect = strscpy (NULL, SMTP_EXPECT);
244         return validate_arguments ();
252 int
253 call_getopt (int argc, char **argv)
255         int c, i = 0;
257 #ifdef HAVE_GETOPT_H
258         int option_index = 0;
259         static struct option long_options[] = {
260                 {"hostname", required_argument, 0, 'H'},
261                 {"expect", required_argument, 0, 'e'},
262                 {"critical", required_argument, 0, 'c'},
263                 {"warning", required_argument, 0, 'w'},
264                 {"port", required_argument, 0, 'p'},
265                 {"from", required_argument, 0, 'f'},
266                 {"verbose", no_argument, 0, 'v'},
267                 {"version", no_argument, 0, 'V'},
268                 {"help", no_argument, 0, 'h'},
269                 {0, 0, 0, 0}
270         };
271 #endif
273         while (1) {
274 #ifdef HAVE_GETOPT_H
275                 c =
276                         getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:", long_options,
277                                                                          &option_index);
278 #else
279                 c = getopt (argc, argv, "+?hVvt:p:f:e:c:w:H:");
280 #endif
282                 i++;
284                 if (c == -1 || c == EOF || c == 1)
285                         break;
287                 switch (c) {
288                 case 't':
289                 case 'p':
290                 case 'e':
291                 case 'f':
292                 case 'c':
293                 case 'w':
294                 case 'H':
295                         i++;
296                 }
298                 switch (c) {
299                 case 'H':                                                                       /* hostname */
300                         if (is_host (optarg)) {
301                                 server_address = optarg;
302                         }
303                         else {
304                                 usage ("Invalid host name\n");
305                         }
306                         break;
307                 case 'p':                                                                       /* port */
308                         if (is_intpos (optarg)) {
309                                 server_port = atoi (optarg);
310                         }
311                         else {
312                                 usage ("Server port must be a positive integer\n");
313                         }
314                         break;
315                 case 'f':                                                                       /* from argument */
316                         from_arg = optarg;
317                         break;
318                 case 'e':                                                                       /* server expect string on 220  */
319                         server_expect = optarg;
320                         break;
321                 case 'c':                                                                       /* critical time threshold */
322                         if (is_intnonneg (optarg)) {
323                                 critical_time = atoi (optarg);
324                                 check_critical_time = TRUE;
325                         }
326                         else {
327                                 usage ("Critical time must be a nonnegative integer\n");
328                         }
329                         break;
330                 case 'w':                                                                       /* warning time threshold */
331                         if (is_intnonneg (optarg)) {
332                                 warning_time = atoi (optarg);
333                                 check_warning_time = TRUE;
334                         }
335                         else {
336                                 usage ("Warning time must be a nonnegative integer\n");
337                         }
338                         break;
339                 case 'v':                                                                       /* verbose */
340                         verbose = TRUE;
341                         break;
342                 case 't':                                                                       /* timeout */
343                         if (is_intnonneg (optarg)) {
344                                 socket_timeout = atoi (optarg);
345                         }
346                         else {
347                                 usage ("Time interval must be a nonnegative integer\n");
348                         }
349                         break;
350                 case 'V':                                                                       /* version */
351                         print_revision (PROGNAME, "$Revision$");
352                         exit (STATE_OK);
353                 case 'h':                                                                       /* help */
354                         print_help ();
355                         exit (STATE_OK);
356                 case '?':                                                                       /* help */
357                         usage ("Invalid argument\n");
358                 }
359         }
360         return i;
367 int
368 validate_arguments (void)
370         return OK;
377 void
378 print_help (void)
380         print_revision (PROGNAME, "$Revision$");
381         printf
382                 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
383                  "This plugin test the SMTP service on the specified host.\n\n");
384         print_usage ();
385         printf
386                 ("\nOptions:\n"
387                  " -H, --hostname=STRING or IPADDRESS\n"
388                  "   Check server on the indicated host\n"
389                  " -p, --port=INTEGER\n"
390                  "   Make connection on the indicated port (default: %d)\n"
391                  " -e, --expect=STRING\n"
392                  "   String to expect in first line of server response (default: %s)\n"
393                  " -f, --from=STRING\n"
394                  "   from address to include in MAIL command (default NULL, Exchange2000 requires one)\n"
395                  " -w, --warning=INTEGER\n"
396                  "   Seconds necessary to result in a warning status\n"
397                  " -c, --critical=INTEGER\n"
398                  "   Seconds necessary to result in a critical status\n"
399                  " -t, --timeout=INTEGER\n"
400                  "   Seconds before connection attempt times out (default: %d)\n"
401                  " -v, --verbose\n"
402                  "   Print extra information (command-line use only)\n"
403                  " -h, --help\n"
404                  "   Print detailed help screen\n"
405                  " -V, --version\n"
406                  "   Print version information\n\n",
407                  SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
408         support ();
415 void
416 print_usage (void)
418         printf
419                 ("Usage: %s -H host [-e expect] [-p port] [-f from addr] [-w warn] [-c crit] [-t timeout] [-v]\n"
420                  "       %s --help\n"
421                  "       %s --version\n", PROGNAME, PROGNAME, PROGNAME);