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 | { |
| 409 | ++server_opts.authkey_count; |
| 410 | server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys); |
| 411 | if (!server_opts.authkeys) { |
| 412 | ERRMEM; |
| 413 | return -1; |
| 414 | } |
| 415 | server_opts.authkeys[server_opts.authkey_count - 1].path = lydict_insert(server_opts.ctx, pubkey_path, 0); |
| 416 | server_opts.authkeys[server_opts.authkey_count - 1].base64 = lydict_insert(server_opts.ctx, pubkey_base64, 0); |
| 417 | server_opts.authkeys[server_opts.authkey_count - 1].type = type; |
| 418 | server_opts.authkeys[server_opts.authkey_count - 1].username = lydict_insert(server_opts.ctx, username, 0); |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | API int |
| 424 | 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] | 425 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 426 | if (!pubkey_path) { |
| 427 | ERRARG("pubkey_path"); |
| 428 | return -1; |
| 429 | } else if (!username) { |
| 430 | ERRARG("username"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 431 | return -1; |
| 432 | } |
| 433 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 434 | return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 438 | 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] | 439 | { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 440 | if (!pubkey_base64) { |
| 441 | ERRARG("pubkey_base64"); |
| 442 | return -1; |
| 443 | } else if (!type) { |
| 444 | ERRARG("type"); |
| 445 | return -1; |
| 446 | } else if (!username) { |
| 447 | ERRARG("username"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 448 | return -1; |
| 449 | } |
| 450 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 451 | return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 455 | nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
| 456 | const char *username) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 457 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 458 | uint32_t i; |
| 459 | int ret = -1; |
| 460 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 461 | /* LOCK */ |
| 462 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 463 | |
| 464 | if (!pubkey_path && !pubkey_base64 && !type && !username) { |
| 465 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 466 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].path); |
| 467 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64); |
| 468 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 469 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 470 | ret = 0; |
| 471 | } |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 472 | free(server_opts.authkeys); |
| 473 | server_opts.authkeys = NULL; |
| 474 | server_opts.authkey_count = 0; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 475 | } else { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 476 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 477 | if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path)) |
| 478 | && (!pubkey_base64 || strcmp(server_opts.authkeys[i].base64, pubkey_base64)) |
| 479 | && (!type || (server_opts.authkeys[i].type == type)) |
| 480 | && (!username || !strcmp(server_opts.authkeys[i].username, username))) { |
| 481 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].path); |
| 482 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64); |
| 483 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].username); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 484 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 485 | --server_opts.authkey_count; |
| 486 | if (i < server_opts.authkey_count) { |
| 487 | memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count], |
| 488 | sizeof *server_opts.authkeys); |
| 489 | } else if (!server_opts.authkey_count) { |
| 490 | free(server_opts.authkeys); |
| 491 | server_opts.authkeys = NULL; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 492 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 493 | |
| 494 | ret = 0; |
| 495 | } |
| 496 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 499 | /* UNLOCK */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 500 | pthread_mutex_unlock(&server_opts.authkey_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 501 | |
| 502 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 506 | nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 507 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 508 | nc_server_ssh_del_hostkey(NULL, opts); |
| 509 | if (opts->banner) { |
| 510 | lydict_remove(server_opts.ctx, opts->banner); |
| 511 | opts->banner = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 512 | } |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 515 | static char * |
| 516 | auth_password_get_pwd_hash(const char *username) |
| 517 | { |
| 518 | struct passwd *pwd, pwd_buf; |
| 519 | struct spwd *spwd, spwd_buf; |
| 520 | char *pass_hash = NULL, buf[256]; |
| 521 | |
| 522 | getpwnam_r(username, &pwd_buf, buf, 256, &pwd); |
| 523 | if (!pwd) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 524 | VRB("User \"%s\" not found locally.", username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | if (!strcmp(pwd->pw_passwd, "x")) { |
| 529 | getspnam_r(username, &spwd_buf, buf, 256, &spwd); |
| 530 | if (!spwd) { |
| 531 | VRB("Failed to retrieve the shadow entry for \"%s\".", username); |
| 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | pass_hash = spwd->sp_pwdp; |
| 536 | } else { |
| 537 | pass_hash = pwd->pw_passwd; |
| 538 | } |
| 539 | |
| 540 | if (!pass_hash) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 541 | ERR("No password could be retrieved for \"%s\".", username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | /* check the hash structure for special meaning */ |
| 546 | if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) { |
| 547 | VRB("User \"%s\" is not allowed to authenticate using a password.", username); |
| 548 | return NULL; |
| 549 | } |
| 550 | if (!strcmp(pass_hash, "*NP*")) { |
| 551 | VRB("Retrieving password for \"%s\" from a NIS+ server not supported.", username); |
| 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | return strdup(pass_hash); |
| 556 | } |
| 557 | |
| 558 | static int |
| 559 | auth_password_compare_pwd(const char *pass_hash, const char *pass_clear) |
| 560 | { |
| 561 | char *new_pass_hash; |
| 562 | struct crypt_data cdata; |
| 563 | |
| 564 | if (!pass_hash[0]) { |
| 565 | if (!pass_clear[0]) { |
| 566 | WRN("User authentication successful with an empty password!"); |
| 567 | return 0; |
| 568 | } else { |
| 569 | /* the user did now know he does not need any password, |
| 570 | * (which should not be used) so deny authentication */ |
| 571 | return 1; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | cdata.initialized = 0; |
| 576 | new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata); |
| 577 | return strcmp(new_pass_hash, pass_hash); |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | nc_sshcb_auth_password(struct nc_session *session, ssh_message msg) |
| 582 | { |
| 583 | char *pass_hash; |
| 584 | |
| 585 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 586 | 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] | 587 | VRB("User \"%s\" authenticated.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 588 | ssh_message_auth_reply_success(msg, 0); |
| 589 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 590 | free(pass_hash); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | free(pass_hash); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 595 | ++session->opts.server.ssh_auth_attempts; |
| 596 | 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] | 597 | ssh_message_reply_default(msg); |
| 598 | } |
| 599 | |
| 600 | static void |
| 601 | nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg) |
| 602 | { |
| 603 | char *pass_hash; |
| 604 | |
| 605 | if (!ssh_message_auth_kbdint_is_response(msg)) { |
| 606 | const char *prompts[] = {"Password: "}; |
| 607 | char echo[] = {0}; |
| 608 | |
| 609 | ssh_message_auth_interactive_request(msg, "Interactive SSH Authentication", "Type your password:", 1, prompts, echo); |
| 610 | } else { |
| 611 | if (ssh_userauth_kbdint_getnanswers(session->ti.libssh.session) != 1) { |
| 612 | ssh_message_reply_default(msg); |
| 613 | return; |
| 614 | } |
| 615 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 616 | if (!pass_hash) { |
| 617 | ssh_message_reply_default(msg); |
| 618 | return; |
| 619 | } |
| 620 | if (!auth_password_compare_pwd(pass_hash, ssh_userauth_kbdint_getanswer(session->ti.libssh.session, 0))) { |
| 621 | VRB("User \"%s\" authenticated.", session->username); |
| 622 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 623 | ssh_message_auth_reply_success(msg, 0); |
| 624 | } else { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 625 | ++session->opts.server.ssh_auth_attempts; |
| 626 | 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] | 627 | ssh_message_reply_default(msg); |
| 628 | } |
Radek Krejci | fb53374 | 2016-03-04 15:12:54 +0100 | [diff] [blame] | 629 | free(pass_hash); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | static const char * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 634 | auth_pubkey_compare_key(ssh_key key) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 635 | { |
| 636 | uint32_t i; |
| 637 | ssh_key pub_key; |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 638 | const char *username = NULL; |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 639 | int ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 640 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 641 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 642 | switch (server_opts.authkeys[i].type) { |
| 643 | case NC_SSH_KEY_UNKNOWN: |
| 644 | ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key); |
| 645 | break; |
| 646 | case NC_SSH_KEY_DSA: |
| 647 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key); |
| 648 | break; |
| 649 | case NC_SSH_KEY_RSA: |
| 650 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key); |
| 651 | break; |
| 652 | case NC_SSH_KEY_ECDSA: |
| 653 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key); |
| 654 | break; |
| 655 | } |
| 656 | |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 657 | if (ret == SSH_EOF) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 658 | 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] | 659 | continue; |
| 660 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 661 | 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] | 662 | continue; |
| 663 | } |
| 664 | |
| 665 | if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) { |
| 666 | ssh_key_free(pub_key); |
| 667 | break; |
| 668 | } |
| 669 | |
| 670 | ssh_key_free(pub_key); |
| 671 | } |
| 672 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 673 | if (i < server_opts.authkey_count) { |
| 674 | username = server_opts.authkeys[i].username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | return username; |
| 678 | } |
| 679 | |
| 680 | static void |
| 681 | nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg) |
| 682 | { |
| 683 | const char *username; |
| 684 | int signature_state; |
| 685 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 686 | if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 687 | VRB("User \"%s\" tried to use an unknown (unauthorized) public key.", session->username); |
| 688 | goto fail; |
| 689 | } else if (strcmp(session->username, username)) { |
| 690 | 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] | 691 | goto fail; |
| 692 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 693 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 694 | signature_state = ssh_message_auth_publickey_state(msg); |
| 695 | if (signature_state == SSH_PUBLICKEY_STATE_VALID) { |
| 696 | VRB("User \"%s\" authenticated.", session->username); |
| 697 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 698 | ssh_message_auth_reply_success(msg, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 699 | } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 700 | /* accepting only the use of a public key */ |
| 701 | ssh_message_auth_reply_pk_ok_simple(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 704 | return; |
| 705 | |
| 706 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 707 | ++session->opts.server.ssh_auth_attempts; |
| 708 | 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] | 709 | ssh_message_reply_default(msg); |
| 710 | } |
| 711 | |
| 712 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 713 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 714 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 715 | ssh_channel chan; |
| 716 | |
| 717 | /* first channel request */ |
| 718 | if (!session->ti.libssh.channel) { |
| 719 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 720 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 721 | return -1; |
| 722 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 723 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 724 | if (!chan) { |
| 725 | ERR("Failed to create a new SSH channel."); |
| 726 | return -1; |
| 727 | } |
| 728 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 729 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 730 | /* additional channel request */ |
| 731 | } else { |
| 732 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 733 | if (!chan) { |
| 734 | ERR("Session %u: failed to create a new SSH channel.", session->id); |
| 735 | return -1; |
| 736 | } |
| 737 | /* 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] | 738 | } |
| 739 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static int |
| 744 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 745 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 746 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 747 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 748 | if (strcmp(subsystem, "netconf")) { |
| 749 | WRN("Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 750 | return -1; |
| 751 | } |
| 752 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 753 | if (session->ti.libssh.channel == channel) { |
| 754 | /* first channel requested */ |
| 755 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 756 | ERRINT; |
| 757 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 758 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 759 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
| 760 | ERR("Session %u: subsystem \"netconf\" requested for the second time.", session->id); |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 765 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 766 | /* additional channel subsystem request, new session is ready as far as SSH is concerned */ |
| 767 | new_session = calloc(1, sizeof *new_session); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 768 | if (!new_session) { |
| 769 | ERRMEM; |
| 770 | return -1; |
| 771 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 772 | |
| 773 | /* insert the new session */ |
| 774 | if (!session->ti.libssh.next) { |
| 775 | new_session->ti.libssh.next = session; |
| 776 | } else { |
| 777 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 778 | } |
| 779 | session->ti.libssh.next = new_session; |
| 780 | |
| 781 | new_session->status = NC_STATUS_STARTING; |
| 782 | new_session->side = NC_SERVER; |
| 783 | new_session->ti_type = NC_TI_LIBSSH; |
| 784 | new_session->ti_lock = session->ti_lock; |
| 785 | new_session->ti.libssh.channel = channel; |
| 786 | new_session->ti.libssh.session = session->ti.libssh.session; |
| 787 | new_session->username = lydict_insert(server_opts.ctx, session->username, 0); |
| 788 | new_session->host = lydict_insert(server_opts.ctx, session->host, 0); |
| 789 | new_session->port = session->port; |
| 790 | new_session->ctx = server_opts.ctx; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 791 | new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX |
| 792 | | (session->flags & NC_SESSION_CALLHOME ? NC_SESSION_CALLHOME : 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 798 | int |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 799 | nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 800 | { |
| 801 | const char *str_type, *str_subtype = NULL, *username; |
| 802 | int subtype, type; |
| 803 | struct nc_session *session = (struct nc_session *)data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 804 | |
| 805 | type = ssh_message_type(msg); |
| 806 | subtype = ssh_message_subtype(msg); |
| 807 | |
| 808 | switch (type) { |
| 809 | case SSH_REQUEST_AUTH: |
| 810 | str_type = "request-auth"; |
| 811 | switch (subtype) { |
| 812 | case SSH_AUTH_METHOD_NONE: |
| 813 | str_subtype = "none"; |
| 814 | break; |
| 815 | case SSH_AUTH_METHOD_PASSWORD: |
| 816 | str_subtype = "password"; |
| 817 | break; |
| 818 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 819 | str_subtype = "publickey"; |
| 820 | break; |
| 821 | case SSH_AUTH_METHOD_HOSTBASED: |
| 822 | str_subtype = "hostbased"; |
| 823 | break; |
| 824 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 825 | str_subtype = "interactive"; |
| 826 | break; |
| 827 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 828 | str_subtype = "gssapi-mic"; |
| 829 | break; |
| 830 | } |
| 831 | break; |
| 832 | |
| 833 | case SSH_REQUEST_CHANNEL_OPEN: |
| 834 | str_type = "request-channel-open"; |
| 835 | switch (subtype) { |
| 836 | case SSH_CHANNEL_SESSION: |
| 837 | str_subtype = "session"; |
| 838 | break; |
| 839 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 840 | str_subtype = "direct-tcpip"; |
| 841 | break; |
| 842 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 843 | str_subtype = "forwarded-tcpip"; |
| 844 | break; |
| 845 | case (int)SSH_CHANNEL_X11: |
| 846 | str_subtype = "channel-x11"; |
| 847 | break; |
| 848 | case SSH_CHANNEL_UNKNOWN: |
| 849 | /* fallthrough */ |
| 850 | default: |
| 851 | str_subtype = "unknown"; |
| 852 | break; |
| 853 | } |
| 854 | break; |
| 855 | |
| 856 | case SSH_REQUEST_CHANNEL: |
| 857 | str_type = "request-channel"; |
| 858 | switch (subtype) { |
| 859 | case SSH_CHANNEL_REQUEST_PTY: |
| 860 | str_subtype = "pty"; |
| 861 | break; |
| 862 | case SSH_CHANNEL_REQUEST_EXEC: |
| 863 | str_subtype = "exec"; |
| 864 | break; |
| 865 | case SSH_CHANNEL_REQUEST_SHELL: |
| 866 | str_subtype = "shell"; |
| 867 | break; |
| 868 | case SSH_CHANNEL_REQUEST_ENV: |
| 869 | str_subtype = "env"; |
| 870 | break; |
| 871 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 872 | str_subtype = "subsystem"; |
| 873 | break; |
| 874 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 875 | str_subtype = "window-change"; |
| 876 | break; |
| 877 | case SSH_CHANNEL_REQUEST_X11: |
| 878 | str_subtype = "x11"; |
| 879 | break; |
| 880 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
| 881 | /* fallthrough */ |
| 882 | default: |
| 883 | str_subtype = "unknown"; |
| 884 | break; |
| 885 | } |
| 886 | break; |
| 887 | |
| 888 | case SSH_REQUEST_SERVICE: |
| 889 | str_type = "request-service"; |
| 890 | str_subtype = ssh_message_service_service(msg); |
| 891 | break; |
| 892 | |
| 893 | case SSH_REQUEST_GLOBAL: |
| 894 | str_type = "request-global"; |
| 895 | switch (subtype) { |
| 896 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 897 | str_subtype = "tcpip-forward"; |
| 898 | break; |
| 899 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 900 | str_subtype = "cancel-tcpip-forward"; |
| 901 | break; |
| 902 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
| 903 | /* fallthrough */ |
| 904 | default: |
| 905 | str_subtype = "unknown"; |
| 906 | break; |
| 907 | } |
| 908 | break; |
| 909 | |
| 910 | default: |
| 911 | str_type = "unknown"; |
| 912 | str_subtype = "unknown"; |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | 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] | 917 | if ((session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
| 918 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 919 | * but we got it now, during session free */ |
| 920 | VRB("SSH message arrived on a %s session, the request will be denied.", |
| 921 | (session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
| 922 | ssh_message_reply_default(msg); |
| 923 | return 0; |
| 924 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 925 | session->flags |= NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 926 | |
| 927 | /* |
| 928 | * process known messages |
| 929 | */ |
| 930 | if (type == SSH_REQUEST_AUTH) { |
| 931 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 932 | ERR("User \"%s\" authenticated, but requested another authentication.", session->username); |
| 933 | ssh_message_reply_default(msg); |
| 934 | return 0; |
| 935 | } |
| 936 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 937 | 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] | 938 | /* too many failed attempts */ |
| 939 | ssh_message_reply_default(msg); |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | /* save the username, do not let the client change it */ |
| 944 | username = ssh_message_auth_user(msg); |
| 945 | if (!session->username) { |
| 946 | if (!username) { |
| 947 | ERR("Denying an auth request without a username."); |
| 948 | return 1; |
| 949 | } |
| 950 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 951 | session->username = lydict_insert(server_opts.ctx, username, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 952 | } else if (username) { |
| 953 | if (strcmp(username, session->username)) { |
| 954 | ERR("User \"%s\" changed its username to \"%s\".", session->username, username); |
| 955 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 956 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 957 | return 1; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | if (subtype == SSH_AUTH_METHOD_NONE) { |
| 962 | /* libssh will return the supported auth methods */ |
| 963 | return 1; |
| 964 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
| 965 | nc_sshcb_auth_password(session, msg); |
| 966 | return 0; |
| 967 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
| 968 | nc_sshcb_auth_pubkey(session, msg); |
| 969 | return 0; |
| 970 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
| 971 | nc_sshcb_auth_kbdint(session, msg); |
| 972 | return 0; |
| 973 | } |
| 974 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 975 | 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] | 976 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 977 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 978 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 979 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 980 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 981 | } 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] | 982 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 983 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 984 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 985 | } else { |
| 986 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 987 | } |
| 988 | return 0; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | /* we did not process it */ |
| 993 | return 1; |
| 994 | } |
| 995 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 996 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 997 | static int |
| 998 | nc_open_netconf_channel(struct nc_session *session, int timeout) |
| 999 | { |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 1000 | int elapsed_usec = 0, ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1001 | |
| 1002 | /* message callback is executed twice to give chance for the channel to be |
| 1003 | * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1004 | if (!timeout) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1005 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1006 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1007 | return -1; |
| 1008 | } |
| 1009 | |
Michal vasko | 50cc94f | 2016-10-04 13:46:20 +0200 | [diff] [blame] | 1010 | ret = nc_timedlock(session->ti_lock, timeout, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1011 | if (ret != 1) { |
| 1012 | return ret; |
| 1013 | } |
| 1014 | |
| 1015 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1016 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1017 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1018 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1019 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1020 | return -1; |
| 1021 | } |
| 1022 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1023 | if (!session->ti.libssh.channel) { |
| 1024 | /* we did not receive channel-open, timeout */ |
| 1025 | pthread_mutex_unlock(session->ti_lock); |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1030 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1031 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1032 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1033 | pthread_mutex_unlock(session->ti_lock); |
| 1034 | return -1; |
| 1035 | } |
| 1036 | pthread_mutex_unlock(session->ti_lock); |
| 1037 | |
| 1038 | if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1039 | /* we did not receive subsystem-request, timeout */ |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | return 1; |
| 1044 | } |
| 1045 | |
| 1046 | while (1) { |
| 1047 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1048 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1049 | return -1; |
| 1050 | } |
| 1051 | |
Michal vasko | 50cc94f | 2016-10-04 13:46:20 +0200 | [diff] [blame] | 1052 | ret = nc_timedlock(session->ti_lock, timeout, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1053 | if (ret != 1) { |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1058 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1059 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1060 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1061 | pthread_mutex_unlock(session->ti_lock); |
| 1062 | return -1; |
| 1063 | } |
| 1064 | |
| 1065 | pthread_mutex_unlock(session->ti_lock); |
| 1066 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1067 | if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1068 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
Michal Vasko | 105bf27 | 2016-02-03 15:34:35 +0100 | [diff] [blame] | 1071 | if ((timeout != -1) && (elapsed_usec / 1000 >= timeout)) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1072 | /* timeout */ |
Michal Vasko | 842522c | 2016-03-01 09:20:13 +0100 | [diff] [blame] | 1073 | ERR("Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1074 | break; |
| 1075 | } |
| 1076 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1077 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 105bf27 | 2016-02-03 15:34:35 +0100 | [diff] [blame] | 1078 | elapsed_usec += NC_TIMEOUT_STEP; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1079 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1080 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1081 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1082 | } |
| 1083 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1084 | static int |
| 1085 | nc_ssh_bind_add_hostkeys(ssh_bind sbind, const char **hostkeys, uint8_t hostkey_count) |
| 1086 | { |
| 1087 | uint8_t i; |
| 1088 | char *privkey_path, *privkey_data; |
| 1089 | int privkey_data_rsa, ret; |
| 1090 | |
| 1091 | if (!server_opts.hostkey_clb) { |
| 1092 | ERR("Callback for retrieving SSH host keys not set."); |
| 1093 | return -1; |
| 1094 | } |
| 1095 | |
| 1096 | for (i = 0; i < hostkey_count; ++i) { |
| 1097 | privkey_path = privkey_data = NULL; |
| 1098 | if (server_opts.hostkey_clb(hostkeys[i], server_opts.hostkey_data, &privkey_path, &privkey_data, &privkey_data_rsa)) { |
| 1099 | ERR("Host key callback failed."); |
| 1100 | return -1; |
| 1101 | } |
| 1102 | |
| 1103 | if (privkey_data) { |
| 1104 | privkey_path = base64der_key_to_tmp_file(privkey_data, privkey_data_rsa); |
| 1105 | if (!privkey_path) { |
| 1106 | ERR("Temporarily storing a host key into a file failed (%s).", strerror(errno)); |
| 1107 | free(privkey_data); |
| 1108 | return -1; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1113 | |
| 1114 | /* cleanup */ |
| 1115 | if (privkey_data && unlink(privkey_path)) { |
| 1116 | WRN("Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno)); |
| 1117 | } |
| 1118 | free(privkey_path); |
| 1119 | free(privkey_data); |
| 1120 | |
| 1121 | if (ret != SSH_OK) { |
| 1122 | ERR("Failed to set hostkey \"%s\" (%s).", hostkeys[i], ssh_get_error(sbind)); |
| 1123 | return -1; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1130 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1131 | nc_accept_ssh_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1132 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1133 | ssh_bind sbind; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1134 | struct nc_server_ssh_opts *opts; |
Michal Vasko | 72387da | 2016-02-02 15:52:41 +0100 | [diff] [blame] | 1135 | int libssh_auth_methods = 0, elapsed_usec = 0, ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1136 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1137 | opts = session->data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1138 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1139 | /* other transport-specific data */ |
| 1140 | session->ti_type = NC_TI_LIBSSH; |
| 1141 | session->ti.libssh.session = ssh_new(); |
| 1142 | if (!session->ti.libssh.session) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1143 | ERR("Failed to initialize a new SSH session."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1144 | close(sock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1145 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1148 | if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1149 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1150 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1151 | if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1152 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1153 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1154 | if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1155 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1156 | } |
| 1157 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1158 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1159 | sbind = ssh_bind_new(); |
| 1160 | if (!sbind) { |
| 1161 | ERR("Failed to create an SSH bind."); |
| 1162 | close(sock); |
| 1163 | return -1; |
| 1164 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1165 | |
| 1166 | if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) { |
| 1167 | close(sock); |
| 1168 | ssh_bind_free(sbind); |
| 1169 | return -1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1170 | } |
| 1171 | if (opts->banner) { |
| 1172 | ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_BANNER, opts->banner); |
| 1173 | } |
| 1174 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1175 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1176 | /* remember that this session was just set as nc_sshcb_msg() parameter */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1177 | session->flags |= NC_SESSION_SSH_MSG_CB; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1178 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1179 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
| 1180 | 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] | 1181 | close(sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1182 | ssh_bind_free(sbind); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1183 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1184 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1185 | ssh_bind_free(sbind); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1186 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1187 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1188 | |
| 1189 | while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
Michal Vasko | e65b449 | 2016-03-03 11:57:51 +0100 | [diff] [blame] | 1190 | /* this tends to take longer */ |
| 1191 | usleep(NC_TIMEOUT_STEP * 20); |
| 1192 | elapsed_usec += NC_TIMEOUT_STEP * 20; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1193 | if ((timeout > -1) && (elapsed_usec / 1000 >= timeout)) { |
| 1194 | break; |
| 1195 | } |
| 1196 | } |
| 1197 | if (ret == SSH_AGAIN) { |
| 1198 | ERR("SSH key exchange timeout."); |
| 1199 | return 0; |
| 1200 | } else if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1201 | 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] | 1202 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | /* authenticate */ |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1206 | elapsed_usec = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1207 | do { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1208 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1209 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1210 | return -1; |
| 1211 | } |
| 1212 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1213 | if (ssh_execute_message_callbacks(session->ti.libssh.session) != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1214 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1215 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1216 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1220 | break; |
| 1221 | } |
| 1222 | |
| 1223 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 72387da | 2016-02-02 15:52:41 +0100 | [diff] [blame] | 1224 | elapsed_usec += NC_TIMEOUT_STEP; |
Michal Vasko | ab63994 | 2016-02-29 16:23:47 +0100 | [diff] [blame] | 1225 | } while (!opts->auth_timeout || (elapsed_usec / 1000000 < opts->auth_timeout)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1226 | |
| 1227 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1228 | /* timeout */ |
Michal Vasko | 842522c | 2016-03-01 09:20:13 +0100 | [diff] [blame] | 1229 | ERR("Client failed to authenticate for too long, disconnecting."); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1230 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1231 | } |
| 1232 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1233 | /* open channel */ |
Michal Vasko | ab63994 | 2016-02-29 16:23:47 +0100 | [diff] [blame] | 1234 | 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] | 1235 | if (ret < 1) { |
| 1236 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1239 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1240 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1241 | } |
| 1242 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1243 | API NC_MSG_TYPE |
| 1244 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1245 | { |
| 1246 | NC_MSG_TYPE msgtype; |
| 1247 | struct nc_session *new_session = NULL; |
| 1248 | |
| 1249 | if (!orig_session) { |
| 1250 | ERRARG("orig_session"); |
| 1251 | return NC_MSG_ERROR; |
| 1252 | } else if (!session) { |
| 1253 | ERRARG("session"); |
| 1254 | return NC_MSG_ERROR; |
| 1255 | } |
| 1256 | |
| 1257 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) |
| 1258 | && orig_session->ti.libssh.next) { |
| 1259 | for (new_session = orig_session->ti.libssh.next; |
| 1260 | new_session != orig_session; |
| 1261 | new_session = new_session->ti.libssh.next) { |
| 1262 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1263 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1264 | /* we found our session */ |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | if (new_session == orig_session) { |
| 1269 | new_session = NULL; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | if (!new_session) { |
| 1274 | ERR("Session does not have a NETCONF SSH channel ready."); |
| 1275 | return NC_MSG_ERROR; |
| 1276 | } |
| 1277 | |
| 1278 | /* assign new SID atomically */ |
| 1279 | pthread_spin_lock(&server_opts.sid_lock); |
| 1280 | new_session->id = server_opts.new_session_id++; |
| 1281 | pthread_spin_unlock(&server_opts.sid_lock); |
| 1282 | |
| 1283 | /* NETCONF handshake */ |
| 1284 | msgtype = nc_handshake(new_session); |
| 1285 | if (msgtype != NC_MSG_HELLO) { |
| 1286 | return msgtype; |
| 1287 | } |
| 1288 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1289 | 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] | 1290 | new_session->status = NC_STATUS_RUNNING; |
| 1291 | *session = new_session; |
| 1292 | |
| 1293 | return msgtype; |
| 1294 | } |
| 1295 | |
| 1296 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1297 | 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] | 1298 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1299 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1300 | NC_MSG_TYPE msgtype; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1301 | struct nc_session *new_session = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1302 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1303 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1304 | if (!ps) { |
| 1305 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1306 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1307 | } else if (!session) { |
| 1308 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1309 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1310 | } |
| 1311 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1312 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1313 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1314 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1315 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1316 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1317 | for (i = 0; i < ps->session_count; ++i) { |
| 1318 | if ((ps->sessions[i]->status == NC_STATUS_RUNNING) && (ps->sessions[i]->ti_type == NC_TI_LIBSSH) |
| 1319 | && ps->sessions[i]->ti.libssh.next) { |
| 1320 | /* an SSH session with more channels */ |
| 1321 | for (new_session = ps->sessions[i]->ti.libssh.next; |
| 1322 | new_session != ps->sessions[i]; |
| 1323 | new_session = new_session->ti.libssh.next) { |
| 1324 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1325 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1326 | /* we found our session */ |
| 1327 | break; |
| 1328 | } |
| 1329 | } |
| 1330 | if (new_session != ps->sessions[i]) { |
| 1331 | break; |
| 1332 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1333 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1334 | new_session = NULL; |
| 1335 | } |
| 1336 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1337 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1338 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1339 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1340 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1341 | if (!new_session) { |
| 1342 | ERR("No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1343 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* assign new SID atomically */ |
| 1347 | pthread_spin_lock(&server_opts.sid_lock); |
| 1348 | new_session->id = server_opts.new_session_id++; |
| 1349 | pthread_spin_unlock(&server_opts.sid_lock); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1350 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1351 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1352 | msgtype = nc_handshake(new_session); |
| 1353 | if (msgtype != NC_MSG_HELLO) { |
| 1354 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1355 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1356 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1357 | 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] | 1358 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1359 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1360 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1361 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1362 | } |