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 | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 65 | ERRARG; |
| 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) { |
| 125 | ERRARG; |
| 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)) { |
| 179 | ERRARG; |
| 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) { |
| 223 | ERRARG; |
| 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 | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 267 | ERRARG; |
| 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 | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 310 | if (!pubkey_path || !username) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 311 | ERRARG; |
| 312 | return -1; |
| 313 | } |
| 314 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 315 | ++opts->authkey_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 316 | opts->authkeys = nc_realloc(opts->authkeys, opts->authkey_count * sizeof *opts->authkeys); |
| 317 | if (!opts->authkeys) { |
| 318 | ERRMEM; |
| 319 | return -1; |
| 320 | } |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 321 | opts->authkeys[opts->authkey_count - 1].path = lydict_insert(server_opts.ctx, pubkey_path, 0); |
| 322 | 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] | 323 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 328 | 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] | 329 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 330 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 331 | struct nc_endpt *endpt; |
| 332 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 333 | /* LOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 334 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 335 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 336 | return -1; |
| 337 | } |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 338 | ret = nc_server_ssh_add_authkey(pubkey_path, username, endpt->ti_opts); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 339 | /* UNLOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 340 | nc_server_endpt_unlock(endpt); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 341 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 342 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 346 | 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] | 347 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 348 | int ret; |
| 349 | |
| 350 | /* OPTS LOCK */ |
| 351 | pthread_mutex_lock(&ssh_ch_opts_lock); |
| 352 | ret = nc_server_ssh_add_authkey(pubkey_path, username, &ssh_ch_opts); |
| 353 | /* OPTS UNLOCK */ |
| 354 | pthread_mutex_unlock(&ssh_ch_opts_lock); |
| 355 | |
| 356 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 360 | 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] | 361 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 362 | uint32_t i; |
| 363 | int ret = -1; |
| 364 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 365 | if (!pubkey_path && !username) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 366 | for (i = 0; i < opts->authkey_count; ++i) { |
| 367 | lydict_remove(server_opts.ctx, opts->authkeys[i].path); |
| 368 | lydict_remove(server_opts.ctx, opts->authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 369 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 370 | ret = 0; |
| 371 | } |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 372 | free(opts->authkeys); |
| 373 | opts->authkeys = NULL; |
| 374 | opts->authkey_count = 0; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 375 | } else { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 376 | for (i = 0; i < opts->authkey_count; ++i) { |
| 377 | if ((!pubkey_path || !strcmp(opts->authkeys[i].path, pubkey_path)) |
| 378 | && (!username || !strcmp(opts->authkeys[i].username, username))) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 379 | lydict_remove(server_opts.ctx, opts->authkeys[i].path); |
| 380 | lydict_remove(server_opts.ctx, opts->authkeys[i].username); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 381 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 382 | --opts->authkey_count; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 383 | if (i < opts->authkey_count) { |
| 384 | memcpy(&opts->authkeys[i], &opts->authkeys[opts->authkey_count], sizeof *opts->authkeys); |
| 385 | } else if (!opts->authkey_count) { |
| 386 | free(opts->authkeys); |
| 387 | opts->authkeys = NULL; |
| 388 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 389 | |
| 390 | ret = 0; |
| 391 | } |
| 392 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | return ret; |
| 396 | } |
| 397 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 398 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 399 | 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] | 400 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 401 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 402 | struct nc_endpt *endpt; |
| 403 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 404 | /* LOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 405 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 406 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 407 | return -1; |
| 408 | } |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 409 | ret = nc_server_ssh_del_authkey(pubkey_path, username, endpt->ti_opts); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 410 | /* UNLOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 411 | nc_server_endpt_unlock(endpt); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 412 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 413 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 417 | 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] | 418 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 419 | int ret; |
| 420 | |
| 421 | /* OPTS LOCK */ |
| 422 | pthread_mutex_lock(&ssh_ch_opts_lock); |
| 423 | ret = nc_server_ssh_del_authkey(pubkey_path, username, &ssh_ch_opts); |
| 424 | /* OPTS UNLOCK */ |
| 425 | pthread_mutex_unlock(&ssh_ch_opts_lock); |
| 426 | |
| 427 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 431 | nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 432 | { |
| 433 | if (opts->sshbind) { |
| 434 | ssh_bind_free(opts->sshbind); |
| 435 | opts->sshbind = NULL; |
| 436 | } |
| 437 | |
| 438 | nc_server_ssh_del_authkey(NULL, NULL, opts); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 441 | API void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 442 | nc_server_ssh_ch_clear_opts(void) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 443 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 444 | /* OPTS LOCK */ |
| 445 | pthread_mutex_lock(&ssh_ch_opts_lock); |
| 446 | nc_server_ssh_clear_opts(&ssh_ch_opts); |
| 447 | /* OPTS UNLOCK */ |
| 448 | pthread_mutex_unlock(&ssh_ch_opts_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static char * |
| 452 | auth_password_get_pwd_hash(const char *username) |
| 453 | { |
| 454 | struct passwd *pwd, pwd_buf; |
| 455 | struct spwd *spwd, spwd_buf; |
| 456 | char *pass_hash = NULL, buf[256]; |
| 457 | |
| 458 | getpwnam_r(username, &pwd_buf, buf, 256, &pwd); |
| 459 | if (!pwd) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 460 | VRB("User \"%s\" not found locally.", username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | if (!strcmp(pwd->pw_passwd, "x")) { |
| 465 | getspnam_r(username, &spwd_buf, buf, 256, &spwd); |
| 466 | if (!spwd) { |
| 467 | VRB("Failed to retrieve the shadow entry for \"%s\".", username); |
| 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | pass_hash = spwd->sp_pwdp; |
| 472 | } else { |
| 473 | pass_hash = pwd->pw_passwd; |
| 474 | } |
| 475 | |
| 476 | if (!pass_hash) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 477 | ERR("No password could be retrieved for \"%s\".", username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | /* check the hash structure for special meaning */ |
| 482 | if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) { |
| 483 | VRB("User \"%s\" is not allowed to authenticate using a password.", username); |
| 484 | return NULL; |
| 485 | } |
| 486 | if (!strcmp(pass_hash, "*NP*")) { |
| 487 | VRB("Retrieving password for \"%s\" from a NIS+ server not supported.", username); |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | return strdup(pass_hash); |
| 492 | } |
| 493 | |
| 494 | static int |
| 495 | auth_password_compare_pwd(const char *pass_hash, const char *pass_clear) |
| 496 | { |
| 497 | char *new_pass_hash; |
| 498 | struct crypt_data cdata; |
| 499 | |
| 500 | if (!pass_hash[0]) { |
| 501 | if (!pass_clear[0]) { |
| 502 | WRN("User authentication successful with an empty password!"); |
| 503 | return 0; |
| 504 | } else { |
| 505 | /* the user did now know he does not need any password, |
| 506 | * (which should not be used) so deny authentication */ |
| 507 | return 1; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | cdata.initialized = 0; |
| 512 | new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata); |
| 513 | return strcmp(new_pass_hash, pass_hash); |
| 514 | } |
| 515 | |
| 516 | static void |
| 517 | nc_sshcb_auth_password(struct nc_session *session, ssh_message msg) |
| 518 | { |
| 519 | char *pass_hash; |
| 520 | |
| 521 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 522 | 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] | 523 | VRB("User \"%s\" authenticated.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 524 | ssh_message_auth_reply_success(msg, 0); |
| 525 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 526 | free(pass_hash); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | free(pass_hash); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 531 | ++session->ssh_auth_attempts; |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 532 | 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] | 533 | ssh_message_reply_default(msg); |
| 534 | } |
| 535 | |
| 536 | static void |
| 537 | nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg) |
| 538 | { |
| 539 | char *pass_hash; |
| 540 | |
| 541 | if (!ssh_message_auth_kbdint_is_response(msg)) { |
| 542 | const char *prompts[] = {"Password: "}; |
| 543 | char echo[] = {0}; |
| 544 | |
| 545 | ssh_message_auth_interactive_request(msg, "Interactive SSH Authentication", "Type your password:", 1, prompts, echo); |
| 546 | } else { |
| 547 | if (ssh_userauth_kbdint_getnanswers(session->ti.libssh.session) != 1) { |
| 548 | ssh_message_reply_default(msg); |
| 549 | return; |
| 550 | } |
| 551 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 552 | if (!pass_hash) { |
| 553 | ssh_message_reply_default(msg); |
| 554 | return; |
| 555 | } |
| 556 | if (!auth_password_compare_pwd(pass_hash, ssh_userauth_kbdint_getanswer(session->ti.libssh.session, 0))) { |
| 557 | VRB("User \"%s\" authenticated.", session->username); |
| 558 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 559 | ssh_message_auth_reply_success(msg, 0); |
| 560 | } else { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 561 | ++session->ssh_auth_attempts; |
| 562 | 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] | 563 | ssh_message_reply_default(msg); |
| 564 | } |
Radek Krejci | fb53374 | 2016-03-04 15:12:54 +0100 | [diff] [blame] | 565 | free(pass_hash); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | static const char * |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 570 | 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] | 571 | { |
| 572 | uint32_t i; |
| 573 | ssh_key pub_key; |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 574 | const char *username = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 575 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 576 | for (i = 0; i < opts->authkey_count; ++i) { |
| 577 | if (ssh_pki_import_pubkey_file(opts->authkeys[i].path, &pub_key) != SSH_OK) { |
| 578 | if (eaccess(opts->authkeys[i].path, R_OK)) { |
| 579 | 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] | 580 | } else { |
Michal Vasko | e5612cf | 2016-02-02 15:52:16 +0100 | [diff] [blame] | 581 | 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] | 582 | } |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) { |
| 587 | ssh_key_free(pub_key); |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | ssh_key_free(pub_key); |
| 592 | } |
| 593 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 594 | if (i < opts->authkey_count) { |
| 595 | username = opts->authkeys[i].username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | return username; |
| 599 | } |
| 600 | |
| 601 | static void |
| 602 | nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg) |
| 603 | { |
| 604 | const char *username; |
| 605 | int signature_state; |
| 606 | |
| 607 | signature_state = ssh_message_auth_publickey_state(msg); |
| 608 | if (signature_state == SSH_PUBLICKEY_STATE_VALID) { |
| 609 | VRB("User \"%s\" authenticated.", session->username); |
| 610 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 611 | ssh_message_auth_reply_success(msg, 0); |
| 612 | return; |
| 613 | |
| 614 | } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 615 | 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] | 616 | VRB("User \"%s\" tried to use an unknown (unauthorized) public key.", session->username); |
| 617 | |
| 618 | } else if (strcmp(session->username, username)) { |
| 619 | VRB("User \"%s\" is not the username identified with the presented public key.", session->username); |
| 620 | |
| 621 | } else { |
| 622 | /* accepting only the use of a public key */ |
| 623 | ssh_message_auth_reply_pk_ok_simple(msg); |
| 624 | return; |
| 625 | } |
| 626 | } |
| 627 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 628 | ++session->ssh_auth_attempts; |
| 629 | 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] | 630 | ssh_message_reply_default(msg); |
| 631 | } |
| 632 | |
| 633 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 634 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 635 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 636 | ssh_channel chan; |
| 637 | |
| 638 | /* first channel request */ |
| 639 | if (!session->ti.libssh.channel) { |
| 640 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 641 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 642 | return -1; |
| 643 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 644 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 645 | if (!chan) { |
| 646 | ERR("Failed to create a new SSH channel."); |
| 647 | return -1; |
| 648 | } |
| 649 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 650 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 651 | /* additional channel request */ |
| 652 | } else { |
| 653 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 654 | if (!chan) { |
| 655 | ERR("Session %u: failed to create a new SSH channel.", session->id); |
| 656 | return -1; |
| 657 | } |
| 658 | /* 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] | 659 | } |
| 660 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static int |
| 665 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 666 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 667 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 668 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 669 | if (strcmp(subsystem, "netconf")) { |
| 670 | WRN("Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 671 | return -1; |
| 672 | } |
| 673 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 674 | if (session->ti.libssh.channel == channel) { |
| 675 | /* first channel requested */ |
| 676 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 677 | ERRINT; |
| 678 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 679 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 680 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
| 681 | ERR("Session %u: subsystem \"netconf\" requested for the second time.", session->id); |
| 682 | return -1; |
| 683 | } |
| 684 | |
| 685 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 686 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 687 | /* additional channel subsystem request, new session is ready as far as SSH is concerned */ |
| 688 | new_session = calloc(1, sizeof *new_session); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 689 | if (!new_session) { |
| 690 | ERRMEM; |
| 691 | return -1; |
| 692 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 693 | |
| 694 | /* insert the new session */ |
| 695 | if (!session->ti.libssh.next) { |
| 696 | new_session->ti.libssh.next = session; |
| 697 | } else { |
| 698 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 699 | } |
| 700 | session->ti.libssh.next = new_session; |
| 701 | |
| 702 | new_session->status = NC_STATUS_STARTING; |
| 703 | new_session->side = NC_SERVER; |
| 704 | new_session->ti_type = NC_TI_LIBSSH; |
| 705 | new_session->ti_lock = session->ti_lock; |
| 706 | new_session->ti.libssh.channel = channel; |
| 707 | new_session->ti.libssh.session = session->ti.libssh.session; |
| 708 | new_session->username = lydict_insert(server_opts.ctx, session->username, 0); |
| 709 | new_session->host = lydict_insert(server_opts.ctx, session->host, 0); |
| 710 | new_session->port = session->port; |
| 711 | new_session->ctx = server_opts.ctx; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 712 | new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX |
| 713 | | (session->flags & NC_SESSION_CALLHOME ? NC_SESSION_CALLHOME : 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 719 | int |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 720 | nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 721 | { |
| 722 | const char *str_type, *str_subtype = NULL, *username; |
| 723 | int subtype, type; |
| 724 | struct nc_session *session = (struct nc_session *)data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 725 | |
| 726 | type = ssh_message_type(msg); |
| 727 | subtype = ssh_message_subtype(msg); |
| 728 | |
| 729 | switch (type) { |
| 730 | case SSH_REQUEST_AUTH: |
| 731 | str_type = "request-auth"; |
| 732 | switch (subtype) { |
| 733 | case SSH_AUTH_METHOD_NONE: |
| 734 | str_subtype = "none"; |
| 735 | break; |
| 736 | case SSH_AUTH_METHOD_PASSWORD: |
| 737 | str_subtype = "password"; |
| 738 | break; |
| 739 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 740 | str_subtype = "publickey"; |
| 741 | break; |
| 742 | case SSH_AUTH_METHOD_HOSTBASED: |
| 743 | str_subtype = "hostbased"; |
| 744 | break; |
| 745 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 746 | str_subtype = "interactive"; |
| 747 | break; |
| 748 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 749 | str_subtype = "gssapi-mic"; |
| 750 | break; |
| 751 | } |
| 752 | break; |
| 753 | |
| 754 | case SSH_REQUEST_CHANNEL_OPEN: |
| 755 | str_type = "request-channel-open"; |
| 756 | switch (subtype) { |
| 757 | case SSH_CHANNEL_SESSION: |
| 758 | str_subtype = "session"; |
| 759 | break; |
| 760 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 761 | str_subtype = "direct-tcpip"; |
| 762 | break; |
| 763 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 764 | str_subtype = "forwarded-tcpip"; |
| 765 | break; |
| 766 | case (int)SSH_CHANNEL_X11: |
| 767 | str_subtype = "channel-x11"; |
| 768 | break; |
| 769 | case SSH_CHANNEL_UNKNOWN: |
| 770 | /* fallthrough */ |
| 771 | default: |
| 772 | str_subtype = "unknown"; |
| 773 | break; |
| 774 | } |
| 775 | break; |
| 776 | |
| 777 | case SSH_REQUEST_CHANNEL: |
| 778 | str_type = "request-channel"; |
| 779 | switch (subtype) { |
| 780 | case SSH_CHANNEL_REQUEST_PTY: |
| 781 | str_subtype = "pty"; |
| 782 | break; |
| 783 | case SSH_CHANNEL_REQUEST_EXEC: |
| 784 | str_subtype = "exec"; |
| 785 | break; |
| 786 | case SSH_CHANNEL_REQUEST_SHELL: |
| 787 | str_subtype = "shell"; |
| 788 | break; |
| 789 | case SSH_CHANNEL_REQUEST_ENV: |
| 790 | str_subtype = "env"; |
| 791 | break; |
| 792 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 793 | str_subtype = "subsystem"; |
| 794 | break; |
| 795 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 796 | str_subtype = "window-change"; |
| 797 | break; |
| 798 | case SSH_CHANNEL_REQUEST_X11: |
| 799 | str_subtype = "x11"; |
| 800 | break; |
| 801 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
| 802 | /* fallthrough */ |
| 803 | default: |
| 804 | str_subtype = "unknown"; |
| 805 | break; |
| 806 | } |
| 807 | break; |
| 808 | |
| 809 | case SSH_REQUEST_SERVICE: |
| 810 | str_type = "request-service"; |
| 811 | str_subtype = ssh_message_service_service(msg); |
| 812 | break; |
| 813 | |
| 814 | case SSH_REQUEST_GLOBAL: |
| 815 | str_type = "request-global"; |
| 816 | switch (subtype) { |
| 817 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 818 | str_subtype = "tcpip-forward"; |
| 819 | break; |
| 820 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 821 | str_subtype = "cancel-tcpip-forward"; |
| 822 | break; |
| 823 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
| 824 | /* fallthrough */ |
| 825 | default: |
| 826 | str_subtype = "unknown"; |
| 827 | break; |
| 828 | } |
| 829 | break; |
| 830 | |
| 831 | default: |
| 832 | str_type = "unknown"; |
| 833 | str_subtype = "unknown"; |
| 834 | break; |
| 835 | } |
| 836 | |
| 837 | 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] | 838 | if ((session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
| 839 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 840 | * but we got it now, during session free */ |
| 841 | VRB("SSH message arrived on a %s session, the request will be denied.", |
| 842 | (session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
| 843 | ssh_message_reply_default(msg); |
| 844 | return 0; |
| 845 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 846 | session->flags |= NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 847 | |
| 848 | /* |
| 849 | * process known messages |
| 850 | */ |
| 851 | if (type == SSH_REQUEST_AUTH) { |
| 852 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 853 | ERR("User \"%s\" authenticated, but requested another authentication.", session->username); |
| 854 | ssh_message_reply_default(msg); |
| 855 | return 0; |
| 856 | } |
| 857 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 858 | 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] | 859 | /* too many failed attempts */ |
| 860 | ssh_message_reply_default(msg); |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | /* save the username, do not let the client change it */ |
| 865 | username = ssh_message_auth_user(msg); |
| 866 | if (!session->username) { |
| 867 | if (!username) { |
| 868 | ERR("Denying an auth request without a username."); |
| 869 | return 1; |
| 870 | } |
| 871 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 872 | session->username = lydict_insert(server_opts.ctx, username, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 873 | } else if (username) { |
| 874 | if (strcmp(username, session->username)) { |
| 875 | ERR("User \"%s\" changed its username to \"%s\".", session->username, username); |
| 876 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 877 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 878 | return 1; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | if (subtype == SSH_AUTH_METHOD_NONE) { |
| 883 | /* libssh will return the supported auth methods */ |
| 884 | return 1; |
| 885 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
| 886 | nc_sshcb_auth_password(session, msg); |
| 887 | return 0; |
| 888 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
| 889 | nc_sshcb_auth_pubkey(session, msg); |
| 890 | return 0; |
| 891 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
| 892 | nc_sshcb_auth_kbdint(session, msg); |
| 893 | return 0; |
| 894 | } |
| 895 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 896 | 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] | 897 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 898 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 899 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 900 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 901 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 902 | } 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] | 903 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 904 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 905 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 906 | } else { |
| 907 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 908 | } |
| 909 | return 0; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | /* we did not process it */ |
| 914 | return 1; |
| 915 | } |
| 916 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 917 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 918 | static int |
| 919 | nc_open_netconf_channel(struct nc_session *session, int timeout) |
| 920 | { |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 921 | int elapsed_usec = 0, ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 922 | |
| 923 | /* message callback is executed twice to give chance for the channel to be |
| 924 | * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 925 | if (!timeout) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 926 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 927 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 928 | return -1; |
| 929 | } |
| 930 | |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 931 | ret = nc_timedlock(session->ti_lock, timeout); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 932 | if (ret != 1) { |
| 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 937 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 938 | ERR("Failed to receive SSH messages on a session (%s).", |
| 939 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 940 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 941 | return -1; |
| 942 | } |
| 943 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 944 | if (!session->ti.libssh.channel) { |
| 945 | /* we did not receive channel-open, timeout */ |
| 946 | pthread_mutex_unlock(session->ti_lock); |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 951 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 952 | ERR("Failed to receive SSH messages on a session (%s).", |
| 953 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 954 | pthread_mutex_unlock(session->ti_lock); |
| 955 | return -1; |
| 956 | } |
| 957 | pthread_mutex_unlock(session->ti_lock); |
| 958 | |
| 959 | if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 960 | /* we did not receive subsystem-request, timeout */ |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | return 1; |
| 965 | } |
| 966 | |
| 967 | while (1) { |
| 968 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 969 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 970 | return -1; |
| 971 | } |
| 972 | |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 973 | ret = nc_timedlock(session->ti_lock, timeout); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 974 | if (ret != 1) { |
| 975 | return ret; |
| 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 | |
| 986 | pthread_mutex_unlock(session->ti_lock); |
| 987 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 988 | if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 989 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 990 | } |
| 991 | |
Michal Vasko | 105bf27 | 2016-02-03 15:34:35 +0100 | [diff] [blame] | 992 | if ((timeout != -1) && (elapsed_usec / 1000 >= timeout)) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 993 | /* timeout */ |
Michal Vasko | 842522c | 2016-03-01 09:20:13 +0100 | [diff] [blame] | 994 | ERR("Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 995 | break; |
| 996 | } |
| 997 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 998 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 105bf27 | 2016-02-03 15:34:35 +0100 | [diff] [blame] | 999 | elapsed_usec += NC_TIMEOUT_STEP; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1000 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1001 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1002 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1003 | } |
| 1004 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1005 | /* ret 0 - timeout, 1 channel has data, 2 some other channel has data, |
| 1006 | * 3 status change, 4 new SSH message, 5 new NETCONF SSH channel, -1 error */ |
| 1007 | int |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 1008 | nc_ssh_pollin(struct nc_session *session, int timeout) |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1009 | { |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 1010 | int ret; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1011 | struct nc_session *new; |
| 1012 | |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 1013 | ret = nc_timedlock(session->ti_lock, timeout); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1014 | |
| 1015 | if (ret != 1) { |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1020 | pthread_mutex_unlock(session->ti_lock); |
| 1021 | |
| 1022 | if (ret != SSH_OK) { |
| 1023 | ERR("Session %u: failed to receive SSH messages (%s).", session->id, |
| 1024 | ssh_get_error(session->ti.libssh.session)); |
| 1025 | session->status = NC_STATUS_INVALID; |
| 1026 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1027 | return 3; |
| 1028 | } |
| 1029 | |
| 1030 | /* new SSH message */ |
| 1031 | if (session->flags & NC_SESSION_SSH_NEW_MSG) { |
| 1032 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1033 | if (session->ti.libssh.next) { |
| 1034 | for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) { |
| 1035 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel |
| 1036 | && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1037 | /* new NETCONF SSH channel */ |
| 1038 | return 5; |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | /* just some SSH message */ |
| 1044 | return 4; |
| 1045 | } |
| 1046 | |
| 1047 | /* no new SSH message, maybe NETCONF data? */ |
| 1048 | ret = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0); |
| 1049 | /* not this one */ |
| 1050 | if (!ret) { |
| 1051 | return 2; |
| 1052 | } else if (ret == SSH_ERROR) { |
| 1053 | ERR("Session %u: SSH channel error (%s).", session->id, |
| 1054 | ssh_get_error(session->ti.libssh.session)); |
| 1055 | session->status = NC_STATUS_INVALID; |
| 1056 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1057 | return 3; |
| 1058 | } else if (ret == SSH_EOF) { |
| 1059 | ERR("Session %u: communication channel unexpectedly closed (libssh).", |
| 1060 | session->id); |
| 1061 | session->status = NC_STATUS_INVALID; |
| 1062 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1063 | return 3; |
| 1064 | } |
| 1065 | |
| 1066 | return 1; |
| 1067 | } |
| 1068 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1069 | API int |
Michal Vasko | 8f5270d | 2016-02-29 16:22:25 +0100 | [diff] [blame] | 1070 | 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] | 1071 | { |
Michal Vasko | 8f5270d | 2016-02-29 16:22:25 +0100 | [diff] [blame] | 1072 | return nc_connect_callhome(host, port, NC_TI_LIBSSH, session); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1076 | nc_accept_ssh_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1077 | { |
| 1078 | struct nc_server_ssh_opts *opts; |
Michal Vasko | 72387da | 2016-02-02 15:52:41 +0100 | [diff] [blame] | 1079 | int libssh_auth_methods = 0, elapsed_usec = 0, ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1080 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1081 | opts = session->data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1082 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1083 | /* other transport-specific data */ |
| 1084 | session->ti_type = NC_TI_LIBSSH; |
| 1085 | session->ti.libssh.session = ssh_new(); |
| 1086 | if (!session->ti.libssh.session) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1087 | ERR("Failed to initialize a new SSH session."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1088 | close(sock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1089 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1092 | if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1093 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1094 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1095 | if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1096 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1097 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1098 | if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1099 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1100 | } |
| 1101 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1102 | |
| 1103 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1104 | /* remember that this session was just set as nc_sshcb_msg() parameter */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1105 | session->flags |= NC_SESSION_SSH_MSG_CB; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1106 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1107 | 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] | 1108 | 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] | 1109 | close(sock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1110 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1113 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1114 | |
| 1115 | while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
Michal Vasko | e65b449 | 2016-03-03 11:57:51 +0100 | [diff] [blame] | 1116 | /* this tends to take longer */ |
| 1117 | usleep(NC_TIMEOUT_STEP * 20); |
| 1118 | elapsed_usec += NC_TIMEOUT_STEP * 20; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1119 | if ((timeout > -1) && (elapsed_usec / 1000 >= timeout)) { |
| 1120 | break; |
| 1121 | } |
| 1122 | } |
| 1123 | if (ret == SSH_AGAIN) { |
| 1124 | ERR("SSH key exchange timeout."); |
| 1125 | return 0; |
| 1126 | } else if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1127 | 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] | 1128 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* authenticate */ |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1132 | elapsed_usec = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1133 | do { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1134 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1135 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1136 | return -1; |
| 1137 | } |
| 1138 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1139 | if (ssh_execute_message_callbacks(session->ti.libssh.session) != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1140 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1141 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1142 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1146 | break; |
| 1147 | } |
| 1148 | |
| 1149 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 72387da | 2016-02-02 15:52:41 +0100 | [diff] [blame] | 1150 | elapsed_usec += NC_TIMEOUT_STEP; |
Michal Vasko | ab63994 | 2016-02-29 16:23:47 +0100 | [diff] [blame] | 1151 | } while (!opts->auth_timeout || (elapsed_usec / 1000000 < opts->auth_timeout)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1152 | |
| 1153 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1154 | /* timeout */ |
Michal Vasko | 842522c | 2016-03-01 09:20:13 +0100 | [diff] [blame] | 1155 | ERR("Client failed to authenticate for too long, disconnecting."); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1156 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1159 | /* open channel */ |
Michal Vasko | ab63994 | 2016-02-29 16:23:47 +0100 | [diff] [blame] | 1160 | 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] | 1161 | if (ret < 1) { |
| 1162 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1165 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1166 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1167 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1168 | } |
| 1169 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1170 | API int |
| 1171 | 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] | 1172 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1173 | struct nc_session *new_session = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1174 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1175 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1176 | if (!ps || !session) { |
| 1177 | ERRARG; |
| 1178 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1179 | } |
| 1180 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1181 | /* LOCK */ |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame^] | 1182 | if (nc_ps_lock(ps)) { |
| 1183 | return -1; |
| 1184 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1185 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1186 | for (i = 0; i < ps->session_count; ++i) { |
| 1187 | if ((ps->sessions[i]->status == NC_STATUS_RUNNING) && (ps->sessions[i]->ti_type == NC_TI_LIBSSH) |
| 1188 | && ps->sessions[i]->ti.libssh.next) { |
| 1189 | /* an SSH session with more channels */ |
| 1190 | for (new_session = ps->sessions[i]->ti.libssh.next; |
| 1191 | new_session != ps->sessions[i]; |
| 1192 | new_session = new_session->ti.libssh.next) { |
| 1193 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1194 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1195 | /* we found our session */ |
| 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | if (new_session != ps->sessions[i]) { |
| 1200 | break; |
| 1201 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1202 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1203 | new_session = NULL; |
| 1204 | } |
| 1205 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1206 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1207 | /* UNLOCK */ |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame^] | 1208 | nc_ps_unlock(ps); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1209 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1210 | if (!new_session) { |
| 1211 | ERR("No session with a NETCONF SSH channel ready was found."); |
| 1212 | return -1; |
| 1213 | } |
| 1214 | |
| 1215 | /* assign new SID atomically */ |
| 1216 | pthread_spin_lock(&server_opts.sid_lock); |
| 1217 | new_session->id = server_opts.new_session_id++; |
| 1218 | pthread_spin_unlock(&server_opts.sid_lock); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1219 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1220 | /* NETCONF handshake */ |
| 1221 | if (nc_handshake(new_session)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1222 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1223 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1224 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1225 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1226 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1227 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1228 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1229 | } |