summaryrefslogtreecommitdiffstats
path: root/plugins/check_smtp.c
diff options
context:
space:
mode:
authorKarl DeBisschop <kdebisschop@users.sourceforge.net>2004-02-28 06:48:05 (GMT)
committerKarl DeBisschop <kdebisschop@users.sourceforge.net>2004-02-28 06:48:05 (GMT)
commit33210cdf40879cf26b891239c1998c06d359fb76 (patch)
tree866fdac25c2a013d8770b5c52ba5ef44d16f95d8 /plugins/check_smtp.c
parent0eda59afa1c95f2e11ae52925169a7c11bb6e352 (diff)
downloadmonitoring-plugins-33210cdf40879cf26b891239c1998c06d359fb76.tar.gz
work in progress to accept multiple command/expect pairs
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@828 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_smtp.c')
-rw-r--r--plugins/check_smtp.c76
1 files changed, 54 insertions, 22 deletions
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c
index 8c4f26e..75cd5a8 100644
--- a/plugins/check_smtp.c
+++ b/plugins/check_smtp.c
@@ -40,9 +40,15 @@ void print_usage (void);
40int server_port = SMTP_PORT; 40int server_port = SMTP_PORT;
41char *server_address = NULL; 41char *server_address = NULL;
42char *server_expect = NULL; 42char *server_expect = NULL;
43int smtp_use_dummycmd = 1; 43int smtp_use_dummycmd = 0;
44char *mail_command; 44char *mail_command = NULL;
45char *from_arg; 45char *from_arg = NULL;
46int ncommands=0;
47int command_size=0;
48int nresponses=0;
49int response_size=0;
50char **commands = NULL;
51char **responses = NULL;
46int warning_time = 0; 52int warning_time = 0;
47int check_warning_time = FALSE; 53int check_warning_time = FALSE;
48int critical_time = 0; 54int critical_time = 0;
@@ -58,11 +64,12 @@ int
58main (int argc, char **argv) 64main (int argc, char **argv)
59{ 65{
60 int sd; 66 int sd;
67 int n = 0;
61 double elapsed_time; 68 double elapsed_time;
62 long microsec; 69 long microsec;
63 int result = STATE_UNKNOWN; 70 int result = STATE_UNKNOWN;
64 char buffer[MAX_INPUT_BUFFER]; 71 char buffer[MAX_INPUT_BUFFER];
65 char *from_str = NULL; 72 char *cmd_str = NULL;
66 char *helocmd = NULL; 73 char *helocmd = NULL;
67 struct timeval tv; 74 struct timeval tv;
68 75
@@ -82,10 +89,10 @@ main (int argc, char **argv)
82 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n"); 89 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
83 90
84 /* initialize the MAIL command with optional FROM command */ 91 /* initialize the MAIL command with optional FROM command */
85 asprintf (&from_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n"); 92 asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
86 93
87 if (verbose) 94 if (verbose && smtp_use_dummycmd)
88 printf ("FROMCMD: %s\n", from_str); 95 printf ("FROM CMD: %s", cmd_str);
89 96
90 /* initialize alarm signal handling */ 97 /* initialize alarm signal handling */
91 (void) signal (SIGALRM, socket_timeout_alarm_handler); 98 (void) signal (SIGALRM, socket_timeout_alarm_handler);
@@ -99,8 +106,7 @@ main (int argc, char **argv)
99 /* try to connect to the host at the given port number */ 106 /* try to connect to the host at the given port number */
100 result = my_tcp_connect (server_address, server_port, &sd); 107 result = my_tcp_connect (server_address, server_port, &sd);
101 108
102 /* we connected, so close connection before exiting */ 109 if (result == STATE_OK) { /* we connected */
103 if (result == STATE_OK) {
104 110
105 /* watch for the SMTP connection string and */ 111 /* watch for the SMTP connection string and */
106 /* return a WARNING status if we couldn't read any data */ 112 /* return a WARNING status if we couldn't read any data */
@@ -109,6 +115,8 @@ main (int argc, char **argv)
109 result = STATE_WARNING; 115 result = STATE_WARNING;
110 } 116 }
111 else { 117 else {
118 if (verbose)
119 printf ("%s", buffer);
112 /* strip the buffer of carriage returns */ 120 /* strip the buffer of carriage returns */
113 strip (buffer); 121 strip (buffer);
114 /* make sure we find the response we are looking for */ 122 /* make sure we find the response we are looking for */
@@ -139,16 +147,25 @@ main (int argc, char **argv)
139 * Use the -f option to provide a FROM address 147 * Use the -f option to provide a FROM address
140 */ 148 */
141 if (smtp_use_dummycmd) { 149 if (smtp_use_dummycmd) {
142 150 send(sd, cmd_str, strlen(cmd_str), 0);
143 send(sd, from_str, strlen(from_str), 0);
144
145 /* allow for response to mail_command to reach us */
146 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0); 151 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
147
148 if (verbose) 152 if (verbose)
149 printf(_("DUMMYCMD: %s\n%s\n"),from_str,buffer); 153 printf("%s", buffer);
154 }
150 155
151 } /* smtp_use_dummycmd */ 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 }
152 169
153 /* tell the server we're done */ 170 /* tell the server we're done */
154 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0); 171 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
@@ -200,6 +217,7 @@ process_arguments (int argc, char **argv)
200 {"port", required_argument, 0, 'p'}, 217 {"port", required_argument, 0, 'p'},
201 {"from", required_argument, 0, 'f'}, 218 {"from", required_argument, 0, 'f'},
202 {"command", required_argument, 0, 'C'}, 219 {"command", required_argument, 0, 'C'},
220 {"response", required_argument, 0, 'R'},
203 {"nocommand", required_argument, 0, 'n'}, 221 {"nocommand", required_argument, 0, 'n'},
204 {"verbose", no_argument, 0, 'v'}, 222 {"verbose", no_argument, 0, 'v'},
205 {"version", no_argument, 0, 'V'}, 223 {"version", no_argument, 0, 'V'},
@@ -222,7 +240,7 @@ process_arguments (int argc, char **argv)
222 } 240 }
223 241
224 while (1) { 242 while (1) {
225 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:", 243 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:",
226 longopts, &option); 244 longopts, &option);
227 245
228 if (c == -1 || c == EOF) 246 if (c == -1 || c == EOF)
@@ -245,16 +263,30 @@ process_arguments (int argc, char **argv)
245 break; 263 break;
246 case 'f': /* from argument */ 264 case 'f': /* from argument */
247 from_arg = optarg; 265 from_arg = optarg;
266 smtp_use_dummycmd = 1;
248 break; 267 break;
249 case 'e': /* server expect string on 220 */ 268 case 'e': /* server expect string on 220 */
250 server_expect = optarg; 269 server_expect = optarg;
251 break; 270 break;
252 case 'C': /* server expect string on 220 */ 271 case 'C': /* commands */
253 mail_command = optarg; 272 if (ncommands >= command_size) {
254 smtp_use_dummycmd = 1; 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++;
255 break; 280 break;
256 case 'n': /* server expect string on 220 */ 281 case 'R': /* server responses */
257 smtp_use_dummycmd = 0; 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++;
258 break; 290 break;
259 case 'c': /* critical time threshold */ 291 case 'c': /* critical time threshold */
260 if (is_intnonneg (optarg)) { 292 if (is_intnonneg (optarg)) {