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