diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 16:00:41 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 16:00:41 +0100 |
commit | 42ef1fa2fa37d7d684b624a4559c479e763457d0 (patch) | |
tree | 0afd3974d8fdd236e16ca6d6c7d6ba2a655606ad | |
parent | 78258c4cd0dff4197e0642e6eb39a15c97c88807 (diff) | |
download | monitoring-plugins-42ef1fa2fa37d7d684b624a4559c479e763457d0.tar.gz |
check_ssh: linter + style fixes
-rw-r--r-- | plugins/check_ssh.c | 76 |
1 files changed, 34 insertions, 42 deletions
diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index b12cda1d..42a88cf9 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c | |||
@@ -49,7 +49,7 @@ static char *remote_version = NULL; | |||
49 | static char *remote_protocol = NULL; | 49 | static char *remote_protocol = NULL; |
50 | static bool verbose = false; | 50 | static bool verbose = false; |
51 | 51 | ||
52 | static int process_arguments(int, char **); | 52 | static int process_arguments(int /*argc*/, char ** /*argv*/); |
53 | static int validate_arguments(void); | 53 | static int validate_arguments(void); |
54 | static void print_help(void); | 54 | static void print_help(void); |
55 | void print_usage(void); | 55 | void print_usage(void); |
@@ -57,8 +57,6 @@ void print_usage(void); | |||
57 | static int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol); | 57 | static int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol); |
58 | 58 | ||
59 | int main(int argc, char **argv) { | 59 | int main(int argc, char **argv) { |
60 | int result = STATE_UNKNOWN; | ||
61 | |||
62 | setlocale(LC_ALL, ""); | 60 | setlocale(LC_ALL, ""); |
63 | bindtextdomain(PACKAGE, LOCALEDIR); | 61 | bindtextdomain(PACKAGE, LOCALEDIR); |
64 | textdomain(PACKAGE); | 62 | textdomain(PACKAGE); |
@@ -75,7 +73,7 @@ int main(int argc, char **argv) { | |||
75 | alarm(socket_timeout); | 73 | alarm(socket_timeout); |
76 | 74 | ||
77 | /* ssh_connect exits if error is found */ | 75 | /* ssh_connect exits if error is found */ |
78 | result = ssh_connect(server_name, port, remote_version, remote_protocol); | 76 | int result = ssh_connect(server_name, port, remote_version, remote_protocol); |
79 | 77 | ||
80 | alarm(0); | 78 | alarm(0); |
81 | 79 | ||
@@ -84,9 +82,6 @@ int main(int argc, char **argv) { | |||
84 | 82 | ||
85 | /* process command-line arguments */ | 83 | /* process command-line arguments */ |
86 | int process_arguments(int argc, char **argv) { | 84 | int process_arguments(int argc, char **argv) { |
87 | int c; | ||
88 | |||
89 | int option = 0; | ||
90 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, | 85 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, |
91 | {"version", no_argument, 0, 'V'}, | 86 | {"version", no_argument, 0, 'V'}, |
92 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ | 87 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ |
@@ -103,17 +98,19 @@ int process_arguments(int argc, char **argv) { | |||
103 | if (argc < 2) | 98 | if (argc < 2) |
104 | return ERROR; | 99 | return ERROR; |
105 | 100 | ||
106 | for (c = 1; c < argc; c++) | 101 | for (int i = 1; i < argc; i++) |
107 | if (strcmp("-to", argv[c]) == 0) | 102 | if (strcmp("-to", argv[i]) == 0) |
108 | strcpy(argv[c], "-t"); | 103 | strcpy(argv[i], "-t"); |
109 | 104 | ||
110 | while (1) { | 105 | int option_char; |
111 | c = getopt_long(argc, argv, "+Vhv46t:r:H:p:P:", longopts, &option); | 106 | while (true) { |
107 | int option = 0; | ||
108 | option_char = getopt_long(argc, argv, "+Vhv46t:r:H:p:P:", longopts, &option); | ||
112 | 109 | ||
113 | if (c == -1 || c == EOF) | 110 | if (option_char == -1 || option_char == EOF) |
114 | break; | 111 | break; |
115 | 112 | ||
116 | switch (c) { | 113 | switch (option_char) { |
117 | case '?': /* help */ | 114 | case '?': /* help */ |
118 | usage5(); | 115 | usage5(); |
119 | case 'V': /* version */ | 116 | case 'V': /* version */ |
@@ -161,16 +158,16 @@ int process_arguments(int argc, char **argv) { | |||
161 | } | 158 | } |
162 | } | 159 | } |
163 | 160 | ||
164 | c = optind; | 161 | option_char = optind; |
165 | if (server_name == NULL && c < argc) { | 162 | if (server_name == NULL && option_char < argc) { |
166 | if (is_host(argv[c])) { | 163 | if (is_host(argv[option_char])) { |
167 | server_name = argv[c++]; | 164 | server_name = argv[option_char++]; |
168 | } | 165 | } |
169 | } | 166 | } |
170 | 167 | ||
171 | if (port == -1 && c < argc) { | 168 | if (port == -1 && option_char < argc) { |
172 | if (is_intpos(argv[c])) { | 169 | if (is_intpos(argv[option_char])) { |
173 | port = atoi(argv[c++]); | 170 | port = atoi(argv[option_char++]); |
174 | } else { | 171 | } else { |
175 | print_usage(); | 172 | print_usage(); |
176 | exit(STATE_UNKNOWN); | 173 | exit(STATE_UNKNOWN); |
@@ -195,34 +192,27 @@ int validate_arguments(void) { | |||
195 | *-----------------------------------------------------------------------*/ | 192 | *-----------------------------------------------------------------------*/ |
196 | 193 | ||
197 | int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol) { | 194 | int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol) { |
198 | int sd; | ||
199 | int result; | ||
200 | int len = 0; | ||
201 | ssize_t recv_ret = 0; | ||
202 | char *version_control_string = NULL; | ||
203 | char *buffer = NULL; | ||
204 | char *ssh_proto = NULL; | ||
205 | char *ssh_server = NULL; | ||
206 | static char *rev_no = VERSION; | ||
207 | struct timeval tv; | 195 | struct timeval tv; |
208 | |||
209 | gettimeofday(&tv, NULL); | 196 | gettimeofday(&tv, NULL); |
210 | 197 | ||
211 | result = my_tcp_connect(haddr, hport, &sd); | 198 | int socket; |
199 | int result = my_tcp_connect(haddr, hport, &socket); | ||
212 | 200 | ||
213 | if (result != STATE_OK) | 201 | if (result != STATE_OK) |
214 | return result; | 202 | return result; |
215 | 203 | ||
216 | char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); | 204 | char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); |
217 | 205 | char *buffer = NULL; | |
206 | ssize_t recv_ret = 0; | ||
207 | char *version_control_string = NULL; | ||
218 | ssize_t byte_offset = 0; | 208 | ssize_t byte_offset = 0; |
219 | 209 | while ((version_control_string == NULL) && (recv_ret = recv(socket, output + byte_offset, BUFF_SZ - byte_offset, 0) > 0)) { | |
220 | while ((version_control_string == NULL) && (recv_ret = recv(sd, output + byte_offset, BUFF_SZ - byte_offset, 0) > 0)) { | ||
221 | 210 | ||
222 | if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ | 211 | if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ |
223 | byte_offset = 0; | 212 | byte_offset = 0; |
224 | 213 | ||
225 | char *index = NULL; | 214 | char *index = NULL; |
215 | int len = 0; | ||
226 | while ((index = strchr(output + byte_offset, '\n')) != NULL) { | 216 | while ((index = strchr(output + byte_offset, '\n')) != NULL) { |
227 | /*Partition the buffer so that this line is a separate string, | 217 | /*Partition the buffer so that this line is a separate string, |
228 | * by replacing the newline with NUL*/ | 218 | * by replacing the newline with NUL*/ |
@@ -241,7 +231,7 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto | |||
241 | 231 | ||
242 | if (version_control_string == NULL) { | 232 | if (version_control_string == NULL) { |
243 | /* move unconsumed data to beginning of buffer, null rest */ | 233 | /* move unconsumed data to beginning of buffer, null rest */ |
244 | memmove((void *)output, (void *)output + byte_offset + 1, BUFF_SZ - len + 1); | 234 | memmove((void *)output, (void *)(output + byte_offset + 1), BUFF_SZ - len + 1); |
245 | memset(output + byte_offset + 1, 0, BUFF_SZ - byte_offset + 1); | 235 | memset(output + byte_offset + 1, 0, BUFF_SZ - byte_offset + 1); |
246 | 236 | ||
247 | /*start reading from end of current line chunk on next recv*/ | 237 | /*start reading from end of current line chunk on next recv*/ |
@@ -271,7 +261,8 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto | |||
271 | strip(version_control_string); | 261 | strip(version_control_string); |
272 | if (verbose) | 262 | if (verbose) |
273 | printf("%s\n", version_control_string); | 263 | printf("%s\n", version_control_string); |
274 | ssh_proto = version_control_string + 4; | 264 | |
265 | char *ssh_proto = version_control_string + 4; | ||
275 | 266 | ||
276 | /* | 267 | /* |
277 | * We assume the protoversion is of the form Major.Minor, although | 268 | * We assume the protoversion is of the form Major.Minor, although |
@@ -289,7 +280,7 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto | |||
289 | * "1.x" (e.g., "1.5" or "1.3")." | 280 | * "1.x" (e.g., "1.5" or "1.3")." |
290 | * - RFC 4253:5 | 281 | * - RFC 4253:5 |
291 | */ | 282 | */ |
292 | ssh_server = ssh_proto + strspn(ssh_proto, "0123456789.") + 1; /* (+1 for the '-' separating protoversion from softwareversion) */ | 283 | char *ssh_server = ssh_proto + strspn(ssh_proto, "0123456789.") + 1; /* (+1 for the '-' separating protoversion from softwareversion) */ |
293 | 284 | ||
294 | /* If there's a space in the version string, whatever's after the space is a comment | 285 | /* If there's a space in the version string, whatever's after the space is a comment |
295 | * (which is NOT part of the server name/version)*/ | 286 | * (which is NOT part of the server name/version)*/ |
@@ -303,14 +294,15 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto | |||
303 | } | 294 | } |
304 | ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; | 295 | ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; |
305 | 296 | ||
297 | static char *rev_no = VERSION; | ||
306 | xasprintf(&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no); | 298 | xasprintf(&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no); |
307 | send(sd, buffer, strlen(buffer), MSG_DONTWAIT); | 299 | send(socket, buffer, strlen(buffer), MSG_DONTWAIT); |
308 | if (verbose) | 300 | if (verbose) |
309 | printf("%s\n", buffer); | 301 | printf("%s\n", buffer); |
310 | 302 | ||
311 | if (remote_version && strcmp(remote_version, ssh_server)) { | 303 | if (remote_version && strcmp(remote_version, ssh_server)) { |
312 | printf(_("SSH CRITICAL - %s (protocol %s) version mismatch, expected '%s'\n"), ssh_server, ssh_proto, remote_version); | 304 | printf(_("SSH CRITICAL - %s (protocol %s) version mismatch, expected '%s'\n"), ssh_server, ssh_proto, remote_version); |
313 | close(sd); | 305 | close(socket); |
314 | exit(STATE_CRITICAL); | 306 | exit(STATE_CRITICAL); |
315 | } | 307 | } |
316 | 308 | ||
@@ -318,13 +310,13 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto | |||
318 | if (remote_protocol && strcmp(remote_protocol, ssh_proto)) { | 310 | if (remote_protocol && strcmp(remote_protocol, ssh_proto)) { |
319 | printf(_("SSH CRITICAL - %s (protocol %s) protocol version mismatch, expected '%s' | %s\n"), ssh_server, ssh_proto, remote_protocol, | 311 | printf(_("SSH CRITICAL - %s (protocol %s) protocol version mismatch, expected '%s' | %s\n"), ssh_server, ssh_proto, remote_protocol, |
320 | fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); | 312 | fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); |
321 | close(sd); | 313 | close(socket); |
322 | exit(STATE_CRITICAL); | 314 | exit(STATE_CRITICAL); |
323 | } | 315 | } |
324 | 316 | ||
325 | printf(_("SSH OK - %s (protocol %s) | %s\n"), ssh_server, ssh_proto, | 317 | printf(_("SSH OK - %s (protocol %s) | %s\n"), ssh_server, ssh_proto, |
326 | fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); | 318 | fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); |
327 | close(sd); | 319 | close(socket); |
328 | exit(STATE_OK); | 320 | exit(STATE_OK); |
329 | } | 321 | } |
330 | 322 | ||