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