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