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