diff options
Diffstat (limited to 'plugins/check_tcp.c')
| -rw-r--r-- | plugins/check_tcp.c | 872 |
1 files changed, 512 insertions, 360 deletions
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 49ad096c..09806373 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | * Monitoring check_tcp plugin | 3 | * Monitoring check_tcp plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team | 6 | * Copyright (c) 1999-2025 Monitoring Plugins Development Team |
| 7 | * | 7 | * |
| 8 | * Description: | 8 | * Description: |
| 9 | * | 9 | * |
| @@ -29,74 +29,64 @@ | |||
| 29 | 29 | ||
| 30 | /* progname "check_tcp" changes depending on symlink called */ | 30 | /* progname "check_tcp" changes depending on symlink called */ |
| 31 | char *progname; | 31 | char *progname; |
| 32 | const char *copyright = "1999-2024"; | 32 | const char *copyright = "1999-2025"; |
| 33 | const char *email = "devel@monitoring-plugins.org"; | 33 | const char *email = "devel@monitoring-plugins.org"; |
| 34 | 34 | ||
| 35 | #include "common.h" | 35 | #include "./common.h" |
| 36 | #include "netutils.h" | 36 | #include "./netutils.h" |
| 37 | #include "utils.h" | 37 | #include "./utils.h" |
| 38 | #include "utils_tcp.h" | 38 | #include "./check_tcp.d/config.h" |
| 39 | #include "output.h" | ||
| 40 | #include "states.h" | ||
| 39 | 41 | ||
| 42 | #include <sys/types.h> | ||
| 40 | #include <ctype.h> | 43 | #include <ctype.h> |
| 41 | #include <sys/select.h> | 44 | #include <sys/select.h> |
| 42 | 45 | ||
| 46 | ssize_t my_recv(int socket_descriptor, char *buf, size_t len, bool use_tls) { | ||
| 43 | #ifdef HAVE_SSL | 47 | #ifdef HAVE_SSL |
| 44 | static bool check_cert = false; | 48 | if (use_tls) { |
| 45 | static int days_till_exp_warn, days_till_exp_crit; | 49 | return np_net_ssl_read(buf, (int)len); |
| 46 | # define my_recv(buf, len) ((flags & FLAG_SSL) ? np_net_ssl_read(buf, len) : read(sd, buf, len)) | 50 | } |
| 47 | # define my_send(buf, len) ((flags & FLAG_SSL) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0)) | 51 | #endif |
| 48 | #else | 52 | return read(socket_descriptor, buf, len); |
| 49 | # define my_recv(buf, len) read(sd, buf, len) | 53 | } |
| 50 | # define my_send(buf, len) send(sd, buf, len, 0) | 54 | |
| 55 | ssize_t my_send(int socket_descriptor, char *buf, size_t len, bool use_tls) { | ||
| 56 | #ifdef HAVE_SSL | ||
| 57 | if (use_tls) { | ||
| 58 | return np_net_ssl_write(buf, (int)len); | ||
| 59 | } | ||
| 51 | #endif | 60 | #endif |
| 61 | return write(socket_descriptor, buf, len); | ||
| 62 | } | ||
| 52 | 63 | ||
| 53 | /* int my_recv(char *, size_t); */ | 64 | typedef struct { |
| 54 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 65 | int errorcode; |
| 55 | static void print_help(void); | 66 | check_tcp_config config; |
| 67 | } check_tcp_config_wrapper; | ||
| 68 | static check_tcp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/, | ||
| 69 | check_tcp_config /*config*/); | ||
| 70 | void print_help(const char *service); | ||
| 56 | void print_usage(void); | 71 | void print_usage(void); |
| 57 | 72 | ||
| 58 | #define EXPECT server_expect[0] | 73 | int verbosity = 0; |
| 59 | static char *SERVICE = "TCP"; | ||
| 60 | static char *SEND = NULL; | ||
| 61 | static char *QUIT = NULL; | ||
| 62 | static int PROTOCOL = IPPROTO_TCP; /* most common is default */ | ||
| 63 | static int PORT = 0; | ||
| 64 | static int READ_TIMEOUT = 2; | ||
| 65 | |||
| 66 | static int server_port = 0; | ||
| 67 | static char *server_address = NULL; | ||
| 68 | static bool host_specified = false; | ||
| 69 | static char *server_send = NULL; | ||
| 70 | static char *server_quit = NULL; | ||
| 71 | static char **server_expect; | ||
| 72 | static size_t server_expect_count = 0; | ||
| 73 | static ssize_t maxbytes = 0; | ||
| 74 | static char **warn_codes = NULL; | ||
| 75 | static size_t warn_codes_count = 0; | ||
| 76 | static char **crit_codes = NULL; | ||
| 77 | static size_t crit_codes_count = 0; | ||
| 78 | static unsigned int delay = 0; | ||
| 79 | static double warning_time = 0; | ||
| 80 | static double critical_time = 0; | ||
| 81 | static double elapsed_time = 0; | ||
| 82 | static long microsec; | ||
| 83 | static int sd = 0; | ||
| 84 | #define MAXBUF 1024 | ||
| 85 | static char buffer[MAXBUF]; | ||
| 86 | static int expect_mismatch_state = STATE_WARNING; | ||
| 87 | static int match_flags = NP_MATCH_EXACT; | ||
| 88 | 74 | ||
| 89 | #ifdef HAVE_SSL | 75 | static const int READ_TIMEOUT = 2; |
| 90 | static char *sni = NULL; | 76 | |
| 91 | static bool sni_specified = false; | 77 | const int MAXBUF = 1024; |
| 92 | #endif | ||
| 93 | 78 | ||
| 94 | #define FLAG_SSL 0x01 | 79 | const int DEFAULT_FTP_PORT = 21; |
| 95 | #define FLAG_VERBOSE 0x02 | 80 | const int DEFAULT_POP_PORT = 110; |
| 96 | #define FLAG_TIME_WARN 0x04 | 81 | const int DEFAULT_SPOP_PORT = 995; |
| 97 | #define FLAG_TIME_CRIT 0x08 | 82 | const int DEFAULT_SMTP_PORT = 25; |
| 98 | #define FLAG_HIDE_OUTPUT 0x10 | 83 | const int DEFAULT_SSMTP_PORT = 465; |
| 99 | static size_t flags; | 84 | const int DEFAULT_IMAP_PORT = 143; |
| 85 | const int DEFAULT_SIMAP_PORT = 993; | ||
| 86 | const int DEFAULT_XMPP_C2S_PORT = 5222; | ||
| 87 | const int DEFAULT_NNTP_PORT = 119; | ||
| 88 | const int DEFAULT_NNTPS_PORT = 563; | ||
| 89 | const int DEFAULT_CLAMD_PORT = 3310; | ||
| 100 | 90 | ||
| 101 | int main(int argc, char **argv) { | 91 | int main(int argc, char **argv) { |
| 102 | setlocale(LC_ALL, ""); | 92 | setlocale(LC_ALL, ""); |
| @@ -105,353 +95,482 @@ int main(int argc, char **argv) { | |||
| 105 | 95 | ||
| 106 | /* determine program- and service-name quickly */ | 96 | /* determine program- and service-name quickly */ |
| 107 | progname = strrchr(argv[0], '/'); | 97 | progname = strrchr(argv[0], '/'); |
| 108 | if (progname != NULL) | 98 | if (progname != NULL) { |
| 109 | progname++; | 99 | progname++; |
| 110 | else | 100 | } else { |
| 111 | progname = argv[0]; | 101 | progname = argv[0]; |
| 102 | } | ||
| 103 | |||
| 104 | // Initialize config here with values from above, | ||
| 105 | // might be changed by on disk config or cli commands | ||
| 106 | check_tcp_config config = check_tcp_config_init(); | ||
| 112 | 107 | ||
| 113 | size_t prog_name_len = strlen(progname); | 108 | size_t prog_name_len = strlen(progname); |
| 114 | if (prog_name_len > 6 && !memcmp(progname, "check_", 6)) { | 109 | const size_t prefix_length = strlen("check_"); |
| 115 | SERVICE = strdup(progname + 6); | 110 | |
| 116 | for (size_t i = 0; i < prog_name_len - 6; i++) | 111 | if (prog_name_len <= prefix_length) { |
| 117 | SERVICE[i] = toupper(SERVICE[i]); | 112 | die(STATE_UNKNOWN, _("Weird progname")); |
| 113 | } | ||
| 114 | |||
| 115 | if (!memcmp(progname, "check_", prefix_length)) { | ||
| 116 | config.service = strdup(progname + prefix_length); | ||
| 117 | if (config.service == NULL) { | ||
| 118 | die(STATE_UNKNOWN, _("Allocation failed")); | ||
| 119 | } | ||
| 120 | |||
| 121 | for (size_t i = 0; i < prog_name_len - prefix_length; i++) { | ||
| 122 | config.service[i] = toupper(config.service[i]); | ||
| 123 | } | ||
| 118 | } | 124 | } |
| 119 | 125 | ||
| 120 | /* set up a reasonable buffer at first (will be realloc()'ed if | 126 | /* set up a reasonable buffer at first (will be realloc()'ed if |
| 121 | * user specifies other options) */ | 127 | * user specifies other options) */ |
| 122 | server_expect = calloc(2, sizeof(char *)); | 128 | config.server_expect = calloc(2, sizeof(char *)); |
| 129 | |||
| 130 | if (config.server_expect == NULL) { | ||
| 131 | die(STATE_UNKNOWN, _("Allocation failed")); | ||
| 132 | } | ||
| 123 | 133 | ||
| 124 | /* determine defaults for this service's protocol */ | 134 | /* determine defaults for this service's protocol */ |
| 125 | if (!strncmp(SERVICE, "UDP", 3)) { | 135 | if (!strncmp(config.service, "UDP", strlen("UDP"))) { |
| 126 | PROTOCOL = IPPROTO_UDP; | 136 | config.protocol = IPPROTO_UDP; |
| 127 | } else if (!strncmp(SERVICE, "FTP", 3)) { | 137 | } else if (!strncmp(config.service, "FTP", strlen("FTP"))) { |
| 128 | EXPECT = "220"; | 138 | config.server_expect[0] = "220"; |
| 129 | QUIT = "QUIT\r\n"; | 139 | config.quit = "QUIT\r\n"; |
| 130 | PORT = 21; | 140 | config.server_port = DEFAULT_FTP_PORT; |
| 131 | } else if (!strncmp(SERVICE, "POP", 3) || !strncmp(SERVICE, "POP3", 4)) { | 141 | } else if (!strncmp(config.service, "POP", strlen("POP")) || |
| 132 | EXPECT = "+OK"; | 142 | !strncmp(config.service, "POP3", strlen("POP3"))) { |
| 133 | QUIT = "QUIT\r\n"; | 143 | config.server_expect[0] = "+OK"; |
| 134 | PORT = 110; | 144 | config.quit = "QUIT\r\n"; |
| 135 | } else if (!strncmp(SERVICE, "SMTP", 4)) { | 145 | config.server_port = DEFAULT_POP_PORT; |
| 136 | EXPECT = "220"; | 146 | } else if (!strncmp(config.service, "SMTP", strlen("SMTP"))) { |
| 137 | QUIT = "QUIT\r\n"; | 147 | config.server_expect[0] = "220"; |
| 138 | PORT = 25; | 148 | config.quit = "QUIT\r\n"; |
| 139 | } else if (!strncmp(SERVICE, "IMAP", 4)) { | 149 | config.server_port = DEFAULT_SMTP_PORT; |
| 140 | EXPECT = "* OK"; | 150 | } else if (!strncmp(config.service, "IMAP", strlen("IMAP"))) { |
| 141 | QUIT = "a1 LOGOUT\r\n"; | 151 | config.server_expect[0] = "* OK"; |
| 142 | PORT = 143; | 152 | config.quit = "a1 LOGOUT\r\n"; |
| 153 | config.server_port = DEFAULT_IMAP_PORT; | ||
| 143 | } | 154 | } |
| 144 | #ifdef HAVE_SSL | 155 | #ifdef HAVE_SSL |
| 145 | else if (!strncmp(SERVICE, "SIMAP", 5)) { | 156 | else if (!strncmp(config.service, "SIMAP", strlen("SIMAP"))) { |
| 146 | EXPECT = "* OK"; | 157 | config.server_expect[0] = "* OK"; |
| 147 | QUIT = "a1 LOGOUT\r\n"; | 158 | config.quit = "a1 LOGOUT\r\n"; |
| 148 | flags |= FLAG_SSL; | 159 | config.use_tls = true; |
| 149 | PORT = 993; | 160 | config.server_port = DEFAULT_SIMAP_PORT; |
| 150 | } else if (!strncmp(SERVICE, "SPOP", 4)) { | 161 | } else if (!strncmp(config.service, "SPOP", strlen("SPOP"))) { |
| 151 | EXPECT = "+OK"; | 162 | config.server_expect[0] = "+OK"; |
| 152 | QUIT = "QUIT\r\n"; | 163 | config.quit = "QUIT\r\n"; |
| 153 | flags |= FLAG_SSL; | 164 | config.use_tls = true; |
| 154 | PORT = 995; | 165 | config.server_port = DEFAULT_SPOP_PORT; |
| 155 | } else if (!strncmp(SERVICE, "SSMTP", 5)) { | 166 | } else if (!strncmp(config.service, "SSMTP", strlen("SSMTP"))) { |
| 156 | EXPECT = "220"; | 167 | config.server_expect[0] = "220"; |
| 157 | QUIT = "QUIT\r\n"; | 168 | config.quit = "QUIT\r\n"; |
| 158 | flags |= FLAG_SSL; | 169 | config.use_tls = true; |
| 159 | PORT = 465; | 170 | config.server_port = DEFAULT_SSMTP_PORT; |
| 160 | } else if (!strncmp(SERVICE, "JABBER", 6)) { | 171 | } else if (!strncmp(config.service, "JABBER", strlen("JABBER"))) { |
| 161 | SEND = "<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n"; | 172 | config.send = "<stream:stream to=\'host\' xmlns=\'jabber:client\' " |
| 162 | EXPECT = "<?xml version=\'1.0\'"; | 173 | "xmlns:stream=\'http://etherx.jabber.org/streams\'>\n"; |
| 163 | QUIT = "</stream:stream>\n"; | 174 | config.server_expect[0] = "<?xml version=\'1.0\'"; |
| 164 | flags |= FLAG_HIDE_OUTPUT; | 175 | config.quit = "</stream:stream>\n"; |
| 165 | PORT = 5222; | 176 | config.hide_output = true; |
| 166 | } else if (!strncmp(SERVICE, "NNTPS", 5)) { | 177 | config.server_port = DEFAULT_XMPP_C2S_PORT; |
| 167 | server_expect_count = 2; | 178 | } else if (!strncmp(config.service, "NNTPS", strlen("NNTPS"))) { |
| 168 | server_expect[0] = "200"; | 179 | config.server_expect_count = 2; |
| 169 | server_expect[1] = "201"; | 180 | config.server_expect[0] = "200"; |
| 170 | QUIT = "QUIT\r\n"; | 181 | config.server_expect[1] = "201"; |
| 171 | flags |= FLAG_SSL; | 182 | config.quit = "QUIT\r\n"; |
| 172 | PORT = 563; | 183 | config.use_tls = true; |
| 184 | config.server_port = DEFAULT_NNTPS_PORT; | ||
| 173 | } | 185 | } |
| 174 | #endif | 186 | #endif |
| 175 | else if (!strncmp(SERVICE, "NNTP", 4)) { | 187 | else if (!strncmp(config.service, "NNTP", strlen("NNTP"))) { |
| 176 | server_expect_count = 2; | 188 | config.server_expect_count = 2; |
| 177 | server_expect = malloc(sizeof(char *) * server_expect_count); | 189 | char **tmp = realloc(config.server_expect, config.server_expect_count * sizeof(char *)); |
| 178 | server_expect[0] = strdup("200"); | 190 | if (tmp == NULL) { |
| 179 | server_expect[1] = strdup("201"); | 191 | free(config.server_expect); |
| 180 | QUIT = "QUIT\r\n"; | 192 | die(STATE_UNKNOWN, _("Allocation failed")); |
| 181 | PORT = 119; | 193 | } |
| 182 | } else if (!strncmp(SERVICE, "CLAMD", 5)) { | 194 | config.server_expect = tmp; |
| 183 | SEND = "PING"; | 195 | |
| 184 | EXPECT = "PONG"; | 196 | config.server_expect[0] = strdup("200"); |
| 185 | QUIT = NULL; | 197 | config.server_expect[1] = strdup("201"); |
| 186 | PORT = 3310; | 198 | config.quit = "QUIT\r\n"; |
| 199 | config.server_port = DEFAULT_NNTP_PORT; | ||
| 200 | } else if (!strncmp(config.service, "CLAMD", strlen("CLAMD"))) { | ||
| 201 | config.send = "PING"; | ||
| 202 | config.server_expect[0] = "PONG"; | ||
| 203 | config.quit = NULL; | ||
| 204 | config.server_port = DEFAULT_CLAMD_PORT; | ||
| 187 | } | 205 | } |
| 188 | /* fallthrough check, so it's supposed to use reverse matching */ | 206 | /* fallthrough check, so it's supposed to use reverse matching */ |
| 189 | else if (strcmp(SERVICE, "TCP")) | 207 | else if (strcmp(config.service, "TCP")) { |
| 190 | usage(_("CRITICAL - Generic check_tcp called with unknown service\n")); | 208 | usage(_("CRITICAL - Generic check_tcp called with unknown service\n")); |
| 191 | 209 | } | |
| 192 | server_address = "127.0.0.1"; | ||
| 193 | server_port = PORT; | ||
| 194 | server_send = SEND; | ||
| 195 | server_quit = QUIT; | ||
| 196 | char *status = NULL; | ||
| 197 | 210 | ||
| 198 | /* Parse extra opts if any */ | 211 | /* Parse extra opts if any */ |
| 199 | argv = np_extra_opts(&argc, argv, progname); | 212 | argv = np_extra_opts(&argc, argv, progname); |
| 200 | 213 | ||
| 201 | if (process_arguments(argc, argv) == ERROR) | 214 | check_tcp_config_wrapper paw = process_arguments(argc, argv, config); |
| 215 | if (paw.errorcode == ERROR) { | ||
| 202 | usage4(_("Could not parse arguments")); | 216 | usage4(_("Could not parse arguments")); |
| 217 | } | ||
| 218 | |||
| 219 | config = paw.config; | ||
| 203 | 220 | ||
| 204 | if (flags & FLAG_VERBOSE) { | 221 | if (verbosity > 0) { |
| 205 | printf("Using service %s\n", SERVICE); | 222 | printf("Using service %s\n", config.service); |
| 206 | printf("Port: %d\n", server_port); | 223 | printf("Port: %d\n", config.server_port); |
| 207 | printf("flags: 0x%x\n", (int)flags); | ||
| 208 | } | 224 | } |
| 209 | 225 | ||
| 210 | if (EXPECT && !server_expect_count) | 226 | if ((config.server_expect_count == 0) && config.server_expect[0]) { |
| 211 | server_expect_count++; | 227 | config.server_expect_count++; |
| 228 | } | ||
| 212 | 229 | ||
| 213 | if (PROTOCOL == IPPROTO_UDP && !(server_expect_count && server_send)) { | 230 | if (config.protocol == IPPROTO_UDP && !(config.server_expect_count && config.send)) { |
| 214 | usage(_("With UDP checks, a send/expect string must be specified.")); | 231 | usage(_("With UDP checks, a send/expect string must be specified.")); |
| 215 | } | 232 | } |
| 216 | 233 | ||
| 234 | // Initialize check stuff before setting timers | ||
| 235 | mp_check overall = mp_check_init(); | ||
| 236 | if (config.output_format_set) { | ||
| 237 | mp_set_format(config.output_format); | ||
| 238 | } | ||
| 239 | |||
| 217 | /* set up the timer */ | 240 | /* set up the timer */ |
| 218 | signal(SIGALRM, socket_timeout_alarm_handler); | 241 | signal(SIGALRM, socket_timeout_alarm_handler); |
| 219 | alarm(socket_timeout); | 242 | alarm(socket_timeout); |
| 220 | 243 | ||
| 221 | /* try to connect to the host at the given port number */ | 244 | /* try to connect to the host at the given port number */ |
| 222 | struct timeval tv; | 245 | struct timeval start_time; |
| 223 | gettimeofday(&tv, NULL); | 246 | gettimeofday(&start_time, NULL); |
| 224 | 247 | ||
| 225 | int result = STATE_UNKNOWN; | 248 | int socket_descriptor = 0; |
| 226 | result = np_net_connect(server_address, server_port, &sd, PROTOCOL); | 249 | mp_subcheck inital_connect_result = mp_subcheck_init(); |
| 227 | if (result == STATE_CRITICAL) | 250 | |
| 228 | return econn_refuse_state; | 251 | // Try initial connection |
| 252 | if (np_net_connect(config.server_address, config.server_port, &socket_descriptor, | ||
| 253 | config.protocol) == STATE_CRITICAL) { | ||
| 254 | // Early exit here, we got connection refused | ||
| 255 | inital_connect_result = | ||
| 256 | mp_set_subcheck_state(inital_connect_result, config.econn_refuse_state); | ||
| 257 | xasprintf(&inital_connect_result.output, "Connection to %s on port %i was REFUSED", | ||
| 258 | config.server_address, config.server_port); | ||
| 259 | mp_add_subcheck_to_check(&overall, inital_connect_result); | ||
| 260 | mp_exit(overall); | ||
| 261 | } else { | ||
| 262 | inital_connect_result = mp_set_subcheck_state(inital_connect_result, STATE_OK); | ||
| 263 | xasprintf(&inital_connect_result.output, "Connection to %s on port %i was a SUCCESS", | ||
| 264 | config.server_address, config.server_port); | ||
| 265 | mp_add_subcheck_to_check(&overall, inital_connect_result); | ||
| 266 | } | ||
| 229 | 267 | ||
| 230 | #ifdef HAVE_SSL | 268 | #ifdef HAVE_SSL |
| 231 | if (flags & FLAG_SSL) { | 269 | if (config.use_tls) { |
| 232 | result = np_net_ssl_init_with_hostname(sd, (sni_specified ? sni : NULL)); | 270 | mp_subcheck tls_connection_result = mp_subcheck_init(); |
| 233 | if (result == STATE_OK && check_cert) { | 271 | mp_state_enum result = np_net_ssl_init_with_hostname( |
| 234 | result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); | 272 | socket_descriptor, (config.sni_specified ? config.sni : NULL)); |
| 273 | tls_connection_result = mp_set_subcheck_default_state(tls_connection_result, result); | ||
| 274 | |||
| 275 | if (result == STATE_OK) { | ||
| 276 | xasprintf(&tls_connection_result.output, "TLS connection succeeded"); | ||
| 277 | |||
| 278 | if (config.check_cert) { | ||
| 279 | result = | ||
| 280 | np_net_ssl_check_cert(config.days_till_exp_warn, config.days_till_exp_crit); | ||
| 281 | |||
| 282 | mp_subcheck tls_certificate_lifetime_result = mp_subcheck_init(); | ||
| 283 | tls_certificate_lifetime_result = | ||
| 284 | mp_set_subcheck_state(tls_certificate_lifetime_result, result); | ||
| 285 | |||
| 286 | if (result == STATE_OK) { | ||
| 287 | xasprintf(&tls_certificate_lifetime_result.output, | ||
| 288 | "Certificate lifetime is within thresholds"); | ||
| 289 | } else if (result == STATE_WARNING) { | ||
| 290 | xasprintf(&tls_certificate_lifetime_result.output, | ||
| 291 | "Certificate lifetime is violating warning threshold (%i)", | ||
| 292 | config.days_till_exp_warn); | ||
| 293 | } else if (result == STATE_CRITICAL) { | ||
| 294 | xasprintf(&tls_certificate_lifetime_result.output, | ||
| 295 | "Certificate lifetime is violating critical threshold (%i)", | ||
| 296 | config.days_till_exp_crit); | ||
| 297 | } else { | ||
| 298 | xasprintf(&tls_certificate_lifetime_result.output, | ||
| 299 | "Certificate lifetime is somehow unknown"); | ||
| 300 | } | ||
| 301 | |||
| 302 | mp_add_subcheck_to_subcheck(&tls_connection_result, | ||
| 303 | tls_certificate_lifetime_result); | ||
| 304 | } | ||
| 305 | |||
| 306 | mp_add_subcheck_to_check(&overall, tls_connection_result); | ||
| 307 | } else { | ||
| 308 | xasprintf(&tls_connection_result.output, "TLS connection failed"); | ||
| 309 | mp_add_subcheck_to_check(&overall, tls_connection_result); | ||
| 310 | |||
| 311 | if (socket_descriptor) { | ||
| 312 | close(socket_descriptor); | ||
| 313 | } | ||
| 314 | np_net_ssl_cleanup(); | ||
| 315 | |||
| 316 | mp_exit(overall); | ||
| 235 | } | 317 | } |
| 236 | } | 318 | } |
| 237 | if (result != STATE_OK) { | ||
| 238 | if (sd) | ||
| 239 | close(sd); | ||
| 240 | np_net_ssl_cleanup(); | ||
| 241 | return result; | ||
| 242 | } | ||
| 243 | #endif /* HAVE_SSL */ | 319 | #endif /* HAVE_SSL */ |
| 244 | 320 | ||
| 245 | if (server_send != NULL) { /* Something to send? */ | 321 | if (config.send != NULL) { /* Something to send? */ |
| 246 | my_send(server_send, strlen(server_send)); | 322 | my_send(socket_descriptor, config.send, strlen(config.send), config.use_tls); |
| 247 | } | 323 | } |
| 248 | 324 | ||
| 249 | if (delay > 0) { | 325 | if (config.delay > 0) { |
| 250 | tv.tv_sec += delay; | 326 | start_time.tv_sec += config.delay; |
| 251 | sleep(delay); | 327 | sleep(config.delay); |
| 252 | } | 328 | } |
| 253 | 329 | ||
| 254 | if (flags & FLAG_VERBOSE) { | 330 | if (verbosity > 0) { |
| 255 | if (server_send) { | 331 | if (config.send) { |
| 256 | printf("Send string: %s\n", server_send); | 332 | printf("Send string: %s\n", config.send); |
| 333 | } | ||
| 334 | if (config.quit) { | ||
| 335 | printf("Quit string: %s\n", config.quit); | ||
| 257 | } | 336 | } |
| 258 | if (server_quit) { | 337 | printf("server_expect_count: %d\n", (int)config.server_expect_count); |
| 259 | printf("Quit string: %s\n", server_quit); | 338 | for (size_t i = 0; i < config.server_expect_count; i++) { |
| 339 | printf("\t%zd: %s\n", i, config.server_expect[i]); | ||
| 260 | } | 340 | } |
| 261 | printf("server_expect_count: %d\n", (int)server_expect_count); | ||
| 262 | for (size_t i = 0; i < server_expect_count; i++) | ||
| 263 | printf("\t%zd: %s\n", i, server_expect[i]); | ||
| 264 | } | 341 | } |
| 265 | 342 | ||
| 266 | /* if(len) later on, we know we have a non-NULL response */ | 343 | /* if(len) later on, we know we have a non-NULL response */ |
| 267 | ssize_t len = 0; | 344 | ssize_t len = 0; |
| 345 | char *received_buffer = NULL; | ||
| 346 | enum np_match_result match = NP_MATCH_NONE; | ||
| 347 | mp_subcheck expected_data_result = mp_subcheck_init(); | ||
| 268 | 348 | ||
| 269 | int match = -1; | 349 | if (config.server_expect_count) { |
| 270 | struct timeval timeout; | ||
| 271 | fd_set rfds; | ||
| 272 | FD_ZERO(&rfds); | ||
| 273 | if (server_expect_count) { | ||
| 274 | ssize_t received = 0; | 350 | ssize_t received = 0; |
| 351 | char buffer[MAXBUF]; | ||
| 275 | 352 | ||
| 276 | /* watch for the expect string */ | 353 | /* watch for the expect string */ |
| 277 | while ((received = my_recv(buffer, sizeof(buffer))) > 0) { | 354 | while ((received = my_recv(socket_descriptor, buffer, sizeof(buffer), config.use_tls)) > |
| 278 | status = realloc(status, len + received + 1); | 355 | 0) { |
| 279 | memcpy(&status[len], buffer, received); | 356 | received_buffer = realloc(received_buffer, len + received + 1); |
| 357 | |||
| 358 | if (received_buffer == NULL) { | ||
| 359 | die(STATE_UNKNOWN, _("Allocation failed")); | ||
| 360 | } | ||
| 361 | |||
| 362 | memcpy(&received_buffer[len], buffer, received); | ||
| 280 | len += received; | 363 | len += received; |
| 281 | status[len] = '\0'; | 364 | received_buffer[len] = '\0'; |
| 282 | 365 | ||
| 283 | /* stop reading if user-forced */ | 366 | /* stop reading if user-forced */ |
| 284 | if (maxbytes && len >= maxbytes) | 367 | if (config.maxbytes && len >= config.maxbytes) { |
| 285 | break; | 368 | break; |
| 369 | } | ||
| 286 | 370 | ||
| 287 | if ((match = np_expect_match(status, server_expect, server_expect_count, match_flags)) != NP_MATCH_RETRY) | 371 | if ((match = np_expect_match(received_buffer, config.server_expect, |
| 372 | config.server_expect_count, config.match_flags)) != | ||
| 373 | NP_MATCH_RETRY) { | ||
| 288 | break; | 374 | break; |
| 375 | } | ||
| 376 | |||
| 377 | fd_set rfds; | ||
| 378 | FD_ZERO(&rfds); | ||
| 379 | FD_SET(socket_descriptor, &rfds); | ||
| 289 | 380 | ||
| 290 | /* some protocols wait for further input, so make sure we don't wait forever */ | 381 | /* some protocols wait for further input, so make sure we don't wait forever */ |
| 291 | FD_SET(sd, &rfds); | 382 | struct timeval timeout; |
| 292 | timeout.tv_sec = READ_TIMEOUT; | 383 | timeout.tv_sec = READ_TIMEOUT; |
| 293 | timeout.tv_usec = 0; | 384 | timeout.tv_usec = 0; |
| 294 | if (select(sd + 1, &rfds, NULL, NULL, &timeout) <= 0) | 385 | |
| 386 | if (select(socket_descriptor + 1, &rfds, NULL, NULL, &timeout) <= 0) { | ||
| 295 | break; | 387 | break; |
| 388 | } | ||
| 296 | } | 389 | } |
| 297 | 390 | ||
| 298 | if (match == NP_MATCH_RETRY) | 391 | if (match == NP_MATCH_RETRY) { |
| 299 | match = NP_MATCH_FAILURE; | 392 | match = NP_MATCH_FAILURE; |
| 393 | } | ||
| 300 | 394 | ||
| 301 | /* no data when expected, so return critical */ | 395 | /* no data when expected, so return critical */ |
| 302 | if (len == 0) | 396 | if (len == 0) { |
| 303 | die(STATE_CRITICAL, _("No data received from host\n")); | 397 | xasprintf(&expected_data_result.output, "Received no data when some was expected"); |
| 398 | expected_data_result = mp_set_subcheck_state(expected_data_result, STATE_CRITICAL); | ||
| 399 | mp_add_subcheck_to_check(&overall, expected_data_result); | ||
| 400 | mp_exit(overall); | ||
| 401 | } | ||
| 304 | 402 | ||
| 305 | /* print raw output if we're debugging */ | 403 | /* print raw output if we're debugging */ |
| 306 | if (flags & FLAG_VERBOSE) | 404 | if (verbosity > 0) { |
| 307 | printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n", (int)len + 1, status); | 405 | printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n", |
| 406 | (int)len + 1, received_buffer); | ||
| 407 | } | ||
| 308 | /* strip whitespace from end of output */ | 408 | /* strip whitespace from end of output */ |
| 309 | while (--len > 0 && isspace(status[len])) | 409 | while (--len > 0 && isspace(received_buffer[len])) { |
| 310 | status[len] = '\0'; | 410 | received_buffer[len] = '\0'; |
| 411 | } | ||
| 311 | } | 412 | } |
| 312 | 413 | ||
| 313 | if (server_quit != NULL) { | 414 | if (config.quit != NULL) { |
| 314 | my_send(server_quit, strlen(server_quit)); | 415 | my_send(socket_descriptor, config.quit, strlen(config.quit), config.use_tls); |
| 416 | } | ||
| 417 | |||
| 418 | if (socket_descriptor) { | ||
| 419 | close(socket_descriptor); | ||
| 315 | } | 420 | } |
| 316 | if (sd) | ||
| 317 | close(sd); | ||
| 318 | #ifdef HAVE_SSL | 421 | #ifdef HAVE_SSL |
| 319 | np_net_ssl_cleanup(); | 422 | np_net_ssl_cleanup(); |
| 320 | #endif | 423 | #endif |
| 321 | 424 | ||
| 322 | microsec = deltime(tv); | 425 | long microsec = deltime(start_time); |
| 323 | elapsed_time = (double)microsec / 1.0e6; | 426 | double elapsed_time = (double)microsec / 1.0e6; |
| 427 | |||
| 428 | mp_subcheck elapsed_time_result = mp_subcheck_init(); | ||
| 429 | |||
| 430 | mp_perfdata time_pd = perfdata_init(); | ||
| 431 | time_pd = mp_set_pd_value(time_pd, elapsed_time); | ||
| 432 | time_pd.label = "time"; | ||
| 433 | time_pd.uom = "s"; | ||
| 434 | |||
| 435 | if (config.critical_time_set && elapsed_time > config.critical_time) { | ||
| 436 | xasprintf(&elapsed_time_result.output, | ||
| 437 | "Connection time %fs exceeded critical threshold (%f)", elapsed_time, | ||
| 438 | config.critical_time); | ||
| 439 | |||
| 440 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_CRITICAL); | ||
| 441 | time_pd.crit_present = true; | ||
| 442 | mp_range crit_val = mp_range_init(); | ||
| 443 | |||
| 444 | crit_val.end = mp_create_pd_value(config.critical_time); | ||
| 445 | crit_val.end_infinity = false; | ||
| 446 | |||
| 447 | time_pd.crit = crit_val; | ||
| 448 | } else if (config.warning_time_set && elapsed_time > config.warning_time) { | ||
| 449 | xasprintf(&elapsed_time_result.output, | ||
| 450 | "Connection time %fs exceeded warning threshold (%f)", elapsed_time, | ||
| 451 | config.critical_time); | ||
| 452 | |||
| 453 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_WARNING); | ||
| 454 | time_pd.warn_present = true; | ||
| 455 | mp_range warn_val = mp_range_init(); | ||
| 456 | warn_val.end = mp_create_pd_value(config.critical_time); | ||
| 457 | warn_val.end_infinity = false; | ||
| 458 | |||
| 459 | time_pd.warn = warn_val; | ||
| 460 | } else { | ||
| 461 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_OK); | ||
| 462 | xasprintf(&elapsed_time_result.output, "Connection time %fs is within thresholds", | ||
| 463 | elapsed_time); | ||
| 464 | } | ||
| 324 | 465 | ||
| 325 | if (flags & FLAG_TIME_CRIT && elapsed_time > critical_time) | 466 | mp_add_perfdata_to_subcheck(&elapsed_time_result, time_pd); |
| 326 | result = STATE_CRITICAL; | 467 | mp_add_subcheck_to_check(&overall, elapsed_time_result); |
| 327 | else if (flags & FLAG_TIME_WARN && elapsed_time > warning_time) | ||
| 328 | result = STATE_WARNING; | ||
| 329 | 468 | ||
| 330 | /* did we get the response we hoped? */ | 469 | /* did we get the response we hoped? */ |
| 331 | if (match == NP_MATCH_FAILURE && result != STATE_CRITICAL) | 470 | if (match == NP_MATCH_FAILURE) { |
| 332 | result = expect_mismatch_state; | 471 | expected_data_result = |
| 472 | mp_set_subcheck_state(expected_data_result, config.expect_mismatch_state); | ||
| 473 | xasprintf(&expected_data_result.output, "Answer failed to match expectation"); | ||
| 474 | mp_add_subcheck_to_check(&overall, expected_data_result); | ||
| 475 | } else if (match == NP_MATCH_SUCCESS) { | ||
| 476 | expected_data_result = mp_set_subcheck_state(expected_data_result, STATE_OK); | ||
| 477 | xasprintf(&expected_data_result.output, "The answer of the server matched the expectation"); | ||
| 478 | mp_add_subcheck_to_check(&overall, expected_data_result); | ||
| 479 | } | ||
| 333 | 480 | ||
| 334 | /* reset the alarm */ | 481 | /* reset the alarm */ |
| 335 | alarm(0); | 482 | alarm(0); |
| 336 | 483 | ||
| 337 | /* this is a bit stupid, because we don't want to print the | 484 | mp_exit(overall); |
| 338 | * response time (which can look ok to the user) if we didn't get | ||
| 339 | * the response we were looking for. if-else */ | ||
| 340 | printf("%s %s - ", SERVICE, state_text(result)); | ||
| 341 | |||
| 342 | if (match == NP_MATCH_FAILURE && len && !(flags & FLAG_HIDE_OUTPUT)) | ||
| 343 | printf("Unexpected response from host/socket: %s", status); | ||
| 344 | else { | ||
| 345 | if (match == NP_MATCH_FAILURE) | ||
| 346 | printf("Unexpected response from host/socket on "); | ||
| 347 | else | ||
| 348 | printf("%.3f second response time on ", elapsed_time); | ||
| 349 | if (server_address[0] != '/') { | ||
| 350 | if (host_specified) | ||
| 351 | printf("%s port %d", server_address, server_port); | ||
| 352 | else | ||
| 353 | printf("port %d", server_port); | ||
| 354 | } else | ||
| 355 | printf("socket %s", server_address); | ||
| 356 | } | ||
| 357 | |||
| 358 | if (match != NP_MATCH_FAILURE && !(flags & FLAG_HIDE_OUTPUT) && len) | ||
| 359 | printf(" [%s]", status); | ||
| 360 | |||
| 361 | /* perf-data doesn't apply when server doesn't talk properly, | ||
| 362 | * so print all zeroes on warn and crit. Use fperfdata since | ||
| 363 | * localisation settings can make different outputs */ | ||
| 364 | if (match == NP_MATCH_FAILURE) | ||
| 365 | printf("|%s", fperfdata("time", elapsed_time, "s", (flags & FLAG_TIME_WARN ? true : false), 0, | ||
| 366 | (flags & FLAG_TIME_CRIT ? true : false), 0, true, 0, true, socket_timeout)); | ||
| 367 | else | ||
| 368 | printf("|%s", fperfdata("time", elapsed_time, "s", (flags & FLAG_TIME_WARN ? true : false), warning_time, | ||
| 369 | (flags & FLAG_TIME_CRIT ? true : false), critical_time, true, 0, true, socket_timeout)); | ||
| 370 | |||
| 371 | putchar('\n'); | ||
| 372 | return result; | ||
| 373 | } | 485 | } |
| 374 | 486 | ||
| 375 | /* process command-line arguments */ | 487 | /* process command-line arguments */ |
| 376 | static int process_arguments(int argc, char **argv) { | 488 | static check_tcp_config_wrapper process_arguments(int argc, char **argv, check_tcp_config config) { |
| 377 | enum { | 489 | enum { |
| 378 | SNI_OPTION = CHAR_MAX + 1 | 490 | SNI_OPTION = CHAR_MAX + 1, |
| 491 | output_format_index, | ||
| 379 | }; | 492 | }; |
| 380 | 493 | ||
| 381 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 494 | static struct option longopts[] = { |
| 382 | {"critical", required_argument, 0, 'c'}, | 495 | {"hostname", required_argument, 0, 'H'}, |
| 383 | {"warning", required_argument, 0, 'w'}, | 496 | {"critical", required_argument, 0, 'c'}, |
| 384 | {"critical-codes", required_argument, 0, 'C'}, | 497 | {"warning", required_argument, 0, 'w'}, |
| 385 | {"warning-codes", required_argument, 0, 'W'}, | 498 | {"critical-codes", required_argument, 0, 'C'}, |
| 386 | {"timeout", required_argument, 0, 't'}, | 499 | {"warning-codes", required_argument, 0, 'W'}, |
| 387 | {"protocol", required_argument, 0, 'P'}, /* FIXME: Unhandled */ | 500 | {"timeout", required_argument, 0, 't'}, |
| 388 | {"port", required_argument, 0, 'p'}, | 501 | {"protocol", required_argument, 0, 'P'}, /* FIXME: Unhandled */ |
| 389 | {"escape", no_argument, 0, 'E'}, | 502 | {"port", required_argument, 0, 'p'}, |
| 390 | {"all", no_argument, 0, 'A'}, | 503 | {"escape", no_argument, 0, 'E'}, |
| 391 | {"send", required_argument, 0, 's'}, | 504 | {"all", no_argument, 0, 'A'}, |
| 392 | {"expect", required_argument, 0, 'e'}, | 505 | {"send", required_argument, 0, 's'}, |
| 393 | {"maxbytes", required_argument, 0, 'm'}, | 506 | {"expect", required_argument, 0, 'e'}, |
| 394 | {"quit", required_argument, 0, 'q'}, | 507 | {"maxbytes", required_argument, 0, 'm'}, |
| 395 | {"jail", no_argument, 0, 'j'}, | 508 | {"quit", required_argument, 0, 'q'}, |
| 396 | {"delay", required_argument, 0, 'd'}, | 509 | {"jail", no_argument, 0, 'j'}, |
| 397 | {"refuse", required_argument, 0, 'r'}, | 510 | {"delay", required_argument, 0, 'd'}, |
| 398 | {"mismatch", required_argument, 0, 'M'}, | 511 | {"refuse", required_argument, 0, 'r'}, |
| 399 | {"use-ipv4", no_argument, 0, '4'}, | 512 | {"mismatch", required_argument, 0, 'M'}, |
| 400 | {"use-ipv6", no_argument, 0, '6'}, | 513 | {"use-ipv4", no_argument, 0, '4'}, |
| 401 | {"verbose", no_argument, 0, 'v'}, | 514 | {"use-ipv6", no_argument, 0, '6'}, |
| 402 | {"version", no_argument, 0, 'V'}, | 515 | {"verbose", no_argument, 0, 'v'}, |
| 403 | {"help", no_argument, 0, 'h'}, | 516 | {"version", no_argument, 0, 'V'}, |
| 404 | {"ssl", no_argument, 0, 'S'}, | 517 | {"help", no_argument, 0, 'h'}, |
| 405 | {"sni", required_argument, 0, SNI_OPTION}, | 518 | {"ssl", no_argument, 0, 'S'}, |
| 406 | {"certificate", required_argument, 0, 'D'}, | 519 | {"sni", required_argument, 0, SNI_OPTION}, |
| 407 | {0, 0, 0, 0}}; | 520 | {"certificate", required_argument, 0, 'D'}, |
| 408 | 521 | {"output-format", required_argument, 0, output_format_index}, | |
| 409 | if (argc < 2) | 522 | {0, 0, 0, 0}}; |
| 523 | |||
| 524 | if (argc < 2) { | ||
| 410 | usage4(_("No arguments found")); | 525 | usage4(_("No arguments found")); |
| 526 | } | ||
| 411 | 527 | ||
| 412 | /* backwards compatibility */ | 528 | /* backwards compatibility */ |
| 413 | for (int i = 1; i < argc; i++) { | 529 | for (int i = 1; i < argc; i++) { |
| 414 | if (strcmp("-to", argv[i]) == 0) | 530 | if (strcmp("-to", argv[i]) == 0) { |
| 415 | strcpy(argv[i], "-t"); | 531 | strcpy(argv[i], "-t"); |
| 416 | else if (strcmp("-wt", argv[i]) == 0) | 532 | } else if (strcmp("-wt", argv[i]) == 0) { |
| 417 | strcpy(argv[i], "-w"); | 533 | strcpy(argv[i], "-w"); |
| 418 | else if (strcmp("-ct", argv[i]) == 0) | 534 | } else if (strcmp("-ct", argv[i]) == 0) { |
| 419 | strcpy(argv[i], "-c"); | 535 | strcpy(argv[i], "-c"); |
| 536 | } | ||
| 420 | } | 537 | } |
| 421 | 538 | ||
| 422 | if (!is_option(argv[1])) { | 539 | if (!is_option(argv[1])) { |
| 423 | server_address = argv[1]; | 540 | config.server_address = argv[1]; |
| 424 | argv[1] = argv[0]; | 541 | argv[1] = argv[0]; |
| 425 | argv = &argv[1]; | 542 | argv = &argv[1]; |
| 426 | argc--; | 543 | argc--; |
| 427 | } | 544 | } |
| 428 | 545 | ||
| 429 | int option_char; | ||
| 430 | bool escape = false; | 546 | bool escape = false; |
| 547 | |||
| 431 | while (true) { | 548 | while (true) { |
| 432 | int option = 0; | 549 | int option = 0; |
| 433 | option_char = getopt_long(argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); | 550 | int option_index = |
| 551 | getopt_long(argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); | ||
| 434 | 552 | ||
| 435 | if (option_char == -1 || option_char == EOF || option_char == 1) | 553 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
| 436 | break; | 554 | break; |
| 555 | } | ||
| 437 | 556 | ||
| 438 | switch (option_char) { | 557 | switch (option_index) { |
| 439 | case '?': /* print short usage statement if args not parsable */ | 558 | case '?': /* print short usage statement if args not parsable */ |
| 440 | usage5(); | 559 | usage5(); |
| 441 | case 'h': /* help */ | 560 | case 'h': /* help */ |
| 442 | print_help(); | 561 | print_help(config.service); |
| 443 | exit(STATE_UNKNOWN); | 562 | exit(STATE_UNKNOWN); |
| 444 | case 'V': /* version */ | 563 | case 'V': /* version */ |
| 445 | print_revision(progname, NP_VERSION); | 564 | print_revision(progname, NP_VERSION); |
| 446 | exit(STATE_UNKNOWN); | 565 | exit(STATE_UNKNOWN); |
| 447 | case 'v': /* verbose mode */ | 566 | case 'v': /* verbose mode */ |
| 448 | flags |= FLAG_VERBOSE; | 567 | verbosity++; |
| 449 | match_flags |= NP_MATCH_VERBOSE; | 568 | config.match_flags |= NP_MATCH_VERBOSE; |
| 450 | break; | 569 | break; |
| 451 | case '4': | 570 | case '4': // Apparently unused TODO |
| 452 | address_family = AF_INET; | 571 | address_family = AF_INET; |
| 453 | break; | 572 | break; |
| 454 | case '6': | 573 | case '6': // Apparently unused TODO |
| 455 | #ifdef USE_IPV6 | 574 | #ifdef USE_IPV6 |
| 456 | address_family = AF_INET6; | 575 | address_family = AF_INET6; |
| 457 | #else | 576 | #else |
| @@ -459,163 +578,192 @@ static int process_arguments(int argc, char **argv) { | |||
| 459 | #endif | 578 | #endif |
| 460 | break; | 579 | break; |
| 461 | case 'H': /* hostname */ | 580 | case 'H': /* hostname */ |
| 462 | host_specified = true; | 581 | config.host_specified = true; |
| 463 | server_address = optarg; | 582 | config.server_address = optarg; |
| 464 | break; | 583 | break; |
| 465 | case 'c': /* critical */ | 584 | case 'c': /* critical */ |
| 466 | critical_time = strtod(optarg, NULL); | 585 | config.critical_time = strtod(optarg, NULL); |
| 467 | flags |= FLAG_TIME_CRIT; | 586 | config.critical_time_set = true; |
| 468 | break; | 587 | break; |
| 469 | case 'j': /* hide output */ | 588 | case 'j': /* hide output */ |
| 470 | flags |= FLAG_HIDE_OUTPUT; | 589 | config.hide_output = true; |
| 471 | break; | 590 | break; |
| 472 | case 'w': /* warning */ | 591 | case 'w': /* warning */ |
| 473 | warning_time = strtod(optarg, NULL); | 592 | config.warning_time = strtod(optarg, NULL); |
| 474 | flags |= FLAG_TIME_WARN; | 593 | config.warning_time_set = true; |
| 475 | break; | ||
| 476 | case 'C': | ||
| 477 | crit_codes = realloc(crit_codes, ++crit_codes_count); | ||
| 478 | crit_codes[crit_codes_count - 1] = optarg; | ||
| 479 | break; | ||
| 480 | case 'W': | ||
| 481 | warn_codes = realloc(warn_codes, ++warn_codes_count); | ||
| 482 | warn_codes[warn_codes_count - 1] = optarg; | ||
| 483 | break; | 594 | break; |
| 484 | case 't': /* timeout */ | 595 | case 't': /* timeout */ |
| 485 | if (!is_intpos(optarg)) | 596 | if (!is_intpos(optarg)) { |
| 486 | usage4(_("Timeout interval must be a positive integer")); | 597 | usage4(_("Timeout interval must be a positive integer")); |
| 487 | else | 598 | } else { |
| 488 | socket_timeout = atoi(optarg); | 599 | socket_timeout = atoi(optarg); |
| 600 | } | ||
| 489 | break; | 601 | break; |
| 490 | case 'p': /* port */ | 602 | case 'p': /* port */ |
| 491 | if (!is_intpos(optarg)) | 603 | if (!is_intpos(optarg)) { |
| 492 | usage4(_("Port must be a positive integer")); | 604 | usage4(_("Port must be a positive integer")); |
| 493 | else | 605 | } else { |
| 494 | server_port = atoi(optarg); | 606 | config.server_port = atoi(optarg); |
| 607 | } | ||
| 495 | break; | 608 | break; |
| 496 | case 'E': | 609 | case 'E': |
| 497 | escape = true; | 610 | escape = true; |
| 498 | break; | 611 | break; |
| 499 | case 's': | 612 | case 's': |
| 500 | if (escape) | 613 | if (escape) { |
| 501 | server_send = np_escaped_string(optarg); | 614 | config.send = np_escaped_string(optarg); |
| 502 | else | 615 | } else { |
| 503 | xasprintf(&server_send, "%s", optarg); | 616 | xasprintf(&config.send, "%s", optarg); |
| 617 | } | ||
| 504 | break; | 618 | break; |
| 505 | case 'e': /* expect string (may be repeated) */ | 619 | case 'e': /* expect string (may be repeated) */ |
| 506 | match_flags &= ~NP_MATCH_EXACT; | 620 | config.match_flags &= ~NP_MATCH_EXACT; |
| 507 | if (server_expect_count == 0) | 621 | if (config.server_expect_count == 0) { |
| 508 | server_expect = malloc(sizeof(char *) * (++server_expect_count)); | 622 | config.server_expect = malloc(sizeof(char *) * (++config.server_expect_count)); |
| 509 | else | 623 | } else { |
| 510 | server_expect = realloc(server_expect, sizeof(char *) * (++server_expect_count)); | 624 | config.server_expect = |
| 511 | server_expect[server_expect_count - 1] = optarg; | 625 | realloc(config.server_expect, sizeof(char *) * (++config.server_expect_count)); |
| 626 | } | ||
| 627 | |||
| 628 | if (config.server_expect == NULL) { | ||
| 629 | die(STATE_UNKNOWN, _("Allocation failed")); | ||
| 630 | } | ||
| 631 | config.server_expect[config.server_expect_count - 1] = optarg; | ||
| 512 | break; | 632 | break; |
| 513 | case 'm': | 633 | case 'm': |
| 514 | if (!is_intpos(optarg)) | 634 | if (!is_intpos(optarg)) { |
| 515 | usage4(_("Maxbytes must be a positive integer")); | 635 | usage4(_("Maxbytes must be a positive integer")); |
| 516 | else | 636 | } else { |
| 517 | maxbytes = strtol(optarg, NULL, 0); | 637 | config.maxbytes = strtol(optarg, NULL, 0); |
| 638 | } | ||
| 518 | break; | 639 | break; |
| 519 | case 'q': | 640 | case 'q': |
| 520 | if (escape) | 641 | if (escape) { |
| 521 | server_quit = np_escaped_string(optarg); | 642 | config.quit = np_escaped_string(optarg); |
| 522 | else | 643 | } else { |
| 523 | xasprintf(&server_quit, "%s\r\n", optarg); | 644 | xasprintf(&config.quit, "%s\r\n", optarg); |
| 645 | } | ||
| 524 | break; | 646 | break; |
| 525 | case 'r': | 647 | case 'r': |
| 526 | if (!strncmp(optarg, "ok", 2)) | 648 | if (!strncmp(optarg, "ok", 2)) { |
| 527 | econn_refuse_state = STATE_OK; | 649 | config.econn_refuse_state = STATE_OK; |
| 528 | else if (!strncmp(optarg, "warn", 4)) | 650 | } else if (!strncmp(optarg, "warn", 4)) { |
| 529 | econn_refuse_state = STATE_WARNING; | 651 | config.econn_refuse_state = STATE_WARNING; |
| 530 | else if (!strncmp(optarg, "crit", 4)) | 652 | } else if (!strncmp(optarg, "crit", 4)) { |
| 531 | econn_refuse_state = STATE_CRITICAL; | 653 | config.econn_refuse_state = STATE_CRITICAL; |
| 532 | else | 654 | } else { |
| 533 | usage4(_("Refuse must be one of ok, warn, crit")); | 655 | usage4(_("Refuse must be one of ok, warn, crit")); |
| 656 | } | ||
| 534 | break; | 657 | break; |
| 535 | case 'M': | 658 | case 'M': |
| 536 | if (!strncmp(optarg, "ok", 2)) | 659 | if (!strncmp(optarg, "ok", 2)) { |
| 537 | expect_mismatch_state = STATE_OK; | 660 | config.expect_mismatch_state = STATE_OK; |
| 538 | else if (!strncmp(optarg, "warn", 4)) | 661 | } else if (!strncmp(optarg, "warn", 4)) { |
| 539 | expect_mismatch_state = STATE_WARNING; | 662 | config.expect_mismatch_state = STATE_WARNING; |
| 540 | else if (!strncmp(optarg, "crit", 4)) | 663 | } else if (!strncmp(optarg, "crit", 4)) { |
| 541 | expect_mismatch_state = STATE_CRITICAL; | 664 | config.expect_mismatch_state = STATE_CRITICAL; |
| 542 | else | 665 | } else { |
| 543 | usage4(_("Mismatch must be one of ok, warn, crit")); | 666 | usage4(_("Mismatch must be one of ok, warn, crit")); |
| 667 | } | ||
| 544 | break; | 668 | break; |
| 545 | case 'd': | 669 | case 'd': |
| 546 | if (is_intpos(optarg)) | 670 | if (is_intpos(optarg)) { |
| 547 | delay = atoi(optarg); | 671 | config.delay = atoi(optarg); |
| 548 | else | 672 | } else { |
| 549 | usage4(_("Delay must be a positive integer")); | 673 | usage4(_("Delay must be a positive integer")); |
| 674 | } | ||
| 550 | break; | 675 | break; |
| 551 | case 'D': { /* Check SSL cert validity - days 'til certificate expiration */ | 676 | case 'D': /* Check SSL cert validity - days 'til certificate expiration */ |
| 552 | #ifdef HAVE_SSL | 677 | #ifdef HAVE_SSL |
| 553 | # ifdef USE_OPENSSL /* XXX */ | 678 | # ifdef USE_OPENSSL /* XXX */ |
| 679 | { | ||
| 554 | char *temp; | 680 | char *temp; |
| 555 | if ((temp = strchr(optarg, ',')) != NULL) { | 681 | if ((temp = strchr(optarg, ',')) != NULL) { |
| 556 | *temp = '\0'; | 682 | *temp = '\0'; |
| 557 | if (!is_intnonneg(optarg)) | 683 | if (!is_intnonneg(optarg)) { |
| 558 | usage2(_("Invalid certificate expiration period"), optarg); | 684 | usage2(_("Invalid certificate expiration period"), optarg); |
| 559 | days_till_exp_warn = atoi(optarg); | 685 | } |
| 686 | config.days_till_exp_warn = atoi(optarg); | ||
| 560 | *temp = ','; | 687 | *temp = ','; |
| 561 | temp++; | 688 | temp++; |
| 562 | if (!is_intnonneg(temp)) | 689 | if (!is_intnonneg(temp)) { |
| 563 | usage2(_("Invalid certificate expiration period"), temp); | 690 | usage2(_("Invalid certificate expiration period"), temp); |
| 564 | days_till_exp_crit = atoi(temp); | 691 | } |
| 692 | config.days_till_exp_crit = atoi(temp); | ||
| 565 | } else { | 693 | } else { |
| 566 | days_till_exp_crit = 0; | 694 | config.days_till_exp_crit = 0; |
| 567 | if (!is_intnonneg(optarg)) | 695 | if (!is_intnonneg(optarg)) { |
| 568 | usage2(_("Invalid certificate expiration period"), optarg); | 696 | usage2(_("Invalid certificate expiration period"), optarg); |
| 569 | days_till_exp_warn = atoi(optarg); | 697 | } |
| 698 | config.days_till_exp_warn = atoi(optarg); | ||
| 570 | } | 699 | } |
| 571 | check_cert = true; | 700 | config.check_cert = true; |
| 572 | flags |= FLAG_SSL; | 701 | config.use_tls = true; |
| 573 | } break; | 702 | } break; |
| 574 | # endif /* USE_OPENSSL */ | 703 | # endif /* USE_OPENSSL */ |
| 575 | #endif | 704 | #endif |
| 576 | /* fallthrough if we don't have ssl */ | 705 | /* fallthrough if we don't have ssl */ |
| 577 | case 'S': | 706 | case 'S': |
| 578 | #ifdef HAVE_SSL | 707 | #ifdef HAVE_SSL |
| 579 | flags |= FLAG_SSL; | 708 | config.use_tls = true; |
| 580 | #else | 709 | #else |
| 581 | die(STATE_UNKNOWN, _("Invalid option - SSL is not available")); | 710 | die(STATE_UNKNOWN, _("Invalid option - SSL is not available")); |
| 582 | #endif | 711 | #endif |
| 583 | break; | 712 | break; |
| 584 | case SNI_OPTION: | 713 | case SNI_OPTION: |
| 585 | #ifdef HAVE_SSL | 714 | #ifdef HAVE_SSL |
| 586 | flags |= FLAG_SSL; | 715 | config.use_tls = true; |
| 587 | sni_specified = true; | 716 | config.sni_specified = true; |
| 588 | sni = optarg; | 717 | config.sni = optarg; |
| 589 | #else | 718 | #else |
| 590 | die(STATE_UNKNOWN, _("Invalid option - SSL is not available")); | 719 | die(STATE_UNKNOWN, _("Invalid option - SSL is not available")); |
| 591 | #endif | 720 | #endif |
| 592 | break; | 721 | break; |
| 593 | case 'A': | 722 | case 'A': |
| 594 | match_flags |= NP_MATCH_ALL; | 723 | config.match_flags |= NP_MATCH_ALL; |
| 724 | break; | ||
| 725 | case output_format_index: { | ||
| 726 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 727 | if (!parser.parsing_success) { | ||
| 728 | // TODO List all available formats here, maybe add anothoer usage function | ||
| 729 | printf("Invalid output format: %s\n", optarg); | ||
| 730 | exit(STATE_UNKNOWN); | ||
| 731 | } | ||
| 732 | |||
| 733 | config.output_format_set = true; | ||
| 734 | config.output_format = parser.output_format; | ||
| 595 | break; | 735 | break; |
| 596 | } | 736 | } |
| 737 | } | ||
| 597 | } | 738 | } |
| 598 | 739 | ||
| 599 | option_char = optind; | 740 | int index = optind; |
| 600 | if (!host_specified && option_char < argc) | 741 | if (!config.host_specified && index < argc) { |
| 601 | server_address = strdup(argv[option_char++]); | 742 | config.server_address = strdup(argv[index++]); |
| 743 | } | ||
| 602 | 744 | ||
| 603 | if (server_address == NULL) | 745 | if (config.server_address == NULL) { |
| 604 | usage4(_("You must provide a server address")); | 746 | usage4(_("You must provide a server address")); |
| 605 | else if (server_address[0] != '/' && !is_host(server_address)) | 747 | } else if (config.server_address[0] != '/' && !is_host(config.server_address)) { |
| 606 | die(STATE_CRITICAL, "%s %s - %s: %s\n", SERVICE, state_text(STATE_CRITICAL), _("Invalid hostname, address or socket"), | 748 | die(STATE_CRITICAL, "%s %s - %s: %s\n", config.service, state_text(STATE_CRITICAL), |
| 607 | server_address); | 749 | _("Invalid hostname, address or socket"), config.server_address); |
| 750 | } | ||
| 608 | 751 | ||
| 609 | return OK; | 752 | check_tcp_config_wrapper result = { |
| 753 | .config = config, | ||
| 754 | .errorcode = OK, | ||
| 755 | }; | ||
| 756 | return result; | ||
| 610 | } | 757 | } |
| 611 | 758 | ||
| 612 | void print_help(void) { | 759 | void print_help(const char *service) { |
| 613 | print_revision(progname, NP_VERSION); | 760 | print_revision(progname, NP_VERSION); |
| 614 | 761 | ||
| 615 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | 762 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
| 616 | printf(COPYRIGHT, copyright, email); | 763 | printf(COPYRIGHT, copyright, email); |
| 617 | 764 | ||
| 618 | printf(_("This plugin tests %s connections with the specified host (or unix socket).\n\n"), SERVICE); | 765 | printf(_("This plugin tests %s connections with the specified host (or unix socket).\n\n"), |
| 766 | service); | ||
| 619 | 767 | ||
| 620 | print_usage(); | 768 | print_usage(); |
| 621 | 769 | ||
| @@ -627,7 +775,8 @@ void print_help(void) { | |||
| 627 | printf(UT_IPv46); | 775 | printf(UT_IPv46); |
| 628 | 776 | ||
| 629 | printf(" %s\n", "-E, --escape"); | 777 | printf(" %s\n", "-E, --escape"); |
| 630 | printf(" %s\n", _("Can use \\n, \\r, \\t or \\\\ in send or quit string. Must come before send or quit option")); | 778 | printf(" %s\n", _("Can use \\n, \\r, \\t or \\\\ in send or quit string. Must come before " |
| 779 | "send or quit option")); | ||
| 631 | printf(" %s\n", _("Default: nothing added to send, \\r\\n added to end of quit")); | 780 | printf(" %s\n", _("Default: nothing added to send, \\r\\n added to end of quit")); |
| 632 | printf(" %s\n", "-s, --send=STRING"); | 781 | printf(" %s\n", "-s, --send=STRING"); |
| 633 | printf(" %s\n", _("String to send to the server")); | 782 | printf(" %s\n", _("String to send to the server")); |
| @@ -640,7 +789,8 @@ void print_help(void) { | |||
| 640 | printf(" %s\n", "-r, --refuse=ok|warn|crit"); | 789 | printf(" %s\n", "-r, --refuse=ok|warn|crit"); |
| 641 | printf(" %s\n", _("Accept TCP refusals with states ok, warn, crit (default: crit)")); | 790 | printf(" %s\n", _("Accept TCP refusals with states ok, warn, crit (default: crit)")); |
| 642 | printf(" %s\n", "-M, --mismatch=ok|warn|crit"); | 791 | printf(" %s\n", "-M, --mismatch=ok|warn|crit"); |
| 643 | printf(" %s\n", _("Accept expected string mismatches with states ok, warn, crit (default: warn)")); | 792 | printf(" %s\n", |
| 793 | _("Accept expected string mismatches with states ok, warn, crit (default: warn)")); | ||
| 644 | printf(" %s\n", "-j, --jail"); | 794 | printf(" %s\n", "-j, --jail"); |
| 645 | printf(" %s\n", _("Hide output from TCP socket")); | 795 | printf(" %s\n", _("Hide output from TCP socket")); |
| 646 | printf(" %s\n", "-m, --maxbytes=INTEGER"); | 796 | printf(" %s\n", "-m, --maxbytes=INTEGER"); |
| @@ -662,6 +812,7 @@ void print_help(void) { | |||
| 662 | 812 | ||
| 663 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 813 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 664 | 814 | ||
| 815 | printf(UT_OUTPUT_FORMAT); | ||
| 665 | printf(UT_VERBOSE); | 816 | printf(UT_VERBOSE); |
| 666 | 817 | ||
| 667 | printf(UT_SUPPORT); | 818 | printf(UT_SUPPORT); |
| @@ -669,7 +820,8 @@ void print_help(void) { | |||
| 669 | 820 | ||
| 670 | void print_usage(void) { | 821 | void print_usage(void) { |
| 671 | printf("%s\n", _("Usage:")); | 822 | printf("%s\n", _("Usage:")); |
| 672 | printf("%s -H host -p port [-w <warning time>] [-c <critical time>] [-s <send string>]\n", progname); | 823 | printf("%s -H host -p port [-w <warning time>] [-c <critical time>] [-s <send string>]\n", |
| 824 | progname); | ||
| 673 | printf("[-e <expect string>] [-q <quit string>][-m <maximum bytes>] [-d <delay>]\n"); | 825 | printf("[-e <expect string>] [-q <quit string>][-m <maximum bytes>] [-d <delay>]\n"); |
| 674 | printf("[-t <timeout seconds>] [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n"); | 826 | printf("[-t <timeout seconds>] [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n"); |
| 675 | printf("[-D <warn days cert expire>[,<crit days cert expire>]] [-S <use SSL>] [-E]\n"); | 827 | printf("[-D <warn days cert expire>[,<crit days cert expire>]] [-S <use SSL>] [-E]\n"); |
