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