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