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