diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/Makefile.am | 5 | ||||
-rw-r--r-- | plugins/check_apt.c | 151 | ||||
-rw-r--r-- | plugins/check_apt.d/config.h | 41 | ||||
-rw-r--r-- | plugins/check_by_ssh.c | 265 | ||||
-rw-r--r-- | plugins/check_by_ssh.d/config.h | 56 | ||||
-rw-r--r-- | plugins/check_smtp.c | 1153 | ||||
-rw-r--r-- | plugins/check_smtp.d/config.h | 92 |
7 files changed, 962 insertions, 801 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index d40a0937..d9269691 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -53,7 +53,10 @@ EXTRA_DIST = t \ | |||
53 | check_game.d \ | 53 | check_game.d \ |
54 | check_dbi.d \ | 54 | check_dbi.d \ |
55 | check_ssh.d \ | 55 | check_ssh.d \ |
56 | check_dns.d | 56 | check_dns.d \ |
57 | check_apt.d \ | ||
58 | check_by_ssh.d \ | ||
59 | check_smtp.d | ||
57 | 60 | ||
58 | PLUGINHDRS = common.h | 61 | PLUGINHDRS = common.h |
59 | 62 | ||
diff --git a/plugins/check_apt.c b/plugins/check_apt.c index 1eda45dd..e840184b 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c | |||
@@ -29,6 +29,7 @@ | |||
29 | * | 29 | * |
30 | *****************************************************************************/ | 30 | *****************************************************************************/ |
31 | 31 | ||
32 | #include "states.h" | ||
32 | const char *progname = "check_apt"; | 33 | const char *progname = "check_apt"; |
33 | const char *copyright = "2006-2024"; | 34 | const char *copyright = "2006-2024"; |
34 | const char *email = "devel@monitoring-plugins.org"; | 35 | const char *email = "devel@monitoring-plugins.org"; |
@@ -37,13 +38,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
37 | #include "runcmd.h" | 38 | #include "runcmd.h" |
38 | #include "utils.h" | 39 | #include "utils.h" |
39 | #include "regex.h" | 40 | #include "regex.h" |
40 | 41 | #include "check_apt.d/config.h" | |
41 | /* some constants */ | ||
42 | typedef enum { | ||
43 | UPGRADE, | ||
44 | DIST_UPGRADE, | ||
45 | NO_UPGRADE | ||
46 | } upgrade_type; | ||
47 | 42 | ||
48 | /* Character for hidden input file option (for testing). */ | 43 | /* Character for hidden input file option (for testing). */ |
49 | #define INPUT_FILE_OPT CHAR_MAX + 1 | 44 | #define INPUT_FILE_OPT CHAR_MAX + 1 |
@@ -61,14 +56,18 @@ typedef enum { | |||
61 | #define SECURITY_RE "^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)" | 56 | #define SECURITY_RE "^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)" |
62 | 57 | ||
63 | /* some standard functions */ | 58 | /* some standard functions */ |
64 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 59 | typedef struct { |
60 | int errorcode; | ||
61 | check_apt_config config; | ||
62 | } check_apt_config_wrapper; | ||
63 | static check_apt_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
65 | static void print_help(void); | 64 | static void print_help(void); |
66 | void print_usage(void); | 65 | void print_usage(void); |
67 | 66 | ||
68 | /* construct the appropriate apt-get cmdline */ | 67 | /* construct the appropriate apt-get cmdline */ |
69 | static char *construct_cmdline(upgrade_type u, const char *opts); | 68 | static char *construct_cmdline(upgrade_type /*u*/, const char * /*opts*/); |
70 | /* run an apt-get update */ | 69 | /* run an apt-get update */ |
71 | static int run_update(void); | 70 | static int run_update(char * /*update_opts*/); |
72 | 71 | ||
73 | typedef struct { | 72 | typedef struct { |
74 | int errorcode; | 73 | int errorcode; |
@@ -79,42 +78,35 @@ typedef struct { | |||
79 | } run_upgrade_result; | 78 | } run_upgrade_result; |
80 | 79 | ||
81 | /* run an apt-get upgrade */ | 80 | /* run an apt-get upgrade */ |
82 | static run_upgrade_result run_upgrade(void); | 81 | run_upgrade_result run_upgrade(upgrade_type upgrade, const char *do_include, const char *do_exclude, const char *do_critical, |
82 | const char *upgrade_opts, const char *input_filename); | ||
83 | 83 | ||
84 | /* add another clause to a regexp */ | 84 | /* add another clause to a regexp */ |
85 | static char *add_to_regexp(char *expr, const char *next); | 85 | static char *add_to_regexp(char * /*expr*/, const char * /*next*/); |
86 | /* extract package name from Inst line */ | 86 | /* extract package name from Inst line */ |
87 | static char *pkg_name(char *line); | 87 | static char *pkg_name(char * /*line*/); |
88 | /* string comparison function for qsort */ | 88 | /* string comparison function for qsort */ |
89 | static int cmpstringp(const void *p1, const void *p2); | 89 | static int cmpstringp(const void * /*p1*/, const void * /*p2*/); |
90 | 90 | ||
91 | /* configuration variables */ | 91 | /* configuration variables */ |
92 | static int verbose = 0; /* -v */ | 92 | static int verbose = 0; /* -v */ |
93 | static bool list = false; /* list packages available for upgrade */ | ||
94 | static bool do_update = false; /* whether to call apt-get update */ | ||
95 | static bool only_critical = false; /* whether to warn about non-critical updates */ | ||
96 | static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */ | ||
97 | static char *upgrade_opts = NULL; /* options to override defaults for upgrade */ | ||
98 | static char *update_opts = NULL; /* options to override defaults for update */ | ||
99 | static char *do_include = NULL; /* regexp to only include certain packages */ | ||
100 | static char *do_exclude = NULL; /* regexp to only exclude certain packages */ | ||
101 | static char *do_critical = NULL; /* regexp specifying critical packages */ | ||
102 | static char *input_filename = NULL; /* input filename for testing */ | ||
103 | /* number of packages available for upgrade to return WARNING status */ | ||
104 | static int packages_warning = 1; | ||
105 | 93 | ||
106 | /* other global variables */ | 94 | /* other global variables */ |
107 | static int stderr_warning = 0; /* if a cmd issued output on stderr */ | 95 | static bool stderr_warning = false; /* if a cmd issued output on stderr */ |
108 | static int exec_warning = 0; /* if a cmd exited non-zero */ | 96 | static bool exec_warning = false; /* if a cmd exited non-zero */ |
109 | 97 | ||
110 | int main(int argc, char **argv) { | 98 | int main(int argc, char **argv) { |
111 | /* Parse extra opts if any */ | 99 | /* Parse extra opts if any */ |
112 | argv = np_extra_opts(&argc, argv, progname); | 100 | argv = np_extra_opts(&argc, argv, progname); |
113 | 101 | ||
114 | if (process_arguments(argc, argv) == ERROR) { | 102 | check_apt_config_wrapper tmp_config = process_arguments(argc, argv); |
103 | |||
104 | if (tmp_config.errorcode == ERROR) { | ||
115 | usage_va(_("Could not parse arguments")); | 105 | usage_va(_("Could not parse arguments")); |
116 | } | 106 | } |
117 | 107 | ||
108 | const check_apt_config config = tmp_config.config; | ||
109 | |||
118 | /* Set signal handling and alarm timeout */ | 110 | /* Set signal handling and alarm timeout */ |
119 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 111 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
120 | usage_va(_("Cannot catch SIGALRM")); | 112 | usage_va(_("Cannot catch SIGALRM")); |
@@ -123,14 +115,15 @@ int main(int argc, char **argv) { | |||
123 | /* handle timeouts gracefully... */ | 115 | /* handle timeouts gracefully... */ |
124 | alarm(timeout_interval); | 116 | alarm(timeout_interval); |
125 | 117 | ||
126 | int result = STATE_UNKNOWN; | 118 | mp_state_enum result = STATE_UNKNOWN; |
127 | /* if they want to run apt-get update first... */ | 119 | /* if they want to run apt-get update first... */ |
128 | if (do_update) { | 120 | if (config.do_update) { |
129 | result = run_update(); | 121 | result = run_update(config.update_opts); |
130 | } | 122 | } |
131 | 123 | ||
132 | /* apt-get upgrade */ | 124 | /* apt-get upgrade */ |
133 | run_upgrade_result upgrad_res = run_upgrade(); | 125 | run_upgrade_result upgrad_res = |
126 | run_upgrade(config.upgrade, config.do_include, config.do_exclude, config.do_critical, config.upgrade_opts, config.input_filename); | ||
134 | 127 | ||
135 | result = max_state(result, upgrad_res.errorcode); | 128 | result = max_state(result, upgrad_res.errorcode); |
136 | int packages_available = upgrad_res.package_count; | 129 | int packages_available = upgrad_res.package_count; |
@@ -140,18 +133,18 @@ int main(int argc, char **argv) { | |||
140 | 133 | ||
141 | if (sec_count > 0) { | 134 | if (sec_count > 0) { |
142 | result = max_state(result, STATE_CRITICAL); | 135 | result = max_state(result, STATE_CRITICAL); |
143 | } else if (packages_available >= packages_warning && only_critical == false) { | 136 | } else if (packages_available >= config.packages_warning && !config.only_critical) { |
144 | result = max_state(result, STATE_WARNING); | 137 | result = max_state(result, STATE_WARNING); |
145 | } else if (result > STATE_UNKNOWN) { | 138 | } else if (result > STATE_UNKNOWN) { |
146 | result = STATE_UNKNOWN; | 139 | result = STATE_UNKNOWN; |
147 | } | 140 | } |
148 | 141 | ||
149 | printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"), | 142 | printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"), |
150 | state_text(result), packages_available, (upgrade == DIST_UPGRADE) ? "dist-upgrade" : "upgrade", sec_count, | 143 | state_text(result), packages_available, (config.upgrade == DIST_UPGRADE) ? "dist-upgrade" : "upgrade", sec_count, |
151 | (stderr_warning) ? " warnings detected" : "", (stderr_warning && exec_warning) ? "," : "", | 144 | (stderr_warning) ? " warnings detected" : "", (stderr_warning && exec_warning) ? "," : "", |
152 | (exec_warning) ? " errors detected" : "", (stderr_warning || exec_warning) ? "." : "", packages_available, sec_count); | 145 | (exec_warning) ? " errors detected" : "", (stderr_warning || exec_warning) ? "." : "", packages_available, sec_count); |
153 | 146 | ||
154 | if (list) { | 147 | if (config.list) { |
155 | qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp); | 148 | qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp); |
156 | qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp); | 149 | qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp); |
157 | 150 | ||
@@ -159,7 +152,7 @@ int main(int argc, char **argv) { | |||
159 | printf("%s (security)\n", secpackages_list[i]); | 152 | printf("%s (security)\n", secpackages_list[i]); |
160 | } | 153 | } |
161 | 154 | ||
162 | if (only_critical == false) { | 155 | if (!config.only_critical) { |
163 | for (int i = 0; i < packages_available - sec_count; i++) { | 156 | for (int i = 0; i < packages_available - sec_count; i++) { |
164 | printf("%s\n", packages_list[i]); | 157 | printf("%s\n", packages_list[i]); |
165 | } | 158 | } |
@@ -170,7 +163,7 @@ int main(int argc, char **argv) { | |||
170 | } | 163 | } |
171 | 164 | ||
172 | /* process command-line arguments */ | 165 | /* process command-line arguments */ |
173 | int process_arguments(int argc, char **argv) { | 166 | check_apt_config_wrapper process_arguments(int argc, char **argv) { |
174 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 167 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
175 | {"help", no_argument, 0, 'h'}, | 168 | {"help", no_argument, 0, 'h'}, |
176 | {"verbose", no_argument, 0, 'v'}, | 169 | {"verbose", no_argument, 0, 'v'}, |
@@ -179,7 +172,7 @@ int process_arguments(int argc, char **argv) { | |||
179 | {"upgrade", optional_argument, 0, 'U'}, | 172 | {"upgrade", optional_argument, 0, 'U'}, |
180 | {"no-upgrade", no_argument, 0, 'n'}, | 173 | {"no-upgrade", no_argument, 0, 'n'}, |
181 | {"dist-upgrade", optional_argument, 0, 'd'}, | 174 | {"dist-upgrade", optional_argument, 0, 'd'}, |
182 | {"list", no_argument, false, 'l'}, | 175 | {"list", no_argument, 0, 'l'}, |
183 | {"include", required_argument, 0, 'i'}, | 176 | {"include", required_argument, 0, 'i'}, |
184 | {"exclude", required_argument, 0, 'e'}, | 177 | {"exclude", required_argument, 0, 'e'}, |
185 | {"critical", required_argument, 0, 'c'}, | 178 | {"critical", required_argument, 0, 'c'}, |
@@ -188,6 +181,11 @@ int process_arguments(int argc, char **argv) { | |||
188 | {"packages-warning", required_argument, 0, 'w'}, | 181 | {"packages-warning", required_argument, 0, 'w'}, |
189 | {0, 0, 0, 0}}; | 182 | {0, 0, 0, 0}}; |
190 | 183 | ||
184 | check_apt_config_wrapper result = { | ||
185 | .errorcode = OK, | ||
186 | .config = check_apt_config_init(), | ||
187 | }; | ||
188 | |||
191 | while (true) { | 189 | while (true) { |
192 | int option_char = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); | 190 | int option_char = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); |
193 | 191 | ||
@@ -209,55 +207,55 @@ int process_arguments(int argc, char **argv) { | |||
209 | timeout_interval = atoi(optarg); | 207 | timeout_interval = atoi(optarg); |
210 | break; | 208 | break; |
211 | case 'd': | 209 | case 'd': |
212 | upgrade = DIST_UPGRADE; | 210 | result.config.upgrade = DIST_UPGRADE; |
213 | if (optarg != NULL) { | 211 | if (optarg != NULL) { |
214 | upgrade_opts = strdup(optarg); | 212 | result.config.upgrade_opts = strdup(optarg); |
215 | if (upgrade_opts == NULL) { | 213 | if (result.config.upgrade_opts == NULL) { |
216 | die(STATE_UNKNOWN, "strdup failed"); | 214 | die(STATE_UNKNOWN, "strdup failed"); |
217 | } | 215 | } |
218 | } | 216 | } |
219 | break; | 217 | break; |
220 | case 'U': | 218 | case 'U': |
221 | upgrade = UPGRADE; | 219 | result.config.upgrade = UPGRADE; |
222 | if (optarg != NULL) { | 220 | if (optarg != NULL) { |
223 | upgrade_opts = strdup(optarg); | 221 | result.config.upgrade_opts = strdup(optarg); |
224 | if (upgrade_opts == NULL) { | 222 | if (result.config.upgrade_opts == NULL) { |
225 | die(STATE_UNKNOWN, "strdup failed"); | 223 | die(STATE_UNKNOWN, "strdup failed"); |
226 | } | 224 | } |
227 | } | 225 | } |
228 | break; | 226 | break; |
229 | case 'n': | 227 | case 'n': |
230 | upgrade = NO_UPGRADE; | 228 | result.config.upgrade = NO_UPGRADE; |
231 | break; | 229 | break; |
232 | case 'u': | 230 | case 'u': |
233 | do_update = true; | 231 | result.config.do_update = true; |
234 | if (optarg != NULL) { | 232 | if (optarg != NULL) { |
235 | update_opts = strdup(optarg); | 233 | result.config.update_opts = strdup(optarg); |
236 | if (update_opts == NULL) { | 234 | if (result.config.update_opts == NULL) { |
237 | die(STATE_UNKNOWN, "strdup failed"); | 235 | die(STATE_UNKNOWN, "strdup failed"); |
238 | } | 236 | } |
239 | } | 237 | } |
240 | break; | 238 | break; |
241 | case 'l': | 239 | case 'l': |
242 | list = true; | 240 | result.config.list = true; |
243 | break; | 241 | break; |
244 | case 'i': | 242 | case 'i': |
245 | do_include = add_to_regexp(do_include, optarg); | 243 | result.config.do_include = add_to_regexp(result.config.do_include, optarg); |
246 | break; | 244 | break; |
247 | case 'e': | 245 | case 'e': |
248 | do_exclude = add_to_regexp(do_exclude, optarg); | 246 | result.config.do_exclude = add_to_regexp(result.config.do_exclude, optarg); |
249 | break; | 247 | break; |
250 | case 'c': | 248 | case 'c': |
251 | do_critical = add_to_regexp(do_critical, optarg); | 249 | result.config.do_critical = add_to_regexp(result.config.do_critical, optarg); |
252 | break; | 250 | break; |
253 | case 'o': | 251 | case 'o': |
254 | only_critical = true; | 252 | result.config.only_critical = true; |
255 | break; | 253 | break; |
256 | case INPUT_FILE_OPT: | 254 | case INPUT_FILE_OPT: |
257 | input_filename = optarg; | 255 | result.config.input_filename = optarg; |
258 | break; | 256 | break; |
259 | case 'w': | 257 | case 'w': |
260 | packages_warning = atoi(optarg); | 258 | result.config.packages_warning = atoi(optarg); |
261 | break; | 259 | break; |
262 | default: | 260 | default: |
263 | /* print short usage statement if args not parsable */ | 261 | /* print short usage statement if args not parsable */ |
@@ -265,11 +263,12 @@ int process_arguments(int argc, char **argv) { | |||
265 | } | 263 | } |
266 | } | 264 | } |
267 | 265 | ||
268 | return OK; | 266 | return result; |
269 | } | 267 | } |
270 | 268 | ||
271 | /* run an apt-get upgrade */ | 269 | /* run an apt-get upgrade */ |
272 | run_upgrade_result run_upgrade(void) { | 270 | run_upgrade_result run_upgrade(const upgrade_type upgrade, const char *do_include, const char *do_exclude, const char *do_critical, |
271 | const char *upgrade_opts, const char *input_filename) { | ||
273 | regex_t ereg; | 272 | regex_t ereg; |
274 | /* initialize ereg as it is possible it is printed while uninitialized */ | 273 | /* initialize ereg as it is possible it is printed while uninitialized */ |
275 | memset(&ereg, '\0', sizeof(ereg.buffer)); | 274 | memset(&ereg, '\0', sizeof(ereg.buffer)); |
@@ -332,7 +331,7 @@ run_upgrade_result run_upgrade(void) { | |||
332 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); | 331 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); |
333 | } | 332 | } |
334 | 333 | ||
335 | char **pkglist = malloc(sizeof(char *) * chld_out.lines); | 334 | char **pkglist = malloc(sizeof(char *) * chld_out.lines); |
336 | if (!pkglist) { | 335 | if (!pkglist) { |
337 | die(STATE_UNKNOWN, "malloc failed!\n"); | 336 | die(STATE_UNKNOWN, "malloc failed!\n"); |
338 | } | 337 | } |
@@ -385,7 +384,7 @@ run_upgrade_result run_upgrade(void) { | |||
385 | 384 | ||
386 | /* If we get anything on stderr, at least set warning */ | 385 | /* If we get anything on stderr, at least set warning */ |
387 | if (input_filename == NULL && chld_err.buflen) { | 386 | if (input_filename == NULL && chld_err.buflen) { |
388 | stderr_warning = 1; | 387 | stderr_warning = true; |
389 | result.errorcode = max_state(result.errorcode, STATE_WARNING); | 388 | result.errorcode = max_state(result.errorcode, STATE_WARNING); |
390 | if (verbose) { | 389 | if (verbose) { |
391 | for (size_t i = 0; i < chld_err.lines; i++) { | 390 | for (size_t i = 0; i < chld_err.lines; i++) { |
@@ -405,7 +404,7 @@ run_upgrade_result run_upgrade(void) { | |||
405 | } | 404 | } |
406 | 405 | ||
407 | /* run an apt-get update (needs root) */ | 406 | /* run an apt-get update (needs root) */ |
408 | int run_update(void) { | 407 | int run_update(char *update_opts) { |
409 | int result = STATE_UNKNOWN; | 408 | int result = STATE_UNKNOWN; |
410 | char *cmdline; | 409 | char *cmdline; |
411 | /* run the update */ | 410 | /* run the update */ |
@@ -418,7 +417,7 @@ int run_update(void) { | |||
418 | * since we were explicitly asked to do so, this is treated as | 417 | * since we were explicitly asked to do so, this is treated as |
419 | * a critical error. */ | 418 | * a critical error. */ |
420 | if (result != 0) { | 419 | if (result != 0) { |
421 | exec_warning = 1; | 420 | exec_warning = true; |
422 | result = STATE_CRITICAL; | 421 | result = STATE_CRITICAL; |
423 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); | 422 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); |
424 | } | 423 | } |
@@ -446,7 +445,7 @@ int run_update(void) { | |||
446 | char *pkg_name(char *line) { | 445 | char *pkg_name(char *line) { |
447 | char *start = line + strlen(PKGINST_PREFIX); | 446 | char *start = line + strlen(PKGINST_PREFIX); |
448 | 447 | ||
449 | int len = strlen(start); | 448 | size_t len = strlen(start); |
450 | 449 | ||
451 | char *space = index(start, ' '); | 450 | char *space = index(start, ' '); |
452 | if (space != NULL) { | 451 | if (space != NULL) { |
@@ -464,35 +463,37 @@ char *pkg_name(char *line) { | |||
464 | return pkg; | 463 | return pkg; |
465 | } | 464 | } |
466 | 465 | ||
467 | int cmpstringp(const void *p1, const void *p2) { return strcmp(*(char *const *)p1, *(char *const *)p2); } | 466 | int cmpstringp(const void *left_string, const void *right_string) { |
467 | return strcmp(*(char *const *)left_string, *(char *const *)right_string); | ||
468 | } | ||
468 | 469 | ||
469 | char *add_to_regexp(char *expr, const char *next) { | 470 | char *add_to_regexp(char *expr, const char *next) { |
470 | char *re = NULL; | 471 | char *regex_string = NULL; |
471 | 472 | ||
472 | if (expr == NULL) { | 473 | if (expr == NULL) { |
473 | re = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1)); | 474 | regex_string = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1)); |
474 | if (!re) { | 475 | if (!regex_string) { |
475 | die(STATE_UNKNOWN, "malloc failed!\n"); | 476 | die(STATE_UNKNOWN, "malloc failed!\n"); |
476 | } | 477 | } |
477 | sprintf(re, "(%s)", next); | 478 | sprintf(regex_string, "(%s)", next); |
478 | } else { | 479 | } else { |
479 | /* resize it, adding an extra char for the new '|' separator */ | 480 | /* resize it, adding an extra char for the new '|' separator */ |
480 | re = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1)); | 481 | regex_string = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1)); |
481 | if (!re) { | 482 | if (!regex_string) { |
482 | die(STATE_UNKNOWN, "realloc failed!\n"); | 483 | die(STATE_UNKNOWN, "realloc failed!\n"); |
483 | } | 484 | } |
484 | /* append it starting at ')' in the old re */ | 485 | /* append it starting at ')' in the old re */ |
485 | sprintf((char *)(re + strlen(re) - 1), "|%s)", next); | 486 | sprintf((char *)(regex_string + strlen(regex_string) - 1), "|%s)", next); |
486 | } | 487 | } |
487 | 488 | ||
488 | return re; | 489 | return regex_string; |
489 | } | 490 | } |
490 | 491 | ||
491 | char *construct_cmdline(upgrade_type u, const char *opts) { | 492 | char *construct_cmdline(upgrade_type upgrade, const char *opts) { |
492 | const char *opts_ptr = NULL; | 493 | const char *opts_ptr = NULL; |
493 | const char *aptcmd = NULL; | 494 | const char *aptcmd = NULL; |
494 | 495 | ||
495 | switch (u) { | 496 | switch (upgrade) { |
496 | case UPGRADE: | 497 | case UPGRADE: |
497 | if (opts == NULL) { | 498 | if (opts == NULL) { |
498 | opts_ptr = UPGRADE_DEFAULT_OPTS; | 499 | opts_ptr = UPGRADE_DEFAULT_OPTS; |
diff --git a/plugins/check_apt.d/config.h b/plugins/check_apt.d/config.h new file mode 100644 index 00000000..981f4f42 --- /dev/null +++ b/plugins/check_apt.d/config.h | |||
@@ -0,0 +1,41 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include <stddef.h> | ||
5 | |||
6 | /* some constants */ | ||
7 | typedef enum { | ||
8 | UPGRADE, | ||
9 | DIST_UPGRADE, | ||
10 | NO_UPGRADE | ||
11 | } upgrade_type; | ||
12 | |||
13 | typedef struct { | ||
14 | bool do_update; /* whether to call apt-get update */ | ||
15 | upgrade_type upgrade; /* which type of upgrade to do */ | ||
16 | bool only_critical; /* whether to warn about non-critical updates */ | ||
17 | bool list; /* list packages available for upgrade */ | ||
18 | /* number of packages available for upgrade to return WARNING status */ | ||
19 | int packages_warning; | ||
20 | |||
21 | char *upgrade_opts; /* options to override defaults for upgrade */ | ||
22 | char *update_opts; /* options to override defaults for update */ | ||
23 | char *do_include; /* regexp to only include certain packages */ | ||
24 | char *do_exclude; /* regexp to only exclude certain packages */ | ||
25 | char *do_critical; /* regexp specifying critical packages */ | ||
26 | char *input_filename; /* input filename for testing */ | ||
27 | } check_apt_config; | ||
28 | |||
29 | check_apt_config check_apt_config_init() { | ||
30 | check_apt_config tmp = {.do_update = false, | ||
31 | .upgrade = UPGRADE, | ||
32 | .only_critical = false, | ||
33 | .list = false, | ||
34 | .packages_warning = 1, | ||
35 | .update_opts = NULL, | ||
36 | .do_include = NULL, | ||
37 | .do_exclude = NULL, | ||
38 | .do_critical = NULL, | ||
39 | .input_filename = NULL}; | ||
40 | return tmp; | ||
41 | } | ||
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 2ac7805d..2bc38d49 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c | |||
@@ -32,48 +32,28 @@ const char *email = "devel@monitoring-plugins.org"; | |||
32 | 32 | ||
33 | #include "common.h" | 33 | #include "common.h" |
34 | #include "utils.h" | 34 | #include "utils.h" |
35 | #include "netutils.h" | ||
36 | #include "utils_cmd.h" | 35 | #include "utils_cmd.h" |
36 | #include "check_by_ssh.d/config.h" | ||
37 | #include "states.h" | ||
37 | 38 | ||
38 | #ifndef NP_MAXARGS | 39 | #ifndef NP_MAXARGS |
39 | # define NP_MAXARGS 1024 | 40 | # define NP_MAXARGS 1024 |
40 | #endif | 41 | #endif |
41 | 42 | ||
42 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 43 | typedef struct { |
43 | static int validate_arguments(void); | 44 | int errorcode; |
44 | static void comm_append(const char * /*str*/); | 45 | check_by_ssh_config config; |
46 | } check_by_ssh_config_wrapper; | ||
47 | static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
48 | static check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/); | ||
49 | |||
50 | static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/); | ||
45 | static void print_help(void); | 51 | static void print_help(void); |
46 | void print_usage(void); | 52 | void print_usage(void); |
47 | 53 | ||
48 | static unsigned int commands = 0; | ||
49 | static unsigned int services = 0; | ||
50 | static int skip_stdout = 0; | ||
51 | static int skip_stderr = 0; | ||
52 | static int warn_on_stderr = 0; | ||
53 | static bool unknown_timeout = false; | ||
54 | static char *remotecmd = NULL; | ||
55 | static char **commargv = NULL; | ||
56 | static int commargc = 0; | ||
57 | static char *hostname = NULL; | ||
58 | static char *outputfile = NULL; | ||
59 | static char *host_shortname = NULL; | ||
60 | static char **service; | ||
61 | static bool passive = false; | ||
62 | static bool verbose = false; | 54 | static bool verbose = false; |
63 | 55 | ||
64 | int main(int argc, char **argv) { | 56 | int main(int argc, char **argv) { |
65 | |||
66 | char *status_text; | ||
67 | int cresult; | ||
68 | int result = STATE_UNKNOWN; | ||
69 | time_t local_time; | ||
70 | FILE *file_pointer = NULL; | ||
71 | output chld_out; | ||
72 | output chld_err; | ||
73 | |||
74 | remotecmd = ""; | ||
75 | comm_append(SSH_COMMAND); | ||
76 | |||
77 | setlocale(LC_ALL, ""); | 57 | setlocale(LC_ALL, ""); |
78 | bindtextdomain(PACKAGE, LOCALEDIR); | 58 | bindtextdomain(PACKAGE, LOCALEDIR); |
79 | textdomain(PACKAGE); | 59 | textdomain(PACKAGE); |
@@ -81,11 +61,15 @@ int main(int argc, char **argv) { | |||
81 | /* Parse extra opts if any */ | 61 | /* Parse extra opts if any */ |
82 | argv = np_extra_opts(&argc, argv, progname); | 62 | argv = np_extra_opts(&argc, argv, progname); |
83 | 63 | ||
64 | check_by_ssh_config_wrapper tmp_config = process_arguments(argc, argv); | ||
65 | |||
84 | /* process arguments */ | 66 | /* process arguments */ |
85 | if (process_arguments(argc, argv) == ERROR) { | 67 | if (tmp_config.errorcode == ERROR) { |
86 | usage_va(_("Could not parse arguments")); | 68 | usage_va(_("Could not parse arguments")); |
87 | } | 69 | } |
88 | 70 | ||
71 | const check_by_ssh_config config = tmp_config.config; | ||
72 | |||
89 | /* Set signal handling and alarm timeout */ | 73 | /* Set signal handling and alarm timeout */ |
90 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 74 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
91 | usage_va(_("Cannot catch SIGALRM")); | 75 | usage_va(_("Cannot catch SIGALRM")); |
@@ -94,16 +78,18 @@ int main(int argc, char **argv) { | |||
94 | 78 | ||
95 | /* run the command */ | 79 | /* run the command */ |
96 | if (verbose) { | 80 | if (verbose) { |
97 | printf("Command: %s\n", commargv[0]); | 81 | printf("Command: %s\n", config.cmd.commargv[0]); |
98 | for (int i = 1; i < commargc; i++) { | 82 | for (int i = 1; i < config.cmd.commargc; i++) { |
99 | printf("Argument %i: %s\n", i, commargv[i]); | 83 | printf("Argument %i: %s\n", i, config.cmd.commargv[i]); |
100 | } | 84 | } |
101 | } | 85 | } |
102 | 86 | ||
103 | result = cmd_run_array(commargv, &chld_out, &chld_err, 0); | 87 | output chld_out; |
88 | output chld_err; | ||
89 | mp_state_enum result = cmd_run_array(config.cmd.commargv, &chld_out, &chld_err, 0); | ||
104 | 90 | ||
105 | /* SSH returns 255 if connection attempt fails; include the first line of error output */ | 91 | /* SSH returns 255 if connection attempt fails; include the first line of error output */ |
106 | if (result == 255 && unknown_timeout) { | 92 | if (result == 255 && config.unknown_timeout) { |
107 | printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); | 93 | printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); |
108 | return STATE_UNKNOWN; | 94 | return STATE_UNKNOWN; |
109 | } | 95 | } |
@@ -117,17 +103,24 @@ int main(int argc, char **argv) { | |||
117 | } | 103 | } |
118 | } | 104 | } |
119 | 105 | ||
120 | if (skip_stdout == -1) { /* --skip-stdout specified without argument */ | 106 | size_t skip_stdout = 0; |
107 | if (config.skip_stdout == -1) { /* --skip-stdout specified without argument */ | ||
121 | skip_stdout = chld_out.lines; | 108 | skip_stdout = chld_out.lines; |
109 | } else { | ||
110 | skip_stdout = config.skip_stdout; | ||
122 | } | 111 | } |
123 | if (skip_stderr == -1) { /* --skip-stderr specified without argument */ | 112 | |
113 | size_t skip_stderr = 0; | ||
114 | if (config.skip_stderr == -1) { /* --skip-stderr specified without argument */ | ||
124 | skip_stderr = chld_err.lines; | 115 | skip_stderr = chld_err.lines; |
116 | } else { | ||
117 | skip_stderr = config.skip_stderr; | ||
125 | } | 118 | } |
126 | 119 | ||
127 | /* UNKNOWN or worse if (non-skipped) output found on stderr */ | 120 | /* UNKNOWN or worse if (non-skipped) output found on stderr */ |
128 | if (chld_err.lines > (size_t)skip_stderr) { | 121 | if (chld_err.lines > (size_t)skip_stderr) { |
129 | printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); | 122 | printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); |
130 | if (warn_on_stderr) { | 123 | if (config.warn_on_stderr) { |
131 | return max_state_alt(result, STATE_WARNING); | 124 | return max_state_alt(result, STATE_WARNING); |
132 | } | 125 | } |
133 | return max_state_alt(result, STATE_UNKNOWN); | 126 | return max_state_alt(result, STATE_UNKNOWN); |
@@ -135,13 +128,13 @@ int main(int argc, char **argv) { | |||
135 | 128 | ||
136 | /* this is simple if we're not supposed to be passive. | 129 | /* this is simple if we're not supposed to be passive. |
137 | * Wrap up quickly and keep the tricks below */ | 130 | * Wrap up quickly and keep the tricks below */ |
138 | if (!passive) { | 131 | if (!config.passive) { |
139 | if (chld_out.lines > (size_t)skip_stdout) { | 132 | if (chld_out.lines > (size_t)skip_stdout) { |
140 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { | 133 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { |
141 | puts(chld_out.line[i]); | 134 | puts(chld_out.line[i]); |
142 | } | 135 | } |
143 | } else { | 136 | } else { |
144 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result); | 137 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), config.remotecmd, result); |
145 | } | 138 | } |
146 | return result; /* return error status from remote command */ | 139 | return result; /* return error status from remote command */ |
147 | } | 140 | } |
@@ -151,36 +144,34 @@ int main(int argc, char **argv) { | |||
151 | */ | 144 | */ |
152 | 145 | ||
153 | /* process output */ | 146 | /* process output */ |
154 | if (!(file_pointer = fopen(outputfile, "a"))) { | 147 | FILE *file_pointer = NULL; |
155 | printf(_("SSH WARNING: could not open %s\n"), outputfile); | 148 | if (!(file_pointer = fopen(config.outputfile, "a"))) { |
149 | printf(_("SSH WARNING: could not open %s\n"), config.outputfile); | ||
156 | exit(STATE_UNKNOWN); | 150 | exit(STATE_UNKNOWN); |
157 | } | 151 | } |
158 | 152 | ||
159 | local_time = time(NULL); | 153 | time_t local_time = time(NULL); |
160 | commands = 0; | 154 | unsigned int commands = 0; |
155 | char *status_text; | ||
156 | int cresult; | ||
161 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { | 157 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { |
162 | status_text = chld_out.line[i++]; | 158 | status_text = chld_out.line[i++]; |
163 | if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) { | 159 | if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) { |
164 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); | 160 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); |
165 | } | 161 | } |
166 | 162 | ||
167 | if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { | 163 | if (config.service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { |
168 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++], | 164 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, config.host_shortname, |
169 | cresult, status_text); | 165 | config.service[commands++], cresult, status_text); |
170 | } | 166 | } |
171 | } | 167 | } |
172 | 168 | ||
173 | /* Multiple commands and passive checking should always return OK */ | 169 | /* Multiple commands and passive checking should always return OK */ |
174 | return result; | 170 | exit(result); |
175 | } | 171 | } |
176 | 172 | ||
177 | /* process command-line arguments */ | 173 | /* process command-line arguments */ |
178 | int process_arguments(int argc, char **argv) { | 174 | check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { |
179 | int c; | ||
180 | char *p1; | ||
181 | char *p2; | ||
182 | |||
183 | int option = 0; | ||
184 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 175 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
185 | {"help", no_argument, 0, 'h'}, | 176 | {"help", no_argument, 0, 'h'}, |
186 | {"verbose", no_argument, 0, 'v'}, | 177 | {"verbose", no_argument, 0, 'v'}, |
@@ -210,24 +201,33 @@ int process_arguments(int argc, char **argv) { | |||
210 | {"configfile", optional_argument, 0, 'F'}, | 201 | {"configfile", optional_argument, 0, 'F'}, |
211 | {0, 0, 0, 0}}; | 202 | {0, 0, 0, 0}}; |
212 | 203 | ||
204 | check_by_ssh_config_wrapper result = { | ||
205 | .errorcode = OK, | ||
206 | .config = check_by_ssh_config_init(), | ||
207 | }; | ||
208 | |||
213 | if (argc < 2) { | 209 | if (argc < 2) { |
214 | return ERROR; | 210 | result.errorcode = ERROR; |
211 | return result; | ||
215 | } | 212 | } |
216 | 213 | ||
217 | for (c = 1; c < argc; c++) { | 214 | for (int index = 1; index < argc; index++) { |
218 | if (strcmp("-to", argv[c]) == 0) { | 215 | if (strcmp("-to", argv[index]) == 0) { |
219 | strcpy(argv[c], "-t"); | 216 | strcpy(argv[index], "-t"); |
220 | } | 217 | } |
221 | } | 218 | } |
222 | 219 | ||
223 | while (1) { | 220 | result.config.cmd = comm_append(result.config.cmd, SSH_COMMAND); |
224 | c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | ||
225 | 221 | ||
226 | if (c == -1 || c == EOF) { | 222 | int option = 0; |
223 | while (true) { | ||
224 | int opt_index = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | ||
225 | |||
226 | if (opt_index == -1 || opt_index == EOF) { | ||
227 | break; | 227 | break; |
228 | } | 228 | } |
229 | 229 | ||
230 | switch (c) { | 230 | switch (opt_index) { |
231 | case 'V': /* version */ | 231 | case 'V': /* version */ |
232 | print_revision(progname, NP_VERSION); | 232 | print_revision(progname, NP_VERSION); |
233 | exit(STATE_UNKNOWN); | 233 | exit(STATE_UNKNOWN); |
@@ -245,169 +245,182 @@ int process_arguments(int argc, char **argv) { | |||
245 | } | 245 | } |
246 | break; | 246 | break; |
247 | case 'U': | 247 | case 'U': |
248 | unknown_timeout = true; | 248 | result.config.unknown_timeout = true; |
249 | break; | 249 | break; |
250 | case 'H': /* host */ | 250 | case 'H': /* host */ |
251 | hostname = optarg; | 251 | result.config.hostname = optarg; |
252 | break; | 252 | break; |
253 | case 'p': /* port number */ | 253 | case 'p': /* port number */ |
254 | if (!is_integer(optarg)) { | 254 | if (!is_integer(optarg)) { |
255 | usage_va(_("Port must be a positive integer")); | 255 | usage_va(_("Port must be a positive integer")); |
256 | } | 256 | } |
257 | comm_append("-p"); | 257 | result.config.cmd = comm_append(result.config.cmd, "-p"); |
258 | comm_append(optarg); | 258 | result.config.cmd = comm_append(result.config.cmd, optarg); |
259 | break; | 259 | break; |
260 | case 'O': /* output file */ | 260 | case 'O': /* output file */ |
261 | outputfile = optarg; | 261 | result.config.outputfile = optarg; |
262 | passive = true; | 262 | result.config.passive = true; |
263 | break; | 263 | break; |
264 | case 's': /* description of service to check */ | 264 | case 's': /* description of service to check */ { |
265 | char *p1; | ||
266 | char *p2; | ||
267 | |||
265 | p1 = optarg; | 268 | p1 = optarg; |
266 | service = realloc(service, (++services) * sizeof(char *)); | 269 | result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); |
267 | while ((p2 = index(p1, ':'))) { | 270 | while ((p2 = index(p1, ':'))) { |
268 | *p2 = '\0'; | 271 | *p2 = '\0'; |
269 | service[services - 1] = p1; | 272 | result.config.service[result.config.number_of_services - 1] = p1; |
270 | service = realloc(service, (++services) * sizeof(char *)); | 273 | result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); |
271 | p1 = p2 + 1; | 274 | p1 = p2 + 1; |
272 | } | 275 | } |
273 | service[services - 1] = p1; | 276 | result.config.service[result.config.number_of_services - 1] = p1; |
274 | break; | 277 | break; |
275 | case 'n': /* short name of host in the monitoring configuration */ | 278 | case 'n': /* short name of host in the monitoring configuration */ |
276 | host_shortname = optarg; | 279 | result.config.host_shortname = optarg; |
277 | break; | 280 | } break; |
278 | |||
279 | case 'u': | 281 | case 'u': |
280 | comm_append("-l"); | 282 | result.config.cmd = comm_append(result.config.cmd, "-l"); |
281 | comm_append(optarg); | 283 | result.config.cmd = comm_append(result.config.cmd, optarg); |
282 | break; | 284 | break; |
283 | case 'l': /* login name */ | 285 | case 'l': /* login name */ |
284 | comm_append("-l"); | 286 | result.config.cmd = comm_append(result.config.cmd, "-l"); |
285 | comm_append(optarg); | 287 | result.config.cmd = comm_append(result.config.cmd, optarg); |
286 | break; | 288 | break; |
287 | case 'i': /* identity */ | 289 | case 'i': /* identity */ |
288 | comm_append("-i"); | 290 | result.config.cmd = comm_append(result.config.cmd, "-i"); |
289 | comm_append(optarg); | 291 | result.config.cmd = comm_append(result.config.cmd, optarg); |
290 | break; | 292 | break; |
291 | 293 | ||
292 | case '1': /* Pass these switches directly to ssh */ | 294 | case '1': /* Pass these switches directly to ssh */ |
293 | comm_append("-1"); | 295 | result.config.cmd = comm_append(result.config.cmd, "-1"); |
294 | break; | 296 | break; |
295 | case '2': /* 1 to force version 1, 2 to force version 2 */ | 297 | case '2': /* 1 to force version 1, 2 to force version 2 */ |
296 | comm_append("-2"); | 298 | result.config.cmd = comm_append(result.config.cmd, "-2"); |
297 | break; | 299 | break; |
298 | case '4': /* -4 for IPv4 */ | 300 | case '4': /* -4 for IPv4 */ |
299 | comm_append("-4"); | 301 | result.config.cmd = comm_append(result.config.cmd, "-4"); |
300 | break; | 302 | break; |
301 | case '6': /* -6 for IPv6 */ | 303 | case '6': /* -6 for IPv6 */ |
302 | comm_append("-6"); | 304 | result.config.cmd = comm_append(result.config.cmd, "-6"); |
303 | break; | 305 | break; |
304 | case 'f': /* fork to background */ | 306 | case 'f': /* fork to background */ |
305 | comm_append("-f"); | 307 | result.config.cmd = comm_append(result.config.cmd, "-f"); |
306 | break; | 308 | break; |
307 | case 'C': /* Command for remote machine */ | 309 | case 'C': /* Command for remote machine */ |
308 | commands++; | 310 | result.config.commands++; |
309 | if (commands > 1) { | 311 | if (result.config.commands > 1) { |
310 | xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | 312 | xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd); |
311 | } | 313 | } |
312 | xasprintf(&remotecmd, "%s%s", remotecmd, optarg); | 314 | xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg); |
313 | break; | 315 | break; |
314 | case 'S': /* skip n (or all) lines on stdout */ | 316 | case 'S': /* skip n (or all) lines on stdout */ |
315 | if (optarg == NULL) { | 317 | if (optarg == NULL) { |
316 | skip_stdout = -1; /* skip all output on stdout */ | 318 | result.config.skip_stdout = -1; /* skip all output on stdout */ |
317 | } else if (!is_integer(optarg)) { | 319 | } else if (!is_integer(optarg)) { |
318 | usage_va(_("skip-stdout argument must be an integer")); | 320 | usage_va(_("skip-stdout argument must be an integer")); |
319 | } else { | 321 | } else { |
320 | skip_stdout = atoi(optarg); | 322 | result.config.skip_stdout = atoi(optarg); |
321 | } | 323 | } |
322 | break; | 324 | break; |
323 | case 'E': /* skip n (or all) lines on stderr */ | 325 | case 'E': /* skip n (or all) lines on stderr */ |
324 | if (optarg == NULL) { | 326 | if (optarg == NULL) { |
325 | skip_stderr = -1; /* skip all output on stderr */ | 327 | result.config.skip_stderr = -1; /* skip all output on stderr */ |
326 | } else if (!is_integer(optarg)) { | 328 | } else if (!is_integer(optarg)) { |
327 | usage_va(_("skip-stderr argument must be an integer")); | 329 | usage_va(_("skip-stderr argument must be an integer")); |
328 | } else { | 330 | } else { |
329 | skip_stderr = atoi(optarg); | 331 | result.config.skip_stderr = atoi(optarg); |
330 | } | 332 | } |
331 | break; | 333 | break; |
332 | case 'W': /* exit with warning if there is an output on stderr */ | 334 | case 'W': /* exit with warning if there is an output on stderr */ |
333 | warn_on_stderr = 1; | 335 | result.config.warn_on_stderr = true; |
334 | break; | 336 | break; |
335 | case 'o': /* Extra options for the ssh command */ | 337 | case 'o': /* Extra options for the ssh command */ |
336 | comm_append("-o"); | 338 | result.config.cmd = comm_append(result.config.cmd, "-o"); |
337 | comm_append(optarg); | 339 | result.config.cmd = comm_append(result.config.cmd, optarg); |
338 | break; | 340 | break; |
339 | case 'q': /* Tell the ssh command to be quiet */ | 341 | case 'q': /* Tell the ssh command to be quiet */ |
340 | comm_append("-q"); | 342 | result.config.cmd = comm_append(result.config.cmd, "-q"); |
341 | break; | 343 | break; |
342 | case 'F': /* ssh configfile */ | 344 | case 'F': /* ssh configfile */ |
343 | comm_append("-F"); | 345 | result.config.cmd = comm_append(result.config.cmd, "-F"); |
344 | comm_append(optarg); | 346 | result.config.cmd = comm_append(result.config.cmd, optarg); |
345 | break; | 347 | break; |
346 | default: /* help */ | 348 | default: /* help */ |
347 | usage5(); | 349 | usage5(); |
348 | } | 350 | } |
349 | } | 351 | } |
350 | 352 | ||
351 | c = optind; | 353 | int c = optind; |
352 | if (hostname == NULL) { | 354 | if (result.config.hostname == NULL) { |
353 | if (c <= argc) { | 355 | if (c <= argc) { |
354 | die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); | 356 | die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); |
355 | } | 357 | } |
356 | hostname = argv[c++]; | 358 | result.config.hostname = argv[c++]; |
357 | } | 359 | } |
358 | 360 | ||
359 | if (strlen(remotecmd) == 0) { | 361 | if (strlen(result.config.remotecmd) == 0) { |
360 | for (; c < argc; c++) { | 362 | for (; c < argc; c++) { |
361 | if (strlen(remotecmd) > 0) { | 363 | if (strlen(result.config.remotecmd) > 0) { |
362 | xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]); | 364 | xasprintf(&result.config.remotecmd, "%s %s", result.config.remotecmd, argv[c]); |
363 | } else { | 365 | } else { |
364 | xasprintf(&remotecmd, "%s", argv[c]); | 366 | xasprintf(&result.config.remotecmd, "%s", argv[c]); |
365 | } | 367 | } |
366 | } | 368 | } |
367 | } | 369 | } |
368 | 370 | ||
369 | if (commands > 1 || passive) { | 371 | if (result.config.commands > 1 || result.config.passive) { |
370 | xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | 372 | xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd); |
371 | } | 373 | } |
372 | 374 | ||
373 | if (remotecmd == NULL || strlen(remotecmd) <= 1) { | 375 | if (result.config.remotecmd == NULL || strlen(result.config.remotecmd) <= 1) { |
374 | usage_va(_("No remotecmd")); | 376 | usage_va(_("No remotecmd")); |
375 | } | 377 | } |
376 | 378 | ||
377 | comm_append(hostname); | 379 | result.config.cmd = comm_append(result.config.cmd, result.config.hostname); |
378 | comm_append(remotecmd); | 380 | result.config.cmd = comm_append(result.config.cmd, result.config.remotecmd); |
379 | 381 | ||
380 | return validate_arguments(); | 382 | return validate_arguments(result); |
381 | } | 383 | } |
382 | 384 | ||
383 | void comm_append(const char *str) { | 385 | command_construct comm_append(command_construct cmd, const char *str) { |
386 | |||
387 | if (verbose) { | ||
388 | for (int i = 0; i < cmd.commargc; i++) { | ||
389 | printf("Current command: [%i] %s\n", i, cmd.commargv[i]); | ||
390 | } | ||
384 | 391 | ||
385 | if (++commargc > NP_MAXARGS) { | 392 | printf("Appending: %s\n", str); |
393 | } | ||
394 | |||
395 | if (++cmd.commargc > NP_MAXARGS) { | ||
386 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); | 396 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); |
387 | } | 397 | } |
388 | 398 | ||
389 | if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL) { | 399 | if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) == NULL) { |
390 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); | 400 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); |
391 | } | 401 | } |
392 | 402 | ||
393 | commargv[commargc - 1] = strdup(str); | 403 | cmd.commargv[cmd.commargc - 1] = strdup(str); |
394 | commargv[commargc] = NULL; | 404 | cmd.commargv[cmd.commargc] = NULL; |
405 | |||
406 | return cmd; | ||
395 | } | 407 | } |
396 | 408 | ||
397 | int validate_arguments(void) { | 409 | check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper config_wrapper) { |
398 | if (remotecmd == NULL || hostname == NULL) { | 410 | if (config_wrapper.config.remotecmd == NULL || config_wrapper.config.hostname == NULL) { |
399 | return ERROR; | 411 | config_wrapper.errorcode = ERROR; |
412 | return config_wrapper; | ||
400 | } | 413 | } |
401 | 414 | ||
402 | if (passive && commands != services) { | 415 | if (config_wrapper.config.passive && config_wrapper.config.commands != config_wrapper.config.number_of_services) { |
403 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); | 416 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); |
404 | } | 417 | } |
405 | 418 | ||
406 | if (passive && host_shortname == NULL) { | 419 | if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) { |
407 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); | 420 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); |
408 | } | 421 | } |
409 | 422 | ||
410 | return OK; | 423 | return config_wrapper; |
411 | } | 424 | } |
412 | 425 | ||
413 | void print_help(void) { | 426 | void print_help(void) { |
diff --git a/plugins/check_by_ssh.d/config.h b/plugins/check_by_ssh.d/config.h new file mode 100644 index 00000000..05435def --- /dev/null +++ b/plugins/check_by_ssh.d/config.h | |||
@@ -0,0 +1,56 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include <stddef.h> | ||
5 | |||
6 | typedef struct { | ||
7 | int commargc; | ||
8 | char **commargv; | ||
9 | } command_construct; | ||
10 | |||
11 | typedef struct { | ||
12 | char *hostname; | ||
13 | char *host_shortname; | ||
14 | |||
15 | char **service; | ||
16 | unsigned int number_of_services; | ||
17 | |||
18 | unsigned int commands; // Not needed during actual test run | ||
19 | char *remotecmd; | ||
20 | |||
21 | command_construct cmd; | ||
22 | |||
23 | bool unknown_timeout; | ||
24 | bool warn_on_stderr; | ||
25 | int skip_stdout; | ||
26 | int skip_stderr; | ||
27 | bool passive; | ||
28 | char *outputfile; | ||
29 | } check_by_ssh_config; | ||
30 | |||
31 | check_by_ssh_config check_by_ssh_config_init() { | ||
32 | check_by_ssh_config tmp = { | ||
33 | .hostname = NULL, | ||
34 | .host_shortname = NULL, | ||
35 | |||
36 | .service = NULL, | ||
37 | .number_of_services = 0, | ||
38 | |||
39 | .commands = 0, | ||
40 | .remotecmd = "", | ||
41 | |||
42 | .cmd = | ||
43 | { | ||
44 | .commargc = 0, | ||
45 | .commargv = NULL, | ||
46 | }, | ||
47 | |||
48 | .unknown_timeout = false, | ||
49 | .warn_on_stderr = false, | ||
50 | .skip_stderr = 0, | ||
51 | .skip_stdout = 0, | ||
52 | .passive = false, | ||
53 | .outputfile = NULL, | ||
54 | }; | ||
55 | return tmp; | ||
56 | } | ||
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c index e6369e63..44b735f9 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 | ||
31 | const char *progname = "check_smtp"; | 31 | const char *progname = "check_smtp"; |
32 | const char *copyright = "2000-2024"; | 32 | const char *copyright = "2000-2024"; |
@@ -36,400 +36,394 @@ const char *email = "devel@monitoring-plugins.org"; | |||
36 | #include "netutils.h" | 36 | #include "netutils.h" |
37 | #include "utils.h" | 37 | #include "utils.h" |
38 | #include "base64.h" | 38 | #include "base64.h" |
39 | #include "regex.h" | ||
39 | 40 | ||
40 | #include <ctype.h> | 41 | #include <ctype.h> |
42 | #include "check_smtp.d/config.h" | ||
43 | #include "../lib/states.h" | ||
44 | |||
45 | #define PROXY_PREFIX "PROXY TCP4 0.0.0.0 0.0.0.0 25 25\r\n" | ||
46 | #define SMTP_HELO "HELO " | ||
47 | #define SMTP_EHLO "EHLO " | ||
48 | #define SMTP_LHLO "LHLO " | ||
49 | #define SMTP_QUIT "QUIT\r\n" | ||
50 | #define SMTP_STARTTLS "STARTTLS\r\n" | ||
51 | #define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n" | ||
41 | 52 | ||
53 | #define EHLO_SUPPORTS_STARTTLS 1 | ||
54 | |||
55 | typedef struct { | ||
56 | int errorcode; | ||
57 | check_smtp_config config; | ||
58 | } check_smtp_config_wrapper; | ||
59 | static check_smtp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
60 | |||
61 | int my_recv(check_smtp_config config, void *buf, int num, int socket_descriptor, bool ssl_established) { | ||
42 | #ifdef HAVE_SSL | 62 | #ifdef HAVE_SSL |
43 | static bool check_cert = false; | 63 | if ((config.use_starttls || config.use_ssl) && ssl_established) { |
44 | static int days_till_exp_warn, days_till_exp_crit; | 64 | return np_net_ssl_read(buf, num); |
45 | # define my_recv(buf, len) (((use_starttls || use_ssl) && ssl_established) ? np_net_ssl_read(buf, len) : read(sd, buf, len)) | 65 | } |
46 | # define my_send(buf, len) (((use_starttls || use_ssl) && ssl_established) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0)) | 66 | return (int)read(socket_descriptor, buf, (size_t)num); |
47 | #else /* ifndef HAVE_SSL */ | 67 | #else /* ifndef HAVE_SSL */ |
48 | # define my_recv(buf, len) read(sd, buf, len) | 68 | return read(socket_descriptor, buf, len) |
49 | # define my_send(buf, len) send(sd, buf, len, 0) | ||
50 | #endif | 69 | #endif |
70 | } | ||
51 | 71 | ||
52 | enum { | 72 | int my_send(check_smtp_config config, void *buf, int num, int socket_descriptor, bool ssl_established) { |
53 | SMTP_PORT = 25, | 73 | #ifdef HAVE_SSL |
54 | SMTPS_PORT = 465 | 74 | if ((config.use_starttls || config.use_ssl) && ssl_established) { |
55 | }; | ||
56 | #define PROXY_PREFIX "PROXY TCP4 0.0.0.0 0.0.0.0 25 25\r\n" | ||
57 | #define SMTP_EXPECT "220" | ||
58 | #define SMTP_HELO "HELO " | ||
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" | ||
64 | 75 | ||
65 | #define EHLO_SUPPORTS_STARTTLS 1 | 76 | return np_net_ssl_write(buf, num); |
77 | } | ||
78 | return (int)send(socket_descriptor, buf, (size_t)num, 0); | ||
79 | #else /* ifndef HAVE_SSL */ | ||
80 | return send(socket_descriptor, buf, len, 0); | ||
81 | #endif | ||
82 | } | ||
66 | 83 | ||
67 | static int process_arguments (int, char **); | 84 | static void print_help(void); |
68 | static int validate_arguments (void); | 85 | void print_usage(void); |
69 | static void print_help (void); | 86 | static char *smtp_quit(check_smtp_config /*config*/, char /*buffer*/[MAX_INPUT_BUFFER], int /*socket_descriptor*/, |
70 | void print_usage (void); | 87 | bool /*ssl_established*/); |
71 | static void smtp_quit(void); | 88 | static int recvline(char * /*buf*/, size_t /*bufsize*/, check_smtp_config /*config*/, int /*socket_descriptor*/, bool /*ssl_established*/); |
72 | static int recvline(char *, size_t); | 89 | static int recvlines(check_smtp_config /*config*/, char * /*buf*/, size_t /*bufsize*/, int /*socket_descriptor*/, bool /*ssl_established*/); |
73 | static int recvlines(char *, size_t); | 90 | static int my_close(int /*socket_descriptor*/); |
74 | static int my_close(void); | ||
75 | 91 | ||
76 | #include "regex.h" | ||
77 | static regex_t preg; | ||
78 | static regmatch_t pmatch[10]; | ||
79 | static char errbuf[MAX_INPUT_BUFFER]; | ||
80 | static int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | ||
81 | static int eflags = 0; | ||
82 | static int errcode, excode; | ||
83 | |||
84 | static int server_port = SMTP_PORT; | ||
85 | static int server_port_option = 0; | ||
86 | static char *server_address = NULL; | ||
87 | static char *server_expect = NULL; | ||
88 | static char *mail_command = NULL; | ||
89 | static char *from_arg = NULL; | ||
90 | static int send_mail_from=0; | ||
91 | static int ncommands=0; | ||
92 | static int command_size=0; | ||
93 | static int nresponses=0; | ||
94 | static int response_size=0; | ||
95 | static char **commands = NULL; | ||
96 | static char **responses = NULL; | ||
97 | static char *authtype = NULL; | ||
98 | static char *authuser = NULL; | ||
99 | static char *authpass = NULL; | ||
100 | static double warning_time = 0; | ||
101 | static bool check_warning_time = false; | ||
102 | static double critical_time = 0; | ||
103 | static bool check_critical_time = false; | ||
104 | static int verbose = 0; | 92 | static int verbose = 0; |
105 | static bool use_ssl = false; | ||
106 | static bool use_starttls = false; | ||
107 | static bool use_sni = false; | ||
108 | static bool use_proxy_prefix = false; | ||
109 | static bool use_ehlo = false; | ||
110 | static bool use_lhlo = false; | ||
111 | static bool ssl_established = false; | ||
112 | static char *localhostname = NULL; | ||
113 | static int sd; | ||
114 | static char buffer[MAX_INPUT_BUFFER]; | ||
115 | enum { | ||
116 | TCP_PROTOCOL = 1, | ||
117 | UDP_PROTOCOL = 2, | ||
118 | }; | ||
119 | static bool ignore_send_quit_failure = false; | ||
120 | |||
121 | |||
122 | int | ||
123 | main (int argc, char **argv) | ||
124 | { | ||
125 | bool supports_tls = false; | ||
126 | int n = 0; | ||
127 | double elapsed_time; | ||
128 | long microsec; | ||
129 | int result = STATE_UNKNOWN; | ||
130 | char *cmd_str = NULL; | ||
131 | char *helocmd = NULL; | ||
132 | char *error_msg = ""; | ||
133 | char *server_response = NULL; | ||
134 | struct timeval tv; | ||
135 | 93 | ||
136 | /* Catch pipe errors in read/write - sometimes occurs when writing QUIT */ | 94 | int main(int argc, char **argv) { |
137 | (void) signal (SIGPIPE, SIG_IGN); | 95 | setlocale(LC_ALL, ""); |
138 | 96 | bindtextdomain(PACKAGE, LOCALEDIR); | |
139 | setlocale (LC_ALL, ""); | 97 | textdomain(PACKAGE); |
140 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
141 | textdomain (PACKAGE); | ||
142 | 98 | ||
143 | /* Parse extra opts if any */ | 99 | /* Parse extra opts if any */ |
144 | argv=np_extra_opts (&argc, argv, progname); | 100 | argv = np_extra_opts(&argc, argv, progname); |
145 | 101 | ||
146 | if (process_arguments (argc, argv) == ERROR) | 102 | check_smtp_config_wrapper tmp_config = process_arguments(argc, argv); |
147 | usage4 (_("Could not parse arguments")); | 103 | |
104 | if (tmp_config.errorcode == ERROR) { | ||
105 | usage4(_("Could not parse arguments")); | ||
106 | } | ||
107 | |||
108 | const check_smtp_config config = tmp_config.config; | ||
148 | 109 | ||
149 | /* If localhostname not set on command line, use gethostname to set */ | 110 | /* If localhostname not set on command line, use gethostname to set */ |
150 | if(! localhostname){ | 111 | char *localhostname = config.localhostname; |
151 | localhostname = malloc (HOST_MAX_BYTES); | 112 | if (!localhostname) { |
152 | if(!localhostname){ | 113 | localhostname = malloc(HOST_MAX_BYTES); |
114 | if (!localhostname) { | ||
153 | printf(_("malloc() failed!\n")); | 115 | printf(_("malloc() failed!\n")); |
154 | return STATE_CRITICAL; | 116 | exit(STATE_CRITICAL); |
155 | } | 117 | } |
156 | if(gethostname(localhostname, HOST_MAX_BYTES)){ | 118 | if (gethostname(localhostname, HOST_MAX_BYTES)) { |
157 | printf(_("gethostname() failed!\n")); | 119 | printf(_("gethostname() failed!\n")); |
158 | return STATE_CRITICAL; | 120 | exit(STATE_CRITICAL); |
159 | } | 121 | } |
160 | } | 122 | } |
161 | if(use_lhlo) | 123 | |
162 | xasprintf (&helocmd, "%s%s%s", SMTP_LHLO, localhostname, "\r\n"); | 124 | char *helocmd = NULL; |
163 | else if(use_ehlo) | 125 | if (config.use_lhlo) { |
164 | xasprintf (&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n"); | 126 | xasprintf(&helocmd, "%s%s%s", SMTP_LHLO, localhostname, "\r\n"); |
165 | else | 127 | } else if (config.use_ehlo) { |
166 | xasprintf (&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n"); | 128 | xasprintf(&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n"); |
167 | 129 | } else { | |
168 | if (verbose) | 130 | xasprintf(&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n"); |
131 | } | ||
132 | |||
133 | if (verbose) { | ||
169 | printf("HELOCMD: %s", helocmd); | 134 | printf("HELOCMD: %s", helocmd); |
135 | } | ||
170 | 136 | ||
137 | char *mail_command = strdup("MAIL "); | ||
138 | char *cmd_str = NULL; | ||
171 | /* initialize the MAIL command with optional FROM command */ | 139 | /* initialize the MAIL command with optional FROM command */ |
172 | xasprintf (&cmd_str, "%sFROM:<%s>%s", mail_command, from_arg, "\r\n"); | 140 | xasprintf(&cmd_str, "%sFROM:<%s>%s", mail_command, config.from_arg, "\r\n"); |
173 | 141 | ||
174 | if (verbose && send_mail_from) | 142 | if (verbose && config.send_mail_from) { |
175 | printf ("FROM CMD: %s", cmd_str); | 143 | printf("FROM CMD: %s", cmd_str); |
144 | } | ||
145 | |||
146 | /* Catch pipe errors in read/write - sometimes occurs when writing QUIT */ | ||
147 | (void)signal(SIGPIPE, SIG_IGN); | ||
176 | 148 | ||
177 | /* initialize alarm signal handling */ | 149 | /* initialize alarm signal handling */ |
178 | (void) signal (SIGALRM, socket_timeout_alarm_handler); | 150 | (void)signal(SIGALRM, socket_timeout_alarm_handler); |
179 | 151 | ||
180 | /* set socket timeout */ | 152 | /* set socket timeout */ |
181 | (void) alarm (socket_timeout); | 153 | (void)alarm(socket_timeout); |
182 | 154 | ||
155 | struct timeval start_time; | ||
183 | /* start timer */ | 156 | /* start timer */ |
184 | gettimeofday (&tv, NULL); | 157 | gettimeofday(&start_time, NULL); |
185 | 158 | ||
159 | int socket_descriptor = 0; | ||
186 | /* try to connect to the host at the given port number */ | 160 | /* try to connect to the host at the given port number */ |
187 | result = my_tcp_connect (server_address, server_port, &sd); | 161 | mp_state_enum result = my_tcp_connect(config.server_address, config.server_port, &socket_descriptor); |
188 | 162 | ||
163 | char *error_msg = ""; | ||
164 | char buffer[MAX_INPUT_BUFFER]; | ||
165 | bool ssl_established = false; | ||
189 | if (result == STATE_OK) { /* we connected */ | 166 | if (result == STATE_OK) { /* we connected */ |
190 | /* If requested, send PROXY header */ | 167 | /* If requested, send PROXY header */ |
191 | if (use_proxy_prefix) { | 168 | if (config.use_proxy_prefix) { |
192 | if (verbose) | 169 | if (verbose) { |
193 | printf ("Sending header %s\n", PROXY_PREFIX); | 170 | printf("Sending header %s\n", PROXY_PREFIX); |
194 | my_send(PROXY_PREFIX, strlen(PROXY_PREFIX)); | 171 | } |
172 | my_send(config, PROXY_PREFIX, strlen(PROXY_PREFIX), socket_descriptor, ssl_established); | ||
195 | } | 173 | } |
196 | 174 | ||
197 | #ifdef HAVE_SSL | 175 | #ifdef HAVE_SSL |
198 | if (use_ssl) { | 176 | if (config.use_ssl) { |
199 | result = np_net_ssl_init_with_hostname(sd, (use_sni ? server_address : NULL)); | 177 | result = np_net_ssl_init_with_hostname(socket_descriptor, (config.use_sni ? config.server_address : NULL)); |
200 | if (result != STATE_OK) { | 178 | if (result != STATE_OK) { |
201 | printf (_("CRITICAL - Cannot create SSL context.\n")); | 179 | printf(_("CRITICAL - Cannot create SSL context.\n")); |
202 | close(sd); | 180 | close(socket_descriptor); |
203 | np_net_ssl_cleanup(); | 181 | np_net_ssl_cleanup(); |
204 | return STATE_CRITICAL; | 182 | exit(STATE_CRITICAL); |
205 | } else { | ||
206 | ssl_established = 1; | ||
207 | } | 183 | } |
184 | ssl_established = true; | ||
208 | } | 185 | } |
209 | #endif | 186 | #endif |
210 | 187 | ||
211 | /* watch for the SMTP connection string and */ | 188 | /* watch for the SMTP connection string and */ |
212 | /* return a WARNING status if we couldn't read any data */ | 189 | /* return a WARNING status if we couldn't read any data */ |
213 | if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) { | 190 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { |
214 | printf (_("recv() failed\n")); | 191 | printf(_("recv() failed\n")); |
215 | return STATE_WARNING; | 192 | exit(STATE_WARNING); |
216 | } | 193 | } |
217 | 194 | ||
195 | char *server_response = NULL; | ||
218 | /* save connect return (220 hostname ..) for later use */ | 196 | /* save connect return (220 hostname ..) for later use */ |
219 | xasprintf(&server_response, "%s", buffer); | 197 | xasprintf(&server_response, "%s", buffer); |
220 | 198 | ||
221 | /* send the HELO/EHLO command */ | 199 | /* send the HELO/EHLO command */ |
222 | my_send(helocmd, strlen(helocmd)); | 200 | my_send(config, helocmd, (int)strlen(helocmd), socket_descriptor, ssl_established); |
223 | 201 | ||
224 | /* allow for response to helo command to reach us */ | 202 | /* allow for response to helo command to reach us */ |
225 | if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) { | 203 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { |
226 | printf (_("recv() failed\n")); | 204 | printf(_("recv() failed\n")); |
227 | return STATE_WARNING; | 205 | exit(STATE_WARNING); |
228 | } else if(use_ehlo || use_lhlo){ | 206 | } |
229 | if(strstr(buffer, "250 STARTTLS") != NULL || | 207 | |
230 | strstr(buffer, "250-STARTTLS") != NULL){ | 208 | bool supports_tls = false; |
231 | supports_tls=true; | 209 | if (config.use_ehlo || config.use_lhlo) { |
210 | if (strstr(buffer, "250 STARTTLS") != NULL || strstr(buffer, "250-STARTTLS") != NULL) { | ||
211 | supports_tls = true; | ||
232 | } | 212 | } |
233 | } | 213 | } |
234 | 214 | ||
235 | if(use_starttls && ! supports_tls){ | 215 | if (config.use_starttls && !supports_tls) { |
236 | printf(_("WARNING - TLS not supported by server\n")); | 216 | printf(_("WARNING - TLS not supported by server\n")); |
237 | smtp_quit(); | 217 | smtp_quit(config, buffer, socket_descriptor, ssl_established); |
238 | return STATE_WARNING; | 218 | exit(STATE_WARNING); |
239 | } | 219 | } |
240 | 220 | ||
241 | #ifdef HAVE_SSL | 221 | #ifdef HAVE_SSL |
242 | if(use_starttls) { | 222 | if (config.use_starttls) { |
243 | /* send the STARTTLS command */ | 223 | /* send the STARTTLS command */ |
244 | send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0); | 224 | send(socket_descriptor, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0); |
245 | 225 | ||
246 | recvlines(buffer, MAX_INPUT_BUFFER); /* wait for it */ | 226 | recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established); /* wait for it */ |
247 | if (!strstr (buffer, SMTP_EXPECT)) { | 227 | if (!strstr(buffer, SMTP_EXPECT)) { |
248 | printf (_("Server does not support STARTTLS\n")); | 228 | printf(_("Server does not support STARTTLS\n")); |
249 | smtp_quit(); | 229 | smtp_quit(config, buffer, socket_descriptor, ssl_established); |
250 | return STATE_UNKNOWN; | 230 | exit(STATE_UNKNOWN); |
251 | } | 231 | } |
252 | result = np_net_ssl_init_with_hostname(sd, (use_sni ? server_address : NULL)); | 232 | |
253 | if(result != STATE_OK) { | 233 | result = np_net_ssl_init_with_hostname(socket_descriptor, (config.use_sni ? config.server_address : NULL)); |
254 | printf (_("CRITICAL - Cannot create SSL context.\n")); | 234 | if (result != STATE_OK) { |
255 | close(sd); | 235 | printf(_("CRITICAL - Cannot create SSL context.\n")); |
256 | np_net_ssl_cleanup(); | 236 | close(socket_descriptor); |
257 | return STATE_CRITICAL; | 237 | np_net_ssl_cleanup(); |
258 | } else { | 238 | exit(STATE_CRITICAL); |
259 | ssl_established = 1; | 239 | } |
260 | } | 240 | |
261 | 241 | ssl_established = true; | |
262 | /* | 242 | |
263 | * Resend the EHLO command. | 243 | /* |
264 | * | 244 | * Resend the EHLO command. |
265 | * RFC 3207 (4.2) says: ``The client MUST discard any knowledge | 245 | * |
266 | * obtained from the server, such as the list of SMTP service | 246 | * RFC 3207 (4.2) says: ``The client MUST discard any knowledge |
267 | * extensions, which was not obtained from the TLS negotiation | 247 | * obtained from the server, such as the list of SMTP service |
268 | * itself. The client SHOULD send an EHLO command as the first | 248 | * extensions, which was not obtained from the TLS negotiation |
269 | * command after a successful TLS negotiation.'' For this | 249 | * itself. The client SHOULD send an EHLO command as the first |
270 | * reason, some MTAs will not allow an AUTH LOGIN command before | 250 | * command after a successful TLS negotiation.'' For this |
271 | * we resent EHLO via TLS. | 251 | * reason, some MTAs will not allow an AUTH LOGIN command before |
272 | */ | 252 | * we resent EHLO via TLS. |
273 | if (my_send(helocmd, strlen(helocmd)) <= 0) { | 253 | */ |
274 | printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS.")); | 254 | if (my_send(config, helocmd, strlen(helocmd), socket_descriptor, ssl_established) <= 0) { |
275 | my_close(); | 255 | printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS.")); |
276 | return STATE_UNKNOWN; | 256 | my_close(socket_descriptor); |
277 | } | 257 | exit(STATE_UNKNOWN); |
278 | if (verbose) | 258 | } |
279 | printf(_("sent %s"), helocmd); | 259 | |
280 | if ((n = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { | 260 | if (verbose) { |
281 | printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS.")); | 261 | printf(_("sent %s"), helocmd); |
282 | my_close(); | 262 | } |
283 | return STATE_UNKNOWN; | ||
284 | } | ||
285 | if (verbose) { | ||
286 | printf("%s", buffer); | ||
287 | } | ||
288 | 263 | ||
289 | # ifdef USE_OPENSSL | 264 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { |
290 | if ( check_cert ) { | 265 | printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS.")); |
291 | result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); | 266 | my_close(socket_descriptor); |
292 | smtp_quit(); | 267 | exit(STATE_UNKNOWN); |
293 | my_close(); | 268 | } |
294 | return result; | 269 | |
295 | } | 270 | if (verbose) { |
296 | # endif /* USE_OPENSSL */ | 271 | printf("%s", buffer); |
272 | } | ||
273 | |||
274 | # ifdef USE_OPENSSL | ||
275 | if (config.check_cert) { | ||
276 | result = np_net_ssl_check_cert(config.days_till_exp_warn, config.days_till_exp_crit); | ||
277 | smtp_quit(config, buffer, socket_descriptor, ssl_established); | ||
278 | my_close(socket_descriptor); | ||
279 | exit(result); | ||
280 | } | ||
281 | # endif /* USE_OPENSSL */ | ||
297 | } | 282 | } |
298 | #endif | 283 | #endif |
299 | 284 | ||
300 | if (verbose) | 285 | if (verbose) { |
301 | printf ("%s", buffer); | 286 | printf("%s", buffer); |
287 | } | ||
302 | 288 | ||
303 | /* save buffer for later use */ | 289 | /* save buffer for later use */ |
304 | xasprintf(&server_response, "%s%s", server_response, buffer); | 290 | xasprintf(&server_response, "%s%s", server_response, buffer); |
305 | /* strip the buffer of carriage returns */ | 291 | /* strip the buffer of carriage returns */ |
306 | strip (server_response); | 292 | strip(server_response); |
307 | 293 | ||
308 | /* make sure we find the droids we are looking for */ | 294 | /* make sure we find the droids we are looking for */ |
309 | if (!strstr (server_response, server_expect)) { | 295 | if (!strstr(server_response, config.server_expect)) { |
310 | if (server_port == SMTP_PORT) | 296 | if (config.server_port == SMTP_PORT) { |
311 | printf (_("Invalid SMTP response received from host: %s\n"), server_response); | 297 | printf(_("Invalid SMTP response received from host: %s\n"), server_response); |
312 | else | 298 | } else { |
313 | printf (_("Invalid SMTP response received from host on port %d: %s\n"), | 299 | printf(_("Invalid SMTP response received from host on port %d: %s\n"), config.server_port, server_response); |
314 | server_port, server_response); | 300 | } |
315 | return STATE_WARNING; | 301 | exit(STATE_WARNING); |
316 | } | 302 | } |
317 | 303 | ||
318 | if (send_mail_from) { | 304 | if (config.send_mail_from) { |
319 | my_send(cmd_str, strlen(cmd_str)); | 305 | my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); |
320 | if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose) | 306 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 && verbose) { |
321 | printf("%s", buffer); | 307 | printf("%s", buffer); |
308 | } | ||
322 | } | 309 | } |
323 | 310 | ||
324 | n = 0; | 311 | int counter = 0; |
325 | while (n < ncommands) { | 312 | while (counter < config.ncommands) { |
326 | xasprintf (&cmd_str, "%s%s", commands[n], "\r\n"); | 313 | xasprintf(&cmd_str, "%s%s", config.commands[counter], "\r\n"); |
327 | my_send(cmd_str, strlen(cmd_str)); | 314 | my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); |
328 | if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose) | 315 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 && verbose) { |
329 | printf("%s", buffer); | 316 | printf("%s", buffer); |
330 | strip (buffer); | 317 | } |
331 | if (n < nresponses) { | 318 | strip(buffer); |
332 | cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | 319 | if (counter < config.nresponses) { |
333 | errcode = regcomp (&preg, responses[n], cflags); | 320 | int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; |
321 | regex_t preg; | ||
322 | int errcode = regcomp(&preg, config.responses[counter], cflags); | ||
323 | char errbuf[MAX_INPUT_BUFFER]; | ||
334 | if (errcode != 0) { | 324 | if (errcode != 0) { |
335 | regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); | 325 | regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); |
336 | printf (_("Could Not Compile Regular Expression")); | 326 | printf(_("Could Not Compile Regular Expression")); |
337 | return ERROR; | 327 | exit(STATE_UNKNOWN); |
338 | } | 328 | } |
339 | excode = regexec (&preg, buffer, 10, pmatch, eflags); | 329 | |
330 | regmatch_t pmatch[10]; | ||
331 | int eflags = 0; | ||
332 | int excode = regexec(&preg, buffer, 10, pmatch, eflags); | ||
340 | if (excode == 0) { | 333 | if (excode == 0) { |
341 | result = STATE_OK; | 334 | result = STATE_OK; |
342 | } | 335 | } else if (excode == REG_NOMATCH) { |
343 | else if (excode == REG_NOMATCH) { | ||
344 | result = STATE_WARNING; | 336 | result = STATE_WARNING; |
345 | printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]); | 337 | printf(_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text(result), buffer, config.commands[counter]); |
346 | } | 338 | } else { |
347 | else { | 339 | regerror(excode, &preg, errbuf, MAX_INPUT_BUFFER); |
348 | regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER); | 340 | printf(_("Execute Error: %s\n"), errbuf); |
349 | printf (_("Execute Error: %s\n"), errbuf); | ||
350 | result = STATE_UNKNOWN; | 341 | result = STATE_UNKNOWN; |
351 | } | 342 | } |
352 | } | 343 | } |
353 | n++; | 344 | counter++; |
354 | } | 345 | } |
355 | 346 | ||
356 | if (authtype != NULL) { | 347 | if (config.authtype != NULL) { |
357 | if (strcmp (authtype, "LOGIN") == 0) { | 348 | if (strcmp(config.authtype, "LOGIN") == 0) { |
358 | char *abuf; | 349 | char *abuf; |
359 | int ret; | 350 | int ret; |
360 | do { | 351 | do { |
361 | if (authuser == NULL) { | 352 | if (config.authuser == NULL) { |
362 | result = STATE_CRITICAL; | 353 | result = STATE_CRITICAL; |
363 | xasprintf(&error_msg, _("no authuser specified, ")); | 354 | xasprintf(&error_msg, _("no authuser specified, ")); |
364 | break; | 355 | break; |
365 | } | 356 | } |
366 | if (authpass == NULL) { | 357 | if (config.authpass == NULL) { |
367 | result = STATE_CRITICAL; | 358 | result = STATE_CRITICAL; |
368 | xasprintf(&error_msg, _("no authpass specified, ")); | 359 | xasprintf(&error_msg, _("no authpass specified, ")); |
369 | break; | 360 | break; |
370 | } | 361 | } |
371 | 362 | ||
372 | /* send AUTH LOGIN */ | 363 | /* send AUTH LOGIN */ |
373 | my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN)); | 364 | my_send(config, SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN), socket_descriptor, ssl_established); |
374 | if (verbose) | 365 | if (verbose) { |
375 | printf (_("sent %s\n"), "AUTH LOGIN"); | 366 | printf(_("sent %s\n"), "AUTH LOGIN"); |
367 | } | ||
376 | 368 | ||
377 | if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { | 369 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { |
378 | xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, ")); | 370 | xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, ")); |
379 | result = STATE_WARNING; | 371 | result = STATE_WARNING; |
380 | break; | 372 | break; |
381 | } | 373 | } |
382 | if (verbose) | 374 | if (verbose) { |
383 | printf (_("received %s\n"), buffer); | 375 | printf(_("received %s\n"), buffer); |
376 | } | ||
384 | 377 | ||
385 | if (strncmp (buffer, "334", 3) != 0) { | 378 | if (strncmp(buffer, "334", 3) != 0) { |
386 | result = STATE_CRITICAL; | 379 | result = STATE_CRITICAL; |
387 | xasprintf(&error_msg, _("invalid response received after AUTH LOGIN, ")); | 380 | xasprintf(&error_msg, _("invalid response received after AUTH LOGIN, ")); |
388 | break; | 381 | break; |
389 | } | 382 | } |
390 | 383 | ||
391 | /* encode authuser with base64 */ | 384 | /* encode authuser with base64 */ |
392 | base64_encode_alloc (authuser, strlen(authuser), &abuf); | 385 | base64_encode_alloc(config.authuser, strlen(config.authuser), &abuf); |
393 | xasprintf(&abuf, "%s\r\n", abuf); | 386 | xasprintf(&abuf, "%s\r\n", abuf); |
394 | my_send(abuf, strlen(abuf)); | 387 | my_send(config, abuf, (int)strlen(abuf), socket_descriptor, ssl_established); |
395 | if (verbose) | 388 | if (verbose) { |
396 | printf (_("sent %s\n"), abuf); | 389 | printf(_("sent %s\n"), abuf); |
390 | } | ||
397 | 391 | ||
398 | if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { | 392 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { |
399 | result = STATE_CRITICAL; | 393 | result = STATE_CRITICAL; |
400 | xasprintf(&error_msg, _("recv() failed after sending authuser, ")); | 394 | xasprintf(&error_msg, _("recv() failed after sending authuser, ")); |
401 | break; | 395 | break; |
402 | } | 396 | } |
403 | if (verbose) { | 397 | if (verbose) { |
404 | printf (_("received %s\n"), buffer); | 398 | printf(_("received %s\n"), buffer); |
405 | } | 399 | } |
406 | if (strncmp (buffer, "334", 3) != 0) { | 400 | if (strncmp(buffer, "334", 3) != 0) { |
407 | result = STATE_CRITICAL; | 401 | result = STATE_CRITICAL; |
408 | xasprintf(&error_msg, _("invalid response received after authuser, ")); | 402 | xasprintf(&error_msg, _("invalid response received after authuser, ")); |
409 | break; | 403 | break; |
410 | } | 404 | } |
411 | /* encode authpass with base64 */ | 405 | /* encode authpass with base64 */ |
412 | base64_encode_alloc (authpass, strlen(authpass), &abuf); | 406 | base64_encode_alloc(config.authpass, strlen(config.authpass), &abuf); |
413 | xasprintf(&abuf, "%s\r\n", abuf); | 407 | xasprintf(&abuf, "%s\r\n", abuf); |
414 | my_send(abuf, strlen(abuf)); | 408 | my_send(config, abuf, (int)strlen(abuf), socket_descriptor, ssl_established); |
415 | if (verbose) { | 409 | if (verbose) { |
416 | printf (_("sent %s\n"), abuf); | 410 | printf(_("sent %s\n"), abuf); |
417 | } | 411 | } |
418 | if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { | 412 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { |
419 | result = STATE_CRITICAL; | 413 | result = STATE_CRITICAL; |
420 | xasprintf(&error_msg, _("recv() failed after sending authpass, ")); | 414 | xasprintf(&error_msg, _("recv() failed after sending authpass, ")); |
421 | break; | 415 | break; |
422 | } | 416 | } |
423 | if (verbose) { | 417 | if (verbose) { |
424 | printf (_("received %s\n"), buffer); | 418 | printf(_("received %s\n"), buffer); |
425 | } | 419 | } |
426 | if (strncmp (buffer, "235", 3) != 0) { | 420 | if (strncmp(buffer, "235", 3) != 0) { |
427 | result = STATE_CRITICAL; | 421 | result = STATE_CRITICAL; |
428 | xasprintf(&error_msg, _("invalid response received after authpass, ")); | 422 | xasprintf(&error_msg, _("invalid response received after authpass, ")); |
429 | break; | 423 | break; |
430 | } | 424 | } |
431 | break; | 425 | break; |
432 | } while (0); | 426 | } while (false); |
433 | } else { | 427 | } else { |
434 | result = STATE_CRITICAL; | 428 | result = STATE_CRITICAL; |
435 | xasprintf(&error_msg, _("only authtype LOGIN is supported, ")); | 429 | xasprintf(&error_msg, _("only authtype LOGIN is supported, ")); |
@@ -437,243 +431,243 @@ main (int argc, char **argv) | |||
437 | } | 431 | } |
438 | 432 | ||
439 | /* tell the server we're done */ | 433 | /* tell the server we're done */ |
440 | smtp_quit(); | 434 | smtp_quit(config, buffer, socket_descriptor, ssl_established); |
441 | 435 | ||
442 | /* finally close the connection */ | 436 | /* finally close the connection */ |
443 | close (sd); | 437 | close(socket_descriptor); |
444 | } | 438 | } |
445 | 439 | ||
446 | /* reset the alarm */ | 440 | /* reset the alarm */ |
447 | alarm (0); | 441 | alarm(0); |
448 | 442 | ||
449 | microsec = deltime (tv); | 443 | long microsec = deltime(start_time); |
450 | elapsed_time = (double)microsec / 1.0e6; | 444 | double elapsed_time = (double)microsec / 1.0e6; |
451 | 445 | ||
452 | if (result == STATE_OK) { | 446 | if (result == STATE_OK) { |
453 | if (check_critical_time && elapsed_time > critical_time) | 447 | if (config.check_critical_time && elapsed_time > config.critical_time) { |
454 | result = STATE_CRITICAL; | 448 | result = STATE_CRITICAL; |
455 | else if (check_warning_time && elapsed_time > warning_time) | 449 | } else if (config.check_warning_time && elapsed_time > config.warning_time) { |
456 | result = STATE_WARNING; | 450 | result = STATE_WARNING; |
451 | } | ||
457 | } | 452 | } |
458 | 453 | ||
459 | printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), | 454 | printf(_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), state_text(result), error_msg, elapsed_time, verbose ? ", " : "", |
460 | state_text (result), | 455 | verbose ? buffer : "", |
461 | error_msg, | 456 | fperfdata("time", elapsed_time, "s", config.check_warning_time, config.warning_time, config.check_critical_time, |
462 | elapsed_time, | 457 | config.critical_time, true, 0, 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 | 458 | ||
469 | return result; | 459 | exit(result); |
470 | } | 460 | } |
471 | 461 | ||
472 | |||
473 | |||
474 | /* process command-line arguments */ | 462 | /* process command-line arguments */ |
475 | int | 463 | check_smtp_config_wrapper process_arguments(int argc, char **argv) { |
476 | process_arguments (int argc, char **argv) | ||
477 | { | ||
478 | int c; | ||
479 | char* temp; | ||
480 | |||
481 | bool implicit_tls = false; | ||
482 | |||
483 | enum { | 464 | enum { |
484 | SNI_OPTION | 465 | SNI_OPTION = CHAR_MAX + 1 |
485 | }; | 466 | }; |
486 | 467 | ||
487 | int option = 0; | 468 | int option = 0; |
488 | static struct option longopts[] = { | 469 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
489 | {"hostname", required_argument, 0, 'H'}, | 470 | {"expect", required_argument, 0, 'e'}, |
490 | {"expect", required_argument, 0, 'e'}, | 471 | {"critical", required_argument, 0, 'c'}, |
491 | {"critical", required_argument, 0, 'c'}, | 472 | {"warning", required_argument, 0, 'w'}, |
492 | {"warning", required_argument, 0, 'w'}, | 473 | {"timeout", required_argument, 0, 't'}, |
493 | {"timeout", required_argument, 0, 't'}, | 474 | {"port", required_argument, 0, 'p'}, |
494 | {"port", required_argument, 0, 'p'}, | 475 | {"from", required_argument, 0, 'f'}, |
495 | {"from", required_argument, 0, 'f'}, | 476 | {"fqdn", required_argument, 0, 'F'}, |
496 | {"fqdn", required_argument, 0, 'F'}, | 477 | {"authtype", required_argument, 0, 'A'}, |
497 | {"authtype", required_argument, 0, 'A'}, | 478 | {"authuser", required_argument, 0, 'U'}, |
498 | {"authuser", required_argument, 0, 'U'}, | 479 | {"authpass", required_argument, 0, 'P'}, |
499 | {"authpass", required_argument, 0, 'P'}, | 480 | {"command", required_argument, 0, 'C'}, |
500 | {"command", required_argument, 0, 'C'}, | 481 | {"response", required_argument, 0, 'R'}, |
501 | {"response", required_argument, 0, 'R'}, | 482 | {"verbose", no_argument, 0, 'v'}, |
502 | {"verbose", no_argument, 0, 'v'}, | 483 | {"version", no_argument, 0, 'V'}, |
503 | {"version", no_argument, 0, 'V'}, | 484 | {"use-ipv4", no_argument, 0, '4'}, |
504 | {"use-ipv4", no_argument, 0, '4'}, | 485 | {"use-ipv6", no_argument, 0, '6'}, |
505 | {"use-ipv6", no_argument, 0, '6'}, | 486 | {"help", no_argument, 0, 'h'}, |
506 | {"help", no_argument, 0, 'h'}, | 487 | {"lmtp", no_argument, 0, 'L'}, |
507 | {"lmtp", no_argument, 0, 'L'}, | 488 | {"ssl", no_argument, 0, 's'}, |
508 | {"ssl", no_argument, 0, 's'}, | 489 | {"tls", no_argument, 0, 's'}, |
509 | {"tls", no_argument, 0, 's'}, | 490 | {"starttls", no_argument, 0, 'S'}, |
510 | {"starttls",no_argument,0,'S'}, | 491 | {"sni", no_argument, 0, SNI_OPTION}, |
511 | {"sni", no_argument, 0, SNI_OPTION}, | 492 | {"certificate", required_argument, 0, 'D'}, |
512 | {"certificate",required_argument,0,'D'}, | 493 | {"ignore-quit-failure", no_argument, 0, 'q'}, |
513 | {"ignore-quit-failure",no_argument,0,'q'}, | 494 | {"proxy", no_argument, 0, 'r'}, |
514 | {"proxy",no_argument,0,'r'}, | 495 | {0, 0, 0, 0}}; |
515 | {0, 0, 0, 0} | 496 | |
497 | check_smtp_config_wrapper result = { | ||
498 | .config = check_smtp_config_init(), | ||
499 | .errorcode = OK, | ||
516 | }; | 500 | }; |
517 | 501 | ||
518 | if (argc < 2) | 502 | if (argc < 2) { |
519 | return ERROR; | 503 | result.errorcode = ERROR; |
504 | return result; | ||
505 | } | ||
520 | 506 | ||
521 | for (c = 1; c < argc; c++) { | 507 | for (int index = 1; index < argc; index++) { |
522 | if (strcmp ("-to", argv[c]) == 0) | 508 | if (strcmp("-to", argv[index]) == 0) { |
523 | strcpy (argv[c], "-t"); | 509 | strcpy(argv[index], "-t"); |
524 | else if (strcmp ("-wt", argv[c]) == 0) | 510 | } else if (strcmp("-wt", argv[index]) == 0) { |
525 | strcpy (argv[c], "-w"); | 511 | strcpy(argv[index], "-w"); |
526 | else if (strcmp ("-ct", argv[c]) == 0) | 512 | } else if (strcmp("-ct", argv[index]) == 0) { |
527 | strcpy (argv[c], "-c"); | 513 | strcpy(argv[index], "-c"); |
514 | } | ||
528 | } | 515 | } |
529 | 516 | ||
530 | while (1) { | 517 | int command_size = 0; |
531 | c = getopt_long (argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", | 518 | int response_size = 0; |
532 | longopts, &option); | 519 | bool implicit_tls = false; |
520 | int server_port_option = 0; | ||
521 | while (true) { | ||
522 | int opt_index = getopt_long(argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", longopts, &option); | ||
533 | 523 | ||
534 | if (c == -1 || c == EOF) | 524 | if (opt_index == -1 || opt_index == EOF) { |
535 | break; | 525 | break; |
526 | } | ||
536 | 527 | ||
537 | switch (c) { | 528 | switch (opt_index) { |
538 | case 'H': /* hostname */ | 529 | case 'H': /* hostname */ |
539 | if (is_host (optarg)) { | 530 | if (is_host(optarg)) { |
540 | server_address = optarg; | 531 | result.config.server_address = optarg; |
541 | } | 532 | } else { |
542 | else { | 533 | usage2(_("Invalid hostname/address"), optarg); |
543 | usage2 (_("Invalid hostname/address"), optarg); | ||
544 | } | 534 | } |
545 | break; | 535 | break; |
546 | case 'p': /* port */ | 536 | case 'p': /* port */ |
547 | if (is_intpos (optarg)) | 537 | if (is_intpos(optarg)) { |
548 | server_port_option = atoi (optarg); | 538 | server_port_option = atoi(optarg); |
549 | else | 539 | } else { |
550 | usage4 (_("Port must be a positive integer")); | 540 | usage4(_("Port must be a positive integer")); |
541 | } | ||
551 | break; | 542 | break; |
552 | case 'F': | 543 | case 'F': |
553 | /* localhostname */ | 544 | /* localhostname */ |
554 | localhostname = strdup(optarg); | 545 | result.config.localhostname = strdup(optarg); |
555 | break; | 546 | break; |
556 | case 'f': /* from argument */ | 547 | case 'f': /* from argument */ |
557 | from_arg = optarg + strspn(optarg, "<"); | 548 | result.config.from_arg = optarg + strspn(optarg, "<"); |
558 | from_arg = strndup(from_arg, strcspn(from_arg, ">")); | 549 | result.config.from_arg = strndup(result.config.from_arg, strcspn(result.config.from_arg, ">")); |
559 | send_mail_from = 1; | 550 | result.config.send_mail_from = true; |
560 | break; | 551 | break; |
561 | case 'A': | 552 | case 'A': |
562 | authtype = optarg; | 553 | result.config.authtype = optarg; |
563 | use_ehlo = true; | 554 | result.config.use_ehlo = true; |
564 | break; | 555 | break; |
565 | case 'U': | 556 | case 'U': |
566 | authuser = optarg; | 557 | result.config.authuser = optarg; |
567 | break; | 558 | break; |
568 | case 'P': | 559 | case 'P': |
569 | authpass = optarg; | 560 | result.config.authpass = optarg; |
570 | break; | 561 | break; |
571 | case 'e': /* server expect string on 220 */ | 562 | case 'e': /* server expect string on 220 */ |
572 | server_expect = optarg; | 563 | result.config.server_expect = optarg; |
573 | break; | 564 | break; |
574 | case 'C': /* commands */ | 565 | case 'C': /* commands */ |
575 | if (ncommands >= command_size) { | 566 | if (result.config.ncommands >= command_size) { |
576 | command_size+=8; | 567 | command_size += 8; |
577 | commands = realloc (commands, sizeof(char *) * command_size); | 568 | result.config.commands = realloc(result.config.commands, sizeof(char *) * command_size); |
578 | if (commands == NULL) | 569 | if (result.config.commands == NULL) { |
579 | die (STATE_UNKNOWN, | 570 | die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), result.config.ncommands); |
580 | _("Could not realloc() units [%d]\n"), ncommands); | 571 | } |
581 | } | 572 | } |
582 | commands[ncommands] = (char *) malloc (sizeof(char) * 255); | 573 | result.config.commands[result.config.ncommands] = (char *)malloc(sizeof(char) * 255); |
583 | strncpy (commands[ncommands], optarg, 255); | 574 | strncpy(result.config.commands[result.config.ncommands], optarg, 255); |
584 | ncommands++; | 575 | result.config.ncommands++; |
585 | break; | 576 | break; |
586 | case 'R': /* server responses */ | 577 | case 'R': /* server responses */ |
587 | if (nresponses >= response_size) { | 578 | if (result.config.nresponses >= response_size) { |
588 | response_size += 8; | 579 | response_size += 8; |
589 | responses = realloc (responses, sizeof(char *) * response_size); | 580 | result.config.responses = realloc(result.config.responses, sizeof(char *) * response_size); |
590 | if (responses == NULL) | 581 | if (result.config.responses == NULL) { |
591 | die (STATE_UNKNOWN, | 582 | die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), result.config.nresponses); |
592 | _("Could not realloc() units [%d]\n"), nresponses); | 583 | } |
593 | } | 584 | } |
594 | responses[nresponses] = (char *) malloc (sizeof(char) * 255); | 585 | result.config.responses[result.config.nresponses] = (char *)malloc(sizeof(char) * 255); |
595 | strncpy (responses[nresponses], optarg, 255); | 586 | strncpy(result.config.responses[result.config.nresponses], optarg, 255); |
596 | nresponses++; | 587 | result.config.nresponses++; |
597 | break; | 588 | break; |
598 | case 'c': /* critical time threshold */ | 589 | case 'c': /* critical time threshold */ |
599 | if (!is_nonnegative (optarg)) | 590 | if (!is_nonnegative(optarg)) { |
600 | usage4 (_("Critical time must be a positive")); | 591 | usage4(_("Critical time must be a positive")); |
601 | else { | 592 | } else { |
602 | critical_time = strtod (optarg, NULL); | 593 | result.config.critical_time = strtod(optarg, NULL); |
603 | check_critical_time = true; | 594 | result.config.check_critical_time = true; |
604 | } | 595 | } |
605 | break; | 596 | break; |
606 | case 'w': /* warning time threshold */ | 597 | case 'w': /* warning time threshold */ |
607 | if (!is_nonnegative (optarg)) | 598 | if (!is_nonnegative(optarg)) { |
608 | usage4 (_("Warning time must be a positive")); | 599 | usage4(_("Warning time must be a positive")); |
609 | else { | 600 | } else { |
610 | warning_time = strtod (optarg, NULL); | 601 | result.config.warning_time = strtod(optarg, NULL); |
611 | check_warning_time = true; | 602 | result.config.check_warning_time = true; |
612 | } | 603 | } |
613 | break; | 604 | break; |
614 | case 'v': /* verbose */ | 605 | case 'v': /* verbose */ |
615 | verbose++; | 606 | verbose++; |
616 | break; | 607 | break; |
617 | case 'q': | 608 | case 'q': |
618 | ignore_send_quit_failure = true; /* ignore problem sending QUIT */ | 609 | result.config.ignore_send_quit_failure = true; /* ignore problem sending QUIT */ |
619 | break; | 610 | break; |
620 | case 't': /* timeout */ | 611 | case 't': /* timeout */ |
621 | if (is_intnonneg (optarg)) { | 612 | if (is_intnonneg(optarg)) { |
622 | socket_timeout = atoi (optarg); | 613 | socket_timeout = atoi(optarg); |
623 | } | 614 | } else { |
624 | else { | 615 | usage4(_("Timeout interval must be a positive integer")); |
625 | usage4 (_("Timeout interval must be a positive integer")); | ||
626 | } | 616 | } |
627 | break; | 617 | break; |
628 | case 'D': | 618 | case 'D': { |
629 | /* Check SSL cert validity */ | 619 | /* Check SSL cert validity */ |
630 | #ifdef USE_OPENSSL | 620 | #ifdef USE_OPENSSL |
631 | if ((temp=strchr(optarg,','))!=NULL) { | 621 | char *temp; |
632 | *temp='\0'; | 622 | if ((temp = strchr(optarg, ',')) != NULL) { |
633 | if (!is_intnonneg (optarg)) | 623 | *temp = '\0'; |
634 | usage2 ("Invalid certificate expiration period", optarg); | 624 | if (!is_intnonneg(optarg)) { |
635 | days_till_exp_warn = atoi(optarg); | 625 | usage2("Invalid certificate expiration period", optarg); |
636 | *temp=','; | 626 | } |
637 | temp++; | 627 | result.config.days_till_exp_warn = atoi(optarg); |
638 | if (!is_intnonneg (temp)) | 628 | *temp = ','; |
639 | usage2 (_("Invalid certificate expiration period"), temp); | 629 | temp++; |
640 | days_till_exp_crit = atoi (temp); | 630 | if (!is_intnonneg(temp)) { |
641 | } | 631 | usage2(_("Invalid certificate expiration period"), temp); |
642 | else { | 632 | } |
643 | days_till_exp_crit=0; | 633 | result.config.days_till_exp_crit = atoi(temp); |
644 | if (!is_intnonneg (optarg)) | 634 | } else { |
645 | usage2 ("Invalid certificate expiration period", optarg); | 635 | result.config.days_till_exp_crit = 0; |
646 | days_till_exp_warn = atoi (optarg); | 636 | if (!is_intnonneg(optarg)) { |
647 | } | 637 | usage2("Invalid certificate expiration period", optarg); |
648 | check_cert = true; | 638 | } |
649 | ignore_send_quit_failure = true; | 639 | result.config.days_till_exp_warn = atoi(optarg); |
640 | } | ||
641 | result.config.check_cert = true; | ||
642 | result.config.ignore_send_quit_failure = true; | ||
650 | #else | 643 | #else |
651 | usage (_("SSL support not available - install OpenSSL and recompile")); | 644 | usage(_("SSL support not available - install OpenSSL and recompile")); |
652 | #endif | 645 | #endif |
653 | implicit_tls = true; | 646 | implicit_tls = true; |
654 | // fallthrough | 647 | // fallthrough |
655 | case 's': | 648 | case 's': |
656 | /* ssl */ | 649 | /* ssl */ |
657 | use_ssl = true; | 650 | result.config.use_ssl = true; |
658 | server_port = SMTPS_PORT; | 651 | result.config.server_port = SMTPS_PORT; |
659 | break; | 652 | break; |
660 | case 'S': | 653 | case 'S': |
661 | /* starttls */ | 654 | /* starttls */ |
662 | use_starttls = true; | 655 | result.config.use_starttls = true; |
663 | use_ehlo = true; | 656 | result.config.use_ehlo = true; |
664 | break; | 657 | break; |
658 | } | ||
665 | case SNI_OPTION: | 659 | case SNI_OPTION: |
666 | #ifdef HAVE_SSL | 660 | #ifdef HAVE_SSL |
667 | use_sni = true; | 661 | result.config.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': |
673 | use_proxy_prefix = true; | 667 | result.config.use_proxy_prefix = true; |
674 | break; | 668 | break; |
675 | case 'L': | 669 | case 'L': |
676 | use_lhlo = true; | 670 | result.config.use_lhlo = true; |
677 | break; | 671 | break; |
678 | case '4': | 672 | case '4': |
679 | address_family = AF_INET; | 673 | address_family = AF_INET; |
@@ -682,102 +676,79 @@ 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 | int c = optind; |
700 | if (server_address == NULL) { | 694 | if (result.config.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 | result.config.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 | result.config.server_address = strdup("localhost"); |
709 | } | 703 | } |
710 | } | 704 | } |
711 | 705 | ||
712 | if (server_expect == NULL) | 706 | if (result.config.use_starttls && result.config.use_ssl) { |
713 | server_expect = strdup (SMTP_EXPECT); | ||
714 | |||
715 | if (mail_command == NULL) | ||
716 | mail_command = strdup("MAIL "); | ||
717 | |||
718 | if (from_arg==NULL) | ||
719 | from_arg = strdup(" "); | ||
720 | |||
721 | if (use_starttls && use_ssl) { | ||
722 | if (implicit_tls) { | 707 | if (implicit_tls) { |
723 | use_ssl = false; | 708 | result.config.use_ssl = false; |
724 | server_port = SMTP_PORT; | ||
725 | } else { | 709 | } else { |
726 | usage4 (_("Set either -s/--ssl/--tls or -S/--starttls")); | 710 | usage4(_("Set either -s/--ssl/--tls or -S/--starttls")); |
727 | } | 711 | } |
728 | } | 712 | } |
729 | 713 | ||
730 | if (server_port_option != 0) { | 714 | if (server_port_option != 0) { |
731 | server_port = server_port_option; | 715 | result.config.server_port = server_port_option; |
732 | } | 716 | } |
733 | 717 | ||
734 | return validate_arguments (); | 718 | return result; |
735 | } | ||
736 | |||
737 | |||
738 | |||
739 | int | ||
740 | validate_arguments (void) | ||
741 | { | ||
742 | return OK; | ||
743 | } | 719 | } |
744 | 720 | ||
745 | 721 | char *smtp_quit(check_smtp_config config, char buffer[MAX_INPUT_BUFFER], int socket_descriptor, bool ssl_established) { | |
746 | void | 722 | int sent_bytes = my_send(config, SMTP_QUIT, strlen(SMTP_QUIT), socket_descriptor, ssl_established); |
747 | smtp_quit(void) | 723 | if (sent_bytes < 0) { |
748 | { | 724 | if (config.ignore_send_quit_failure) { |
749 | int bytes; | 725 | if (verbose) { |
750 | int n; | ||
751 | |||
752 | n = my_send(SMTP_QUIT, strlen(SMTP_QUIT)); | ||
753 | if(n < 0) { | ||
754 | if(ignore_send_quit_failure) { | ||
755 | if(verbose) { | ||
756 | printf(_("Connection closed by server before sending QUIT command\n")); | 726 | printf(_("Connection closed by server before sending QUIT command\n")); |
757 | } | 727 | } |
758 | return; | 728 | return buffer; |
759 | } | 729 | } |
760 | die (STATE_UNKNOWN, | 730 | die(STATE_UNKNOWN, _("Connection closed by server before sending QUIT command\n")); |
761 | _("Connection closed by server before sending QUIT command\n")); | ||
762 | } | 731 | } |
763 | 732 | ||
764 | if (verbose) | 733 | if (verbose) { |
765 | printf(_("sent %s\n"), "QUIT"); | 734 | printf(_("sent %s\n"), "QUIT"); |
735 | } | ||
766 | 736 | ||
767 | /* read the response but don't care about problems */ | 737 | /* read the response but don't care about problems */ |
768 | bytes = recvlines(buffer, MAX_INPUT_BUFFER); | 738 | int bytes = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established); |
769 | if (verbose) { | 739 | if (verbose) { |
770 | if (bytes < 0) | 740 | if (bytes < 0) { |
771 | printf(_("recv() failed after QUIT.")); | 741 | printf(_("recv() failed after QUIT.")); |
772 | else if (bytes == 0) | 742 | } else if (bytes == 0) { |
773 | printf(_("Connection reset by peer.")); | 743 | printf(_("Connection reset by peer.")); |
774 | else { | 744 | } else { |
775 | buffer[bytes] = '\0'; | 745 | buffer[bytes] = '\0'; |
776 | printf(_("received %s\n"), buffer); | 746 | printf(_("received %s\n"), buffer); |
777 | } | 747 | } |
778 | } | 748 | } |
779 | } | ||
780 | 749 | ||
750 | return buffer; | ||
751 | } | ||
781 | 752 | ||
782 | /* | 753 | /* |
783 | * Receive one line, copy it into buf and nul-terminate it. Returns the | 754 | * Receive one line, copy it into buf and nul-terminate it. Returns the |
@@ -788,24 +759,22 @@ smtp_quit(void) | |||
788 | * function which buffers the data, move that to netutils.c and change | 759 | * 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. | 760 | * check_smtp and other plugins to use that. Also, remove (\r)\n. |
790 | */ | 761 | */ |
791 | int | 762 | int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_descriptor, bool ssl_established) { |
792 | recvline(char *buf, size_t bufsize) | ||
793 | { | ||
794 | int result; | 763 | int result; |
795 | unsigned i; | 764 | int counter; |
796 | 765 | ||
797 | for (i = result = 0; i < bufsize - 1; i++) { | 766 | for (counter = result = 0; counter < bufsize - 1; counter++) { |
798 | if ((result = my_recv(&buf[i], 1)) != 1) | 767 | if ((result = my_recv(config, &buf[counter], 1, socket_descriptor, ssl_established)) != 1) { |
799 | break; | 768 | break; |
800 | if (buf[i] == '\n') { | 769 | } |
801 | buf[++i] = '\0'; | 770 | if (buf[counter] == '\n') { |
802 | return i; | 771 | buf[++counter] = '\0'; |
772 | return counter; | ||
803 | } | 773 | } |
804 | } | 774 | } |
805 | return (result == 1 || i == 0) ? -2 : result; /* -2 if out of space */ | 775 | return (result == 1 || counter == 0) ? -2 : result; /* -2 if out of space */ |
806 | } | 776 | } |
807 | 777 | ||
808 | |||
809 | /* | 778 | /* |
810 | * Receive one or more lines, copy them into buf and nul-terminate it. Returns | 779 | * 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 | 780 | * the number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on |
@@ -820,117 +789,103 @@ recvline(char *buf, size_t bufsize) | |||
820 | * | 789 | * |
821 | * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n. | 790 | * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n. |
822 | */ | 791 | */ |
823 | int | 792 | int recvlines(check_smtp_config config, char *buf, size_t bufsize, int socket_descriptor, bool ssl_established) { |
824 | recvlines(char *buf, size_t bufsize) | 793 | int result; |
825 | { | 794 | int counter; |
826 | int result, i; | 795 | |
827 | 796 | for (counter = 0; /* forever */; counter += result) { | |
828 | for (i = 0; /* forever */; i += result) | 797 | if (!((result = recvline(buf + counter, bufsize - counter, config, socket_descriptor, ssl_established)) > 3 && |
829 | if (!((result = recvline(buf + i, bufsize - i)) > 3 && | 798 | isdigit((int)buf[counter]) && isdigit((int)buf[counter + 1]) && isdigit((int)buf[counter + 2]) && buf[counter + 3] == '-')) { |
830 | isdigit((int)buf[i]) && | ||
831 | isdigit((int)buf[i + 1]) && | ||
832 | isdigit((int)buf[i + 2]) && | ||
833 | buf[i + 3] == '-')) | ||
834 | break; | 799 | break; |
800 | } | ||
801 | } | ||
835 | 802 | ||
836 | return (result <= 0) ? result : result + i; | 803 | return (result <= 0) ? result : result + counter; |
837 | } | 804 | } |
838 | 805 | ||
839 | 806 | int my_close(int socket_descriptor) { | |
840 | int | ||
841 | my_close (void) | ||
842 | { | ||
843 | int result; | 807 | int result; |
844 | result = close(sd); | 808 | result = close(socket_descriptor); |
845 | #ifdef HAVE_SSL | 809 | #ifdef HAVE_SSL |
846 | np_net_ssl_cleanup(); | 810 | np_net_ssl_cleanup(); |
847 | #endif | 811 | #endif |
848 | return result; | 812 | return result; |
849 | } | 813 | } |
850 | 814 | ||
851 | 815 | void print_help(void) { | |
852 | void | ||
853 | print_help (void) | ||
854 | { | ||
855 | char *myport; | 816 | char *myport; |
856 | xasprintf (&myport, "%d", SMTP_PORT); | 817 | xasprintf(&myport, "%d", SMTP_PORT); |
857 | 818 | ||
858 | print_revision (progname, NP_VERSION); | 819 | print_revision(progname, NP_VERSION); |
859 | 820 | ||
860 | printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"); | 821 | printf("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"); |
861 | printf (COPYRIGHT, copyright, email); | 822 | printf(COPYRIGHT, copyright, email); |
862 | 823 | ||
863 | printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host.")); | 824 | printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host.")); |
864 | 825 | ||
865 | printf ("\n\n"); | 826 | printf("\n\n"); |
866 | 827 | ||
867 | print_usage (); | 828 | print_usage(); |
868 | 829 | ||
869 | printf (UT_HELP_VRSN); | 830 | printf(UT_HELP_VRSN); |
870 | printf (UT_EXTRA_OPTS); | 831 | printf(UT_EXTRA_OPTS); |
871 | 832 | ||
872 | printf (UT_HOST_PORT, 'p', myport); | 833 | printf(UT_HOST_PORT, 'p', myport); |
873 | 834 | ||
874 | printf (UT_IPv46); | 835 | printf(UT_IPv46); |
875 | 836 | ||
876 | printf (" %s\n", "-e, --expect=STRING"); | 837 | printf(" %s\n", "-e, --expect=STRING"); |
877 | printf (_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT); | 838 | printf(_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT); |
878 | printf (" %s\n", "-C, --command=STRING"); | 839 | printf(" %s\n", "-C, --command=STRING"); |
879 | printf (" %s\n", _("SMTP command (may be used repeatedly)")); | 840 | printf(" %s\n", _("SMTP command (may be used repeatedly)")); |
880 | printf (" %s\n", "-R, --response=STRING"); | 841 | printf(" %s\n", "-R, --response=STRING"); |
881 | printf (" %s\n", _("Expected response to command (may be used repeatedly)")); | 842 | printf(" %s\n", _("Expected response to command (may be used repeatedly)")); |
882 | printf (" %s\n", "-f, --from=STRING"); | 843 | printf(" %s\n", "-f, --from=STRING"); |
883 | printf (" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")), | 844 | 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"); | 845 | printf(" %s\n", _("FQDN used for HELO")); |
885 | printf (" %s\n", _("FQDN used for HELO")); | 846 | printf(" %s\n", "-r, --proxy"); |
886 | printf (" %s\n", "-r, --proxy"); | 847 | 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 | 848 | #ifdef HAVE_SSL |
889 | printf (" %s\n", "-D, --certificate=INTEGER[,INTEGER]"); | 849 | printf(" %s\n", "-D, --certificate=INTEGER[,INTEGER]"); |
890 | printf (" %s\n", _("Minimum number of days a certificate has to be valid.")); | 850 | printf(" %s\n", _("Minimum number of days a certificate has to be valid.")); |
891 | printf (" %s\n", "-s, --ssl, --tls"); | 851 | printf(" %s\n", "-s, --ssl, --tls"); |
892 | printf (" %s\n", _("Use SSL/TLS for the connection.")); | 852 | printf(" %s\n", _("Use SSL/TLS for the connection.")); |
893 | printf (_(" Sets default port to %d.\n"), SMTPS_PORT); | 853 | printf(_(" Sets default port to %d.\n"), SMTPS_PORT); |
894 | printf (" %s\n", "-S, --starttls"); | 854 | printf(" %s\n", "-S, --starttls"); |
895 | printf (" %s\n", _("Use STARTTLS for the connection.")); | 855 | printf(" %s\n", _("Use STARTTLS for the connection.")); |
896 | printf (" %s\n", "--sni"); | 856 | printf(" %s\n", "--sni"); |
897 | printf (" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); | 857 | printf(" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); |
898 | #endif | 858 | #endif |
899 | 859 | ||
900 | printf (" %s\n", "-A, --authtype=STRING"); | 860 | printf(" %s\n", "-A, --authtype=STRING"); |
901 | printf (" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)")); | 861 | printf(" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)")); |
902 | printf (" %s\n", "-U, --authuser=STRING"); | 862 | printf(" %s\n", "-U, --authuser=STRING"); |
903 | printf (" %s\n", _("SMTP AUTH username")); | 863 | printf(" %s\n", _("SMTP AUTH username")); |
904 | printf (" %s\n", "-P, --authpass=STRING"); | 864 | printf(" %s\n", "-P, --authpass=STRING"); |
905 | printf (" %s\n", _("SMTP AUTH password")); | 865 | printf(" %s\n", _("SMTP AUTH password")); |
906 | printf (" %s\n", "-L, --lmtp"); | 866 | printf(" %s\n", "-L, --lmtp"); |
907 | printf (" %s\n", _("Send LHLO instead of HELO/EHLO")); | 867 | printf(" %s\n", _("Send LHLO instead of HELO/EHLO")); |
908 | printf (" %s\n", "-q, --ignore-quit-failure"); | 868 | printf(" %s\n", "-q, --ignore-quit-failure"); |
909 | printf (" %s\n", _("Ignore failure when sending QUIT command to server")); | 869 | printf(" %s\n", _("Ignore failure when sending QUIT command to server")); |
910 | |||
911 | printf (UT_WARN_CRIT); | ||
912 | 870 | ||
913 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 871 | printf(UT_WARN_CRIT); |
914 | 872 | ||
915 | printf (UT_VERBOSE); | 873 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
874 | |||
875 | printf(UT_VERBOSE); | ||
916 | 876 | ||
917 | printf("\n"); | 877 | printf("\n"); |
918 | printf ("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); | 878 | printf("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); |
919 | printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful")); | 879 | 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")); | 880 | printf("%s\n", _("connects, but incorrect response messages from the host result in")); |
921 | printf ("%s\n", _("STATE_WARNING return values.")); | 881 | printf("%s\n", _("STATE_WARNING return values.")); |
922 | 882 | ||
923 | printf (UT_SUPPORT); | 883 | printf(UT_SUPPORT); |
924 | } | 884 | } |
925 | 885 | ||
926 | 886 | void print_usage(void) { | |
927 | 887 | printf("%s\n", _("Usage:")); | |
928 | void | 888 | printf("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n", progname); |
929 | print_usage (void) | 889 | printf("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n"); |
930 | { | 890 | 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 | } | 891 | } |
936 | |||
diff --git a/plugins/check_smtp.d/config.h b/plugins/check_smtp.d/config.h new file mode 100644 index 00000000..0a6511ef --- /dev/null +++ b/plugins/check_smtp.d/config.h | |||
@@ -0,0 +1,92 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include <stddef.h> | ||
5 | #include <string.h> | ||
6 | |||
7 | enum { | ||
8 | SMTP_PORT = 25, | ||
9 | SMTPS_PORT = 465 | ||
10 | }; | ||
11 | |||
12 | #define SMTP_EXPECT "220" | ||
13 | |||
14 | typedef struct { | ||
15 | int server_port; | ||
16 | char *server_address; | ||
17 | char *localhostname; | ||
18 | char *server_expect; | ||
19 | bool ignore_send_quit_failure; | ||
20 | |||
21 | double warning_time; | ||
22 | bool check_warning_time; | ||
23 | double critical_time; | ||
24 | bool check_critical_time; | ||
25 | bool use_ehlo; | ||
26 | bool use_lhlo; | ||
27 | |||
28 | char *from_arg; | ||
29 | bool send_mail_from; | ||
30 | |||
31 | int ncommands; | ||
32 | char **commands; | ||
33 | |||
34 | int nresponses; | ||
35 | char **responses; | ||
36 | |||
37 | char *authtype; | ||
38 | char *authuser; | ||
39 | char *authpass; | ||
40 | |||
41 | bool use_proxy_prefix; | ||
42 | #ifdef HAVE_SSL | ||
43 | bool check_cert; | ||
44 | int days_till_exp_warn; | ||
45 | int days_till_exp_crit; | ||
46 | bool use_ssl; | ||
47 | bool use_starttls; | ||
48 | bool use_sni; | ||
49 | #endif | ||
50 | } check_smtp_config; | ||
51 | |||
52 | check_smtp_config check_smtp_config_init() { | ||
53 | check_smtp_config tmp = { | ||
54 | .server_port = SMTP_PORT, | ||
55 | .server_address = NULL, | ||
56 | .localhostname = NULL, | ||
57 | |||
58 | .server_expect = SMTP_EXPECT, | ||
59 | .ignore_send_quit_failure = false, | ||
60 | |||
61 | .warning_time = 0, | ||
62 | .check_warning_time = false, | ||
63 | .critical_time = 0, | ||
64 | .check_critical_time = false, | ||
65 | .use_ehlo = false, | ||
66 | .use_lhlo = false, | ||
67 | |||
68 | .from_arg = strdup(" "), | ||
69 | .send_mail_from = false, | ||
70 | |||
71 | .ncommands = 0, | ||
72 | .commands = NULL, | ||
73 | |||
74 | .nresponses = 0, | ||
75 | .responses = NULL, | ||
76 | |||
77 | .authtype = NULL, | ||
78 | .authuser = NULL, | ||
79 | .authpass = NULL, | ||
80 | |||
81 | .use_proxy_prefix = false, | ||
82 | #ifdef HAVE_SSL | ||
83 | .check_cert = false, | ||
84 | .days_till_exp_warn = 0, | ||
85 | .days_till_exp_crit = 0, | ||
86 | .use_ssl = false, | ||
87 | .use_starttls = false, | ||
88 | .use_sni = false, | ||
89 | #endif | ||
90 | }; | ||
91 | return tmp; | ||
92 | } | ||