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