Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file session_server_ssh.c |
| 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 SSH server session manipulation functions |
| 5 | * |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 6 | * Copyright (c) 2017 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 17 | #include "config.h" /* Expose HAVE_SHADOW and HAVE_CRYPT */ |
| 18 | |
| 19 | #ifdef HAVE_SHADOW |
| 20 | #include <shadow.h> |
| 21 | #endif |
| 22 | #ifdef HAVE_CRYPT |
| 23 | #include <crypt.h> |
| 24 | #endif |
| 25 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 29 | #include <sys/stat.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 30 | #include <pwd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 31 | #include <errno.h> |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 32 | #include <time.h> |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 35 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 36 | #include "session_server.h" |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 37 | #include "session_server_ch.h" |
| 38 | #include "libnetconf.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 39 | |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 40 | #if !defined(HAVE_CRYPT_R) |
| 41 | pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER; |
| 42 | #endif |
| 43 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 44 | extern struct nc_server_opts server_opts; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 45 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 46 | static char * |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 47 | base64der_key_to_tmp_file(const char *in, const char *key_str) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 48 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 49 | char path[12] = "/tmp/XXXXXX"; |
| 50 | int fd, written; |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 51 | mode_t umode; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 52 | FILE *file; |
| 53 | |
| 54 | if (in == NULL) { |
| 55 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 56 | } |
| 57 | |
mekleo | b31878b | 2019-09-09 14:10:47 +0200 | [diff] [blame] | 58 | umode = umask(0177); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 59 | fd = mkstemp(path); |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 60 | umask(umode); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 61 | if (fd == -1) { |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Michal Vasko | 3964a83 | 2018-09-18 14:37:39 +0200 | [diff] [blame] | 65 | file = fdopen(fd, "w"); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 66 | if (!file) { |
| 67 | close(fd); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | /* write the key into the file */ |
| 72 | written = fwrite("-----BEGIN ", 1, 11, file); |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 73 | written += fwrite(key_str, 1, strlen(key_str), file); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 74 | written += fwrite(" PRIVATE KEY-----\n", 1, 18, file); |
| 75 | written += fwrite(in, 1, strlen(in), file); |
| 76 | written += fwrite("\n-----END ", 1, 10, file); |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 77 | written += fwrite(key_str, 1, strlen(key_str), file); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 78 | written += fwrite(" PRIVATE KEY-----", 1, 17, file); |
| 79 | |
| 80 | fclose(file); |
Michal Vasko | 841bf9e | 2020-04-27 15:07:37 +0200 | [diff] [blame] | 81 | if ((unsigned)written != 11 + strlen(key_str) + 18 + strlen(in) + 10 + strlen(key_str) + 17) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 82 | unlink(path); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | return strdup(path); |
| 87 | } |
| 88 | |
| 89 | static int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 90 | nc_server_ssh_add_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts) |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 91 | { |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 92 | uint8_t i; |
| 93 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 94 | if (!name) { |
| 95 | ERRARG("name"); |
Michal Vasko | 5fcc714 | 2016-02-02 12:21:10 +0100 | [diff] [blame] | 96 | return -1; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 97 | } else if (idx > opts->hostkey_count) { |
| 98 | ERRARG("idx"); |
| 99 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 100 | } |
Michal Vasko | d45e25a | 2016-01-08 15:48:44 +0100 | [diff] [blame] | 101 | |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 102 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 103 | if (!strcmp(opts->hostkeys[i], name)) { |
| 104 | ERRARG("name"); |
| 105 | return -1; |
| 106 | } |
| 107 | } |
| 108 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 109 | ++opts->hostkey_count; |
| 110 | opts->hostkeys = nc_realloc(opts->hostkeys, opts->hostkey_count * sizeof *opts->hostkeys); |
| 111 | if (!opts->hostkeys) { |
| 112 | ERRMEM; |
| 113 | return -1; |
| 114 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 115 | |
| 116 | if (idx < 0) { |
| 117 | idx = opts->hostkey_count - 1; |
| 118 | } |
| 119 | if (idx != opts->hostkey_count - 1) { |
| 120 | memmove(opts->hostkeys + idx + 1, opts->hostkeys + idx, opts->hostkey_count - idx); |
| 121 | } |
| 122 | opts->hostkeys[idx] = lydict_insert(server_opts.ctx, name, 0); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 123 | |
Michal Vasko | 5fcc714 | 2016-02-02 12:21:10 +0100 | [diff] [blame] | 124 | return 0; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | API int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 128 | nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 129 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 130 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 131 | struct nc_endpt *endpt; |
| 132 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 133 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 134 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 135 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 136 | return -1; |
| 137 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 138 | ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 139 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 140 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 141 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 142 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Michal Vasko | 974410a | 2018-04-03 09:36:57 +0200 | [diff] [blame] | 145 | API void |
| 146 | nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, void *user_data), |
| 147 | void *user_data, void (*free_user_data)(void *user_data)) |
| 148 | { |
| 149 | server_opts.passwd_auth_clb = passwd_auth_clb; |
| 150 | server_opts.passwd_auth_data = user_data; |
| 151 | server_opts.passwd_auth_data_free = free_user_data; |
| 152 | } |
| 153 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 154 | API void |
| 155 | nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_message msg, void *user_data), |
| 156 | void *user_data, void (*free_user_data)(void *user_data)) |
| 157 | { |
| 158 | server_opts.interactive_auth_clb = interactive_auth_clb; |
| 159 | server_opts.interactive_auth_data = user_data; |
| 160 | server_opts.interactive_auth_data_free = free_user_data; |
| 161 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 162 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 163 | API void |
| 164 | nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data), |
| 165 | void *user_data, void (*free_user_data)(void *user_data)) |
| 166 | { |
| 167 | server_opts.pubkey_auth_clb = pubkey_auth_clb; |
| 168 | server_opts.pubkey_auth_data = user_data; |
| 169 | server_opts.pubkey_auth_data_free = free_user_data; |
| 170 | } |
| 171 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 172 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 173 | nc_server_ssh_ch_client_endpt_add_hostkey(const char *client_name, const char *endpt_name, const char *name, int16_t idx) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 174 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 175 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 176 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 177 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 178 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 179 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 180 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 181 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 182 | return -1; |
| 183 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 184 | ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 185 | /* UNLOCK */ |
| 186 | nc_server_ch_client_unlock(client); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 191 | API void |
| 192 | nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 193 | char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data, void (*free_user_data)(void *user_data)) |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 194 | { |
| 195 | if (!hostkey_clb) { |
| 196 | ERRARG("hostkey_clb"); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | server_opts.hostkey_clb = hostkey_clb; |
| 201 | server_opts.hostkey_data = user_data; |
| 202 | server_opts.hostkey_data_free = free_user_data; |
| 203 | } |
| 204 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 205 | static int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 206 | nc_server_ssh_del_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 207 | { |
| 208 | uint8_t i; |
| 209 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 210 | if (name && (idx > -1)) { |
| 211 | ERRARG("name and idx"); |
| 212 | return -1; |
| 213 | } else if (idx >= opts->hostkey_count) { |
| 214 | ERRARG("idx"); |
| 215 | } |
| 216 | |
| 217 | if (!name && (idx < 0)) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 218 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 219 | lydict_remove(server_opts.ctx, opts->hostkeys[i]); |
| 220 | } |
| 221 | free(opts->hostkeys); |
| 222 | opts->hostkeys = NULL; |
| 223 | opts->hostkey_count = 0; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 224 | } else if (name) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 225 | for (i = 0; i < opts->hostkey_count; ++i) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 226 | if (!strcmp(opts->hostkeys[i], name)) { |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 227 | idx = i; |
| 228 | goto remove_idx; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 232 | ERRARG("name"); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 233 | return -1; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 234 | } else { |
| 235 | remove_idx: |
| 236 | --opts->hostkey_count; |
| 237 | lydict_remove(server_opts.ctx, opts->hostkeys[idx]); |
| 238 | if (idx < opts->hostkey_count - 1) { |
| 239 | memmove(opts->hostkeys + idx, opts->hostkeys + idx + 1, (opts->hostkey_count - idx) * sizeof *opts->hostkeys); |
| 240 | } |
| 241 | if (!opts->hostkey_count) { |
| 242 | free(opts->hostkeys); |
| 243 | opts->hostkeys = NULL; |
| 244 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | API int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 251 | nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 252 | { |
| 253 | int ret; |
| 254 | struct nc_endpt *endpt; |
| 255 | |
| 256 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 257 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 258 | if (!endpt) { |
| 259 | return -1; |
| 260 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 261 | ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 262 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 263 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 269 | nc_server_ssh_ch_client_endpt_del_hostkey(const char *client_name, const char *endpt_name, const char *name, int16_t idx) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 270 | { |
| 271 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 272 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 273 | struct nc_ch_endpt *endpt; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 274 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 275 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 276 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 277 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 278 | return -1; |
| 279 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 280 | ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 281 | /* UNLOCK */ |
| 282 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 283 | |
| 284 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 288 | nc_server_ssh_mov_hostkey(const char *key_mov, const char *key_after, struct nc_server_ssh_opts *opts) |
| 289 | { |
| 290 | uint8_t i; |
| 291 | int16_t mov_idx = -1, after_idx = -1; |
| 292 | const char *bckup; |
| 293 | |
| 294 | if (!key_mov) { |
| 295 | ERRARG("key_mov"); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 300 | if (key_after && (after_idx == -1) && !strcmp(opts->hostkeys[i], key_after)) { |
| 301 | after_idx = i; |
| 302 | } |
| 303 | if ((mov_idx == -1) && !strcmp(opts->hostkeys[i], key_mov)) { |
| 304 | mov_idx = i; |
| 305 | } |
| 306 | |
| 307 | if ((!key_after || (after_idx > -1)) && (mov_idx > -1)) { |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (key_after && (after_idx == -1)) { |
| 313 | ERRARG("key_after"); |
| 314 | return -1; |
| 315 | } |
| 316 | if (mov_idx == -1) { |
| 317 | ERRARG("key_mov"); |
| 318 | return -1; |
| 319 | } |
| 320 | if ((mov_idx == after_idx) || (mov_idx == after_idx + 1)) { |
| 321 | /* nothing to do */ |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | /* finally move the key */ |
| 326 | bckup = opts->hostkeys[mov_idx]; |
| 327 | if (mov_idx > after_idx) { |
| 328 | memmove(opts->hostkeys + after_idx + 2, opts->hostkeys + after_idx + 1, |
| 329 | ((mov_idx - after_idx) - 1) * sizeof *opts->hostkeys); |
| 330 | opts->hostkeys[after_idx + 1] = bckup; |
| 331 | } else { |
| 332 | memmove(opts->hostkeys + mov_idx, opts->hostkeys + mov_idx + 1, (after_idx - mov_idx) * sizeof *opts->hostkeys); |
| 333 | opts->hostkeys[after_idx] = bckup; |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | API int |
| 340 | nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after) |
| 341 | { |
| 342 | int ret; |
| 343 | struct nc_endpt *endpt; |
| 344 | |
| 345 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 346 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 347 | if (!endpt) { |
| 348 | return -1; |
| 349 | } |
| 350 | ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh); |
| 351 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 352 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 353 | |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 358 | nc_server_ssh_ch_client_endpt_mov_hostkey(const char *client_name, const char *endpt_name, const char *key_mov, |
| 359 | const char *key_after) |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 360 | { |
| 361 | int ret; |
| 362 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 363 | struct nc_ch_endpt *endpt; |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 364 | |
| 365 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 366 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 367 | if (!endpt) { |
| 368 | return -1; |
| 369 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 370 | ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh); |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 371 | /* UNLOCK */ |
| 372 | nc_server_ch_client_unlock(client); |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 378 | nc_server_ssh_set_auth_methods(int auth_methods, struct nc_server_ssh_opts *opts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 379 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 380 | opts->auth_methods = auth_methods; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 385 | nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 386 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 387 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 388 | struct nc_endpt *endpt; |
| 389 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 390 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 391 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 392 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 393 | return -1; |
| 394 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 395 | ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 396 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 397 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 398 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 399 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 403 | nc_server_ssh_ch_client_endpt_set_auth_methods(const char *client_name, const char *endpt_name, int auth_methods) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 404 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 405 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 406 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 407 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 408 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 409 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 410 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 411 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 412 | return -1; |
| 413 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 414 | ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 415 | /* UNLOCK */ |
| 416 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 417 | |
| 418 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 421 | API int |
| 422 | nc_server_ssh_endpt_get_auth_methods(const char *endpt_name) |
| 423 | { |
| 424 | int ret; |
| 425 | struct nc_endpt *endpt; |
| 426 | |
| 427 | /* LOCK */ |
| 428 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
| 429 | if (!endpt) { |
| 430 | return -1; |
| 431 | } |
| 432 | ret = endpt->opts.ssh->auth_methods; |
| 433 | /* UNLOCK */ |
| 434 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 440 | nc_server_ssh_ch_client_endpt_get_auth_methods(const char *client_name, const char *endpt_name) |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 441 | { |
| 442 | int ret; |
| 443 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 444 | struct nc_ch_endpt *endpt; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 445 | |
| 446 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 447 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 448 | if (!endpt) { |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 449 | return -1; |
| 450 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 451 | ret = endpt->opts.ssh->auth_methods; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 452 | /* UNLOCK */ |
| 453 | nc_server_ch_client_unlock(client); |
| 454 | |
| 455 | return ret; |
| 456 | } |
| 457 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 458 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 459 | nc_server_ssh_set_auth_attempts(uint16_t auth_attempts, struct nc_server_ssh_opts *opts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 460 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 461 | if (!auth_attempts) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 462 | ERRARG("auth_attempts"); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 463 | return -1; |
| 464 | } |
| 465 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 466 | opts->auth_attempts = auth_attempts; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 471 | nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 472 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 473 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 474 | struct nc_endpt *endpt; |
| 475 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 476 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 477 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 478 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 479 | return -1; |
| 480 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 481 | ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 482 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 483 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 484 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 485 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | API int |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 489 | nc_server_ssh_ch_client_endpt_set_auth_attempts(const char *client_name, const char *endpt_name, uint16_t auth_attempts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 490 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 491 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 492 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 493 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 494 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 495 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 496 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 497 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 498 | return -1; |
| 499 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 500 | ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 501 | /* UNLOCK */ |
| 502 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 503 | |
| 504 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 508 | nc_server_ssh_set_auth_timeout(uint16_t auth_timeout, struct nc_server_ssh_opts *opts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 509 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 510 | if (!auth_timeout) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 511 | ERRARG("auth_timeout"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 512 | return -1; |
| 513 | } |
| 514 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 515 | opts->auth_timeout = auth_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 520 | nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 521 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 522 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 523 | struct nc_endpt *endpt; |
| 524 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 525 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 526 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 527 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 528 | return -1; |
| 529 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 530 | ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 531 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 532 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 533 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 534 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | API int |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 538 | nc_server_ssh_ch_client_endpt_set_auth_timeout(const char *client_name, const char *endpt_name, uint16_t auth_timeout) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 539 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 540 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 541 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 542 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 543 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 544 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 545 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 546 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 547 | return -1; |
| 548 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 549 | ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 550 | /* UNLOCK */ |
| 551 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 552 | |
| 553 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 557 | _nc_server_ssh_add_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
| 558 | const char *username) |
| 559 | { |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 560 | /* LOCK */ |
| 561 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 562 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 563 | ++server_opts.authkey_count; |
| 564 | server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys); |
| 565 | if (!server_opts.authkeys) { |
| 566 | ERRMEM; |
| 567 | return -1; |
| 568 | } |
| 569 | server_opts.authkeys[server_opts.authkey_count - 1].path = lydict_insert(server_opts.ctx, pubkey_path, 0); |
| 570 | server_opts.authkeys[server_opts.authkey_count - 1].base64 = lydict_insert(server_opts.ctx, pubkey_base64, 0); |
| 571 | server_opts.authkeys[server_opts.authkey_count - 1].type = type; |
| 572 | server_opts.authkeys[server_opts.authkey_count - 1].username = lydict_insert(server_opts.ctx, username, 0); |
| 573 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 574 | /* UNLOCK */ |
| 575 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 576 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | API int |
| 581 | nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 582 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 583 | if (!pubkey_path) { |
| 584 | ERRARG("pubkey_path"); |
| 585 | return -1; |
| 586 | } else if (!username) { |
| 587 | ERRARG("username"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 588 | return -1; |
| 589 | } |
| 590 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 591 | return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 595 | nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 596 | { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 597 | if (!pubkey_base64) { |
| 598 | ERRARG("pubkey_base64"); |
| 599 | return -1; |
| 600 | } else if (!type) { |
| 601 | ERRARG("type"); |
| 602 | return -1; |
| 603 | } else if (!username) { |
| 604 | ERRARG("username"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 605 | return -1; |
| 606 | } |
| 607 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 608 | return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 612 | nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
| 613 | const char *username) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 614 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 615 | uint32_t i; |
| 616 | int ret = -1; |
| 617 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 618 | /* LOCK */ |
| 619 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 620 | |
| 621 | if (!pubkey_path && !pubkey_base64 && !type && !username) { |
| 622 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 623 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].path); |
| 624 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64); |
| 625 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 626 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 627 | ret = 0; |
| 628 | } |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 629 | free(server_opts.authkeys); |
| 630 | server_opts.authkeys = NULL; |
| 631 | server_opts.authkey_count = 0; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 632 | } else { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 633 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 634 | if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path)) |
Michal Vasko | e846aee | 2019-03-28 08:38:41 +0100 | [diff] [blame] | 635 | && (!pubkey_base64 || !strcmp(server_opts.authkeys[i].base64, pubkey_base64)) |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 636 | && (!type || (server_opts.authkeys[i].type == type)) |
| 637 | && (!username || !strcmp(server_opts.authkeys[i].username, username))) { |
| 638 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].path); |
| 639 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64); |
| 640 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].username); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 641 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 642 | --server_opts.authkey_count; |
| 643 | if (i < server_opts.authkey_count) { |
| 644 | memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count], |
| 645 | sizeof *server_opts.authkeys); |
| 646 | } else if (!server_opts.authkey_count) { |
| 647 | free(server_opts.authkeys); |
| 648 | server_opts.authkeys = NULL; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 649 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 650 | |
| 651 | ret = 0; |
| 652 | } |
| 653 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 656 | /* UNLOCK */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 657 | pthread_mutex_unlock(&server_opts.authkey_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 658 | |
| 659 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 663 | nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 664 | { |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 665 | nc_server_ssh_del_hostkey(NULL, -1, opts); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 668 | static char * |
| 669 | auth_password_get_pwd_hash(const char *username) |
| 670 | { |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 671 | #ifdef HAVE_SHADOW |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 672 | struct passwd *pwd, pwd_buf; |
| 673 | struct spwd *spwd, spwd_buf; |
| 674 | char *pass_hash = NULL, buf[256]; |
| 675 | |
| 676 | getpwnam_r(username, &pwd_buf, buf, 256, &pwd); |
| 677 | if (!pwd) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 678 | VRB("User \"%s\" not found locally.", username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | if (!strcmp(pwd->pw_passwd, "x")) { |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 683 | #ifndef __QNXNTO__ |
| 684 | getspnam_r(username, &spwd_buf, buf, 256, &spwd); |
| 685 | #else |
| 686 | spwd = getspnam_r(username, &spwd_buf, buf, 256); |
| 687 | #endif |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 688 | if (!spwd) { |
| 689 | VRB("Failed to retrieve the shadow entry for \"%s\".", username); |
| 690 | return NULL; |
| 691 | } |
| 692 | |
| 693 | pass_hash = spwd->sp_pwdp; |
| 694 | } else { |
| 695 | pass_hash = pwd->pw_passwd; |
| 696 | } |
| 697 | |
| 698 | if (!pass_hash) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 699 | ERR("No password could be retrieved for \"%s\".", username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 700 | return NULL; |
| 701 | } |
| 702 | |
| 703 | /* check the hash structure for special meaning */ |
| 704 | if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) { |
| 705 | VRB("User \"%s\" is not allowed to authenticate using a password.", username); |
| 706 | return NULL; |
| 707 | } |
| 708 | if (!strcmp(pass_hash, "*NP*")) { |
| 709 | VRB("Retrieving password for \"%s\" from a NIS+ server not supported.", username); |
| 710 | return NULL; |
| 711 | } |
| 712 | |
| 713 | return strdup(pass_hash); |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 714 | #else |
| 715 | return strdup(""); |
| 716 | #endif |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static int |
| 720 | auth_password_compare_pwd(const char *pass_hash, const char *pass_clear) |
| 721 | { |
| 722 | char *new_pass_hash; |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 723 | #if defined(HAVE_CRYPT_R) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 724 | struct crypt_data cdata; |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 725 | #endif |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 726 | |
| 727 | if (!pass_hash[0]) { |
| 728 | if (!pass_clear[0]) { |
| 729 | WRN("User authentication successful with an empty password!"); |
| 730 | return 0; |
| 731 | } else { |
| 732 | /* the user did now know he does not need any password, |
| 733 | * (which should not be used) so deny authentication */ |
| 734 | return 1; |
| 735 | } |
| 736 | } |
| 737 | |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 738 | #if defined(HAVE_CRYPT_R) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 739 | cdata.initialized = 0; |
| 740 | new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 741 | #else |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 742 | pthread_mutex_lock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 743 | new_pass_hash = crypt(pass_clear, pass_hash); |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 744 | pthread_mutex_unlock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 745 | #endif |
Andrew Langefeld | 158d6fd | 2018-06-11 18:51:44 -0500 | [diff] [blame] | 746 | |
| 747 | if (!new_pass_hash) { |
| 748 | return 1; |
| 749 | } |
| 750 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 751 | return strcmp(new_pass_hash, pass_hash); |
| 752 | } |
| 753 | |
| 754 | static void |
| 755 | nc_sshcb_auth_password(struct nc_session *session, ssh_message msg) |
| 756 | { |
| 757 | char *pass_hash; |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 758 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 759 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 760 | if (server_opts.passwd_auth_clb) { |
| 761 | auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data); |
| 762 | } else { |
| 763 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 764 | if (pass_hash) { |
| 765 | auth_ret = auth_password_compare_pwd(pass_hash, ssh_message_auth_password(msg)); |
| 766 | free(pass_hash); |
| 767 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 770 | if (!auth_ret) { |
| 771 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 772 | VRB("User \"%s\" authenticated.", session->username); |
| 773 | ssh_message_auth_reply_success(msg, 0); |
| 774 | } else { |
| 775 | ++session->opts.server.ssh_auth_attempts; |
| 776 | VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts); |
| 777 | ssh_message_reply_default(msg); |
| 778 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static void |
| 782 | nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg) |
| 783 | { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 784 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 785 | char *pass_hash; |
| 786 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 787 | if (server_opts.interactive_auth_clb) { |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 788 | auth_ret = server_opts.interactive_auth_clb(session, msg, server_opts.interactive_auth_data); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 789 | } else { |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 790 | if (!ssh_message_auth_kbdint_is_response(msg)) { |
| 791 | const char *prompts[] = {"Password: "}; |
| 792 | char echo[] = {0}; |
| 793 | |
| 794 | ssh_message_auth_interactive_request(msg, "Interactive SSH Authentication", "Type your password:", 1, prompts, echo); |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 795 | auth_ret = -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 796 | } else { |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 797 | if (ssh_userauth_kbdint_getnanswers(session->ti.libssh.session) != 1) {// failed session |
| 798 | ssh_message_reply_default(msg); |
| 799 | return; |
| 800 | } |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 801 | pass_hash = auth_password_get_pwd_hash(session->username);// get hashed password |
| 802 | if (pass_hash) { |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 803 | /* Normalize auth_password_compare_pwd result to 0 or 1 */ |
| 804 | auth_ret = !!auth_password_compare_pwd(pass_hash, ssh_userauth_kbdint_getanswer(session->ti.libssh.session, 0)); |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 805 | free(pass_hash);// free hashed password |
| 806 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 807 | } |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 808 | } |
| 809 | |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 810 | /* We have already sent a reply */ |
| 811 | if (auth_ret == -1) { |
| 812 | return; |
| 813 | } |
| 814 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 815 | /* Authenticate message based on outcome */ |
| 816 | if (!auth_ret) { |
| 817 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 818 | VRB("User \"%s\" authenticated.", session->username); |
| 819 | ssh_message_auth_reply_success(msg, 0); |
| 820 | } else { |
| 821 | ++session->opts.server.ssh_auth_attempts; |
| 822 | VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts); |
| 823 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
| 827 | static const char * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 828 | auth_pubkey_compare_key(ssh_key key) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 829 | { |
| 830 | uint32_t i; |
| 831 | ssh_key pub_key; |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 832 | const char *username = NULL; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 833 | int ret = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 834 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 835 | /* LOCK */ |
| 836 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 837 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 838 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 839 | switch (server_opts.authkeys[i].type) { |
| 840 | case NC_SSH_KEY_UNKNOWN: |
| 841 | ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key); |
| 842 | break; |
| 843 | case NC_SSH_KEY_DSA: |
| 844 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key); |
| 845 | break; |
| 846 | case NC_SSH_KEY_RSA: |
| 847 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key); |
| 848 | break; |
| 849 | case NC_SSH_KEY_ECDSA: |
| 850 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key); |
| 851 | break; |
| 852 | } |
| 853 | |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 854 | if (ret == SSH_EOF) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 855 | WRN("Failed to import a public key of \"%s\" (File access problem).", server_opts.authkeys[i].username); |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 856 | continue; |
| 857 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 858 | WRN("Failed to import a public key of \"%s\" (SSH error).", server_opts.authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 859 | continue; |
| 860 | } |
| 861 | |
| 862 | if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) { |
| 863 | ssh_key_free(pub_key); |
| 864 | break; |
| 865 | } |
| 866 | |
| 867 | ssh_key_free(pub_key); |
| 868 | } |
| 869 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 870 | if (i < server_opts.authkey_count) { |
| 871 | username = server_opts.authkeys[i].username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 872 | } |
| 873 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 874 | /* UNLOCK */ |
| 875 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 876 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 877 | return username; |
| 878 | } |
| 879 | |
| 880 | static void |
| 881 | nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg) |
| 882 | { |
| 883 | const char *username; |
| 884 | int signature_state; |
| 885 | |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 886 | if (server_opts.pubkey_auth_clb) { |
| 887 | if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 888 | goto fail; |
| 889 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 890 | } else { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 891 | if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) { |
| 892 | VRB("User \"%s\" tried to use an unknown (unauthorized) public key.", session->username); |
| 893 | goto fail; |
| 894 | } else if (strcmp(session->username, username)) { |
| 895 | VRB("User \"%s\" is not the username identified with the presented public key.", session->username); |
| 896 | goto fail; |
| 897 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 898 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 899 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 900 | signature_state = ssh_message_auth_publickey_state(msg); |
| 901 | if (signature_state == SSH_PUBLICKEY_STATE_VALID) { |
| 902 | VRB("User \"%s\" authenticated.", session->username); |
| 903 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 904 | ssh_message_auth_reply_success(msg, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 905 | } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 906 | /* accepting only the use of a public key */ |
| 907 | ssh_message_auth_reply_pk_ok_simple(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 908 | } |
| 909 | |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 910 | return; |
| 911 | |
| 912 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 913 | ++session->opts.server.ssh_auth_attempts; |
| 914 | VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 915 | ssh_message_reply_default(msg); |
| 916 | } |
| 917 | |
| 918 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 919 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 920 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 921 | ssh_channel chan; |
| 922 | |
| 923 | /* first channel request */ |
| 924 | if (!session->ti.libssh.channel) { |
| 925 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 926 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 927 | return -1; |
| 928 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 929 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 930 | if (!chan) { |
| 931 | ERR("Failed to create a new SSH channel."); |
| 932 | return -1; |
| 933 | } |
| 934 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 935 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 936 | /* additional channel request */ |
| 937 | } else { |
| 938 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 939 | if (!chan) { |
| 940 | ERR("Session %u: failed to create a new SSH channel.", session->id); |
| 941 | return -1; |
| 942 | } |
| 943 | /* channel was created and libssh stored it internally in the ssh_session structure, good enough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 944 | } |
| 945 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | static int |
| 950 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 951 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 952 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 953 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 954 | if (strcmp(subsystem, "netconf")) { |
| 955 | WRN("Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 956 | return -1; |
| 957 | } |
| 958 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 959 | if (session->ti.libssh.channel == channel) { |
| 960 | /* first channel requested */ |
| 961 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 962 | ERRINT; |
| 963 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 964 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 965 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
| 966 | ERR("Session %u: subsystem \"netconf\" requested for the second time.", session->id); |
| 967 | return -1; |
| 968 | } |
| 969 | |
| 970 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 971 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 972 | /* additional channel subsystem request, new session is ready as far as SSH is concerned */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 973 | new_session = nc_new_session(NC_SERVER, 1); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 974 | if (!new_session) { |
| 975 | ERRMEM; |
| 976 | return -1; |
| 977 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 978 | |
| 979 | /* insert the new session */ |
| 980 | if (!session->ti.libssh.next) { |
| 981 | new_session->ti.libssh.next = session; |
| 982 | } else { |
| 983 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 984 | } |
| 985 | session->ti.libssh.next = new_session; |
| 986 | |
| 987 | new_session->status = NC_STATUS_STARTING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 988 | new_session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 989 | new_session->io_lock = session->io_lock; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 990 | new_session->ti.libssh.channel = channel; |
| 991 | new_session->ti.libssh.session = session->ti.libssh.session; |
| 992 | new_session->username = lydict_insert(server_opts.ctx, session->username, 0); |
| 993 | new_session->host = lydict_insert(server_opts.ctx, session->host, 0); |
| 994 | new_session->port = session->port; |
| 995 | new_session->ctx = server_opts.ctx; |
Michal Vasko | 83d1532 | 2018-09-27 09:44:02 +0200 | [diff] [blame] | 996 | new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1002 | int |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1003 | nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1004 | { |
| 1005 | const char *str_type, *str_subtype = NULL, *username; |
| 1006 | int subtype, type; |
| 1007 | struct nc_session *session = (struct nc_session *)data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1008 | |
| 1009 | type = ssh_message_type(msg); |
| 1010 | subtype = ssh_message_subtype(msg); |
| 1011 | |
| 1012 | switch (type) { |
| 1013 | case SSH_REQUEST_AUTH: |
| 1014 | str_type = "request-auth"; |
| 1015 | switch (subtype) { |
| 1016 | case SSH_AUTH_METHOD_NONE: |
| 1017 | str_subtype = "none"; |
| 1018 | break; |
| 1019 | case SSH_AUTH_METHOD_PASSWORD: |
| 1020 | str_subtype = "password"; |
| 1021 | break; |
| 1022 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 1023 | str_subtype = "publickey"; |
| 1024 | break; |
| 1025 | case SSH_AUTH_METHOD_HOSTBASED: |
| 1026 | str_subtype = "hostbased"; |
| 1027 | break; |
| 1028 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 1029 | str_subtype = "interactive"; |
| 1030 | break; |
| 1031 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 1032 | str_subtype = "gssapi-mic"; |
| 1033 | break; |
| 1034 | } |
| 1035 | break; |
| 1036 | |
| 1037 | case SSH_REQUEST_CHANNEL_OPEN: |
| 1038 | str_type = "request-channel-open"; |
| 1039 | switch (subtype) { |
| 1040 | case SSH_CHANNEL_SESSION: |
| 1041 | str_subtype = "session"; |
| 1042 | break; |
| 1043 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 1044 | str_subtype = "direct-tcpip"; |
| 1045 | break; |
| 1046 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 1047 | str_subtype = "forwarded-tcpip"; |
| 1048 | break; |
| 1049 | case (int)SSH_CHANNEL_X11: |
| 1050 | str_subtype = "channel-x11"; |
| 1051 | break; |
| 1052 | case SSH_CHANNEL_UNKNOWN: |
| 1053 | /* fallthrough */ |
| 1054 | default: |
| 1055 | str_subtype = "unknown"; |
| 1056 | break; |
| 1057 | } |
| 1058 | break; |
| 1059 | |
| 1060 | case SSH_REQUEST_CHANNEL: |
| 1061 | str_type = "request-channel"; |
| 1062 | switch (subtype) { |
| 1063 | case SSH_CHANNEL_REQUEST_PTY: |
| 1064 | str_subtype = "pty"; |
| 1065 | break; |
| 1066 | case SSH_CHANNEL_REQUEST_EXEC: |
| 1067 | str_subtype = "exec"; |
| 1068 | break; |
| 1069 | case SSH_CHANNEL_REQUEST_SHELL: |
| 1070 | str_subtype = "shell"; |
| 1071 | break; |
| 1072 | case SSH_CHANNEL_REQUEST_ENV: |
| 1073 | str_subtype = "env"; |
| 1074 | break; |
| 1075 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 1076 | str_subtype = "subsystem"; |
| 1077 | break; |
| 1078 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 1079 | str_subtype = "window-change"; |
| 1080 | break; |
| 1081 | case SSH_CHANNEL_REQUEST_X11: |
| 1082 | str_subtype = "x11"; |
| 1083 | break; |
| 1084 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
| 1085 | /* fallthrough */ |
| 1086 | default: |
| 1087 | str_subtype = "unknown"; |
| 1088 | break; |
| 1089 | } |
| 1090 | break; |
| 1091 | |
| 1092 | case SSH_REQUEST_SERVICE: |
| 1093 | str_type = "request-service"; |
| 1094 | str_subtype = ssh_message_service_service(msg); |
| 1095 | break; |
| 1096 | |
| 1097 | case SSH_REQUEST_GLOBAL: |
| 1098 | str_type = "request-global"; |
| 1099 | switch (subtype) { |
| 1100 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 1101 | str_subtype = "tcpip-forward"; |
| 1102 | break; |
| 1103 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 1104 | str_subtype = "cancel-tcpip-forward"; |
| 1105 | break; |
| 1106 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
| 1107 | /* fallthrough */ |
| 1108 | default: |
| 1109 | str_subtype = "unknown"; |
| 1110 | break; |
| 1111 | } |
| 1112 | break; |
| 1113 | |
| 1114 | default: |
| 1115 | str_type = "unknown"; |
| 1116 | str_subtype = "unknown"; |
| 1117 | break; |
| 1118 | } |
| 1119 | |
| 1120 | VRB("Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype); |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1121 | if ((session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
| 1122 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 1123 | * but we got it now, during session free */ |
| 1124 | VRB("SSH message arrived on a %s session, the request will be denied.", |
| 1125 | (session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
| 1126 | ssh_message_reply_default(msg); |
| 1127 | return 0; |
| 1128 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1129 | session->flags |= NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1130 | |
| 1131 | /* |
| 1132 | * process known messages |
| 1133 | */ |
| 1134 | if (type == SSH_REQUEST_AUTH) { |
| 1135 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1136 | ERR("User \"%s\" authenticated, but requested another authentication.", session->username); |
| 1137 | ssh_message_reply_default(msg); |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1141 | /* save the username, do not let the client change it */ |
| 1142 | username = ssh_message_auth_user(msg); |
| 1143 | if (!session->username) { |
| 1144 | if (!username) { |
| 1145 | ERR("Denying an auth request without a username."); |
| 1146 | return 1; |
| 1147 | } |
| 1148 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1149 | session->username = lydict_insert(server_opts.ctx, username, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1150 | } else if (username) { |
| 1151 | if (strcmp(username, session->username)) { |
| 1152 | ERR("User \"%s\" changed its username to \"%s\".", session->username, username); |
| 1153 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1154 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1155 | return 1; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | if (subtype == SSH_AUTH_METHOD_NONE) { |
| 1160 | /* libssh will return the supported auth methods */ |
| 1161 | return 1; |
| 1162 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
| 1163 | nc_sshcb_auth_password(session, msg); |
| 1164 | return 0; |
| 1165 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
| 1166 | nc_sshcb_auth_pubkey(session, msg); |
| 1167 | return 0; |
| 1168 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
| 1169 | nc_sshcb_auth_kbdint(session, msg); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1173 | if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1174 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1175 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1176 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1177 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1178 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1179 | } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1180 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1181 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1182 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1183 | } else { |
| 1184 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1185 | } |
| 1186 | return 0; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | /* we did not process it */ |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1194 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1195 | static int |
| 1196 | nc_open_netconf_channel(struct nc_session *session, int timeout) |
| 1197 | { |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1198 | int ret; |
| 1199 | struct timespec ts_timeout, ts_cur; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* message callback is executed twice to give chance for the channel to be |
| 1202 | * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1203 | if (!timeout) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1204 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1205 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1206 | return -1; |
| 1207 | } |
| 1208 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1209 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1210 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1211 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1212 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1213 | return -1; |
| 1214 | } |
| 1215 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1216 | if (!session->ti.libssh.channel) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1221 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1222 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1223 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1224 | return -1; |
| 1225 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1226 | |
| 1227 | if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1228 | /* we did not receive subsystem-request, timeout */ |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | return 1; |
| 1233 | } |
| 1234 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1235 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1236 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1237 | nc_addtimespec(&ts_timeout, timeout); |
| 1238 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1239 | while (1) { |
| 1240 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1241 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1242 | return -1; |
| 1243 | } |
| 1244 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1245 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1246 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1247 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1248 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1249 | return -1; |
| 1250 | } |
| 1251 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1252 | if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1253 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1254 | } |
| 1255 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1256 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1257 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1258 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1259 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1260 | /* timeout */ |
| 1261 | ERR("Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
| 1262 | break; |
| 1263 | } |
| 1264 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1265 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1266 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1267 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1270 | static int |
| 1271 | nc_ssh_bind_add_hostkeys(ssh_bind sbind, const char **hostkeys, uint8_t hostkey_count) |
| 1272 | { |
| 1273 | uint8_t i; |
| 1274 | char *privkey_path, *privkey_data; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1275 | int ret; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1276 | NC_SSH_KEY_TYPE privkey_type; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1277 | |
| 1278 | if (!server_opts.hostkey_clb) { |
| 1279 | ERR("Callback for retrieving SSH host keys not set."); |
| 1280 | return -1; |
| 1281 | } |
| 1282 | |
| 1283 | for (i = 0; i < hostkey_count; ++i) { |
| 1284 | privkey_path = privkey_data = NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1285 | if (server_opts.hostkey_clb(hostkeys[i], server_opts.hostkey_data, &privkey_path, &privkey_data, &privkey_type)) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1286 | ERR("Host key callback failed."); |
| 1287 | return -1; |
| 1288 | } |
| 1289 | |
| 1290 | if (privkey_data) { |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1291 | privkey_path = base64der_key_to_tmp_file(privkey_data, nc_keytype2str(privkey_type)); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1292 | if (!privkey_path) { |
| 1293 | ERR("Temporarily storing a host key into a file failed (%s).", strerror(errno)); |
| 1294 | free(privkey_data); |
| 1295 | return -1; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1300 | |
| 1301 | /* cleanup */ |
| 1302 | if (privkey_data && unlink(privkey_path)) { |
| 1303 | WRN("Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno)); |
| 1304 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1305 | free(privkey_data); |
| 1306 | |
| 1307 | if (ret != SSH_OK) { |
Michal Vasko | 80075de | 2017-07-10 11:38:52 +0200 | [diff] [blame] | 1308 | ERR("Failed to set hostkey \"%s\" (%s).", hostkeys[i], privkey_path); |
| 1309 | } |
| 1310 | free(privkey_path); |
| 1311 | |
| 1312 | if (ret != SSH_OK) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1313 | return -1; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1320 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1321 | nc_accept_ssh_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1322 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1323 | ssh_bind sbind; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1324 | struct nc_server_ssh_opts *opts; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1325 | int libssh_auth_methods = 0, ret; |
| 1326 | struct timespec ts_timeout, ts_cur; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1327 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1328 | opts = session->data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1329 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1330 | /* other transport-specific data */ |
| 1331 | session->ti_type = NC_TI_LIBSSH; |
| 1332 | session->ti.libssh.session = ssh_new(); |
| 1333 | if (!session->ti.libssh.session) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1334 | ERR("Failed to initialize a new SSH session."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1335 | close(sock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1336 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1337 | } |
| 1338 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1339 | if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1340 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1341 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1342 | if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1343 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1344 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1345 | if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1346 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1347 | } |
| 1348 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1349 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1350 | sbind = ssh_bind_new(); |
| 1351 | if (!sbind) { |
| 1352 | ERR("Failed to create an SSH bind."); |
| 1353 | close(sock); |
| 1354 | return -1; |
| 1355 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1356 | |
| 1357 | if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) { |
| 1358 | close(sock); |
| 1359 | ssh_bind_free(sbind); |
| 1360 | return -1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1361 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1362 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1363 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1364 | /* remember that this session was just set as nc_sshcb_msg() parameter */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1365 | session->flags |= NC_SESSION_SSH_MSG_CB; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1366 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1367 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
| 1368 | ERR("SSH failed to accept a new connection (%s).", ssh_get_error(sbind)); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1369 | close(sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1370 | ssh_bind_free(sbind); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1371 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1372 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1373 | ssh_bind_free(sbind); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1374 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1375 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1376 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1377 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1378 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1379 | nc_addtimespec(&ts_timeout, timeout); |
| 1380 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1381 | while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
Michal Vasko | e65b449 | 2016-03-03 11:57:51 +0100 | [diff] [blame] | 1382 | /* this tends to take longer */ |
| 1383 | usleep(NC_TIMEOUT_STEP * 20); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1384 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1385 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1386 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1387 | break; |
| 1388 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | if (ret == SSH_AGAIN) { |
| 1392 | ERR("SSH key exchange timeout."); |
| 1393 | return 0; |
| 1394 | } else if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1395 | ERR("SSH key exchange error (%s).", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1396 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /* authenticate */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1400 | if (opts->auth_timeout) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1401 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1402 | nc_addtimespec(&ts_timeout, opts->auth_timeout * 1000); |
| 1403 | } |
| 1404 | while (1) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1405 | if (!nc_session_is_connected(session)) { |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1406 | ERR("Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1407 | return -1; |
| 1408 | } |
| 1409 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1410 | if (ssh_execute_message_callbacks(session->ti.libssh.session) != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1411 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1412 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1413 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1416 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1417 | break; |
| 1418 | } |
| 1419 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1420 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
| 1421 | ERR("Too many failed authentication attempts of user \"%s\".", session->username); |
| 1422 | return -1; |
| 1423 | } |
| 1424 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1425 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1426 | if (opts->auth_timeout) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1427 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1428 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1429 | /* timeout */ |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
| 1433 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1434 | |
| 1435 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1436 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1437 | if (session->username) { |
| 1438 | ERR("User \"%s\" failed to authenticate for too long, disconnecting.", session->username); |
| 1439 | } else { |
| 1440 | ERR("User failed to authenticate for too long, disconnecting."); |
| 1441 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1442 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1445 | /* open channel */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1446 | ret = nc_open_netconf_channel(session, timeout); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1447 | if (ret < 1) { |
| 1448 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1451 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1452 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1455 | API NC_MSG_TYPE |
| 1456 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1457 | { |
| 1458 | NC_MSG_TYPE msgtype; |
| 1459 | struct nc_session *new_session = NULL; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1460 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1461 | |
| 1462 | if (!orig_session) { |
| 1463 | ERRARG("orig_session"); |
| 1464 | return NC_MSG_ERROR; |
| 1465 | } else if (!session) { |
| 1466 | ERRARG("session"); |
| 1467 | return NC_MSG_ERROR; |
| 1468 | } |
| 1469 | |
| 1470 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) |
| 1471 | && orig_session->ti.libssh.next) { |
| 1472 | for (new_session = orig_session->ti.libssh.next; |
| 1473 | new_session != orig_session; |
| 1474 | new_session = new_session->ti.libssh.next) { |
| 1475 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1476 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1477 | /* we found our session */ |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | if (new_session == orig_session) { |
| 1482 | new_session = NULL; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | if (!new_session) { |
| 1487 | ERR("Session does not have a NETCONF SSH channel ready."); |
| 1488 | return NC_MSG_ERROR; |
| 1489 | } |
| 1490 | |
| 1491 | /* assign new SID atomically */ |
Michal Vasko | 69a3ff6 | 2018-11-09 09:31:48 +0100 | [diff] [blame] | 1492 | new_session->id = ATOMIC_INC(&server_opts.new_session_id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1493 | |
| 1494 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1495 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1496 | if (msgtype != NC_MSG_HELLO) { |
| 1497 | return msgtype; |
| 1498 | } |
| 1499 | |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1500 | nc_gettimespec_real(&ts_cur); |
| 1501 | new_session->opts.server.session_start = ts_cur.tv_sec; |
| 1502 | nc_gettimespec_mono(&ts_cur); |
| 1503 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1504 | new_session->status = NC_STATUS_RUNNING; |
| 1505 | *session = new_session; |
| 1506 | |
| 1507 | return msgtype; |
| 1508 | } |
| 1509 | |
| 1510 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1511 | nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1512 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1513 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1514 | NC_MSG_TYPE msgtype; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1515 | struct nc_session *new_session = NULL, *cur_session; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1516 | struct timespec ts_cur; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1517 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1518 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1519 | if (!ps) { |
| 1520 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1521 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1522 | } else if (!session) { |
| 1523 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1524 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1525 | } |
| 1526 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1527 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1528 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1529 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1530 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1531 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1532 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1533 | cur_session = ps->sessions[i]->session; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1534 | if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) |
| 1535 | && cur_session->ti.libssh.next) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1536 | /* an SSH session with more channels */ |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1537 | for (new_session = cur_session->ti.libssh.next; |
| 1538 | new_session != cur_session; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1539 | new_session = new_session->ti.libssh.next) { |
| 1540 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1541 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1542 | /* we found our session */ |
| 1543 | break; |
| 1544 | } |
| 1545 | } |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1546 | if (new_session != cur_session) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1547 | break; |
| 1548 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1549 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1550 | new_session = NULL; |
| 1551 | } |
| 1552 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1553 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1554 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1555 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1556 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1557 | if (!new_session) { |
| 1558 | ERR("No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1559 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* assign new SID atomically */ |
Michal Vasko | 69a3ff6 | 2018-11-09 09:31:48 +0100 | [diff] [blame] | 1563 | new_session->id = ATOMIC_INC(&server_opts.new_session_id); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1564 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1565 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1566 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1567 | if (msgtype != NC_MSG_HELLO) { |
| 1568 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1569 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1570 | |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1571 | nc_gettimespec_real(&ts_cur); |
| 1572 | new_session->opts.server.session_start = ts_cur.tv_sec; |
| 1573 | nc_gettimespec_mono(&ts_cur); |
| 1574 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1575 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1576 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1577 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1578 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1579 | } |