diff options
Diffstat (limited to 'plugins/check_ssh.c')
-rw-r--r-- | plugins/check_ssh.c | 522 |
1 files changed, 279 insertions, 243 deletions
diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 34ef37b7..9d0d7cde 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c | |||
@@ -1,144 +1,158 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_ssh plugin | 3 | * Monitoring check_ssh plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2000-2007 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_ssh plugin | 10 | * This file contains the check_ssh plugin |
11 | * | 11 | * |
12 | * Try to connect to an SSH server at specified server and port | 12 | * Try to connect to an SSH server at specified server and port |
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 | #include "output.h" | ||
32 | #include "perfdata.h" | ||
33 | #include "states.h" | ||
31 | const char *progname = "check_ssh"; | 34 | const char *progname = "check_ssh"; |
32 | const char *copyright = "2000-2007"; | 35 | const char *copyright = "2000-2024"; |
33 | const char *email = "devel@monitoring-plugins.org"; | 36 | const char *email = "devel@monitoring-plugins.org"; |
34 | 37 | ||
35 | #include "./common.h" | 38 | #include "./common.h" |
36 | #include "./netutils.h" | 39 | #include "./netutils.h" |
37 | #include "utils.h" | 40 | #include "utils.h" |
41 | #include "./check_ssh.d/config.h" | ||
38 | 42 | ||
39 | #ifndef MSG_DONTWAIT | 43 | #ifndef MSG_DONTWAIT |
40 | #define MSG_DONTWAIT 0 | 44 | # define MSG_DONTWAIT 0 |
41 | #endif | 45 | #endif |
42 | 46 | ||
43 | #define SSH_DFL_PORT 22 | 47 | #define BUFF_SZ 256 |
44 | #define BUFF_SZ 256 | ||
45 | |||
46 | int port = -1; | ||
47 | char *server_name = NULL; | ||
48 | char *remote_version = NULL; | ||
49 | char *remote_protocol = NULL; | ||
50 | bool verbose = false; | ||
51 | 48 | ||
52 | int process_arguments (int, char **); | 49 | static bool verbose = false; |
53 | int validate_arguments (void); | ||
54 | void print_help (void); | ||
55 | void print_usage (void); | ||
56 | 50 | ||
57 | int ssh_connect (char *haddr, int hport, char *remote_version, char *remote_protocol); | 51 | typedef struct process_arguments_wrapper { |
52 | int errorcode; | ||
53 | check_ssh_config config; | ||
54 | } process_arguments_wrapper; | ||
58 | 55 | ||
56 | static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
57 | static void print_help(void); | ||
58 | void print_usage(void); | ||
59 | 59 | ||
60 | int | 60 | static int ssh_connect(mp_check *overall, char *haddr, int hport, char *remote_version, char *remote_protocol); |
61 | main (int argc, char **argv) | ||
62 | { | ||
63 | int result = STATE_UNKNOWN; | ||
64 | 61 | ||
65 | setlocale (LC_ALL, ""); | 62 | int main(int argc, char **argv) { |
66 | bindtextdomain (PACKAGE, LOCALEDIR); | 63 | setlocale(LC_ALL, ""); |
67 | textdomain (PACKAGE); | 64 | bindtextdomain(PACKAGE, LOCALEDIR); |
65 | textdomain(PACKAGE); | ||
68 | 66 | ||
69 | /* Parse extra opts if any */ | 67 | /* Parse extra opts if any */ |
70 | argv=np_extra_opts (&argc, argv, progname); | 68 | argv = np_extra_opts(&argc, argv, progname); |
71 | 69 | ||
72 | if (process_arguments (argc, argv) == ERROR) | 70 | process_arguments_wrapper tmp_config = process_arguments(argc, argv); |
73 | usage4 (_("Could not parse arguments")); | ||
74 | 71 | ||
75 | /* initialize alarm signal handling */ | 72 | if (tmp_config.errorcode == ERROR) { |
76 | signal (SIGALRM, socket_timeout_alarm_handler); | 73 | usage4(_("Could not parse arguments")); |
74 | } | ||
75 | |||
76 | check_ssh_config config = tmp_config.config; | ||
77 | |||
78 | mp_check overall = mp_check_init(); | ||
79 | if (config.output_format_is_set) { | ||
80 | mp_set_format(config.output_format); | ||
81 | } | ||
77 | 82 | ||
78 | alarm (socket_timeout); | 83 | /* initialize alarm signal handling */ |
84 | signal(SIGALRM, socket_timeout_alarm_handler); | ||
85 | alarm(socket_timeout); | ||
79 | 86 | ||
80 | /* ssh_connect exits if error is found */ | 87 | /* ssh_connect exits if error is found */ |
81 | result = ssh_connect (server_name, port, remote_version, remote_protocol); | 88 | ssh_connect(&overall, config.server_name, config.port, config.remote_version, config.remote_protocol); |
82 | 89 | ||
83 | alarm (0); | 90 | alarm(0); |
84 | 91 | ||
85 | return (result); | 92 | mp_exit(overall); |
86 | } | 93 | } |
87 | 94 | ||
88 | 95 | #define output_format_index CHAR_MAX + 1 | |
89 | 96 | ||
90 | /* process command-line arguments */ | 97 | /* process command-line arguments */ |
91 | int | 98 | process_arguments_wrapper process_arguments(int argc, char **argv) { |
92 | process_arguments (int argc, char **argv) | 99 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, |
93 | { | 100 | {"version", no_argument, 0, 'V'}, |
94 | int c; | 101 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ |
95 | 102 | {"hostname", required_argument, 0, 'H'}, | |
96 | int option = 0; | 103 | {"port", required_argument, 0, 'p'}, |
97 | static struct option longopts[] = { | 104 | {"use-ipv4", no_argument, 0, '4'}, |
98 | {"help", no_argument, 0, 'h'}, | 105 | {"use-ipv6", no_argument, 0, '6'}, |
99 | {"version", no_argument, 0, 'V'}, | 106 | {"timeout", required_argument, 0, 't'}, |
100 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ | 107 | {"verbose", no_argument, 0, 'v'}, |
101 | {"hostname", required_argument, 0, 'H'}, | 108 | {"remote-version", required_argument, 0, 'r'}, |
102 | {"port", required_argument, 0, 'p'}, | 109 | {"remote-protocol", required_argument, 0, 'P'}, |
103 | {"use-ipv4", no_argument, 0, '4'}, | 110 | {"output-format", required_argument, 0, output_format_index}, |
104 | {"use-ipv6", no_argument, 0, '6'}, | 111 | {0, 0, 0, 0}}; |
105 | {"timeout", required_argument, 0, 't'}, | 112 | |
106 | {"verbose", no_argument, 0, 'v'}, | 113 | process_arguments_wrapper result = { |
107 | {"remote-version", required_argument, 0, 'r'}, | 114 | .config = check_ssh_config_init(), |
108 | {"remote-protocol", required_argument, 0, 'P'}, | 115 | .errorcode = OK, |
109 | {0, 0, 0, 0} | ||
110 | }; | 116 | }; |
111 | 117 | ||
112 | if (argc < 2) | 118 | if (argc < 2) { |
113 | return ERROR; | 119 | result.errorcode = ERROR; |
120 | return result; | ||
121 | } | ||
114 | 122 | ||
115 | for (c = 1; c < argc; c++) | 123 | for (int i = 1; i < argc; i++) { |
116 | if (strcmp ("-to", argv[c]) == 0) | 124 | if (strcmp("-to", argv[i]) == 0) { |
117 | strcpy (argv[c], "-t"); | 125 | strcpy(argv[i], "-t"); |
126 | } | ||
127 | } | ||
118 | 128 | ||
119 | while (1) { | 129 | int option_char; |
120 | c = getopt_long (argc, argv, "+Vhv46t:r:H:p:P:", longopts, &option); | 130 | while (true) { |
131 | int option = 0; | ||
132 | option_char = getopt_long(argc, argv, "+Vhv46t:r:H:p:P:", longopts, &option); | ||
121 | 133 | ||
122 | if (c == -1 || c == EOF) | 134 | if (option_char == -1 || option_char == EOF) { |
123 | break; | 135 | break; |
136 | } | ||
124 | 137 | ||
125 | switch (c) { | 138 | switch (option_char) { |
126 | case '?': /* help */ | 139 | case '?': /* help */ |
127 | usage5 (); | 140 | usage5(); |
128 | case 'V': /* version */ | 141 | case 'V': /* version */ |
129 | print_revision (progname, NP_VERSION); | 142 | print_revision(progname, NP_VERSION); |
130 | exit (STATE_UNKNOWN); | 143 | exit(STATE_UNKNOWN); |
131 | case 'h': /* help */ | 144 | case 'h': /* help */ |
132 | print_help (); | 145 | print_help(); |
133 | exit (STATE_UNKNOWN); | 146 | exit(STATE_UNKNOWN); |
134 | case 'v': /* verbose */ | 147 | case 'v': /* verbose */ |
135 | verbose = true; | 148 | verbose = true; |
136 | break; | 149 | break; |
137 | case 't': /* timeout period */ | 150 | case 't': /* timeout period */ |
138 | if (!is_integer (optarg)) | 151 | if (!is_intpos(optarg)) { |
139 | usage2 (_("Timeout interval must be a positive integer"), optarg); | 152 | usage2(_("Timeout interval must be a positive integer"), optarg); |
140 | else | 153 | } else { |
141 | socket_timeout = atoi (optarg); | 154 | socket_timeout = (unsigned int)atoi(optarg); |
155 | } | ||
142 | break; | 156 | break; |
143 | case '4': | 157 | case '4': |
144 | address_family = AF_INET; | 158 | address_family = AF_INET; |
@@ -147,121 +161,121 @@ process_arguments (int argc, char **argv) | |||
147 | #ifdef USE_IPV6 | 161 | #ifdef USE_IPV6 |
148 | address_family = AF_INET6; | 162 | address_family = AF_INET6; |
149 | #else | 163 | #else |
150 | usage4 (_("IPv6 support not available")); | 164 | usage4(_("IPv6 support not available")); |
151 | #endif | 165 | #endif |
152 | break; | 166 | break; |
153 | case 'r': /* remote version */ | 167 | case 'r': /* remote version */ |
154 | remote_version = optarg; | 168 | result.config.remote_version = optarg; |
155 | break; | 169 | break; |
156 | case 'P': /* remote version */ | 170 | case 'P': /* remote version */ |
157 | remote_protocol = optarg; | 171 | result.config.remote_protocol = optarg; |
158 | break; | 172 | break; |
159 | case 'H': /* host */ | 173 | case 'H': /* host */ |
160 | if (!is_host (optarg)) | 174 | if (!is_host(optarg)) { |
161 | usage2 (_("Invalid hostname/address"), optarg); | 175 | usage2(_("Invalid hostname/address"), optarg); |
162 | server_name = optarg; | 176 | } |
177 | result.config.server_name = optarg; | ||
163 | break; | 178 | break; |
164 | case 'p': /* port */ | 179 | case 'p': /* port */ |
165 | if (is_intpos (optarg)) { | 180 | if (is_intpos(optarg)) { |
166 | port = atoi (optarg); | 181 | result.config.port = atoi(optarg); |
182 | } else { | ||
183 | usage2(_("Port number must be a positive integer"), optarg); | ||
167 | } | 184 | } |
168 | else { | 185 | break; |
169 | usage2 (_("Port number must be a positive integer"), optarg); | 186 | case output_format_index: { |
187 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
188 | if (!parser.parsing_success) { | ||
189 | // TODO List all available formats here, maybe add anothoer usage function | ||
190 | printf("Invalid output format: %s\n", optarg); | ||
191 | exit(STATE_UNKNOWN); | ||
170 | } | 192 | } |
193 | |||
194 | result.config.output_format_is_set = true; | ||
195 | result.config.output_format = parser.output_format; | ||
196 | break; | ||
197 | } | ||
171 | } | 198 | } |
172 | } | 199 | } |
173 | 200 | ||
174 | c = optind; | 201 | option_char = optind; |
175 | if (server_name == NULL && c < argc) { | 202 | if (result.config.server_name == NULL && option_char < argc) { |
176 | if (is_host (argv[c])) { | 203 | if (is_host(argv[option_char])) { |
177 | server_name = argv[c++]; | 204 | result.config.server_name = argv[option_char++]; |
178 | } | 205 | } |
179 | } | 206 | } |
180 | 207 | ||
181 | if (port == -1 && c < argc) { | 208 | if (result.config.port == -1 && option_char < argc) { |
182 | if (is_intpos (argv[c])) { | 209 | if (is_intpos(argv[option_char])) { |
183 | port = atoi (argv[c++]); | 210 | result.config.port = atoi(argv[option_char++]); |
184 | } | 211 | } else { |
185 | else { | 212 | print_usage(); |
186 | print_usage (); | 213 | exit(STATE_UNKNOWN); |
187 | exit (STATE_UNKNOWN); | ||
188 | } | 214 | } |
189 | } | 215 | } |
190 | 216 | ||
191 | return validate_arguments (); | 217 | if (result.config.server_name == NULL) { |
192 | } | 218 | result.errorcode = ERROR; |
219 | return result; | ||
220 | } | ||
193 | 221 | ||
194 | int | 222 | return result; |
195 | validate_arguments (void) | ||
196 | { | ||
197 | if (server_name == NULL) | ||
198 | return ERROR; | ||
199 | if (port == -1) /* funky, but allows -p to override stray integer in args */ | ||
200 | port = SSH_DFL_PORT; | ||
201 | return OK; | ||
202 | } | 223 | } |
203 | 224 | ||
204 | |||
205 | /************************************************************************ | 225 | /************************************************************************ |
206 | * | 226 | * |
207 | * Try to connect to SSH server at specified server and port | 227 | * Try to connect to SSH server at specified server and port |
208 | * | 228 | * |
209 | *-----------------------------------------------------------------------*/ | 229 | *-----------------------------------------------------------------------*/ |
210 | |||
211 | |||
212 | int | ||
213 | ssh_connect (char *haddr, int hport, char *remote_version, char *remote_protocol) | ||
214 | { | ||
215 | int sd; | ||
216 | int result; | ||
217 | int len = 0; | ||
218 | ssize_t recv_ret = 0; | ||
219 | char *version_control_string = NULL; | ||
220 | char *buffer = NULL; | ||
221 | char *ssh_proto = NULL; | ||
222 | char *ssh_server = NULL; | ||
223 | static char *rev_no = VERSION; | ||
224 | struct timeval tv; | ||
225 | double elapsed_time; | ||
226 | 230 | ||
231 | int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_version, char *desired_remote_protocol) { | ||
232 | struct timeval tv; | ||
227 | gettimeofday(&tv, NULL); | 233 | gettimeofday(&tv, NULL); |
228 | 234 | ||
229 | result = my_tcp_connect (haddr, hport, &sd); | 235 | int socket; |
236 | int result = my_tcp_connect(haddr, hport, &socket); | ||
230 | 237 | ||
231 | if (result != STATE_OK) | 238 | mp_subcheck connection_sc = mp_subcheck_init(); |
239 | if (result != STATE_OK) { | ||
240 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); | ||
241 | xasprintf(&connection_sc.output, "Failed to establish TCP connection to Host %s and Port %d", haddr, hport); | ||
242 | mp_add_subcheck_to_check(overall, connection_sc); | ||
232 | return result; | 243 | return result; |
244 | } | ||
233 | 245 | ||
234 | char *output = (char *) calloc (BUFF_SZ + 1, sizeof(char)); | 246 | char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); |
235 | 247 | char *buffer = NULL; | |
236 | unsigned int iteration = 0; | 248 | size_t recv_ret = 0; |
237 | ssize_t byte_offset = 0; | 249 | char *version_control_string = NULL; |
238 | 250 | size_t byte_offset = 0; | |
239 | while ((version_control_string == NULL) && (recv_ret = recv(sd, output+byte_offset, BUFF_SZ - byte_offset, 0) > 0)) { | 251 | while ((version_control_string == NULL) && |
252 | (recv_ret = recv(socket, output + byte_offset, (unsigned long)(BUFF_SZ - byte_offset), 0) > 0)) { | ||
240 | 253 | ||
241 | if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ | 254 | if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ |
242 | byte_offset = 0; | 255 | byte_offset = 0; |
243 | 256 | ||
244 | char *index = NULL; | 257 | char *index = NULL; |
245 | while ((index = strchr(output+byte_offset, '\n')) != NULL) { | 258 | unsigned long len = 0; |
259 | while ((index = strchr(output + byte_offset, '\n')) != NULL) { | ||
246 | /*Partition the buffer so that this line is a separate string, | 260 | /*Partition the buffer so that this line is a separate string, |
247 | * by replacing the newline with NUL*/ | 261 | * by replacing the newline with NUL*/ |
248 | output[(index - output)] = '\0'; | 262 | output[(index - output)] = '\0'; |
249 | len = strlen(output + byte_offset); | 263 | len = strlen(output + byte_offset); |
250 | 264 | ||
251 | if ((len >= 4) && (strncmp (output+byte_offset, "SSH-", 4) == 0)) { | 265 | if ((len >= 4) && (strncmp(output + byte_offset, "SSH-", 4) == 0)) { |
252 | /*if the string starts with SSH-, this _should_ be a valid version control string*/ | 266 | /*if the string starts with SSH-, this _should_ be a valid version control string*/ |
253 | version_control_string = output+byte_offset; | 267 | version_control_string = output + byte_offset; |
254 | break; | 268 | break; |
255 | } | 269 | } |
256 | 270 | ||
257 | /*the start of the next line (if one exists) will be after the current one (+ NUL)*/ | 271 | /*the start of the next line (if one exists) will be after the current one (+ NUL)*/ |
258 | byte_offset += (len + 1); | 272 | byte_offset += (len + 1); |
259 | } | 273 | } |
260 | 274 | ||
261 | if(version_control_string == NULL) { | 275 | if (version_control_string == NULL) { |
262 | /* move unconsumed data to beginning of buffer, null rest */ | 276 | /* move unconsumed data to beginning of buffer, null rest */ |
263 | memmove((void *)output, (void *)output+byte_offset+1, BUFF_SZ - len+1); | 277 | memmove((void *)output, (void *)(output + byte_offset + 1), BUFF_SZ - len + 1); |
264 | memset(output+byte_offset+1, 0, BUFF_SZ-byte_offset+1); | 278 | memset(output + byte_offset + 1, 0, BUFF_SZ - byte_offset + 1); |
265 | 279 | ||
266 | /*start reading from end of current line chunk on next recv*/ | 280 | /*start reading from end of current line chunk on next recv*/ |
267 | byte_offset = strlen(output); | 281 | byte_offset = strlen(output); |
@@ -272,14 +286,23 @@ ssh_connect (char *haddr, int hport, char *remote_version, char *remote_protocol | |||
272 | } | 286 | } |
273 | 287 | ||
274 | if (recv_ret < 0) { | 288 | if (recv_ret < 0) { |
275 | printf("SSH CRITICAL - %s", strerror(errno)); | 289 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); |
276 | exit(STATE_CRITICAL); | 290 | xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - %s", strerror(errno)); |
291 | mp_add_subcheck_to_check(overall, connection_sc); | ||
292 | return OK; | ||
277 | } | 293 | } |
278 | 294 | ||
279 | if (version_control_string == NULL) { | 295 | if (version_control_string == NULL) { |
280 | printf("SSH CRITICAL - No version control string received"); | 296 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); |
281 | exit(STATE_CRITICAL); | 297 | xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - No version control string received"); |
298 | mp_add_subcheck_to_check(overall, connection_sc); | ||
299 | return OK; | ||
282 | } | 300 | } |
301 | |||
302 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_OK); | ||
303 | xasprintf(&connection_sc.output, "%s", "Initial connection succeeded"); | ||
304 | mp_add_subcheck_to_check(overall, connection_sc); | ||
305 | |||
283 | /* | 306 | /* |
284 | * "When the connection has been established, both sides MUST send an | 307 | * "When the connection has been established, both sides MUST send an |
285 | * identification string. This identification string MUST be | 308 | * identification string. This identification string MUST be |
@@ -287,10 +310,12 @@ ssh_connect (char *haddr, int hport, char *remote_version, char *remote_protocol | |||
287 | * SSH-protoversion-softwareversion SP comments CR LF" | 310 | * SSH-protoversion-softwareversion SP comments CR LF" |
288 | * - RFC 4253:4.2 | 311 | * - RFC 4253:4.2 |
289 | */ | 312 | */ |
290 | strip (version_control_string); | 313 | strip(version_control_string); |
291 | if (verbose) | 314 | if (verbose) { |
292 | printf ("%s\n", version_control_string); | 315 | printf("%s\n", version_control_string); |
293 | ssh_proto = version_control_string + 4; | 316 | } |
317 | |||
318 | char *ssh_proto = version_control_string + 4; | ||
294 | 319 | ||
295 | /* | 320 | /* |
296 | * We assume the protoversion is of the form Major.Minor, although | 321 | * We assume the protoversion is of the form Major.Minor, although |
@@ -308,7 +333,7 @@ ssh_connect (char *haddr, int hport, char *remote_version, char *remote_protocol | |||
308 | * "1.x" (e.g., "1.5" or "1.3")." | 333 | * "1.x" (e.g., "1.5" or "1.3")." |
309 | * - RFC 4253:5 | 334 | * - RFC 4253:5 |
310 | */ | 335 | */ |
311 | ssh_server = ssh_proto + strspn (ssh_proto, "0123456789.") + 1; /* (+1 for the '-' separating protoversion from softwareversion) */ | 336 | char *ssh_server = ssh_proto + strspn(ssh_proto, "0123456789.") + 1; /* (+1 for the '-' separating protoversion from softwareversion) */ |
312 | 337 | ||
313 | /* If there's a space in the version string, whatever's after the space is a comment | 338 | /* If there's a space in the version string, whatever's after the space is a comment |
314 | * (which is NOT part of the server name/version)*/ | 339 | * (which is NOT part of the server name/version)*/ |
@@ -316,88 +341,99 @@ ssh_connect (char *haddr, int hport, char *remote_version, char *remote_protocol | |||
316 | if (tmp) { | 341 | if (tmp) { |
317 | ssh_server[tmp - ssh_server] = '\0'; | 342 | ssh_server[tmp - ssh_server] = '\0'; |
318 | } | 343 | } |
344 | |||
345 | mp_subcheck protocol_validity_sc = mp_subcheck_init(); | ||
319 | if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) { | 346 | if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) { |
320 | printf(_("SSH CRITICAL - Invalid protocol version control string %s\n"), version_control_string); | 347 | protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_CRITICAL); |
321 | exit (STATE_CRITICAL); | 348 | xasprintf(&protocol_validity_sc.output, "Invalid protocol version control string %s", version_control_string); |
349 | mp_add_subcheck_to_check(overall, protocol_validity_sc); | ||
350 | return OK; | ||
322 | } | 351 | } |
323 | ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0; | 352 | |
324 | 353 | protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_OK); | |
325 | xasprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no); | 354 | xasprintf(&protocol_validity_sc.output, "Valid protocol version control string %s", version_control_string); |
326 | send (sd, buffer, strlen (buffer), MSG_DONTWAIT); | 355 | mp_add_subcheck_to_check(overall, protocol_validity_sc); |
327 | if (verbose) | 356 | |
328 | printf ("%s\n", buffer); | 357 | ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; |
329 | 358 | ||
330 | if (remote_version && strcmp(remote_version, ssh_server)) { | 359 | static char *rev_no = VERSION; |
331 | printf | 360 | xasprintf(&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no); |
332 | (_("SSH CRITICAL - %s (protocol %s) version mismatch, expected '%s'\n"), | 361 | send(socket, buffer, strlen(buffer), MSG_DONTWAIT); |
333 | ssh_server, ssh_proto, remote_version); | 362 | if (verbose) { |
334 | close(sd); | 363 | printf("%s\n", buffer); |
335 | exit (STATE_CRITICAL); | ||
336 | } | 364 | } |
337 | 365 | ||
338 | if (remote_protocol && strcmp(remote_protocol, ssh_proto)) { | 366 | if (desired_remote_version && strcmp(desired_remote_version, ssh_server)) { |
339 | printf | 367 | mp_subcheck remote_version_sc = mp_subcheck_init(); |
340 | (_("SSH CRITICAL - %s (protocol %s) protocol version mismatch, expected '%s' | %s\n"), | 368 | remote_version_sc = mp_set_subcheck_state(remote_version_sc, STATE_CRITICAL); |
341 | ssh_server, ssh_proto, remote_protocol, fperfdata("time", elapsed_time, "s", | 369 | xasprintf(&remote_version_sc.output, _("%s (protocol %s) version mismatch, expected '%s'"), ssh_server, ssh_proto, |
342 | false, 0, false, 0, true, 0, true, (int)socket_timeout)); | 370 | desired_remote_version); |
343 | close(sd); | 371 | close(socket); |
344 | exit (STATE_CRITICAL); | 372 | mp_add_subcheck_to_check(overall, remote_version_sc); |
373 | return OK; | ||
345 | } | 374 | } |
346 | elapsed_time = (double)deltime(tv) / 1.0e6; | ||
347 | |||
348 | printf | ||
349 | (_("SSH OK - %s (protocol %s) | %s\n"), | ||
350 | ssh_server, ssh_proto, fperfdata("time", elapsed_time, "s", | ||
351 | false, 0, false, 0, true, 0, true, (int)socket_timeout)); | ||
352 | close(sd); | ||
353 | exit (STATE_OK); | ||
354 | } | ||
355 | 375 | ||
376 | double elapsed_time = (double)deltime(tv) / 1.0e6; | ||
377 | mp_perfdata time_pd = perfdata_init(); | ||
378 | time_pd.value = mp_create_pd_value(elapsed_time); | ||
379 | time_pd.label = "time"; | ||
380 | time_pd.max_present = true; | ||
381 | time_pd.max = mp_create_pd_value(socket_timeout); | ||
382 | |||
383 | mp_subcheck protocol_version_sc = mp_subcheck_init(); | ||
384 | mp_add_perfdata_to_subcheck(&protocol_version_sc, time_pd); | ||
385 | |||
386 | if (desired_remote_protocol && strcmp(desired_remote_protocol, ssh_proto)) { | ||
387 | protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_CRITICAL); | ||
388 | xasprintf(&protocol_version_sc.output, _("%s (protocol %s) protocol version mismatch, expected '%s'"), ssh_server, ssh_proto, | ||
389 | desired_remote_protocol); | ||
390 | } else { | ||
391 | protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_OK); | ||
392 | xasprintf(&protocol_version_sc.output, "SSH server version: %s (protocol version: %s)", ssh_server, ssh_proto); | ||
393 | } | ||
356 | 394 | ||
395 | mp_add_subcheck_to_check(overall, protocol_version_sc); | ||
396 | close(socket); | ||
397 | return OK; | ||
398 | } | ||
357 | 399 | ||
358 | void | 400 | void print_help(void) { |
359 | print_help (void) | ||
360 | { | ||
361 | char *myport; | 401 | char *myport; |
362 | xasprintf (&myport, "%d", SSH_DFL_PORT); | 402 | xasprintf(&myport, "%d", default_ssh_port); |
363 | 403 | ||
364 | print_revision (progname, NP_VERSION); | 404 | print_revision(progname, NP_VERSION); |
365 | 405 | ||
366 | printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n"); | 406 | printf("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n"); |
367 | printf (COPYRIGHT, copyright, email); | 407 | printf(COPYRIGHT, copyright, email); |
368 | 408 | ||
369 | printf ("%s\n", _("Try to connect to an SSH server at specified server and port")); | 409 | printf("%s\n", _("Try to connect to an SSH server at specified server and port")); |
370 | 410 | ||
371 | printf ("\n\n"); | 411 | printf("\n\n"); |
372 | 412 | ||
373 | print_usage (); | 413 | print_usage(); |
374 | 414 | ||
375 | printf (UT_HELP_VRSN); | 415 | printf(UT_HELP_VRSN); |
376 | printf (UT_EXTRA_OPTS); | 416 | printf(UT_EXTRA_OPTS); |
377 | 417 | ||
378 | printf (UT_HOST_PORT, 'p', myport); | 418 | printf(UT_HOST_PORT, 'p', myport); |
379 | 419 | ||
380 | printf (UT_IPv46); | 420 | printf(UT_IPv46); |
381 | 421 | ||
382 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 422 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
383 | 423 | ||
384 | printf (" %s\n", "-r, --remote-version=STRING"); | 424 | printf(" %s\n", "-r, --remote-version=STRING"); |
385 | printf (" %s\n", _("Alert if string doesn't match expected server version (ex: OpenSSH_3.9p1)")); | 425 | printf(" %s\n", _("Alert if string doesn't match expected server version (ex: OpenSSH_3.9p1)")); |
386 | 426 | ||
387 | printf (" %s\n", "-P, --remote-protocol=STRING"); | 427 | printf(" %s\n", "-P, --remote-protocol=STRING"); |
388 | printf (" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)")); | 428 | printf(" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)")); |
429 | printf(UT_OUTPUT_FORMAT); | ||
389 | 430 | ||
390 | printf (UT_VERBOSE); | 431 | printf(UT_VERBOSE); |
391 | 432 | ||
392 | printf (UT_SUPPORT); | 433 | printf(UT_SUPPORT); |
393 | } | 434 | } |
394 | 435 | ||
395 | 436 | void print_usage(void) { | |
396 | 437 | printf("%s\n", _("Usage:")); | |
397 | void | 438 | printf("%s [-4|-6] [-t <timeout>] [-r <remote version>] [-p <port>] --hostname <host>\n", progname); |
398 | print_usage (void) | ||
399 | { | ||
400 | printf ("%s\n", _("Usage:")); | ||
401 | printf ("%s [-4|-6] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname); | ||
402 | } | 439 | } |
403 | |||