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