Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file session.c |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 - general session functions |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | b85767d | 2018-03-21 12:21:10 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [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 |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 13 | */ |
Michal Vasko | b85767d | 2018-03-21 12:21:10 +0100 | [diff] [blame] | 14 | #define _DEFAULT_SOURCE |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 15 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 16 | #include <assert.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 17 | #include <errno.h> |
Radek Krejci | 952eb86 | 2016-01-08 14:22:55 +0100 | [diff] [blame] | 18 | #include <stdlib.h> |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 19 | #include <string.h> |
Michal Vasko | b85767d | 2018-03-21 12:21:10 +0100 | [diff] [blame] | 20 | #include <unistd.h> |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 21 | #include <pthread.h> |
Radek Krejci | d6b73e1 | 2016-07-15 12:00:23 +0200 | [diff] [blame] | 22 | #include <sys/time.h> |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <netinet/tcp.h> |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 27 | #include <time.h> |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 28 | #include <ctype.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 29 | #include <libyang/libyang.h> |
| 30 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 31 | #include "compat.h" |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 32 | #include "session.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 33 | #include "libnetconf.h" |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 34 | #include "session_server.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 35 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 36 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 37 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 38 | # include <libssh/libssh.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 39 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 40 | #endif /* NC_ENABLED_SSH */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 41 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 42 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 43 | |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 44 | # include <openssl/conf.h> |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 45 | # include <openssl/err.h> |
| 46 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 47 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 48 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 49 | /* in seconds */ |
| 50 | #define NC_CLIENT_HELLO_TIMEOUT 60 |
fanchanghu | 75888b6 | 2017-08-01 19:51:20 +0800 | [diff] [blame] | 51 | #define NC_SERVER_HELLO_TIMEOUT 60 |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 52 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 53 | /* in milliseconds */ |
| 54 | #define NC_CLOSE_REPLY_TIMEOUT 200 |
| 55 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 56 | extern struct nc_server_opts server_opts; |
| 57 | |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 58 | int |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 59 | nc_gettimespec_mono(struct timespec *ts) |
| 60 | { |
| 61 | #ifdef CLOCK_MONOTONIC_RAW |
| 62 | return clock_gettime(CLOCK_MONOTONIC_RAW, ts); |
| 63 | #elif defined(CLOCK_MONOTONIC) |
| 64 | return clock_gettime(CLOCK_MONOTONIC, ts); |
| 65 | #else |
| 66 | /* no monotonic clock available, return realtime */ |
| 67 | return nc_gettimespec_real(ts); |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | int |
| 72 | nc_gettimespec_real(struct timespec *ts) |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 73 | { |
Michal Vasko | 0ef46df | 2016-09-21 14:03:18 +0200 | [diff] [blame] | 74 | #ifdef CLOCK_REALTIME |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 75 | return clock_gettime(CLOCK_REALTIME, ts); |
| 76 | #else |
| 77 | int rc; |
| 78 | struct timeval tv; |
| 79 | |
| 80 | rc = gettimeofday(&tv, NULL); |
| 81 | if (!rc) { |
| 82 | ts->tv_sec = (time_t)tv.tv_sec; |
| 83 | ts->tv_nsec = 1000L * (long)tv.tv_usec; |
| 84 | } |
| 85 | return rc; |
| 86 | #endif |
| 87 | } |
| 88 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 89 | /* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */ |
| 90 | int32_t |
Radek Krejci | da0afb2 | 2017-05-26 16:25:59 +0200 | [diff] [blame] | 91 | nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2) |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 92 | { |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 93 | int64_t nsec_diff = 0; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 94 | |
Michal Vasko | a82bd20 | 2017-03-03 14:07:29 +0100 | [diff] [blame] | 95 | nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L; |
| 96 | nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec); |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 97 | |
| 98 | return (nsec_diff ? nsec_diff / 1000000L : 0); |
| 99 | } |
| 100 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 101 | void |
| 102 | nc_addtimespec(struct timespec *ts, uint32_t msec) |
| 103 | { |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 104 | assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L)); |
| 105 | |
Michal Vasko | 0dfcd33 | 2017-03-03 13:14:39 +0100 | [diff] [blame] | 106 | ts->tv_sec += msec / 1000; |
| 107 | ts->tv_nsec += (msec % 1000) * 1000000L; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 108 | |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 109 | if (ts->tv_nsec >= 1000000000L) { |
| 110 | ++ts->tv_sec; |
| 111 | ts->tv_nsec -= 1000000000L; |
| 112 | } else if (ts->tv_nsec < 0) { |
| 113 | --ts->tv_sec; |
| 114 | ts->tv_nsec += 1000000000L; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 115 | } |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 116 | |
| 117 | assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L)); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 120 | const char * |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 121 | nc_keytype2str(NC_SSH_KEY_TYPE type) |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 122 | { |
| 123 | switch (type) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 124 | case NC_SSH_KEY_DSA: |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 125 | return "DSA"; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 126 | case NC_SSH_KEY_RSA: |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 127 | return "RSA"; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 128 | case NC_SSH_KEY_ECDSA: |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 129 | return "EC"; |
| 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | return NULL; |
| 135 | } |
| 136 | |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 137 | int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 138 | nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka) |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 139 | { |
| 140 | int opt; |
| 141 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 142 | opt = ka->enabled; |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 143 | if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) { |
| 144 | ERR("Could not set SO_KEEPALIVE option (%s).", strerror(errno)); |
| 145 | return -1; |
| 146 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 147 | if (!ka->enabled) { |
| 148 | return 0; |
| 149 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 150 | |
| 151 | #ifdef TCP_KEEPIDLE |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 152 | opt = ka->idle_time; |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 153 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) { |
| 154 | ERR("Setsockopt failed (%s).", strerror(errno)); |
| 155 | return -1; |
| 156 | } |
| 157 | #endif |
| 158 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 159 | #ifdef TCP_KEEPCNT |
| 160 | opt = ka->max_probes; |
| 161 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) { |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 162 | ERR("Setsockopt failed (%s).", strerror(errno)); |
| 163 | return -1; |
| 164 | } |
| 165 | #endif |
| 166 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 167 | #ifdef TCP_KEEPINTVL |
| 168 | opt = ka->probe_interval; |
| 169 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) { |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 170 | ERR("Setsockopt failed (%s).", strerror(errno)); |
| 171 | return -1; |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Radek Krejci | 2847292 | 2016-07-15 11:51:16 +0200 | [diff] [blame] | 178 | #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 179 | int |
| 180 | pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) |
| 181 | { |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 182 | int32_t diff; |
Radek Krejci | 2847292 | 2016-07-15 11:51:16 +0200 | [diff] [blame] | 183 | int rc; |
| 184 | struct timespec cur, dur; |
| 185 | |
| 186 | /* Try to acquire the lock and, if we fail, sleep for 5ms. */ |
| 187 | while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) { |
Michal Vasko | 145619f | 2018-02-16 10:08:45 +0100 | [diff] [blame] | 188 | nc_gettimespec_real(&cur); |
Radek Krejci | 2847292 | 2016-07-15 11:51:16 +0200 | [diff] [blame] | 189 | |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 190 | if ((diff = nc_difftimespec(&cur, abstime)) < 1) { |
| 191 | /* timeout */ |
Radek Krejci | 2847292 | 2016-07-15 11:51:16 +0200 | [diff] [blame] | 192 | break; |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 193 | } else if (diff < 5) { |
| 194 | /* sleep until timeout */ |
Michal Vasko | bf378cb | 2018-09-21 15:18:52 +0200 | [diff] [blame] | 195 | dur.tv_sec = 0; |
| 196 | dur.tv_nsec = (long)diff * 1000000; |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 197 | } else { |
| 198 | /* sleep 5 ms */ |
Radek Krejci | 2847292 | 2016-07-15 11:51:16 +0200 | [diff] [blame] | 199 | dur.tv_sec = 0; |
| 200 | dur.tv_nsec = 5000000; |
| 201 | } |
| 202 | |
| 203 | nanosleep(&dur, NULL); |
| 204 | } |
| 205 | |
| 206 | return rc; |
| 207 | } |
| 208 | #endif |
| 209 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 210 | struct nc_session * |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 211 | nc_new_session(NC_SIDE side, int shared_ti) |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 212 | { |
| 213 | struct nc_session *sess; |
| 214 | |
| 215 | sess = calloc(1, sizeof *sess); |
| 216 | if (!sess) { |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 220 | sess->side = side; |
| 221 | |
| 222 | if (side == NC_SERVER) { |
| 223 | sess->opts.server.rpc_lock = malloc(sizeof *sess->opts.server.rpc_lock); |
| 224 | sess->opts.server.rpc_cond = malloc(sizeof *sess->opts.server.rpc_cond); |
| 225 | sess->opts.server.rpc_inuse = malloc(sizeof *sess->opts.server.rpc_inuse); |
| 226 | if (!sess->opts.server.rpc_lock || !sess->opts.server.rpc_cond || !sess->opts.server.rpc_inuse) { |
| 227 | goto error; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 228 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 229 | pthread_mutex_init(sess->opts.server.rpc_lock, NULL); |
| 230 | pthread_cond_init(sess->opts.server.rpc_cond, NULL); |
| 231 | *sess->opts.server.rpc_inuse = 0; |
| 232 | } |
| 233 | |
| 234 | if (!shared_ti) { |
| 235 | sess->io_lock = malloc(sizeof *sess->io_lock); |
| 236 | if (!sess->io_lock) { |
| 237 | goto error; |
| 238 | } |
| 239 | pthread_mutex_init(sess->io_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | return sess; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 243 | |
| 244 | error: |
| 245 | if (side == NC_SERVER) { |
| 246 | free(sess->opts.server.rpc_lock); |
| 247 | free(sess->opts.server.rpc_cond); |
| 248 | free((int *)sess->opts.server.rpc_inuse); |
| 249 | } |
| 250 | free(sess); |
| 251 | return NULL; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 254 | /* |
| 255 | * @return 1 - success |
| 256 | * 0 - timeout |
| 257 | * -1 - error |
| 258 | */ |
| 259 | int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 260 | nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func) |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 261 | { |
| 262 | int ret; |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 263 | struct timespec ts_timeout; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 264 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 265 | if (session->side != NC_SERVER) { |
| 266 | ERRINT; |
| 267 | return -1; |
| 268 | } |
| 269 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 270 | if (timeout > 0) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 271 | nc_gettimespec_real(&ts_timeout); |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 272 | nc_addtimespec(&ts_timeout, timeout); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 273 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 274 | /* LOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 275 | ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 276 | if (!ret) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 277 | while (*session->opts.server.rpc_inuse) { |
| 278 | ret = pthread_cond_timedwait(session->opts.server.rpc_cond, session->opts.server.rpc_lock, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 279 | if (ret) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 280 | pthread_mutex_unlock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | } |
| 284 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 285 | } else if (!timeout) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 286 | /* LOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 287 | ret = pthread_mutex_trylock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 288 | if (!ret) { |
| 289 | /* be extra careful, someone could have been faster */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 290 | if (*session->opts.server.rpc_inuse) { |
| 291 | pthread_mutex_unlock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 292 | return 0; |
| 293 | } |
Michal vasko | 2f8e4b5 | 2016-10-05 13:04:11 +0200 | [diff] [blame] | 294 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 295 | } else { /* timeout == -1 */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 296 | /* LOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 297 | ret = pthread_mutex_lock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 298 | if (!ret) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 299 | while (*session->opts.server.rpc_inuse) { |
| 300 | ret = pthread_cond_wait(session->opts.server.rpc_cond, session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 301 | if (ret) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 302 | pthread_mutex_unlock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | } |
| 306 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 309 | if (ret) { |
| 310 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 311 | /* timeout */ |
| 312 | return 0; |
| 313 | } |
| 314 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 315 | /* error */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 316 | ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret)); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | /* ok */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 321 | assert(*session->opts.server.rpc_inuse == 0); |
| 322 | *session->opts.server.rpc_inuse = 1; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 323 | |
| 324 | /* UNLOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 325 | ret = pthread_mutex_unlock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 326 | if (ret) { |
| 327 | /* error */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 328 | ERR("%s: faile to RPC unlock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 336 | nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func) |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 337 | { |
| 338 | int ret; |
| 339 | struct timespec ts_timeout; |
| 340 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 341 | if (session->side != NC_SERVER) { |
| 342 | ERRINT; |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | assert(*session->opts.server.rpc_inuse); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 347 | |
| 348 | if (timeout > 0) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 349 | nc_gettimespec_real(&ts_timeout); |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 350 | nc_addtimespec(&ts_timeout, timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 351 | |
| 352 | /* LOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 353 | ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 354 | } else if (!timeout) { |
| 355 | /* LOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 356 | ret = pthread_mutex_trylock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 357 | } else { /* timeout == -1 */ |
| 358 | /* LOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 359 | ret = pthread_mutex_lock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) { |
| 363 | /* error */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 364 | ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 365 | return -1; |
| 366 | } else if (ret) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 367 | WRN("%s: session RPC lock timeout, should not happen."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 370 | *session->opts.server.rpc_inuse = 0; |
| 371 | pthread_cond_signal(session->opts.server.rpc_cond); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 372 | |
| 373 | if (!ret) { |
| 374 | /* UNLOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 375 | ret = pthread_mutex_unlock(session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 376 | if (ret) { |
| 377 | /* error */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 378 | ERR("%s: failed to RPC unlock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | } |
| 382 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 383 | return 1; |
| 384 | } |
| 385 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 386 | /* |
| 387 | * @return 1 - success |
| 388 | * 0 - timeout |
| 389 | * -1 - error |
| 390 | */ |
| 391 | int |
| 392 | nc_session_io_lock(struct nc_session *session, int timeout, const char *func) |
| 393 | { |
| 394 | int ret; |
| 395 | struct timespec ts_timeout; |
| 396 | |
| 397 | if (timeout > 0) { |
| 398 | nc_gettimespec_real(&ts_timeout); |
| 399 | nc_addtimespec(&ts_timeout, timeout); |
| 400 | |
| 401 | ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout); |
| 402 | } else if (!timeout) { |
| 403 | ret = pthread_mutex_trylock(session->io_lock); |
| 404 | } else { /* timeout == -1 */ |
Robin Jarry | 54ea296 | 2018-10-10 10:33:40 +0200 | [diff] [blame] | 405 | ret = pthread_mutex_lock(session->io_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if (ret) { |
| 409 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 410 | /* timeout */ |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* error */ |
| 415 | ERR("%s: failed to IO lock a session (%s).", func, strerror(ret)); |
| 416 | return -1; |
| 417 | } |
| 418 | |
| 419 | return 1; |
| 420 | } |
| 421 | |
| 422 | int |
| 423 | nc_session_io_unlock(struct nc_session *session, const char *func) |
| 424 | { |
| 425 | int ret; |
| 426 | |
| 427 | ret = pthread_mutex_unlock(session->io_lock); |
| 428 | if (ret) { |
| 429 | /* error */ |
| 430 | ERR("%s: failed to IO unlock a session (%s).", func, strerror(ret)); |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | return 1; |
| 435 | } |
| 436 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 437 | API NC_STATUS |
| 438 | nc_session_get_status(const struct nc_session *session) |
| 439 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 440 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 441 | ERRARG("session"); |
Radek Krejci | 465308c | 2017-05-22 14:49:10 +0200 | [diff] [blame] | 442 | return NC_STATUS_ERR; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 445 | return session->status; |
| 446 | } |
| 447 | |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 448 | API NC_SESSION_TERM_REASON |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 449 | nc_session_get_term_reason(const struct nc_session *session) |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 450 | { |
| 451 | if (!session) { |
| 452 | ERRARG("session"); |
Radek Krejci | 465308c | 2017-05-22 14:49:10 +0200 | [diff] [blame] | 453 | return NC_SESSION_TERM_ERR; |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return session->term_reason; |
| 457 | } |
| 458 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 459 | API uint32_t |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 460 | nc_session_get_killed_by(const struct nc_session *session) |
| 461 | { |
| 462 | if (!session) { |
| 463 | ERRARG("session"); |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | return session->killed_by; |
| 468 | } |
| 469 | |
| 470 | API uint32_t |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 471 | nc_session_get_id(const struct nc_session *session) |
| 472 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 473 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 474 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 478 | return session->id; |
| 479 | } |
| 480 | |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 481 | API int |
| 482 | nc_session_get_version(const struct nc_session *session) |
| 483 | { |
| 484 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 485 | ERRARG("session"); |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | return (session->version == NC_VERSION_10 ? 0 : 1); |
| 490 | } |
| 491 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 492 | API NC_TRANSPORT_IMPL |
| 493 | nc_session_get_ti(const struct nc_session *session) |
| 494 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 495 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 496 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 500 | return session->ti_type; |
| 501 | } |
| 502 | |
| 503 | API const char * |
| 504 | nc_session_get_username(const struct nc_session *session) |
| 505 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 506 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 507 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 508 | return NULL; |
| 509 | } |
| 510 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 511 | return session->username; |
| 512 | } |
| 513 | |
| 514 | API const char * |
| 515 | nc_session_get_host(const struct nc_session *session) |
| 516 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 517 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 518 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 519 | return NULL; |
| 520 | } |
| 521 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 522 | return session->host; |
| 523 | } |
| 524 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 525 | API const char * |
| 526 | nc_session_get_path(const struct nc_session *session) |
| 527 | { |
| 528 | if (!session) { |
| 529 | ERRARG("session"); |
| 530 | return NULL; |
| 531 | } |
| 532 | if (session->ti_type != NC_TI_UNIX) |
| 533 | return NULL; |
| 534 | |
| 535 | return session->path; |
| 536 | } |
| 537 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 538 | API uint16_t |
| 539 | nc_session_get_port(const struct nc_session *session) |
| 540 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 541 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 542 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 546 | return session->port; |
| 547 | } |
| 548 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 549 | API struct ly_ctx * |
| 550 | nc_session_get_ctx(const struct nc_session *session) |
| 551 | { |
| 552 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 553 | ERRARG("session"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | return session->ctx; |
| 558 | } |
| 559 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 560 | API void |
| 561 | nc_session_set_data(struct nc_session *session, void *data) |
| 562 | { |
| 563 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 564 | ERRARG("session"); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
| 568 | session->data = data; |
| 569 | } |
| 570 | |
| 571 | API void * |
| 572 | nc_session_get_data(const struct nc_session *session) |
| 573 | { |
| 574 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 575 | ERRARG("session"); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | return session->data; |
| 580 | } |
| 581 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 582 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 583 | nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 584 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 585 | if (session->ctx != op->schema->module->ctx) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 586 | ERR("Session %u: RPC \"%s\" was created in different context than that of the session.", |
| 587 | session->id, op->schema->name); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 588 | return NC_MSG_ERROR; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 589 | } |
| 590 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 591 | return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL); |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 594 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 595 | nc_session_free(struct nc_session *session, void (*data_free)(void *)) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 596 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 597 | int r, i, rpc_locked = 0, sock = -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 598 | int connected; /* flag to indicate whether the transport socket is still connected */ |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 599 | int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */ |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 600 | struct nc_session *siter; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 601 | struct nc_msg_cont *contiter; |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 602 | struct lyxml_elem *rpl, *child; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 603 | struct lyd_node *close_rpc; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 604 | const struct lys_module *ietfnc; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 605 | void *p; |
| 606 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 607 | if (!session || (session->status == NC_STATUS_CLOSING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 608 | return; |
| 609 | } |
| 610 | |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 611 | /* stop notifications loop if any */ |
Michal Vasko | 7f1fa3c | 2020-09-08 16:30:41 +0200 | [diff] [blame] | 612 | if ((session->side == NC_CLIENT) && ATOMIC_LOAD(session->opts.client.ntf_tid)) { |
Michal Vasko | b639e9c | 2020-09-09 09:51:48 +0200 | [diff] [blame] | 613 | ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)NULL); |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 614 | /* the thread now knows it should quit */ |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 617 | if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) { |
| 618 | r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 619 | if (r == -1) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 620 | return; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 621 | } else if (r) { |
| 622 | rpc_locked = 1; |
| 623 | } /* else failed to lock it, too bad */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 626 | if (session->side == NC_CLIENT) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 627 | /* cleanup message queues */ |
| 628 | /* notifications */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 629 | for (contiter = session->opts.client.notifs; contiter; ) { |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 630 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 631 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 632 | p = contiter; |
| 633 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 634 | free(p); |
| 635 | } |
| 636 | |
| 637 | /* rpc replies */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 638 | for (contiter = session->opts.client.replies; contiter; ) { |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 639 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 640 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 641 | p = contiter; |
| 642 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 643 | free(p); |
| 644 | } |
| 645 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 646 | if (session->status == NC_STATUS_RUNNING) { |
| 647 | /* send closing info to the other side */ |
| 648 | ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1); |
| 649 | if (!ietfnc) { |
| 650 | WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id); |
| 651 | } else { |
| 652 | close_rpc = lyd_new(NULL, ietfnc, "close-session"); |
| 653 | nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc); |
| 654 | lyd_free(close_rpc); |
| 655 | switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) { |
| 656 | case NC_MSG_REPLY: |
| 657 | LY_TREE_FOR(rpl->child, child) { |
| 658 | if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) { |
| 659 | break; |
| 660 | } |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 661 | } |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 662 | if (!child) { |
| 663 | WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id); |
| 664 | } |
| 665 | lyxml_free(session->ctx, rpl); |
| 666 | break; |
| 667 | case NC_MSG_WOULDBLOCK: |
| 668 | WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id); |
| 669 | break; |
| 670 | case NC_MSG_ERROR: |
| 671 | ERR("Session %u: failed to receive a reply to <close-session>.", session->id); |
| 672 | break; |
| 673 | default: |
| 674 | /* cannot happen */ |
| 675 | break; |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 676 | } |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 677 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* list of server's capabilities */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 681 | if (session->opts.client.cpblts) { |
| 682 | for (i = 0; session->opts.client.cpblts[i]; i++) { |
Michal Vasko | 96fc4bb | 2017-05-23 14:58:34 +0200 | [diff] [blame] | 683 | free(session->opts.client.cpblts[i]); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 684 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 685 | free(session->opts.client.cpblts); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 689 | if (session->data && data_free) { |
| 690 | data_free(session->data); |
| 691 | } |
| 692 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 693 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 694 | /* CH LOCK */ |
| 695 | pthread_mutex_lock(session->opts.server.ch_lock); |
| 696 | } |
| 697 | |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 698 | /* mark session for closing */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 699 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 700 | |
| 701 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 702 | pthread_cond_signal(session->opts.server.ch_cond); |
| 703 | |
| 704 | /* CH UNLOCK */ |
| 705 | pthread_mutex_unlock(session->opts.server.ch_lock); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 706 | |
| 707 | /* wait for CH thread to actually wake up */ |
| 708 | i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP; |
| 709 | while (i && (session->flags & NC_SESSION_CALLHOME)) { |
| 710 | usleep(NC_TIMEOUT_STEP); |
| 711 | --i; |
| 712 | } |
| 713 | if (session->flags & NC_SESSION_CALLHOME) { |
| 714 | ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id); |
| 715 | } |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 718 | connected = nc_session_is_connected(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 719 | |
| 720 | /* transport implementation cleanup */ |
| 721 | switch (session->ti_type) { |
| 722 | case NC_TI_FD: |
| 723 | /* nothing needed - file descriptors were provided by caller, |
| 724 | * so it is up to the caller to close them correctly |
| 725 | * TODO use callbacks |
| 726 | */ |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 727 | /* just to avoid compiler warning */ |
| 728 | (void)connected; |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 729 | (void)siter; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 730 | break; |
| 731 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 732 | case NC_TI_UNIX: |
| 733 | sock = session->ti.unixsock.sock; |
| 734 | (void)connected; |
| 735 | (void)siter; |
| 736 | break; |
| 737 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 738 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 739 | case NC_TI_LIBSSH: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 740 | if (connected) { |
| 741 | ssh_channel_free(session->ti.libssh.channel); |
| 742 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 743 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 744 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
Michal Vasko | e50b6b1 | 2018-10-11 10:27:50 +0200 | [diff] [blame] | 745 | * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session. |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 746 | */ |
Michal Vasko | e50b6b1 | 2018-10-11 10:27:50 +0200 | [diff] [blame] | 747 | /* SESSION IO LOCK */ |
| 748 | r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
| 749 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 750 | multisession = 0; |
| 751 | if (session->ti.libssh.next) { |
| 752 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 753 | if (siter->status != NC_STATUS_STARTING) { |
| 754 | multisession = 1; |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if (!multisession) { |
| 761 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 762 | if (session->ti.libssh.next) { |
| 763 | do { |
| 764 | siter = session->ti.libssh.next; |
| 765 | session->ti.libssh.next = siter->ti.libssh.next; |
| 766 | |
| 767 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 768 | lydict_remove(session->ctx, session->username); |
| 769 | lydict_remove(session->ctx, session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 770 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 771 | ly_ctx_destroy(session->ctx, NULL); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | free(siter); |
| 775 | } while (session->ti.libssh.next != session); |
| 776 | } |
Michal Vasko | 4d57e1b | 2018-03-21 12:21:25 +0100 | [diff] [blame] | 777 | /* remember sock so we can close it */ |
| 778 | sock = ssh_get_fd(session->ti.libssh.session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 779 | if (connected) { |
| 780 | ssh_disconnect(session->ti.libssh.session); |
Michal Vasko | 06e0fc2 | 2019-05-09 09:32:27 +0200 | [diff] [blame] | 781 | sock = -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 782 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 783 | ssh_free(session->ti.libssh.session); |
| 784 | } else { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 785 | /* remove the session from the list */ |
| 786 | for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next); |
Michal Vasko | aec4f21 | 2015-10-26 15:37:45 +0100 | [diff] [blame] | 787 | if (session->ti.libssh.next == siter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 788 | /* there will be only one session */ |
| 789 | siter->ti.libssh.next = NULL; |
| 790 | } else { |
| 791 | /* there are still multiple sessions, keep the ring list */ |
| 792 | siter->ti.libssh.next = session->ti.libssh.next; |
| 793 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 794 | /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */ |
| 795 | if (session->flags & NC_SESSION_SSH_MSG_CB) { |
Michal Vasko | 5e0edd8 | 2020-07-29 15:26:13 +0200 | [diff] [blame] | 796 | siter = session->ti.libssh.next; |
| 797 | while (siter && siter->status != NC_STATUS_RUNNING) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 798 | if (siter->ti.libssh.next == session) { |
| 799 | ERRINT; |
| 800 | break; |
| 801 | } |
Michal Vasko | 5e0edd8 | 2020-07-29 15:26:13 +0200 | [diff] [blame] | 802 | siter = siter->ti.libssh.next; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 803 | } |
Michal Vasko | 5e0edd8 | 2020-07-29 15:26:13 +0200 | [diff] [blame] | 804 | /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected), |
| 805 | * we set session to NULL because we do not expect any new message to arrive */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 806 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter); |
Michal Vasko | 5e0edd8 | 2020-07-29 15:26:13 +0200 | [diff] [blame] | 807 | if (siter) { |
| 808 | siter->flags |= NC_SESSION_SSH_MSG_CB; |
| 809 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 810 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 811 | } |
Michal Vasko | e50b6b1 | 2018-10-11 10:27:50 +0200 | [diff] [blame] | 812 | |
| 813 | /* SESSION IO UNLOCK */ |
| 814 | if (r == 1) { |
| 815 | nc_session_io_unlock(session, __func__); |
| 816 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 817 | break; |
| 818 | #endif |
| 819 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 820 | #ifdef NC_ENABLED_TLS |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 821 | case NC_TI_OPENSSL: |
Michal Vasko | 4d57e1b | 2018-03-21 12:21:25 +0100 | [diff] [blame] | 822 | /* remember sock so we can close it */ |
| 823 | sock = SSL_get_fd(session->ti.tls); |
| 824 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 825 | if (connected) { |
| 826 | SSL_shutdown(session->ti.tls); |
| 827 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 828 | SSL_free(session->ti.tls); |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 829 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 830 | if (session->side == NC_SERVER) { |
| 831 | X509_free(session->opts.server.client_cert); |
| 832 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 833 | break; |
| 834 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 835 | case NC_TI_NONE: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 836 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 837 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 838 | |
Michal Vasko | 4d57e1b | 2018-03-21 12:21:25 +0100 | [diff] [blame] | 839 | /* close socket separately */ |
| 840 | if (sock > -1) { |
| 841 | close(sock); |
| 842 | } |
| 843 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 844 | lydict_remove(session->ctx, session->username); |
| 845 | lydict_remove(session->ctx, session->host); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 846 | lydict_remove(session->ctx, session->path); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 847 | |
| 848 | /* final cleanup */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 849 | if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) { |
| 850 | if (rpc_locked) { |
| 851 | nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 852 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 853 | pthread_mutex_destroy(session->opts.server.rpc_lock); |
| 854 | pthread_cond_destroy(session->opts.server.rpc_cond); |
| 855 | free(session->opts.server.rpc_lock); |
| 856 | free(session->opts.server.rpc_cond); |
| 857 | free((int *)session->opts.server.rpc_inuse); |
| 858 | } |
| 859 | |
| 860 | if (session->io_lock && !multisession) { |
| 861 | pthread_mutex_destroy(session->io_lock); |
| 862 | free(session->io_lock); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 866 | ly_ctx_destroy(session->ctx, NULL); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 867 | } |
| 868 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 869 | if (session->side == NC_SERVER) { |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 870 | /* free CH synchronization structures if used */ |
| 871 | if (session->opts.server.ch_cond) { |
| 872 | pthread_cond_destroy(session->opts.server.ch_cond); |
| 873 | free(session->opts.server.ch_cond); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 874 | } |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 875 | if (session->opts.server.ch_lock) { |
| 876 | pthread_mutex_destroy(session->opts.server.ch_lock); |
| 877 | free(session->opts.server.ch_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 881 | free(session); |
| 882 | } |
| 883 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 884 | static void |
| 885 | add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count) |
| 886 | { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 887 | size_t len; |
| 888 | int i; |
| 889 | char *p; |
| 890 | |
| 891 | if (capab) { |
| 892 | /* check if already present */ |
| 893 | p = strchr(capab, '?'); |
| 894 | if (p) { |
| 895 | len = p - capab; |
| 896 | } else { |
| 897 | len = strlen(capab); |
| 898 | } |
| 899 | for (i = 0; i < *count; i++) { |
fanchanghu | 5244bf4 | 2017-03-13 16:27:48 +0800 | [diff] [blame] | 900 | if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 901 | /* already present, do not duplicate it */ |
| 902 | return; |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | /* add another capability */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 908 | if (*count == *size) { |
| 909 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 910 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 911 | if (!(*cpblts)) { |
| 912 | ERRMEM; |
| 913 | return; |
| 914 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | if (capab) { |
| 918 | (*cpblts)[*count] = lydict_insert(ctx, capab, 0); |
| 919 | } else { |
| 920 | (*cpblts)[*count] = NULL; |
| 921 | } |
| 922 | ++(*count); |
| 923 | } |
| 924 | |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 925 | static struct lys_feature * |
| 926 | nc_lys_next_feature(struct lys_feature *last, const struct lys_module *ly_mod, int *idx) |
| 927 | { |
| 928 | uint8_t i; |
| 929 | |
| 930 | assert(ly_mod); |
| 931 | |
| 932 | /* find the (sub)module of the last feature */ |
| 933 | if (last) { |
| 934 | for (i = 0; i < ly_mod->inc_size; ++i) { |
| 935 | if (*idx >= ly_mod->inc[i].submodule->features_size) { |
| 936 | /* not a feature from this submodule, skip */ |
| 937 | continue; |
| 938 | } |
| 939 | if (last != ly_mod->inc[i].submodule->features + *idx) { |
| 940 | /* feature is not from this submodule */ |
| 941 | continue; |
| 942 | } |
| 943 | |
| 944 | /* we have found the submodule */ |
| 945 | break; |
| 946 | } |
| 947 | |
| 948 | /* feature not found in submodules, it must be in the main module */ |
| 949 | assert((i < ly_mod->inc_size) || ((*idx < ly_mod->features_size) && (last == ly_mod->features + *idx))); |
| 950 | |
| 951 | /* we want the next feature */ |
| 952 | ++(*idx); |
| 953 | } else { |
| 954 | i = 0; |
| 955 | *idx = 0; |
| 956 | } |
| 957 | |
| 958 | /* find the (sub)module of the next feature */ |
| 959 | while ((i < ly_mod->inc_size) && (*idx == ly_mod->inc[i].submodule->features_size)) { |
| 960 | /* next submodule */ |
| 961 | ++i; |
| 962 | *idx = 0; |
| 963 | } |
| 964 | |
| 965 | /* get the next feature */ |
| 966 | if (i < ly_mod->inc_size) { |
| 967 | last = ly_mod->inc[i].submodule->features + *idx; |
| 968 | } else if (*idx < ly_mod->features_size) { |
| 969 | last = ly_mod->features + *idx; |
| 970 | } else { |
| 971 | last = NULL; |
| 972 | } |
| 973 | |
| 974 | return last; |
| 975 | } |
| 976 | |
| 977 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 978 | API const char ** |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 979 | nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 980 | { |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 981 | const char **cpblts; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 982 | const struct lys_module *mod, *devmod; |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 983 | struct lys_feature *feat; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 984 | int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len; |
| 985 | unsigned int u, v, module_set_id; |
| 986 | char *s; |
| 987 | #define NC_CPBLT_BUF_LEN 4096 |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 988 | char str[NC_CPBLT_BUF_LEN]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 989 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 990 | if (!ctx) { |
| 991 | ERRARG("ctx"); |
| 992 | return NULL; |
| 993 | } |
| 994 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 995 | cpblts = malloc(size * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 996 | if (!cpblts) { |
| 997 | ERRMEM; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 998 | goto error; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 999 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1000 | cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 1001 | cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 1002 | count = 2; |
| 1003 | |
| 1004 | /* capabilities */ |
| 1005 | |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 1006 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1007 | if (mod) { |
| 1008 | if (lys_features_state(mod, "writable-running") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1009 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1010 | } |
| 1011 | if (lys_features_state(mod, "candidate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1012 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1013 | if (lys_features_state(mod, "confirmed-commit") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1014 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | if (lys_features_state(mod, "rollback-on-error") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1018 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1019 | } |
| 1020 | if (lys_features_state(mod, "validate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1021 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1022 | } |
| 1023 | if (lys_features_state(mod, "startup") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1024 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1025 | } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1026 | |
| 1027 | /* The URL capability must be set manually using nc_server_set_capability() |
| 1028 | * because of the need for supported protocols to be included. |
| 1029 | * https://tools.ietf.org/html/rfc6241#section-8.8.3 |
| 1030 | */ |
mekleo | a8de5e9 | 2020-02-13 09:05:56 +0100 | [diff] [blame] | 1031 | // if (lys_features_state(mod, "url") == 1) { |
| 1032 | // add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count); |
| 1033 | // } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1034 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1035 | if (lys_features_state(mod, "xpath") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1036 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 1040 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1041 | if (mod) { |
| 1042 | if (!server_opts.wd_basic_mode) { |
| 1043 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 1044 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1045 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1046 | switch (server_opts.wd_basic_mode) { |
| 1047 | case NC_WD_ALL: |
| 1048 | strcat(str, "?basic-mode=report-all"); |
| 1049 | break; |
| 1050 | case NC_WD_TRIM: |
| 1051 | strcat(str, "?basic-mode=trim"); |
| 1052 | break; |
| 1053 | case NC_WD_EXPLICIT: |
| 1054 | strcat(str, "?basic-mode=explicit"); |
| 1055 | break; |
| 1056 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1057 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1058 | break; |
| 1059 | } |
| 1060 | |
| 1061 | if (server_opts.wd_also_supported) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1062 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1063 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 1064 | strcat(str, "report-all,"); |
| 1065 | } |
| 1066 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 1067 | strcat(str, "report-all-tagged,"); |
| 1068 | } |
| 1069 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 1070 | strcat(str, "trim,"); |
| 1071 | } |
| 1072 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 1073 | strcat(str, "explicit,"); |
| 1074 | } |
| 1075 | str[strlen(str) - 1] = '\0'; |
| 1076 | |
| 1077 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1082 | /* other capabilities */ |
| 1083 | for (u = 0; u < server_opts.capabilities_count; u++) { |
| 1084 | add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* models */ |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1088 | u = module_set_id = 0; |
| 1089 | while ((mod = ly_ctx_get_module_iter(ctx, &u))) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1090 | if (!strcmp(mod->name, "ietf-yang-library")) { |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1091 | if (!mod->rev_size || (strcmp(mod->rev[0].date, "2016-06-21") && strcmp(mod->rev[0].date, "2019-01-04"))) { |
| 1092 | ERR("Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported."); |
| 1093 | goto error; |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1094 | } |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1095 | |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1096 | if (!strcmp(mod->rev[0].date, "2019-01-04")) { |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1097 | /* new one (capab defined in RFC 8526 section 2) */ |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1098 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%u", |
| 1099 | mod->rev[0].date, ly_ctx_get_module_set_id(ctx)); |
| 1100 | add_cpblt(ctx, str, &cpblts, &size, &count); |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1101 | } else { |
| 1102 | /* old one (capab defined in RFC 7950 section 5.6.4) */ |
| 1103 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%u", |
| 1104 | mod->rev[0].date, ly_ctx_get_module_set_id(ctx)); |
| 1105 | add_cpblt(ctx, str, &cpblts, &size, &count); |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1106 | } |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1107 | continue; |
| 1108 | } else if (mod->type) { |
| 1109 | /* skip submodules */ |
| 1110 | continue; |
| 1111 | } else if (version == LYS_VERSION_1 && mod->version > version) { |
| 1112 | /* skip YANG 1.1 schemas */ |
| 1113 | continue; |
| 1114 | } else if (version == LYS_VERSION_1_1 && mod->version != version) { |
| 1115 | /* skip YANG 1.0 schemas */ |
| 1116 | continue; |
| 1117 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1118 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1119 | str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, |
| 1120 | mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : ""); |
| 1121 | |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1122 | features_count = 0; |
| 1123 | i = 0; |
| 1124 | feat = NULL; |
| 1125 | while ((feat = nc_lys_next_feature(feat, mod, &i))) { |
| 1126 | if (!(feat->flags & LYS_FENABLED)) { |
| 1127 | continue; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 1128 | } |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1129 | if (!features_count) { |
| 1130 | strcat(str, "&features="); |
| 1131 | str_len += 10; |
| 1132 | } |
| 1133 | len = strlen(feat->name); |
| 1134 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1135 | ERRINT; |
| 1136 | break; |
| 1137 | } |
| 1138 | if (features_count) { |
| 1139 | strcat(str, ","); |
| 1140 | ++str_len; |
| 1141 | } |
| 1142 | strcat(str, feat->name); |
| 1143 | str_len += len; |
| 1144 | features_count++; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1145 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1146 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1147 | if (mod->deviated) { |
| 1148 | strcat(str, "&deviations="); |
| 1149 | str_len += 12; |
| 1150 | dev_count = 0; |
Frank Rimpler | cb9274f | 2019-03-13 16:40:34 +0000 | [diff] [blame] | 1151 | v = 0; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1152 | while ((devmod = ly_ctx_get_module_iter(ctx, &v))) { |
| 1153 | if (devmod == mod) { |
| 1154 | continue; |
| 1155 | } |
| 1156 | |
| 1157 | for (i = 0; i < devmod->deviation_size; ++i) { |
| 1158 | s = strstr(devmod->deviation[i].target_name, mod->name); |
| 1159 | if (s && s[strlen(mod->name)] == ':') { |
| 1160 | /* we have the module deviating the module being processed */ |
| 1161 | len = strlen(devmod->name); |
| 1162 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1163 | ERRINT; |
| 1164 | break; |
| 1165 | } |
| 1166 | if (dev_count) { |
| 1167 | strcat(str, ","); |
| 1168 | ++str_len; |
| 1169 | } |
| 1170 | strcat(str, devmod->name); |
| 1171 | str_len += len; |
| 1172 | dev_count++; |
| 1173 | |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 1181 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1182 | |
| 1183 | /* ending NULL capability */ |
| 1184 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 1185 | |
| 1186 | return cpblts; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1187 | |
| 1188 | error: |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1189 | free(cpblts); |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1190 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1193 | API const char ** |
| 1194 | nc_server_get_cpblts(struct ly_ctx *ctx) |
| 1195 | { |
| 1196 | return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF); |
| 1197 | } |
| 1198 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1199 | static int |
Michal Vasko | 2cb24b4 | 2017-05-23 14:59:11 +0200 | [diff] [blame] | 1200 | parse_cpblts(struct lyxml_elem *xml, char ***list) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1201 | { |
| 1202 | struct lyxml_elem *cpblt; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1203 | int ver = -1, i = 0; |
| 1204 | const char *cpb_start, *cpb_end; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1205 | |
| 1206 | if (list) { |
| 1207 | /* get the storage for server's capabilities */ |
| 1208 | LY_TREE_FOR(xml->child, cpblt) { |
| 1209 | i++; |
| 1210 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1211 | /* last item remains NULL */ |
| 1212 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1213 | if (!*list) { |
| 1214 | ERRMEM; |
| 1215 | return -1; |
| 1216 | } |
| 1217 | i = 0; |
| 1218 | } |
| 1219 | |
| 1220 | LY_TREE_FOR(xml->child, cpblt) { |
Michal Vasko | 5ce6d7d | 2018-10-12 09:33:33 +0200 | [diff] [blame] | 1221 | if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1222 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 1223 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 1224 | return -1; |
Michal Vasko | 5ce6d7d | 2018-10-12 09:33:33 +0200 | [diff] [blame] | 1225 | } else if (!cpblt->name || !cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1226 | continue; |
| 1227 | } |
| 1228 | |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1229 | /* skip leading/trailing whitespaces */ |
| 1230 | for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start); |
| 1231 | for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end); |
| 1232 | if (!cpb_start[0] || (cpb_end == cpblt->content)) { |
| 1233 | ERR("Empty capability \"%s\" received.", cpblt->content); |
| 1234 | return -1; |
| 1235 | } |
| 1236 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1237 | /* detect NETCONF version */ |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1238 | if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1239 | ver = 0; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1240 | } else if (ver < 1 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.1", cpb_end - cpb_start)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1241 | ver = 1; |
| 1242 | } |
| 1243 | |
| 1244 | /* store capabilities */ |
| 1245 | if (list) { |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1246 | (*list)[i] = strndup(cpb_start, cpb_end - cpb_start); |
| 1247 | if (!(*list)[i]) { |
| 1248 | ERRMEM; |
| 1249 | return -1; |
| 1250 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1251 | i++; |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1256 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | return ver; |
| 1260 | } |
| 1261 | |
| 1262 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1263 | nc_send_hello_io(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1264 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1265 | NC_MSG_TYPE ret; |
| 1266 | int i, io_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1267 | const char **cpblts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1268 | uint32_t *sid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1269 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1270 | if (session->side == NC_CLIENT) { |
| 1271 | /* client side hello - send only NETCONF base capabilities */ |
| 1272 | cpblts = malloc(3 * sizeof *cpblts); |
| 1273 | if (!cpblts) { |
| 1274 | ERRMEM; |
| 1275 | return NC_MSG_ERROR; |
| 1276 | } |
| 1277 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 1278 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 1279 | cpblts[2] = NULL; |
| 1280 | |
| 1281 | io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000; |
| 1282 | sid = NULL; |
| 1283 | } else { |
| 1284 | cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1); |
Michal Vasko | 5b24b6b | 2020-12-04 09:29:47 +0100 | [diff] [blame] | 1285 | if (!cpblts) { |
| 1286 | return NC_MSG_ERROR; |
| 1287 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1288 | |
| 1289 | io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000; |
| 1290 | sid = &session->id; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1291 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1292 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1293 | ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1294 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1295 | for (i = 0; cpblts[i]; ++i) { |
| 1296 | lydict_remove(session->ctx, cpblts[i]); |
| 1297 | } |
| 1298 | free(cpblts); |
| 1299 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1300 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1304 | nc_recv_client_hello_io(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1305 | { |
| 1306 | struct lyxml_elem *xml = NULL, *node; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1307 | NC_MSG_TYPE msgtype; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1308 | int ver = -1; |
| 1309 | char *str; |
| 1310 | long long int id; |
| 1311 | int flag = 0; |
| 1312 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1313 | msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1314 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1315 | switch (msgtype) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1316 | case NC_MSG_HELLO: |
| 1317 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1318 | LY_TREE_FOR(xml->child, node) { |
| 1319 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 1320 | continue; |
| 1321 | } else if (!strcmp(node->name, "session-id")) { |
| 1322 | if (!node->content || !strlen(node->content)) { |
| 1323 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1324 | goto error; |
| 1325 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1326 | str = NULL; |
| 1327 | id = strtoll(node->content, &str, 10); |
| 1328 | if (*str || id < 1 || id > UINT32_MAX) { |
| 1329 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1330 | goto error; |
| 1331 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1332 | session->id = (uint32_t)id; |
| 1333 | continue; |
| 1334 | } else if (strcmp(node->name, "capabilities")) { |
| 1335 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1336 | goto error; |
| 1337 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1338 | |
| 1339 | if (flag) { |
| 1340 | /* multiple capabilities elements */ |
| 1341 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 1342 | goto error; |
| 1343 | } |
| 1344 | flag = 1; |
| 1345 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1346 | if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1347 | goto error; |
| 1348 | } |
| 1349 | session->version = ver; |
| 1350 | } |
| 1351 | |
| 1352 | if (!session->id) { |
| 1353 | ERR("Missing <session-id> in server's <hello>."); |
| 1354 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1355 | } |
| 1356 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1357 | case NC_MSG_WOULDBLOCK: |
| 1358 | ERR("Server's <hello> timeout elapsed."); |
| 1359 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1360 | case NC_MSG_ERROR: |
| 1361 | /* nothing special, just pass it out */ |
| 1362 | break; |
| 1363 | default: |
| 1364 | ERR("Unexpected message received instead of <hello>."); |
| 1365 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1366 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1370 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1371 | |
| 1372 | return msgtype; |
| 1373 | |
| 1374 | error: |
| 1375 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1376 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1377 | |
| 1378 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 1379 | } |
| 1380 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1381 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1382 | nc_recv_server_hello_io(struct nc_session *session) |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1383 | { |
| 1384 | struct lyxml_elem *xml = NULL, *node; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1385 | NC_MSG_TYPE msgtype; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1386 | int ver = -1; |
| 1387 | int flag = 0; |
| 1388 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1389 | msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ? |
| 1390 | server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1391 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1392 | switch (msgtype) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1393 | case NC_MSG_HELLO: |
| 1394 | /* get know NETCONF version */ |
| 1395 | LY_TREE_FOR(xml->child, node) { |
| 1396 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 1397 | continue; |
| 1398 | } else if (strcmp(node->name, "capabilities")) { |
| 1399 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1400 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1401 | goto cleanup; |
| 1402 | } |
| 1403 | |
| 1404 | if (flag) { |
| 1405 | /* multiple capabilities elements */ |
| 1406 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1407 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1408 | goto cleanup; |
| 1409 | } |
| 1410 | flag = 1; |
| 1411 | |
| 1412 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1413 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1414 | goto cleanup; |
| 1415 | } |
| 1416 | session->version = ver; |
| 1417 | } |
| 1418 | break; |
| 1419 | case NC_MSG_ERROR: |
| 1420 | /* nothing special, just pass it out */ |
| 1421 | break; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1422 | case NC_MSG_WOULDBLOCK: |
| 1423 | ERR("Client's <hello> timeout elapsed."); |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1424 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1425 | default: |
| 1426 | ERR("Unexpected message received instead of <hello>."); |
| 1427 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1428 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | cleanup: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1432 | lyxml_free(session->ctx, xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1433 | return msgtype; |
| 1434 | } |
| 1435 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1436 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1437 | nc_handshake_io(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1438 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1439 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1440 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1441 | type = nc_send_hello_io(session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1442 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1443 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1444 | } |
| 1445 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1446 | if (session->side == NC_CLIENT) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1447 | type = nc_recv_client_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1448 | } else { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1449 | type = nc_recv_server_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1452 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1453 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1454 | |
Michal Vasko | 889162e | 2019-09-06 14:43:37 +0200 | [diff] [blame] | 1455 | #ifdef NC_ENABLED_SSH |
| 1456 | |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1457 | static void |
| 1458 | nc_ssh_init(void) |
| 1459 | { |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1460 | #if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0)) |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1461 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 1462 | ssh_init(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1463 | #endif |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | static void |
| 1467 | nc_ssh_destroy(void) |
| 1468 | { |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1469 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1470 | FIPS_mode_set(0); |
| 1471 | CONF_modules_unload(1); |
| 1472 | nc_thread_destroy(); |
Michal Vasko | 889162e | 2019-09-06 14:43:37 +0200 | [diff] [blame] | 1473 | #endif |
| 1474 | |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1475 | #if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0)) |
| 1476 | ssh_finalize(); |
| 1477 | #endif |
| 1478 | } |
| 1479 | |
Michal Vasko | 889162e | 2019-09-06 14:43:37 +0200 | [diff] [blame] | 1480 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1481 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1482 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1483 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1484 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1485 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1486 | struct CRYPTO_dynlock_value { |
| 1487 | pthread_mutex_t lock; |
| 1488 | }; |
| 1489 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1490 | static struct CRYPTO_dynlock_value * |
| 1491 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 1492 | { |
| 1493 | struct CRYPTO_dynlock_value *value; |
| 1494 | |
| 1495 | value = malloc(sizeof *value); |
| 1496 | if (!value) { |
| 1497 | ERRMEM; |
| 1498 | return NULL; |
| 1499 | } |
| 1500 | pthread_mutex_init(&value->lock, NULL); |
| 1501 | |
| 1502 | return value; |
| 1503 | } |
| 1504 | |
| 1505 | static void |
| 1506 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1507 | { |
| 1508 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 1509 | * I found ignored this fact, what do I know... */ |
| 1510 | if (mode & CRYPTO_LOCK) { |
| 1511 | pthread_mutex_lock(&l->lock); |
| 1512 | } else { |
| 1513 | pthread_mutex_unlock(&l->lock); |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | static void |
| 1518 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1519 | { |
| 1520 | pthread_mutex_destroy(&l->lock); |
| 1521 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1522 | } |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1523 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1524 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1525 | #endif /* NC_ENABLED_TLS */ |
| 1526 | |
| 1527 | #if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH) |
| 1528 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1529 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1530 | static pthread_mutex_t *tls_locks; |
| 1531 | |
| 1532 | static void |
| 1533 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 1534 | { |
| 1535 | if (mode & CRYPTO_LOCK) { |
| 1536 | pthread_mutex_lock(tls_locks + n); |
| 1537 | } else { |
| 1538 | pthread_mutex_unlock(tls_locks + n); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | static void |
| 1543 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 1544 | { |
| 1545 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 1546 | } |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1547 | #endif |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1548 | |
| 1549 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1550 | nc_tls_init(void) |
| 1551 | { |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 1552 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1553 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1554 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1555 | SSL_library_init(); |
| 1556 | |
Michal Vasko | f89948c | 2018-01-04 09:19:46 +0100 | [diff] [blame] | 1557 | int i; |
| 1558 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1559 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1560 | if (!tls_locks) { |
| 1561 | ERRMEM; |
| 1562 | return; |
| 1563 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1564 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1565 | pthread_mutex_init(tls_locks + i, NULL); |
| 1566 | } |
| 1567 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1568 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1569 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1570 | |
| 1571 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1572 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1573 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1574 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1575 | } |
| 1576 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1577 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1578 | nc_tls_destroy(void) |
| 1579 | { |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 1580 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1581 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1582 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1583 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1584 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1585 | ERR_free_strings(); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1586 | #if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1587 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1588 | #elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1589 | SSL_COMP_free_compression_methods(); |
Jan Kundrát | 47e9e1a | 2016-11-21 09:41:59 +0100 | [diff] [blame] | 1590 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1591 | |
Michal Vasko | f89948c | 2018-01-04 09:19:46 +0100 | [diff] [blame] | 1592 | int i; |
| 1593 | |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1594 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1595 | CRYPTO_set_locking_callback(NULL); |
| 1596 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1597 | pthread_mutex_destroy(tls_locks + i); |
| 1598 | } |
| 1599 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1600 | |
| 1601 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1602 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1603 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1604 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1605 | } |
| 1606 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1607 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1608 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1609 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1610 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1611 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1612 | nc_ssh_tls_init(void) |
| 1613 | { |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 1614 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1615 | SSL_load_error_strings(); |
| 1616 | ERR_load_BIO_strings(); |
| 1617 | SSL_library_init(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1618 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1619 | |
| 1620 | nc_ssh_init(); |
| 1621 | |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1622 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1623 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1624 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1625 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1626 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1629 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1630 | nc_ssh_tls_destroy(void) |
| 1631 | { |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 1632 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1633 | ERR_free_strings(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1634 | # if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1635 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1636 | # elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1637 | SSL_COMP_free_compression_methods(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1638 | # endif |
Jan Kundrát | 47e9e1a | 2016-11-21 09:41:59 +0100 | [diff] [blame] | 1639 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1640 | |
| 1641 | nc_ssh_destroy(); |
| 1642 | |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1643 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1644 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1645 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1646 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1647 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1648 | } |
| 1649 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1650 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1651 | |
| 1652 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1653 | |
| 1654 | API void |
| 1655 | nc_thread_destroy(void) |
| 1656 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1657 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
| 1658 | //CRYPTO_cleanup_all_ex_data(); |
| 1659 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1660 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1661 | CRYPTO_THREADID crypto_tid; |
| 1662 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1663 | CRYPTO_THREADID_current(&crypto_tid); |
| 1664 | ERR_remove_thread_state(&crypto_tid); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1665 | #endif |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1666 | } |
| 1667 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1668 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 1669 | |
| 1670 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1671 | nc_init(void) |
| 1672 | { |
| 1673 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1674 | nc_ssh_tls_init(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1675 | #elif defined(NC_ENABLED_SSH) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1676 | nc_ssh_init(); |
| 1677 | #elif defined(NC_ENABLED_TLS) |
| 1678 | nc_tls_init(); |
| 1679 | #endif |
| 1680 | } |
| 1681 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1682 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1683 | nc_destroy(void) |
| 1684 | { |
| 1685 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1686 | nc_ssh_tls_destroy(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1687 | #elif defined(NC_ENABLED_SSH) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1688 | nc_ssh_destroy(); |
| 1689 | #elif defined(NC_ENABLED_TLS) |
| 1690 | nc_tls_destroy(); |
| 1691 | #endif |
| 1692 | } |