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 | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 600 | /* LOCK */ |
| 601 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 602 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 603 | ++server_opts.authkey_count; |
| 604 | server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys); |
| 605 | if (!server_opts.authkeys) { |
| 606 | ERRMEM; |
| 607 | return -1; |
| 608 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 609 | lydict_insert(server_opts.ctx, pubkey_path, 0, &server_opts.authkeys[server_opts.authkey_count - 1].path); |
| 610 | 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] | 611 | server_opts.authkeys[server_opts.authkey_count - 1].type = type; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 612 | 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] | 613 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 614 | /* UNLOCK */ |
| 615 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 616 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | API int |
| 621 | 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] | 622 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 623 | if (!pubkey_path) { |
| 624 | ERRARG("pubkey_path"); |
| 625 | return -1; |
| 626 | } else if (!username) { |
| 627 | ERRARG("username"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 628 | return -1; |
| 629 | } |
| 630 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 631 | return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 635 | 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] | 636 | { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 637 | if (!pubkey_base64) { |
| 638 | ERRARG("pubkey_base64"); |
| 639 | return -1; |
| 640 | } else if (!type) { |
| 641 | ERRARG("type"); |
| 642 | return -1; |
| 643 | } else if (!username) { |
| 644 | ERRARG("username"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 645 | return -1; |
| 646 | } |
| 647 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 648 | return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 652 | 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] | 653 | const char *username) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 654 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 655 | uint32_t i; |
| 656 | int ret = -1; |
| 657 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 658 | /* LOCK */ |
| 659 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 660 | |
| 661 | if (!pubkey_path && !pubkey_base64 && !type && !username) { |
| 662 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 663 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].path); |
| 664 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64); |
| 665 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 666 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 667 | ret = 0; |
| 668 | } |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 669 | free(server_opts.authkeys); |
| 670 | server_opts.authkeys = NULL; |
| 671 | server_opts.authkey_count = 0; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 672 | } else { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 673 | for (i = 0; i < server_opts.authkey_count; ++i) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 674 | if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path)) && |
| 675 | (!pubkey_base64 || !strcmp(server_opts.authkeys[i].base64, pubkey_base64)) && |
| 676 | (!type || (server_opts.authkeys[i].type == type)) && |
| 677 | (!username || !strcmp(server_opts.authkeys[i].username, username))) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 678 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].path); |
| 679 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64); |
| 680 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].username); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 681 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 682 | --server_opts.authkey_count; |
| 683 | if (i < server_opts.authkey_count) { |
| 684 | memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count], |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 685 | sizeof *server_opts.authkeys); |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 686 | } else if (!server_opts.authkey_count) { |
| 687 | free(server_opts.authkeys); |
| 688 | server_opts.authkeys = NULL; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 689 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 690 | |
| 691 | ret = 0; |
| 692 | } |
| 693 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 696 | /* UNLOCK */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 697 | pthread_mutex_unlock(&server_opts.authkey_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 698 | |
| 699 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 703 | nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 704 | { |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 705 | nc_server_ssh_del_hostkey(NULL, -1, opts); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 706 | } |
| 707 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 708 | #ifdef HAVE_SHADOW |
| 709 | |
| 710 | static struct passwd * |
| 711 | auth_password_getpwnam(const char *username, struct passwd *pwd_buf, char **buf, size_t *buf_size) |
| 712 | { |
| 713 | struct passwd *pwd = NULL; |
| 714 | char *mem; |
| 715 | |
| 716 | do { |
| 717 | errno = 0; |
| 718 | getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd); |
| 719 | if (pwd) { |
| 720 | /* entry found */ |
| 721 | break; |
| 722 | } |
| 723 | |
| 724 | if (errno == ERANGE) { |
| 725 | /* small buffer, enlarge */ |
| 726 | *buf_size <<= 2; |
| 727 | mem = realloc(*buf, *buf_size); |
| 728 | if (!mem) { |
| 729 | ERRMEM; |
| 730 | return NULL; |
| 731 | } |
| 732 | *buf = mem; |
| 733 | } |
| 734 | } while (errno == ERANGE); |
| 735 | |
| 736 | return pwd; |
| 737 | } |
| 738 | |
| 739 | static struct spwd * |
| 740 | auth_password_getspnam(const char *username, struct spwd *spwd_buf, char **buf, size_t *buf_size) |
| 741 | { |
| 742 | struct spwd *spwd = NULL; |
| 743 | char *mem; |
| 744 | |
| 745 | do { |
| 746 | errno = 0; |
| 747 | # ifndef __QNXNTO__ |
| 748 | getspnam_r(username, spwd_buf, *buf, *buf_size, &spwd); |
| 749 | # else |
| 750 | spwd = getspnam_r(username, spwd_buf, *buf, *buf_size); |
| 751 | # endif |
| 752 | if (spwd) { |
| 753 | /* entry found */ |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | if (errno == ERANGE) { |
| 758 | /* small buffer, enlarge */ |
| 759 | *buf_size <<= 2; |
| 760 | mem = realloc(*buf, *buf_size); |
| 761 | if (!mem) { |
| 762 | ERRMEM; |
| 763 | return NULL; |
| 764 | } |
| 765 | *buf = mem; |
| 766 | } |
| 767 | } while (errno == ERANGE); |
| 768 | |
| 769 | return spwd; |
| 770 | } |
| 771 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 772 | static char * |
| 773 | auth_password_get_pwd_hash(const char *username) |
| 774 | { |
| 775 | struct passwd *pwd, pwd_buf; |
| 776 | struct spwd *spwd, spwd_buf; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 777 | char *pass_hash = NULL, *buf = NULL; |
| 778 | size_t buf_size = 256; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 779 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 780 | buf = malloc(buf_size); |
| 781 | if (!buf) { |
| 782 | ERRMEM; |
| 783 | goto error; |
| 784 | } |
| 785 | |
| 786 | pwd = auth_password_getpwnam(username, &pwd_buf, &buf, &buf_size); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 787 | if (!pwd) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 788 | VRB(NULL, "User \"%s\" not found locally.", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 789 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (!strcmp(pwd->pw_passwd, "x")) { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 793 | spwd = auth_password_getspnam(username, &spwd_buf, &buf, &buf_size); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 794 | if (!spwd) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 795 | VRB(NULL, "Failed to retrieve the shadow entry for \"%s\".", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 796 | goto error; |
Michal Vasko | 22b4fe7 | 2020-11-04 08:52:29 +0100 | [diff] [blame] | 797 | } 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] | 798 | WRN(NULL, "User \"%s\" account has expired.", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 799 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | pass_hash = spwd->sp_pwdp; |
| 803 | } else { |
| 804 | pass_hash = pwd->pw_passwd; |
| 805 | } |
| 806 | |
| 807 | if (!pass_hash) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 808 | ERR(NULL, "No password could be retrieved for \"%s\".", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 809 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /* check the hash structure for special meaning */ |
| 813 | if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 814 | 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] | 815 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 816 | } |
| 817 | if (!strcmp(pass_hash, "*NP*")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 818 | 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] | 819 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 822 | free(buf); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 823 | return strdup(pass_hash); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 824 | |
| 825 | error: |
| 826 | free(buf); |
| 827 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 828 | } |
| 829 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 830 | #else |
| 831 | |
| 832 | static char * |
| 833 | auth_password_get_pwd_hash(const char *username) |
| 834 | { |
| 835 | (void)username; |
| 836 | return strdup(""); |
| 837 | } |
| 838 | |
| 839 | #endif |
| 840 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 841 | static int |
| 842 | auth_password_compare_pwd(const char *pass_hash, const char *pass_clear) |
| 843 | { |
| 844 | char *new_pass_hash; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 845 | |
| 846 | #if defined (HAVE_CRYPT_R) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 847 | struct crypt_data cdata; |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 848 | #endif |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 849 | |
| 850 | if (!pass_hash[0]) { |
| 851 | if (!pass_clear[0]) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 852 | WRN(NULL, "User authentication successful with an empty password!"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 853 | return 0; |
| 854 | } else { |
| 855 | /* the user did now know he does not need any password, |
| 856 | * (which should not be used) so deny authentication */ |
| 857 | return 1; |
| 858 | } |
| 859 | } |
| 860 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 861 | #if defined (HAVE_CRYPT_R) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 862 | cdata.initialized = 0; |
| 863 | new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 864 | #else |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 865 | pthread_mutex_lock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 866 | new_pass_hash = crypt(pass_clear, pass_hash); |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 867 | pthread_mutex_unlock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 868 | #endif |
Andrew Langefeld | 158d6fd | 2018-06-11 18:51:44 -0500 | [diff] [blame] | 869 | |
| 870 | if (!new_pass_hash) { |
| 871 | return 1; |
| 872 | } |
| 873 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 874 | return strcmp(new_pass_hash, pass_hash); |
| 875 | } |
| 876 | |
| 877 | static void |
| 878 | nc_sshcb_auth_password(struct nc_session *session, ssh_message msg) |
| 879 | { |
| 880 | char *pass_hash; |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 881 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 882 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 883 | if (server_opts.passwd_auth_clb) { |
| 884 | auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data); |
| 885 | } else { |
| 886 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 887 | if (pass_hash) { |
| 888 | auth_ret = auth_password_compare_pwd(pass_hash, ssh_message_auth_password(msg)); |
| 889 | free(pass_hash); |
| 890 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 893 | if (!auth_ret) { |
| 894 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 895 | VRB(session, "User \"%s\" authenticated.", session->username); |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 896 | ssh_message_auth_reply_success(msg, 0); |
| 897 | } else { |
| 898 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 899 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 900 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 901 | ssh_message_reply_default(msg); |
| 902 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static void |
| 906 | nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg) |
| 907 | { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 908 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 909 | char *pass_hash; |
| 910 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 911 | if (server_opts.interactive_auth_clb) { |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 912 | 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] | 913 | } else { |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 914 | if (!ssh_message_auth_kbdint_is_response(msg)) { |
| 915 | const char *prompts[] = {"Password: "}; |
| 916 | char echo[] = {0}; |
| 917 | |
| 918 | 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] | 919 | auth_ret = -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 920 | } else { |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 921 | if (ssh_userauth_kbdint_getnanswers(session->ti.libssh.session) != 1) {// failed session |
| 922 | ssh_message_reply_default(msg); |
| 923 | return; |
| 924 | } |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 925 | pass_hash = auth_password_get_pwd_hash(session->username);// get hashed password |
| 926 | if (pass_hash) { |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 927 | /* Normalize auth_password_compare_pwd result to 0 or 1 */ |
| 928 | 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] | 929 | free(pass_hash);// free hashed password |
| 930 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 931 | } |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 932 | } |
| 933 | |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 934 | /* We have already sent a reply */ |
| 935 | if (auth_ret == -1) { |
| 936 | return; |
| 937 | } |
| 938 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 939 | /* Authenticate message based on outcome */ |
| 940 | if (!auth_ret) { |
| 941 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 942 | VRB(session, "User \"%s\" authenticated.", session->username); |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 943 | ssh_message_auth_reply_success(msg, 0); |
| 944 | } else { |
| 945 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 946 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 947 | session->opts.server.ssh_auth_attempts); |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 948 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
| 952 | static const char * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 953 | auth_pubkey_compare_key(ssh_key key) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 954 | { |
| 955 | uint32_t i; |
| 956 | ssh_key pub_key; |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 957 | const char *username = NULL; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 958 | int ret = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 959 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 960 | /* LOCK */ |
| 961 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 962 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 963 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 964 | switch (server_opts.authkeys[i].type) { |
| 965 | case NC_SSH_KEY_UNKNOWN: |
| 966 | ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key); |
| 967 | break; |
| 968 | case NC_SSH_KEY_DSA: |
| 969 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key); |
| 970 | break; |
| 971 | case NC_SSH_KEY_RSA: |
| 972 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key); |
| 973 | break; |
| 974 | case NC_SSH_KEY_ECDSA: |
| 975 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key); |
| 976 | break; |
| 977 | } |
| 978 | |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 979 | if (ret == SSH_EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 980 | 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] | 981 | continue; |
| 982 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 983 | 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] | 984 | continue; |
| 985 | } |
| 986 | |
| 987 | if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) { |
| 988 | ssh_key_free(pub_key); |
| 989 | break; |
| 990 | } |
| 991 | |
| 992 | ssh_key_free(pub_key); |
| 993 | } |
| 994 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 995 | if (i < server_opts.authkey_count) { |
| 996 | username = server_opts.authkeys[i].username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 997 | } |
| 998 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 999 | /* UNLOCK */ |
| 1000 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 1001 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1002 | return username; |
| 1003 | } |
| 1004 | |
| 1005 | static void |
| 1006 | nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg) |
| 1007 | { |
| 1008 | const char *username; |
| 1009 | int signature_state; |
| 1010 | |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 1011 | if (server_opts.pubkey_auth_clb) { |
| 1012 | 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] | 1013 | goto fail; |
| 1014 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 1015 | } else { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 1016 | if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1017 | 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] | 1018 | goto fail; |
| 1019 | } else if (strcmp(session->username, username)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1020 | 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] | 1021 | goto fail; |
| 1022 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1023 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1024 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1025 | signature_state = ssh_message_auth_publickey_state(msg); |
| 1026 | if (signature_state == SSH_PUBLICKEY_STATE_VALID) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1027 | VRB(session, "User \"%s\" authenticated.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1028 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 1029 | ssh_message_auth_reply_success(msg, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1030 | } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1031 | /* accepting only the use of a public key */ |
| 1032 | ssh_message_auth_reply_pk_ok_simple(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1035 | return; |
| 1036 | |
| 1037 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1038 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1039 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 1040 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1041 | ssh_message_reply_default(msg); |
| 1042 | } |
| 1043 | |
| 1044 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1045 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1046 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1047 | ssh_channel chan; |
| 1048 | |
| 1049 | /* first channel request */ |
| 1050 | if (!session->ti.libssh.channel) { |
| 1051 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1052 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1053 | return -1; |
| 1054 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1055 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1056 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1057 | ERR(session, "Failed to create a new SSH channel."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1058 | return -1; |
| 1059 | } |
| 1060 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1061 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1062 | /* additional channel request */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1063 | } else { |
| 1064 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1065 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1066 | 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] | 1067 | return -1; |
| 1068 | } |
| 1069 | /* 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] | 1070 | } |
| 1071 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | static int |
| 1076 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 1077 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1078 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1079 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1080 | if (strcmp(subsystem, "netconf")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1081 | WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1082 | return -1; |
| 1083 | } |
| 1084 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1085 | if (session->ti.libssh.channel == channel) { |
| 1086 | /* first channel requested */ |
| 1087 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 1088 | ERRINT; |
| 1089 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1090 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1091 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1092 | ERR(session, "Subsystem \"netconf\" requested for the second time."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1093 | return -1; |
| 1094 | } |
| 1095 | |
| 1096 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1097 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1098 | /* 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] | 1099 | new_session = nc_new_session(NC_SERVER, 1); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1100 | if (!new_session) { |
| 1101 | ERRMEM; |
| 1102 | return -1; |
| 1103 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1104 | |
| 1105 | /* insert the new session */ |
| 1106 | if (!session->ti.libssh.next) { |
| 1107 | new_session->ti.libssh.next = session; |
| 1108 | } else { |
| 1109 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 1110 | } |
| 1111 | session->ti.libssh.next = new_session; |
| 1112 | |
| 1113 | new_session->status = NC_STATUS_STARTING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1114 | new_session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1115 | new_session->io_lock = session->io_lock; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1116 | new_session->ti.libssh.channel = channel; |
| 1117 | new_session->ti.libssh.session = session->ti.libssh.session; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1118 | lydict_insert(server_opts.ctx, session->username, 0, &new_session->username); |
| 1119 | lydict_insert(server_opts.ctx, session->host, 0, &new_session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1120 | new_session->port = session->port; |
| 1121 | new_session->ctx = server_opts.ctx; |
Michal Vasko | 83d1532 | 2018-09-27 09:44:02 +0200 | [diff] [blame] | 1122 | 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] | 1123 | } |
| 1124 | |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1128 | int |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1129 | nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1130 | { |
| 1131 | const char *str_type, *str_subtype = NULL, *username; |
| 1132 | int subtype, type; |
| 1133 | struct nc_session *session = (struct nc_session *)data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1134 | |
| 1135 | type = ssh_message_type(msg); |
| 1136 | subtype = ssh_message_subtype(msg); |
| 1137 | |
| 1138 | switch (type) { |
| 1139 | case SSH_REQUEST_AUTH: |
| 1140 | str_type = "request-auth"; |
| 1141 | switch (subtype) { |
| 1142 | case SSH_AUTH_METHOD_NONE: |
| 1143 | str_subtype = "none"; |
| 1144 | break; |
| 1145 | case SSH_AUTH_METHOD_PASSWORD: |
| 1146 | str_subtype = "password"; |
| 1147 | break; |
| 1148 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 1149 | str_subtype = "publickey"; |
| 1150 | break; |
| 1151 | case SSH_AUTH_METHOD_HOSTBASED: |
| 1152 | str_subtype = "hostbased"; |
| 1153 | break; |
| 1154 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 1155 | str_subtype = "interactive"; |
| 1156 | break; |
| 1157 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 1158 | str_subtype = "gssapi-mic"; |
| 1159 | break; |
| 1160 | } |
| 1161 | break; |
| 1162 | |
| 1163 | case SSH_REQUEST_CHANNEL_OPEN: |
| 1164 | str_type = "request-channel-open"; |
| 1165 | switch (subtype) { |
| 1166 | case SSH_CHANNEL_SESSION: |
| 1167 | str_subtype = "session"; |
| 1168 | break; |
| 1169 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 1170 | str_subtype = "direct-tcpip"; |
| 1171 | break; |
| 1172 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 1173 | str_subtype = "forwarded-tcpip"; |
| 1174 | break; |
| 1175 | case (int)SSH_CHANNEL_X11: |
| 1176 | str_subtype = "channel-x11"; |
| 1177 | break; |
| 1178 | case SSH_CHANNEL_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1179 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1180 | default: |
| 1181 | str_subtype = "unknown"; |
| 1182 | break; |
| 1183 | } |
| 1184 | break; |
| 1185 | |
| 1186 | case SSH_REQUEST_CHANNEL: |
| 1187 | str_type = "request-channel"; |
| 1188 | switch (subtype) { |
| 1189 | case SSH_CHANNEL_REQUEST_PTY: |
| 1190 | str_subtype = "pty"; |
| 1191 | break; |
| 1192 | case SSH_CHANNEL_REQUEST_EXEC: |
| 1193 | str_subtype = "exec"; |
| 1194 | break; |
| 1195 | case SSH_CHANNEL_REQUEST_SHELL: |
| 1196 | str_subtype = "shell"; |
| 1197 | break; |
| 1198 | case SSH_CHANNEL_REQUEST_ENV: |
| 1199 | str_subtype = "env"; |
| 1200 | break; |
| 1201 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 1202 | str_subtype = "subsystem"; |
| 1203 | break; |
| 1204 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 1205 | str_subtype = "window-change"; |
| 1206 | break; |
| 1207 | case SSH_CHANNEL_REQUEST_X11: |
| 1208 | str_subtype = "x11"; |
| 1209 | break; |
| 1210 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1211 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1212 | default: |
| 1213 | str_subtype = "unknown"; |
| 1214 | break; |
| 1215 | } |
| 1216 | break; |
| 1217 | |
| 1218 | case SSH_REQUEST_SERVICE: |
| 1219 | str_type = "request-service"; |
| 1220 | str_subtype = ssh_message_service_service(msg); |
| 1221 | break; |
| 1222 | |
| 1223 | case SSH_REQUEST_GLOBAL: |
| 1224 | str_type = "request-global"; |
| 1225 | switch (subtype) { |
| 1226 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 1227 | str_subtype = "tcpip-forward"; |
| 1228 | break; |
| 1229 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 1230 | str_subtype = "cancel-tcpip-forward"; |
| 1231 | break; |
| 1232 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1233 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1234 | default: |
| 1235 | str_subtype = "unknown"; |
| 1236 | break; |
| 1237 | } |
| 1238 | break; |
| 1239 | |
| 1240 | default: |
| 1241 | str_type = "unknown"; |
| 1242 | str_subtype = "unknown"; |
| 1243 | break; |
| 1244 | } |
| 1245 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1246 | 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] | 1247 | if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1248 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 1249 | * but we got it now, during session free */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1250 | 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] | 1251 | (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1252 | ssh_message_reply_default(msg); |
| 1253 | return 0; |
| 1254 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1255 | session->flags |= NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1256 | |
| 1257 | /* |
| 1258 | * process known messages |
| 1259 | */ |
| 1260 | if (type == SSH_REQUEST_AUTH) { |
| 1261 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1262 | ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1263 | ssh_message_reply_default(msg); |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1267 | /* save the username, do not let the client change it */ |
| 1268 | username = ssh_message_auth_user(msg); |
| 1269 | if (!session->username) { |
| 1270 | if (!username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1271 | ERR(session, "Denying an auth request without a username."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1272 | return 1; |
| 1273 | } |
| 1274 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1275 | lydict_insert(server_opts.ctx, username, 0, &session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1276 | } else if (username) { |
| 1277 | if (strcmp(username, session->username)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1278 | ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1279 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1280 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1281 | return 1; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | if (subtype == SSH_AUTH_METHOD_NONE) { |
| 1286 | /* libssh will return the supported auth methods */ |
| 1287 | return 1; |
| 1288 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
| 1289 | nc_sshcb_auth_password(session, msg); |
| 1290 | return 0; |
| 1291 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
| 1292 | nc_sshcb_auth_pubkey(session, msg); |
| 1293 | return 0; |
| 1294 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
| 1295 | nc_sshcb_auth_kbdint(session, msg); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1299 | 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] | 1300 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1301 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1302 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1303 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1304 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1305 | } 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] | 1306 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1307 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1308 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1309 | } else { |
| 1310 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1311 | } |
| 1312 | return 0; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* we did not process it */ |
| 1317 | return 1; |
| 1318 | } |
| 1319 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1320 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1321 | static int |
| 1322 | nc_open_netconf_channel(struct nc_session *session, int timeout) |
| 1323 | { |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1324 | int ret; |
| 1325 | struct timespec ts_timeout, ts_cur; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1326 | |
| 1327 | /* message callback is executed twice to give chance for the channel to be |
| 1328 | * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1329 | if (!timeout) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1330 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1331 | ERR(session, "Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1332 | return -1; |
| 1333 | } |
| 1334 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1335 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1336 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1337 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1338 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1339 | return -1; |
| 1340 | } |
| 1341 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1342 | if (!session->ti.libssh.channel) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1347 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1348 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1349 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1350 | return -1; |
| 1351 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1352 | |
| 1353 | if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1354 | /* we did not receive subsystem-request, timeout */ |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1361 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1362 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1363 | nc_addtimespec(&ts_timeout, timeout); |
| 1364 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1365 | while (1) { |
| 1366 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1367 | ERR(session, "Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1368 | return -1; |
| 1369 | } |
| 1370 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1371 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1372 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1373 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1374 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1375 | return -1; |
| 1376 | } |
| 1377 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1378 | if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1379 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1380 | } |
| 1381 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1382 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1383 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1384 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1385 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1386 | /* timeout */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1387 | ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1388 | break; |
| 1389 | } |
| 1390 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1391 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1392 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1393 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1396 | static int |
| 1397 | nc_ssh_bind_add_hostkeys(ssh_bind sbind, const char **hostkeys, uint8_t hostkey_count) |
| 1398 | { |
| 1399 | uint8_t i; |
| 1400 | char *privkey_path, *privkey_data; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1401 | int ret; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1402 | NC_SSH_KEY_TYPE privkey_type; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1403 | |
| 1404 | if (!server_opts.hostkey_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1405 | ERR(NULL, "Callback for retrieving SSH host keys not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1406 | return -1; |
| 1407 | } |
| 1408 | |
| 1409 | for (i = 0; i < hostkey_count; ++i) { |
| 1410 | privkey_path = privkey_data = NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1411 | 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] | 1412 | ERR(NULL, "Host key callback failed."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1413 | return -1; |
| 1414 | } |
| 1415 | |
| 1416 | if (privkey_data) { |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1417 | 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] | 1418 | if (!privkey_path) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1419 | 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] | 1420 | free(privkey_data); |
| 1421 | return -1; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1426 | |
| 1427 | /* cleanup */ |
| 1428 | if (privkey_data && unlink(privkey_path)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1429 | 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] | 1430 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1431 | free(privkey_data); |
| 1432 | |
| 1433 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1434 | ERR(NULL, "Failed to set hostkey \"%s\" (%s).", hostkeys[i], privkey_path); |
Michal Vasko | 80075de | 2017-07-10 11:38:52 +0200 | [diff] [blame] | 1435 | } |
| 1436 | free(privkey_path); |
| 1437 | |
| 1438 | if (ret != SSH_OK) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1439 | return -1; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1446 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1447 | nc_accept_ssh_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1448 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1449 | ssh_bind sbind; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1450 | struct nc_server_ssh_opts *opts; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1451 | int libssh_auth_methods = 0, ret; |
| 1452 | struct timespec ts_timeout, ts_cur; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1453 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1454 | opts = session->data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1455 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1456 | /* other transport-specific data */ |
| 1457 | session->ti_type = NC_TI_LIBSSH; |
| 1458 | session->ti.libssh.session = ssh_new(); |
| 1459 | if (!session->ti.libssh.session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1460 | ERR(NULL, "Failed to initialize a new SSH session."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1461 | close(sock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1462 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1463 | } |
| 1464 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1465 | if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1466 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1467 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1468 | if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1469 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1470 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1471 | if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1472 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1473 | } |
| 1474 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1475 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1476 | sbind = ssh_bind_new(); |
| 1477 | if (!sbind) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1478 | ERR(session, "Failed to create an SSH bind."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1479 | close(sock); |
| 1480 | return -1; |
| 1481 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1482 | |
| 1483 | if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) { |
| 1484 | close(sock); |
| 1485 | ssh_bind_free(sbind); |
| 1486 | return -1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1487 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1488 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1489 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1490 | /* remember that this session was just set as nc_sshcb_msg() parameter */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1491 | session->flags |= NC_SESSION_SSH_MSG_CB; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1492 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1493 | 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] | 1494 | 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] | 1495 | close(sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1496 | ssh_bind_free(sbind); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1497 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1498 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1499 | ssh_bind_free(sbind); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1500 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1501 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1502 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1503 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1504 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1505 | nc_addtimespec(&ts_timeout, timeout); |
| 1506 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1507 | while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
Michal Vasko | e65b449 | 2016-03-03 11:57:51 +0100 | [diff] [blame] | 1508 | /* this tends to take longer */ |
| 1509 | usleep(NC_TIMEOUT_STEP * 20); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1510 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1511 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1512 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1513 | break; |
| 1514 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | if (ret == SSH_AGAIN) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1518 | ERR(session, "SSH key exchange timeout."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1519 | return 0; |
| 1520 | } else if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1521 | 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] | 1522 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | /* authenticate */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1526 | if (opts->auth_timeout) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1527 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1528 | nc_addtimespec(&ts_timeout, opts->auth_timeout * 1000); |
| 1529 | } |
| 1530 | while (1) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1531 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1532 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1533 | return -1; |
| 1534 | } |
| 1535 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1536 | if (ssh_execute_message_callbacks(session->ti.libssh.session) != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1537 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1538 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1539 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1540 | } |
| 1541 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1542 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1543 | break; |
| 1544 | } |
| 1545 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1546 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1547 | ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username); |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1548 | return -1; |
| 1549 | } |
| 1550 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1551 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1552 | if (opts->auth_timeout) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1553 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1554 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1555 | /* timeout */ |
| 1556 | break; |
| 1557 | } |
| 1558 | } |
| 1559 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1560 | |
| 1561 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1562 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1563 | if (session->username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1564 | 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] | 1565 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1566 | ERR(session, "User failed to authenticate for too long, disconnecting."); |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1567 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1568 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1571 | /* open channel */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1572 | ret = nc_open_netconf_channel(session, timeout); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1573 | if (ret < 1) { |
| 1574 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1575 | } |
| 1576 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1577 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1578 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1579 | } |
| 1580 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1581 | API NC_MSG_TYPE |
| 1582 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1583 | { |
| 1584 | NC_MSG_TYPE msgtype; |
| 1585 | struct nc_session *new_session = NULL; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1586 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1587 | |
| 1588 | if (!orig_session) { |
| 1589 | ERRARG("orig_session"); |
| 1590 | return NC_MSG_ERROR; |
| 1591 | } else if (!session) { |
| 1592 | ERRARG("session"); |
| 1593 | return NC_MSG_ERROR; |
| 1594 | } |
| 1595 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1596 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) && |
| 1597 | orig_session->ti.libssh.next) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1598 | for (new_session = orig_session->ti.libssh.next; |
| 1599 | new_session != orig_session; |
| 1600 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1601 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1602 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1603 | /* we found our session */ |
| 1604 | break; |
| 1605 | } |
| 1606 | } |
| 1607 | if (new_session == orig_session) { |
| 1608 | new_session = NULL; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1613 | ERR(orig_session, "Session does not have a NETCONF SSH channel ready."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1614 | return NC_MSG_ERROR; |
| 1615 | } |
| 1616 | |
| 1617 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1618 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1619 | |
| 1620 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1621 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1622 | if (msgtype != NC_MSG_HELLO) { |
| 1623 | return msgtype; |
| 1624 | } |
| 1625 | |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1626 | nc_gettimespec_real(&ts_cur); |
| 1627 | new_session->opts.server.session_start = ts_cur.tv_sec; |
| 1628 | nc_gettimespec_mono(&ts_cur); |
| 1629 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1630 | new_session->status = NC_STATUS_RUNNING; |
| 1631 | *session = new_session; |
| 1632 | |
| 1633 | return msgtype; |
| 1634 | } |
| 1635 | |
| 1636 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1637 | 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] | 1638 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1639 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1640 | NC_MSG_TYPE msgtype; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1641 | struct nc_session *new_session = NULL, *cur_session; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1642 | struct timespec ts_cur; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1643 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1644 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1645 | if (!ps) { |
| 1646 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1647 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1648 | } else if (!session) { |
| 1649 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1650 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1651 | } |
| 1652 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1653 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1654 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1655 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1656 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1657 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1658 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1659 | cur_session = ps->sessions[i]->session; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1660 | if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) && |
| 1661 | cur_session->ti.libssh.next) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1662 | /* an SSH session with more channels */ |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1663 | for (new_session = cur_session->ti.libssh.next; |
| 1664 | new_session != cur_session; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1665 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1666 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1667 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1668 | /* we found our session */ |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1672 | if (new_session != cur_session) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1673 | break; |
| 1674 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1675 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1676 | new_session = NULL; |
| 1677 | } |
| 1678 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1679 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1680 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1681 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1682 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1683 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1684 | ERR(NULL, "No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1685 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1689 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1690 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1691 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1692 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1693 | if (msgtype != NC_MSG_HELLO) { |
| 1694 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1695 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1696 | |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1697 | nc_gettimespec_real(&ts_cur); |
| 1698 | new_session->opts.server.session_start = ts_cur.tv_sec; |
| 1699 | nc_gettimespec_mono(&ts_cur); |
| 1700 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1701 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1702 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1703 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1704 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1705 | } |