summaryrefslogtreecommitdiffstats
path: root/plugins/check_smtp.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-09 23:40:05 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-09 23:40:05 +0100
commit0867319268898bc63f483e6257c65123e10603ba (patch)
treef3a7462cf97f18e993ea5346e061c827d406706c /plugins/check_smtp.c
parent75568a3409fccdebf01446312a9f109cc1a11d15 (diff)
downloadmonitoring-plugins-0867319268898bc63f483e6257c65123e10603ba.tar.gz
check_smtp: clang-format
Diffstat (limited to 'plugins/check_smtp.c')
-rw-r--r--plugins/check_smtp.c850
1 files changed, 410 insertions, 440 deletions
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c
index e6369e63..bb45e01b 100644
--- a/plugins/check_smtp.c
+++ b/plugins/check_smtp.c
@@ -1,32 +1,32 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_smtp plugin 3 * Monitoring check_smtp plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2000-2024 Monitoring Plugins Development Team 6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the check_smtp plugin 10 * This file contains the check_smtp plugin
11* 11 *
12* This plugin will attempt to open an SMTP connection with the host. 12 * This plugin will attempt to open an SMTP connection with the host.
13* 13 *
14* 14 *
15* This program is free software: you can redistribute it and/or modify 15 * This program is free software: you can redistribute it and/or modify
16* it under the terms of the GNU General Public License as published by 16 * it under the terms of the GNU General Public License as published by
17* the Free Software Foundation, either version 3 of the License, or 17 * the Free Software Foundation, either version 3 of the License, or
18* (at your option) any later version. 18 * (at your option) any later version.
19* 19 *
20* This program is distributed in the hope that it will be useful, 20 * This program is distributed in the hope that it will be useful,
21* but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23* GNU General Public License for more details. 23 * GNU General Public License for more details.
24* 24 *
25* You should have received a copy of the GNU General Public License 25 * You should have received a copy of the GNU General Public License
26* along with this program. If not, see <http://www.gnu.org/licenses/>. 26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27* 27 *
28* 28 *
29*****************************************************************************/ 29 *****************************************************************************/
30 30
31const char *progname = "check_smtp"; 31const char *progname = "check_smtp";
32const char *copyright = "2000-2024"; 32const char *copyright = "2000-2024";
@@ -42,24 +42,20 @@ const char *email = "devel@monitoring-plugins.org";
42#ifdef HAVE_SSL 42#ifdef HAVE_SSL
43static bool check_cert = false; 43static bool check_cert = false;
44static int days_till_exp_warn, days_till_exp_crit; 44static int days_till_exp_warn, days_till_exp_crit;
45# define my_recv(buf, len) (((use_starttls || use_ssl) && ssl_established) ? np_net_ssl_read(buf, len) : read(sd, buf, len)) 45# define my_recv(buf, len) (((use_starttls || use_ssl) && ssl_established) ? np_net_ssl_read(buf, len) : read(sd, buf, len))
46# define my_send(buf, len) (((use_starttls || use_ssl) && ssl_established) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0)) 46# define my_send(buf, len) (((use_starttls || use_ssl) && ssl_established) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0))
47#else /* ifndef HAVE_SSL */ 47#else /* ifndef HAVE_SSL */
48# define my_recv(buf, len) read(sd, buf, len) 48# define my_recv(buf, len) read(sd, buf, len)
49# define my_send(buf, len) send(sd, buf, len, 0) 49# define my_send(buf, len) send(sd, buf, len, 0)
50#endif 50#endif
51 51
52enum { 52#define PROXY_PREFIX "PROXY TCP4 0.0.0.0 0.0.0.0 25 25\r\n"
53 SMTP_PORT = 25, 53#define SMTP_EXPECT "220"
54 SMTPS_PORT = 465 54#define SMTP_HELO "HELO "
55}; 55#define SMTP_EHLO "EHLO "
56#define PROXY_PREFIX "PROXY TCP4 0.0.0.0 0.0.0.0 25 25\r\n" 56#define SMTP_LHLO "LHLO "
57#define SMTP_EXPECT "220" 57#define SMTP_QUIT "QUIT\r\n"
58#define SMTP_HELO "HELO " 58#define SMTP_STARTTLS "STARTTLS\r\n"
59#define SMTP_EHLO "EHLO "
60#define SMTP_LHLO "LHLO "
61#define SMTP_QUIT "QUIT\r\n"
62#define SMTP_STARTTLS "STARTTLS\r\n"
63#define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n" 59#define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n"
64 60
65#define EHLO_SUPPORTS_STARTTLS 1 61#define EHLO_SUPPORTS_STARTTLS 1
@@ -81,17 +77,16 @@ static int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
81static int eflags = 0; 77static int eflags = 0;
82static int errcode, excode; 78static int errcode, excode;
83 79
84static int server_port = SMTP_PORT;
85static int server_port_option = 0; 80static int server_port_option = 0;
86static char *server_address = NULL; 81static char *server_address = NULL;
87static char *server_expect = NULL; 82static char *server_expect = NULL;
88static char *mail_command = NULL; 83static char *mail_command = NULL;
89static char *from_arg = NULL; 84static char *from_arg = NULL;
90static int send_mail_from=0; 85static int send_mail_from = 0;
91static int ncommands=0; 86static int ncommands = 0;
92static int command_size=0; 87static int command_size = 0;
93static int nresponses=0; 88static int nresponses = 0;
94static int response_size=0; 89static int response_size = 0;
95static char **commands = NULL; 90static char **commands = NULL;
96static char **responses = NULL; 91static char **responses = NULL;
97static char *authtype = NULL; 92static char *authtype = NULL;
@@ -113,15 +108,12 @@ static char *localhostname = NULL;
113static int sd; 108static int sd;
114static char buffer[MAX_INPUT_BUFFER]; 109static char buffer[MAX_INPUT_BUFFER];
115enum { 110enum {
116 TCP_PROTOCOL = 1, 111 TCP_PROTOCOL = 1,
117 UDP_PROTOCOL = 2, 112 UDP_PROTOCOL = 2,
118}; 113};
119static bool ignore_send_quit_failure = false; 114static bool ignore_send_quit_failure = false;
120 115
121 116int main(int argc, char **argv) {
122int
123main (int argc, char **argv)
124{
125 bool supports_tls = false; 117 bool supports_tls = false;
126 int n = 0; 118 int n = 0;
127 double elapsed_time; 119 double elapsed_time;
@@ -134,63 +126,68 @@ main (int argc, char **argv)
134 struct timeval tv; 126 struct timeval tv;
135 127
136 /* Catch pipe errors in read/write - sometimes occurs when writing QUIT */ 128 /* Catch pipe errors in read/write - sometimes occurs when writing QUIT */
137 (void) signal (SIGPIPE, SIG_IGN); 129 (void)signal(SIGPIPE, SIG_IGN);
138 130
139 setlocale (LC_ALL, ""); 131 setlocale(LC_ALL, "");
140 bindtextdomain (PACKAGE, LOCALEDIR); 132 bindtextdomain(PACKAGE, LOCALEDIR);
141 textdomain (PACKAGE); 133 textdomain(PACKAGE);
142 134
143 /* Parse extra opts if any */ 135 /* Parse extra opts if any */
144 argv=np_extra_opts (&argc, argv, progname); 136 argv = np_extra_opts(&argc, argv, progname);
145 137
146 if (process_arguments (argc, argv) == ERROR) 138 if (process_arguments(argc, argv) == ERROR) {
147 usage4 (_("Could not parse arguments")); 139 usage4(_("Could not parse arguments"));
140 }
148 141
149 /* If localhostname not set on command line, use gethostname to set */ 142 /* If localhostname not set on command line, use gethostname to set */
150 if(! localhostname){ 143 if (!localhostname) {
151 localhostname = malloc (HOST_MAX_BYTES); 144 localhostname = malloc(HOST_MAX_BYTES);
152 if(!localhostname){ 145 if (!localhostname) {
153 printf(_("malloc() failed!\n")); 146 printf(_("malloc() failed!\n"));
154 return STATE_CRITICAL; 147 return STATE_CRITICAL;
155 } 148 }
156 if(gethostname(localhostname, HOST_MAX_BYTES)){ 149 if (gethostname(localhostname, HOST_MAX_BYTES)) {
157 printf(_("gethostname() failed!\n")); 150 printf(_("gethostname() failed!\n"));
158 return STATE_CRITICAL; 151 return STATE_CRITICAL;
159 } 152 }
160 } 153 }
161 if(use_lhlo) 154 if (use_lhlo) {
162 xasprintf (&helocmd, "%s%s%s", SMTP_LHLO, localhostname, "\r\n"); 155 xasprintf(&helocmd, "%s%s%s", SMTP_LHLO, localhostname, "\r\n");
163 else if(use_ehlo) 156 } else if (use_ehlo) {
164 xasprintf (&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n"); 157 xasprintf(&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n");
165 else 158 } else {
166 xasprintf (&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n"); 159 xasprintf(&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n");
167 160 }
168 if (verbose) 161
162 if (verbose) {
169 printf("HELOCMD: %s", helocmd); 163 printf("HELOCMD: %s", helocmd);
164 }
170 165
171 /* initialize the MAIL command with optional FROM command */ 166 /* initialize the MAIL command with optional FROM command */
172 xasprintf (&cmd_str, "%sFROM:<%s>%s", mail_command, from_arg, "\r\n"); 167 xasprintf(&cmd_str, "%sFROM:<%s>%s", mail_command, from_arg, "\r\n");
173 168
174 if (verbose && send_mail_from) 169 if (verbose && send_mail_from) {
175 printf ("FROM CMD: %s", cmd_str); 170 printf("FROM CMD: %s", cmd_str);
171 }
176 172
177 /* initialize alarm signal handling */ 173 /* initialize alarm signal handling */
178 (void) signal (SIGALRM, socket_timeout_alarm_handler); 174 (void)signal(SIGALRM, socket_timeout_alarm_handler);
179 175
180 /* set socket timeout */ 176 /* set socket timeout */
181 (void) alarm (socket_timeout); 177 (void)alarm(socket_timeout);
182 178
183 /* start timer */ 179 /* start timer */
184 gettimeofday (&tv, NULL); 180 gettimeofday(&tv, NULL);
185 181
186 /* try to connect to the host at the given port number */ 182 /* try to connect to the host at the given port number */
187 result = my_tcp_connect (server_address, server_port, &sd); 183 result = my_tcp_connect(server_address, server_port, &sd);
188 184
189 if (result == STATE_OK) { /* we connected */ 185 if (result == STATE_OK) { /* we connected */
190 /* If requested, send PROXY header */ 186 /* If requested, send PROXY header */
191 if (use_proxy_prefix) { 187 if (use_proxy_prefix) {
192 if (verbose) 188 if (verbose) {
193 printf ("Sending header %s\n", PROXY_PREFIX); 189 printf("Sending header %s\n", PROXY_PREFIX);
190 }
194 my_send(PROXY_PREFIX, strlen(PROXY_PREFIX)); 191 my_send(PROXY_PREFIX, strlen(PROXY_PREFIX));
195 } 192 }
196 193
@@ -198,7 +195,7 @@ main (int argc, char **argv)
198 if (use_ssl) { 195 if (use_ssl) {
199 result = np_net_ssl_init_with_hostname(sd, (use_sni ? server_address : NULL)); 196 result = np_net_ssl_init_with_hostname(sd, (use_sni ? server_address : NULL));
200 if (result != STATE_OK) { 197 if (result != STATE_OK) {
201 printf (_("CRITICAL - Cannot create SSL context.\n")); 198 printf(_("CRITICAL - Cannot create SSL context.\n"));
202 close(sd); 199 close(sd);
203 np_net_ssl_cleanup(); 200 np_net_ssl_cleanup();
204 return STATE_CRITICAL; 201 return STATE_CRITICAL;
@@ -211,7 +208,7 @@ main (int argc, char **argv)
211 /* watch for the SMTP connection string and */ 208 /* watch for the SMTP connection string and */
212 /* return a WARNING status if we couldn't read any data */ 209 /* return a WARNING status if we couldn't read any data */
213 if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) { 210 if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) {
214 printf (_("recv() failed\n")); 211 printf(_("recv() failed\n"));
215 return STATE_WARNING; 212 return STATE_WARNING;
216 } 213 }
217 214
@@ -223,130 +220,131 @@ main (int argc, char **argv)
223 220
224 /* allow for response to helo command to reach us */ 221 /* allow for response to helo command to reach us */
225 if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) { 222 if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) {
226 printf (_("recv() failed\n")); 223 printf(_("recv() failed\n"));
227 return STATE_WARNING; 224 return STATE_WARNING;
228 } else if(use_ehlo || use_lhlo){ 225 } else if (use_ehlo || use_lhlo) {
229 if(strstr(buffer, "250 STARTTLS") != NULL || 226 if (strstr(buffer, "250 STARTTLS") != NULL || strstr(buffer, "250-STARTTLS") != NULL) {
230 strstr(buffer, "250-STARTTLS") != NULL){ 227 supports_tls = true;
231 supports_tls=true;
232 } 228 }
233 } 229 }
234 230
235 if(use_starttls && ! supports_tls){ 231 if (use_starttls && !supports_tls) {
236 printf(_("WARNING - TLS not supported by server\n")); 232 printf(_("WARNING - TLS not supported by server\n"));
237 smtp_quit(); 233 smtp_quit();
238 return STATE_WARNING; 234 return STATE_WARNING;
239 } 235 }
240 236
241#ifdef HAVE_SSL 237#ifdef HAVE_SSL
242 if(use_starttls) { 238 if (use_starttls) {
243 /* send the STARTTLS command */ 239 /* send the STARTTLS command */
244 send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0); 240 send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0);
245 241
246 recvlines(buffer, MAX_INPUT_BUFFER); /* wait for it */ 242 recvlines(buffer, MAX_INPUT_BUFFER); /* wait for it */
247 if (!strstr (buffer, SMTP_EXPECT)) { 243 if (!strstr(buffer, SMTP_EXPECT)) {
248 printf (_("Server does not support STARTTLS\n")); 244 printf(_("Server does not support STARTTLS\n"));
249 smtp_quit(); 245 smtp_quit();
250 return STATE_UNKNOWN; 246 return STATE_UNKNOWN;
251 } 247 }
252 result = np_net_ssl_init_with_hostname(sd, (use_sni ? server_address : NULL)); 248 result = np_net_ssl_init_with_hostname(sd, (use_sni ? server_address : NULL));
253 if(result != STATE_OK) { 249 if (result != STATE_OK) {
254 printf (_("CRITICAL - Cannot create SSL context.\n")); 250 printf(_("CRITICAL - Cannot create SSL context.\n"));
255 close(sd); 251 close(sd);
256 np_net_ssl_cleanup(); 252 np_net_ssl_cleanup();
257 return STATE_CRITICAL; 253 return STATE_CRITICAL;
258 } else { 254 } else {
259 ssl_established = 1; 255 ssl_established = 1;
260 } 256 }
261 257
262 /* 258 /*
263 * Resend the EHLO command. 259 * Resend the EHLO command.
264 * 260 *
265 * RFC 3207 (4.2) says: ``The client MUST discard any knowledge 261 * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
266 * obtained from the server, such as the list of SMTP service 262 * obtained from the server, such as the list of SMTP service
267 * extensions, which was not obtained from the TLS negotiation 263 * extensions, which was not obtained from the TLS negotiation
268 * itself. The client SHOULD send an EHLO command as the first 264 * itself. The client SHOULD send an EHLO command as the first
269 * command after a successful TLS negotiation.'' For this 265 * command after a successful TLS negotiation.'' For this
270 * reason, some MTAs will not allow an AUTH LOGIN command before 266 * reason, some MTAs will not allow an AUTH LOGIN command before
271 * we resent EHLO via TLS. 267 * we resent EHLO via TLS.
272 */ 268 */
273 if (my_send(helocmd, strlen(helocmd)) <= 0) { 269 if (my_send(helocmd, strlen(helocmd)) <= 0) {
274 printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS.")); 270 printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS."));
275 my_close(); 271 my_close();
276 return STATE_UNKNOWN; 272 return STATE_UNKNOWN;
277 } 273 }
278 if (verbose) 274 if (verbose) {
279 printf(_("sent %s"), helocmd); 275 printf(_("sent %s"), helocmd);
280 if ((n = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { 276 }
281 printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS.")); 277 if ((n = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
282 my_close(); 278 printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS."));
283 return STATE_UNKNOWN; 279 my_close();
284 } 280 return STATE_UNKNOWN;
285 if (verbose) { 281 }
286 printf("%s", buffer); 282 if (verbose) {
287 } 283 printf("%s", buffer);
284 }
288 285
289# ifdef USE_OPENSSL 286# ifdef USE_OPENSSL
290 if ( check_cert ) { 287 if (check_cert) {
291 result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); 288 result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit);
292 smtp_quit(); 289 smtp_quit();
293 my_close(); 290 my_close();
294 return result; 291 return result;
295 } 292 }
296# endif /* USE_OPENSSL */ 293# endif /* USE_OPENSSL */
297 } 294 }
298#endif 295#endif
299 296
300 if (verbose) 297 if (verbose) {
301 printf ("%s", buffer); 298 printf("%s", buffer);
299 }
302 300
303 /* save buffer for later use */ 301 /* save buffer for later use */
304 xasprintf(&server_response, "%s%s", server_response, buffer); 302 xasprintf(&server_response, "%s%s", server_response, buffer);
305 /* strip the buffer of carriage returns */ 303 /* strip the buffer of carriage returns */
306 strip (server_response); 304 strip(server_response);
307 305
308 /* make sure we find the droids we are looking for */ 306 /* make sure we find the droids we are looking for */
309 if (!strstr (server_response, server_expect)) { 307 if (!strstr(server_response, server_expect)) {
310 if (server_port == SMTP_PORT) 308 if (server_port == SMTP_PORT) {
311 printf (_("Invalid SMTP response received from host: %s\n"), server_response); 309 printf(_("Invalid SMTP response received from host: %s\n"), server_response);
312 else 310 } else {
313 printf (_("Invalid SMTP response received from host on port %d: %s\n"), 311 printf(_("Invalid SMTP response received from host on port %d: %s\n"), server_port, server_response);
314 server_port, server_response); 312 }
315 return STATE_WARNING; 313 return STATE_WARNING;
316 } 314 }
317 315
318 if (send_mail_from) { 316 if (send_mail_from) {
319 my_send(cmd_str, strlen(cmd_str)); 317 my_send(cmd_str, strlen(cmd_str));
320 if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose) 318 if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose) {
321 printf("%s", buffer); 319 printf("%s", buffer);
320 }
322 } 321 }
323 322
324 n = 0; 323 n = 0;
325 while (n < ncommands) { 324 while (n < ncommands) {
326 xasprintf (&cmd_str, "%s%s", commands[n], "\r\n"); 325 xasprintf(&cmd_str, "%s%s", commands[n], "\r\n");
327 my_send(cmd_str, strlen(cmd_str)); 326 my_send(cmd_str, strlen(cmd_str));
328 if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose) 327 if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose) {
329 printf("%s", buffer); 328 printf("%s", buffer);
330 strip (buffer); 329 }
330 strip(buffer);
331 if (n < nresponses) { 331 if (n < nresponses) {
332 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; 332 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
333 errcode = regcomp (&preg, responses[n], cflags); 333 errcode = regcomp(&preg, responses[n], cflags);
334 if (errcode != 0) { 334 if (errcode != 0) {
335 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); 335 regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER);
336 printf (_("Could Not Compile Regular Expression")); 336 printf(_("Could Not Compile Regular Expression"));
337 return ERROR; 337 return ERROR;
338 } 338 }
339 excode = regexec (&preg, buffer, 10, pmatch, eflags); 339 excode = regexec(&preg, buffer, 10, pmatch, eflags);
340 if (excode == 0) { 340 if (excode == 0) {
341 result = STATE_OK; 341 result = STATE_OK;
342 } 342 } else if (excode == REG_NOMATCH) {
343 else if (excode == REG_NOMATCH) {
344 result = STATE_WARNING; 343 result = STATE_WARNING;
345 printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]); 344 printf(_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text(result), buffer, commands[n]);
346 } 345 } else {
347 else { 346 regerror(excode, &preg, errbuf, MAX_INPUT_BUFFER);
348 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER); 347 printf(_("Execute Error: %s\n"), errbuf);
349 printf (_("Execute Error: %s\n"), errbuf);
350 result = STATE_UNKNOWN; 348 result = STATE_UNKNOWN;
351 } 349 }
352 } 350 }
@@ -354,7 +352,7 @@ main (int argc, char **argv)
354 } 352 }
355 353
356 if (authtype != NULL) { 354 if (authtype != NULL) {
357 if (strcmp (authtype, "LOGIN") == 0) { 355 if (strcmp(authtype, "LOGIN") == 0) {
358 char *abuf; 356 char *abuf;
359 int ret; 357 int ret;
360 do { 358 do {
@@ -371,29 +369,32 @@ main (int argc, char **argv)
371 369
372 /* send AUTH LOGIN */ 370 /* send AUTH LOGIN */
373 my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN)); 371 my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN));
374 if (verbose) 372 if (verbose) {
375 printf (_("sent %s\n"), "AUTH LOGIN"); 373 printf(_("sent %s\n"), "AUTH LOGIN");
374 }
376 375
377 if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { 376 if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
378 xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, ")); 377 xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, "));
379 result = STATE_WARNING; 378 result = STATE_WARNING;
380 break; 379 break;
381 } 380 }
382 if (verbose) 381 if (verbose) {
383 printf (_("received %s\n"), buffer); 382 printf(_("received %s\n"), buffer);
383 }
384 384
385 if (strncmp (buffer, "334", 3) != 0) { 385 if (strncmp(buffer, "334", 3) != 0) {
386 result = STATE_CRITICAL; 386 result = STATE_CRITICAL;
387 xasprintf(&error_msg, _("invalid response received after AUTH LOGIN, ")); 387 xasprintf(&error_msg, _("invalid response received after AUTH LOGIN, "));
388 break; 388 break;
389 } 389 }
390 390
391 /* encode authuser with base64 */ 391 /* encode authuser with base64 */
392 base64_encode_alloc (authuser, strlen(authuser), &abuf); 392 base64_encode_alloc(authuser, strlen(authuser), &abuf);
393 xasprintf(&abuf, "%s\r\n", abuf); 393 xasprintf(&abuf, "%s\r\n", abuf);
394 my_send(abuf, strlen(abuf)); 394 my_send(abuf, strlen(abuf));
395 if (verbose) 395 if (verbose) {
396 printf (_("sent %s\n"), abuf); 396 printf(_("sent %s\n"), abuf);
397 }
397 398
398 if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { 399 if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
399 result = STATE_CRITICAL; 400 result = STATE_CRITICAL;
@@ -401,19 +402,19 @@ main (int argc, char **argv)
401 break; 402 break;
402 } 403 }
403 if (verbose) { 404 if (verbose) {
404 printf (_("received %s\n"), buffer); 405 printf(_("received %s\n"), buffer);
405 } 406 }
406 if (strncmp (buffer, "334", 3) != 0) { 407 if (strncmp(buffer, "334", 3) != 0) {
407 result = STATE_CRITICAL; 408 result = STATE_CRITICAL;
408 xasprintf(&error_msg, _("invalid response received after authuser, ")); 409 xasprintf(&error_msg, _("invalid response received after authuser, "));
409 break; 410 break;
410 } 411 }
411 /* encode authpass with base64 */ 412 /* encode authpass with base64 */
412 base64_encode_alloc (authpass, strlen(authpass), &abuf); 413 base64_encode_alloc(authpass, strlen(authpass), &abuf);
413 xasprintf(&abuf, "%s\r\n", abuf); 414 xasprintf(&abuf, "%s\r\n", abuf);
414 my_send(abuf, strlen(abuf)); 415 my_send(abuf, strlen(abuf));
415 if (verbose) { 416 if (verbose) {
416 printf (_("sent %s\n"), abuf); 417 printf(_("sent %s\n"), abuf);
417 } 418 }
418 if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { 419 if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
419 result = STATE_CRITICAL; 420 result = STATE_CRITICAL;
@@ -421,9 +422,9 @@ main (int argc, char **argv)
421 break; 422 break;
422 } 423 }
423 if (verbose) { 424 if (verbose) {
424 printf (_("received %s\n"), buffer); 425 printf(_("received %s\n"), buffer);
425 } 426 }
426 if (strncmp (buffer, "235", 3) != 0) { 427 if (strncmp(buffer, "235", 3) != 0) {
427 result = STATE_CRITICAL; 428 result = STATE_CRITICAL;
428 xasprintf(&error_msg, _("invalid response received after authpass, ")); 429 xasprintf(&error_msg, _("invalid response received after authpass, "));
429 break; 430 break;
@@ -440,120 +441,112 @@ main (int argc, char **argv)
440 smtp_quit(); 441 smtp_quit();
441 442
442 /* finally close the connection */ 443 /* finally close the connection */
443 close (sd); 444 close(sd);
444 } 445 }
445 446
446 /* reset the alarm */ 447 /* reset the alarm */
447 alarm (0); 448 alarm(0);
448 449
449 microsec = deltime (tv); 450 microsec = deltime(tv);
450 elapsed_time = (double)microsec / 1.0e6; 451 elapsed_time = (double)microsec / 1.0e6;
451 452
452 if (result == STATE_OK) { 453 if (result == STATE_OK) {
453 if (check_critical_time && elapsed_time > critical_time) 454 if (check_critical_time && elapsed_time > critical_time) {
454 result = STATE_CRITICAL; 455 result = STATE_CRITICAL;
455 else if (check_warning_time && elapsed_time > warning_time) 456 } else if (check_warning_time && elapsed_time > warning_time) {
456 result = STATE_WARNING; 457 result = STATE_WARNING;
458 }
457 } 459 }
458 460
459 printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), 461 printf(_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), state_text(result), error_msg, elapsed_time, verbose ? ", " : "",
460 state_text (result), 462 verbose ? buffer : "",
461 error_msg, 463 fperfdata("time", elapsed_time, "s", (int)check_warning_time, warning_time, (int)check_critical_time, critical_time, true, 0,
462 elapsed_time, 464 false, 0));
463 verbose?", ":"", verbose?buffer:"",
464 fperfdata ("time", elapsed_time, "s",
465 (int)check_warning_time, warning_time,
466 (int)check_critical_time, critical_time,
467 true, 0, false, 0));
468 465
469 return result; 466 return result;
470} 467}
471 468
472
473
474/* process command-line arguments */ 469/* process command-line arguments */
475int 470int process_arguments(int argc, char **argv) {
476process_arguments (int argc, char **argv)
477{
478 int c; 471 int c;
479 char* temp; 472 char *temp;
480 473
481 bool implicit_tls = false; 474 bool implicit_tls = false;
482 475
483 enum { 476 enum {
484 SNI_OPTION 477 SNI_OPTION
485 }; 478 };
486 479
487 int option = 0; 480 int option = 0;
488 static struct option longopts[] = { 481 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
489 {"hostname", required_argument, 0, 'H'}, 482 {"expect", required_argument, 0, 'e'},
490 {"expect", required_argument, 0, 'e'}, 483 {"critical", required_argument, 0, 'c'},
491 {"critical", required_argument, 0, 'c'}, 484 {"warning", required_argument, 0, 'w'},
492 {"warning", required_argument, 0, 'w'}, 485 {"timeout", required_argument, 0, 't'},
493 {"timeout", required_argument, 0, 't'}, 486 {"port", required_argument, 0, 'p'},
494 {"port", required_argument, 0, 'p'}, 487 {"from", required_argument, 0, 'f'},
495 {"from", required_argument, 0, 'f'}, 488 {"fqdn", required_argument, 0, 'F'},
496 {"fqdn", required_argument, 0, 'F'}, 489 {"authtype", required_argument, 0, 'A'},
497 {"authtype", required_argument, 0, 'A'}, 490 {"authuser", required_argument, 0, 'U'},
498 {"authuser", required_argument, 0, 'U'}, 491 {"authpass", required_argument, 0, 'P'},
499 {"authpass", required_argument, 0, 'P'}, 492 {"command", required_argument, 0, 'C'},
500 {"command", required_argument, 0, 'C'}, 493 {"response", required_argument, 0, 'R'},
501 {"response", required_argument, 0, 'R'}, 494 {"verbose", no_argument, 0, 'v'},
502 {"verbose", no_argument, 0, 'v'}, 495 {"version", no_argument, 0, 'V'},
503 {"version", no_argument, 0, 'V'}, 496 {"use-ipv4", no_argument, 0, '4'},
504 {"use-ipv4", no_argument, 0, '4'}, 497 {"use-ipv6", no_argument, 0, '6'},
505 {"use-ipv6", no_argument, 0, '6'}, 498 {"help", no_argument, 0, 'h'},
506 {"help", no_argument, 0, 'h'}, 499 {"lmtp", no_argument, 0, 'L'},
507 {"lmtp", no_argument, 0, 'L'}, 500 {"ssl", no_argument, 0, 's'},
508 {"ssl", no_argument, 0, 's'}, 501 {"tls", no_argument, 0, 's'},
509 {"tls", no_argument, 0, 's'}, 502 {"starttls", no_argument, 0, 'S'},
510 {"starttls",no_argument,0,'S'}, 503 {"sni", no_argument, 0, SNI_OPTION},
511 {"sni", no_argument, 0, SNI_OPTION}, 504 {"certificate", required_argument, 0, 'D'},
512 {"certificate",required_argument,0,'D'}, 505 {"ignore-quit-failure", no_argument, 0, 'q'},
513 {"ignore-quit-failure",no_argument,0,'q'}, 506 {"proxy", no_argument, 0, 'r'},
514 {"proxy",no_argument,0,'r'}, 507 {0, 0, 0, 0}};
515 {0, 0, 0, 0} 508
516 }; 509 if (argc < 2) {
517
518 if (argc < 2)
519 return ERROR; 510 return ERROR;
511 }
520 512
521 for (c = 1; c < argc; c++) { 513 for (c = 1; c < argc; c++) {
522 if (strcmp ("-to", argv[c]) == 0) 514 if (strcmp("-to", argv[c]) == 0) {
523 strcpy (argv[c], "-t"); 515 strcpy(argv[c], "-t");
524 else if (strcmp ("-wt", argv[c]) == 0) 516 } else if (strcmp("-wt", argv[c]) == 0) {
525 strcpy (argv[c], "-w"); 517 strcpy(argv[c], "-w");
526 else if (strcmp ("-ct", argv[c]) == 0) 518 } else if (strcmp("-ct", argv[c]) == 0) {
527 strcpy (argv[c], "-c"); 519 strcpy(argv[c], "-c");
520 }
528 } 521 }
529 522
530 while (1) { 523 while (1) {
531 c = getopt_long (argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", 524 c = getopt_long(argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", longopts, &option);
532 longopts, &option);
533 525
534 if (c == -1 || c == EOF) 526 if (c == -1 || c == EOF) {
535 break; 527 break;
528 }
536 529
537 switch (c) { 530 switch (c) {
538 case 'H': /* hostname */ 531 case 'H': /* hostname */
539 if (is_host (optarg)) { 532 if (is_host(optarg)) {
540 server_address = optarg; 533 server_address = optarg;
541 } 534 } else {
542 else { 535 usage2(_("Invalid hostname/address"), optarg);
543 usage2 (_("Invalid hostname/address"), optarg);
544 } 536 }
545 break; 537 break;
546 case 'p': /* port */ 538 case 'p': /* port */
547 if (is_intpos (optarg)) 539 if (is_intpos(optarg)) {
548 server_port_option = atoi (optarg); 540 server_port_option = atoi(optarg);
549 else 541 } else {
550 usage4 (_("Port must be a positive integer")); 542 usage4(_("Port must be a positive integer"));
543 }
551 break; 544 break;
552 case 'F': 545 case 'F':
553 /* localhostname */ 546 /* localhostname */
554 localhostname = strdup(optarg); 547 localhostname = strdup(optarg);
555 break; 548 break;
556 case 'f': /* from argument */ 549 case 'f': /* from argument */
557 from_arg = optarg + strspn(optarg, "<"); 550 from_arg = optarg + strspn(optarg, "<");
558 from_arg = strndup(from_arg, strcspn(from_arg, ">")); 551 from_arg = strndup(from_arg, strcspn(from_arg, ">"));
559 send_mail_from = 1; 552 send_mail_from = 1;
@@ -568,97 +561,98 @@ process_arguments (int argc, char **argv)
568 case 'P': 561 case 'P':
569 authpass = optarg; 562 authpass = optarg;
570 break; 563 break;
571 case 'e': /* server expect string on 220 */ 564 case 'e': /* server expect string on 220 */
572 server_expect = optarg; 565 server_expect = optarg;
573 break; 566 break;
574 case 'C': /* commands */ 567 case 'C': /* commands */
575 if (ncommands >= command_size) { 568 if (ncommands >= command_size) {
576 command_size+=8; 569 command_size += 8;
577 commands = realloc (commands, sizeof(char *) * command_size); 570 commands = realloc(commands, sizeof(char *) * command_size);
578 if (commands == NULL) 571 if (commands == NULL) {
579 die (STATE_UNKNOWN, 572 die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), ncommands);
580 _("Could not realloc() units [%d]\n"), ncommands); 573 }
581 } 574 }
582 commands[ncommands] = (char *) malloc (sizeof(char) * 255); 575 commands[ncommands] = (char *)malloc(sizeof(char) * 255);
583 strncpy (commands[ncommands], optarg, 255); 576 strncpy(commands[ncommands], optarg, 255);
584 ncommands++; 577 ncommands++;
585 break; 578 break;
586 case 'R': /* server responses */ 579 case 'R': /* server responses */
587 if (nresponses >= response_size) { 580 if (nresponses >= response_size) {
588 response_size += 8; 581 response_size += 8;
589 responses = realloc (responses, sizeof(char *) * response_size); 582 responses = realloc(responses, sizeof(char *) * response_size);
590 if (responses == NULL) 583 if (responses == NULL) {
591 die (STATE_UNKNOWN, 584 die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), nresponses);
592 _("Could not realloc() units [%d]\n"), nresponses); 585 }
593 } 586 }
594 responses[nresponses] = (char *) malloc (sizeof(char) * 255); 587 responses[nresponses] = (char *)malloc(sizeof(char) * 255);
595 strncpy (responses[nresponses], optarg, 255); 588 strncpy(responses[nresponses], optarg, 255);
596 nresponses++; 589 nresponses++;
597 break; 590 break;
598 case 'c': /* critical time threshold */ 591 case 'c': /* critical time threshold */
599 if (!is_nonnegative (optarg)) 592 if (!is_nonnegative(optarg)) {
600 usage4 (_("Critical time must be a positive")); 593 usage4(_("Critical time must be a positive"));
601 else { 594 } else {
602 critical_time = strtod (optarg, NULL); 595 critical_time = strtod(optarg, NULL);
603 check_critical_time = true; 596 check_critical_time = true;
604 } 597 }
605 break; 598 break;
606 case 'w': /* warning time threshold */ 599 case 'w': /* warning time threshold */
607 if (!is_nonnegative (optarg)) 600 if (!is_nonnegative(optarg)) {
608 usage4 (_("Warning time must be a positive")); 601 usage4(_("Warning time must be a positive"));
609 else { 602 } else {
610 warning_time = strtod (optarg, NULL); 603 warning_time = strtod(optarg, NULL);
611 check_warning_time = true; 604 check_warning_time = true;
612 } 605 }
613 break; 606 break;
614 case 'v': /* verbose */ 607 case 'v': /* verbose */
615 verbose++; 608 verbose++;
616 break; 609 break;
617 case 'q': 610 case 'q':
618 ignore_send_quit_failure = true; /* ignore problem sending QUIT */ 611 ignore_send_quit_failure = true; /* ignore problem sending QUIT */
619 break; 612 break;
620 case 't': /* timeout */ 613 case 't': /* timeout */
621 if (is_intnonneg (optarg)) { 614 if (is_intnonneg(optarg)) {
622 socket_timeout = atoi (optarg); 615 socket_timeout = atoi(optarg);
623 } 616 } else {
624 else { 617 usage4(_("Timeout interval must be a positive integer"));
625 usage4 (_("Timeout interval must be a positive integer"));
626 } 618 }
627 break; 619 break;
628 case 'D': 620 case 'D':
629 /* Check SSL cert validity */ 621 /* Check SSL cert validity */
630#ifdef USE_OPENSSL 622#ifdef USE_OPENSSL
631 if ((temp=strchr(optarg,','))!=NULL) { 623 if ((temp = strchr(optarg, ',')) != NULL) {
632 *temp='\0'; 624 *temp = '\0';
633 if (!is_intnonneg (optarg)) 625 if (!is_intnonneg(optarg)) {
634 usage2 ("Invalid certificate expiration period", optarg); 626 usage2("Invalid certificate expiration period", optarg);
635 days_till_exp_warn = atoi(optarg); 627 }
636 *temp=','; 628 days_till_exp_warn = atoi(optarg);
637 temp++; 629 *temp = ',';
638 if (!is_intnonneg (temp)) 630 temp++;
639 usage2 (_("Invalid certificate expiration period"), temp); 631 if (!is_intnonneg(temp)) {
640 days_till_exp_crit = atoi (temp); 632 usage2(_("Invalid certificate expiration period"), temp);
641 } 633 }
642 else { 634 days_till_exp_crit = atoi(temp);
643 days_till_exp_crit=0; 635 } else {
644 if (!is_intnonneg (optarg)) 636 days_till_exp_crit = 0;
645 usage2 ("Invalid certificate expiration period", optarg); 637 if (!is_intnonneg(optarg)) {
646 days_till_exp_warn = atoi (optarg); 638 usage2("Invalid certificate expiration period", optarg);
647 } 639 }
640 days_till_exp_warn = atoi(optarg);
641 }
648 check_cert = true; 642 check_cert = true;
649 ignore_send_quit_failure = true; 643 ignore_send_quit_failure = true;
650#else 644#else
651 usage (_("SSL support not available - install OpenSSL and recompile")); 645 usage(_("SSL support not available - install OpenSSL and recompile"));
652#endif 646#endif
653 implicit_tls = true; 647 implicit_tls = true;
654 // fallthrough 648 // fallthrough
655 case 's': 649 case 's':
656 /* ssl */ 650 /* ssl */
657 use_ssl = true; 651 use_ssl = true;
658 server_port = SMTPS_PORT; 652 server_port = SMTPS_PORT;
659 break; 653 break;
660 case 'S': 654 case 'S':
661 /* starttls */ 655 /* starttls */
662 use_starttls = true; 656 use_starttls = true;
663 use_ehlo = true; 657 use_ehlo = true;
664 break; 658 break;
@@ -666,7 +660,7 @@ process_arguments (int argc, char **argv)
666#ifdef HAVE_SSL 660#ifdef HAVE_SSL
667 use_sni = true; 661 use_sni = true;
668#else 662#else
669 usage (_("SSL support not available - install OpenSSL and recompile")); 663 usage(_("SSL support not available - install OpenSSL and recompile"));
670#endif 664#endif
671 break; 665 break;
672 case 'r': 666 case 'r':
@@ -682,48 +676,51 @@ process_arguments (int argc, char **argv)
682#ifdef USE_IPV6 676#ifdef USE_IPV6
683 address_family = AF_INET6; 677 address_family = AF_INET6;
684#else 678#else
685 usage4 (_("IPv6 support not available")); 679 usage4(_("IPv6 support not available"));
686#endif 680#endif
687 break; 681 break;
688 case 'V': /* version */ 682 case 'V': /* version */
689 print_revision (progname, NP_VERSION); 683 print_revision(progname, NP_VERSION);
690 exit (STATE_UNKNOWN); 684 exit(STATE_UNKNOWN);
691 case 'h': /* help */ 685 case 'h': /* help */
692 print_help (); 686 print_help();
693 exit (STATE_UNKNOWN); 687 exit(STATE_UNKNOWN);
694 case '?': /* help */ 688 case '?': /* help */
695 usage5 (); 689 usage5();
696 } 690 }
697 } 691 }
698 692
699 c = optind; 693 c = optind;
700 if (server_address == NULL) { 694 if (server_address == NULL) {
701 if (argv[c]) { 695 if (argv[c]) {
702 if (is_host (argv[c])) 696 if (is_host(argv[c])) {
703 server_address = argv[c]; 697 server_address = argv[c];
704 else 698 } else {
705 usage2 (_("Invalid hostname/address"), argv[c]); 699 usage2(_("Invalid hostname/address"), argv[c]);
706 } 700 }
707 else { 701 } else {
708 xasprintf (&server_address, "127.0.0.1"); 702 xasprintf(&server_address, "127.0.0.1");
709 } 703 }
710 } 704 }
711 705
712 if (server_expect == NULL) 706 if (server_expect == NULL) {
713 server_expect = strdup (SMTP_EXPECT); 707 server_expect = strdup(SMTP_EXPECT);
708 }
714 709
715 if (mail_command == NULL) 710 if (mail_command == NULL) {
716 mail_command = strdup("MAIL "); 711 mail_command = strdup("MAIL ");
712 }
717 713
718 if (from_arg==NULL) 714 if (from_arg == NULL) {
719 from_arg = strdup(" "); 715 from_arg = strdup(" ");
716 }
720 717
721 if (use_starttls && use_ssl) { 718 if (use_starttls && use_ssl) {
722 if (implicit_tls) { 719 if (implicit_tls) {
723 use_ssl = false; 720 use_ssl = false;
724 server_port = SMTP_PORT; 721 server_port = SMTP_PORT;
725 } else { 722 } else {
726 usage4 (_("Set either -s/--ssl/--tls or -S/--starttls")); 723 usage4(_("Set either -s/--ssl/--tls or -S/--starttls"));
727 } 724 }
728 } 725 }
729 726
@@ -731,54 +728,44 @@ process_arguments (int argc, char **argv)
731 server_port = server_port_option; 728 server_port = server_port_option;
732 } 729 }
733 730
734 return validate_arguments (); 731 return validate_arguments();
735}
736
737
738
739int
740validate_arguments (void)
741{
742 return OK;
743} 732}
744 733
734int validate_arguments(void) { return OK; }
745 735
746void 736void smtp_quit(void) {
747smtp_quit(void)
748{
749 int bytes; 737 int bytes;
750 int n; 738 int n;
751 739
752 n = my_send(SMTP_QUIT, strlen(SMTP_QUIT)); 740 n = my_send(SMTP_QUIT, strlen(SMTP_QUIT));
753 if(n < 0) { 741 if (n < 0) {
754 if(ignore_send_quit_failure) { 742 if (ignore_send_quit_failure) {
755 if(verbose) { 743 if (verbose) {
756 printf(_("Connection closed by server before sending QUIT command\n")); 744 printf(_("Connection closed by server before sending QUIT command\n"));
757 } 745 }
758 return; 746 return;
759 } 747 }
760 die (STATE_UNKNOWN, 748 die(STATE_UNKNOWN, _("Connection closed by server before sending QUIT command\n"));
761 _("Connection closed by server before sending QUIT command\n"));
762 } 749 }
763 750
764 if (verbose) 751 if (verbose) {
765 printf(_("sent %s\n"), "QUIT"); 752 printf(_("sent %s\n"), "QUIT");
753 }
766 754
767 /* read the response but don't care about problems */ 755 /* read the response but don't care about problems */
768 bytes = recvlines(buffer, MAX_INPUT_BUFFER); 756 bytes = recvlines(buffer, MAX_INPUT_BUFFER);
769 if (verbose) { 757 if (verbose) {
770 if (bytes < 0) 758 if (bytes < 0) {
771 printf(_("recv() failed after QUIT.")); 759 printf(_("recv() failed after QUIT."));
772 else if (bytes == 0) 760 } else if (bytes == 0) {
773 printf(_("Connection reset by peer.")); 761 printf(_("Connection reset by peer."));
774 else { 762 } else {
775 buffer[bytes] = '\0'; 763 buffer[bytes] = '\0';
776 printf(_("received %s\n"), buffer); 764 printf(_("received %s\n"), buffer);
777 } 765 }
778 } 766 }
779} 767}
780 768
781
782/* 769/*
783 * Receive one line, copy it into buf and nul-terminate it. Returns the 770 * Receive one line, copy it into buf and nul-terminate it. Returns the
784 * number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on 771 * number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on
@@ -788,24 +775,22 @@ smtp_quit(void)
788 * function which buffers the data, move that to netutils.c and change 775 * function which buffers the data, move that to netutils.c and change
789 * check_smtp and other plugins to use that. Also, remove (\r)\n. 776 * check_smtp and other plugins to use that. Also, remove (\r)\n.
790 */ 777 */
791int 778int recvline(char *buf, size_t bufsize) {
792recvline(char *buf, size_t bufsize)
793{
794 int result; 779 int result;
795 unsigned i; 780 unsigned i;
796 781
797 for (i = result = 0; i < bufsize - 1; i++) { 782 for (i = result = 0; i < bufsize - 1; i++) {
798 if ((result = my_recv(&buf[i], 1)) != 1) 783 if ((result = my_recv(&buf[i], 1)) != 1) {
799 break; 784 break;
785 }
800 if (buf[i] == '\n') { 786 if (buf[i] == '\n') {
801 buf[++i] = '\0'; 787 buf[++i] = '\0';
802 return i; 788 return i;
803 } 789 }
804 } 790 }
805 return (result == 1 || i == 0) ? -2 : result; /* -2 if out of space */ 791 return (result == 1 || i == 0) ? -2 : result; /* -2 if out of space */
806} 792}
807 793
808
809/* 794/*
810 * Receive one or more lines, copy them into buf and nul-terminate it. Returns 795 * Receive one or more lines, copy them into buf and nul-terminate it. Returns
811 * the number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on 796 * the number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on
@@ -820,26 +805,20 @@ recvline(char *buf, size_t bufsize)
820 * 805 *
821 * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n. 806 * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n.
822 */ 807 */
823int 808int recvlines(char *buf, size_t bufsize) {
824recvlines(char *buf, size_t bufsize)
825{
826 int result, i; 809 int result, i;
827 810
828 for (i = 0; /* forever */; i += result) 811 for (i = 0; /* forever */; i += result) {
829 if (!((result = recvline(buf + i, bufsize - i)) > 3 && 812 if (!((result = recvline(buf + i, bufsize - i)) > 3 && isdigit((int)buf[i]) && isdigit((int)buf[i + 1]) &&
830 isdigit((int)buf[i]) && 813 isdigit((int)buf[i + 2]) && buf[i + 3] == '-')) {
831 isdigit((int)buf[i + 1]) &&
832 isdigit((int)buf[i + 2]) &&
833 buf[i + 3] == '-'))
834 break; 814 break;
815 }
816 }
835 817
836 return (result <= 0) ? result : result + i; 818 return (result <= 0) ? result : result + i;
837} 819}
838 820
839 821int my_close(void) {
840int
841my_close (void)
842{
843 int result; 822 int result;
844 result = close(sd); 823 result = close(sd);
845#ifdef HAVE_SSL 824#ifdef HAVE_SSL
@@ -848,89 +827,80 @@ my_close (void)
848 return result; 827 return result;
849} 828}
850 829
851 830void print_help(void) {
852void
853print_help (void)
854{
855 char *myport; 831 char *myport;
856 xasprintf (&myport, "%d", SMTP_PORT); 832 xasprintf(&myport, "%d", SMTP_PORT);
857 833
858 print_revision (progname, NP_VERSION); 834 print_revision(progname, NP_VERSION);
859 835
860 printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"); 836 printf("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n");
861 printf (COPYRIGHT, copyright, email); 837 printf(COPYRIGHT, copyright, email);
862 838
863 printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host.")); 839 printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host."));
864 840
865 printf ("\n\n"); 841 printf("\n\n");
866 842
867 print_usage (); 843 print_usage();
868 844
869 printf (UT_HELP_VRSN); 845 printf(UT_HELP_VRSN);
870 printf (UT_EXTRA_OPTS); 846 printf(UT_EXTRA_OPTS);
871 847
872 printf (UT_HOST_PORT, 'p', myport); 848 printf(UT_HOST_PORT, 'p', myport);
873 849
874 printf (UT_IPv46); 850 printf(UT_IPv46);
875 851
876 printf (" %s\n", "-e, --expect=STRING"); 852 printf(" %s\n", "-e, --expect=STRING");
877 printf (_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT); 853 printf(_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT);
878 printf (" %s\n", "-C, --command=STRING"); 854 printf(" %s\n", "-C, --command=STRING");
879 printf (" %s\n", _("SMTP command (may be used repeatedly)")); 855 printf(" %s\n", _("SMTP command (may be used repeatedly)"));
880 printf (" %s\n", "-R, --response=STRING"); 856 printf(" %s\n", "-R, --response=STRING");
881 printf (" %s\n", _("Expected response to command (may be used repeatedly)")); 857 printf(" %s\n", _("Expected response to command (may be used repeatedly)"));
882 printf (" %s\n", "-f, --from=STRING"); 858 printf(" %s\n", "-f, --from=STRING");
883 printf (" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")), 859 printf(" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")), printf(" %s\n", "-F, --fqdn=STRING");
884 printf (" %s\n", "-F, --fqdn=STRING"); 860 printf(" %s\n", _("FQDN used for HELO"));
885 printf (" %s\n", _("FQDN used for HELO")); 861 printf(" %s\n", "-r, --proxy");
886 printf (" %s\n", "-r, --proxy"); 862 printf(" %s\n", _("Use PROXY protocol prefix for the connection."));
887 printf (" %s\n", _("Use PROXY protocol prefix for the connection."));
888#ifdef HAVE_SSL 863#ifdef HAVE_SSL
889 printf (" %s\n", "-D, --certificate=INTEGER[,INTEGER]"); 864 printf(" %s\n", "-D, --certificate=INTEGER[,INTEGER]");
890 printf (" %s\n", _("Minimum number of days a certificate has to be valid.")); 865 printf(" %s\n", _("Minimum number of days a certificate has to be valid."));
891 printf (" %s\n", "-s, --ssl, --tls"); 866 printf(" %s\n", "-s, --ssl, --tls");
892 printf (" %s\n", _("Use SSL/TLS for the connection.")); 867 printf(" %s\n", _("Use SSL/TLS for the connection."));
893 printf (_(" Sets default port to %d.\n"), SMTPS_PORT); 868 printf(_(" Sets default port to %d.\n"), SMTPS_PORT);
894 printf (" %s\n", "-S, --starttls"); 869 printf(" %s\n", "-S, --starttls");
895 printf (" %s\n", _("Use STARTTLS for the connection.")); 870 printf(" %s\n", _("Use STARTTLS for the connection."));
896 printf (" %s\n", "--sni"); 871 printf(" %s\n", "--sni");
897 printf (" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); 872 printf(" %s\n", _("Enable SSL/TLS hostname extension support (SNI)"));
898#endif 873#endif
899 874
900 printf (" %s\n", "-A, --authtype=STRING"); 875 printf(" %s\n", "-A, --authtype=STRING");
901 printf (" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)")); 876 printf(" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)"));
902 printf (" %s\n", "-U, --authuser=STRING"); 877 printf(" %s\n", "-U, --authuser=STRING");
903 printf (" %s\n", _("SMTP AUTH username")); 878 printf(" %s\n", _("SMTP AUTH username"));
904 printf (" %s\n", "-P, --authpass=STRING"); 879 printf(" %s\n", "-P, --authpass=STRING");
905 printf (" %s\n", _("SMTP AUTH password")); 880 printf(" %s\n", _("SMTP AUTH password"));
906 printf (" %s\n", "-L, --lmtp"); 881 printf(" %s\n", "-L, --lmtp");
907 printf (" %s\n", _("Send LHLO instead of HELO/EHLO")); 882 printf(" %s\n", _("Send LHLO instead of HELO/EHLO"));
908 printf (" %s\n", "-q, --ignore-quit-failure"); 883 printf(" %s\n", "-q, --ignore-quit-failure");
909 printf (" %s\n", _("Ignore failure when sending QUIT command to server")); 884 printf(" %s\n", _("Ignore failure when sending QUIT command to server"));
910 885
911 printf (UT_WARN_CRIT); 886 printf(UT_WARN_CRIT);
912 887
913 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 888 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
914 889
915 printf (UT_VERBOSE); 890 printf(UT_VERBOSE);
916 891
917 printf("\n"); 892 printf("\n");
918 printf ("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); 893 printf("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return"));
919 printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful")); 894 printf("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful"));
920 printf ("%s\n", _("connects, but incorrect response messages from the host result in")); 895 printf("%s\n", _("connects, but incorrect response messages from the host result in"));
921 printf ("%s\n", _("STATE_WARNING return values.")); 896 printf("%s\n", _("STATE_WARNING return values."));
922 897
923 printf (UT_SUPPORT); 898 printf(UT_SUPPORT);
924} 899}
925 900
926 901void print_usage(void) {
927 902 printf("%s\n", _("Usage:"));
928void 903 printf("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n", progname);
929print_usage (void) 904 printf("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n");
930{ 905 printf("[-F fqdn] [-S] [-L] [-D warn days cert expire[,crit days cert expire]] [-r] [--sni] [-v] \n");
931 printf ("%s\n", _("Usage:"));
932 printf ("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n", progname);
933 printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n");
934 printf ("[-F fqdn] [-S] [-L] [-D warn days cert expire[,crit days cert expire]] [-r] [--sni] [-v] \n");
935} 906}
936