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