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