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