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 | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 924 | const char **cpblts; |
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 | } |
| 968 | if (lys_features_state(mod, "url") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 969 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 970 | } |
| 971 | if (lys_features_state(mod, "xpath") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 972 | 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] | 973 | } |
| 974 | } |
| 975 | |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 976 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 977 | if (mod) { |
| 978 | if (!server_opts.wd_basic_mode) { |
| 979 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 980 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 981 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 982 | switch (server_opts.wd_basic_mode) { |
| 983 | case NC_WD_ALL: |
| 984 | strcat(str, "?basic-mode=report-all"); |
| 985 | break; |
| 986 | case NC_WD_TRIM: |
| 987 | strcat(str, "?basic-mode=trim"); |
| 988 | break; |
| 989 | case NC_WD_EXPLICIT: |
| 990 | strcat(str, "?basic-mode=explicit"); |
| 991 | break; |
| 992 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 993 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 994 | break; |
| 995 | } |
| 996 | |
| 997 | if (server_opts.wd_also_supported) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 998 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 999 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 1000 | strcat(str, "report-all,"); |
| 1001 | } |
| 1002 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 1003 | strcat(str, "report-all-tagged,"); |
| 1004 | } |
| 1005 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 1006 | strcat(str, "trim,"); |
| 1007 | } |
| 1008 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 1009 | strcat(str, "explicit,"); |
| 1010 | } |
| 1011 | str[strlen(str) - 1] = '\0'; |
| 1012 | |
| 1013 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1018 | /* other capabilities */ |
| 1019 | for (u = 0; u < server_opts.capabilities_count; u++) { |
| 1020 | add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /* models */ |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1024 | u = module_set_id = 0; |
| 1025 | while ((mod = ly_ctx_get_module_iter(ctx, &u))) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1026 | if (!strcmp(mod->name, "ietf-yang-library")) { |
Andrew Langefeld | 50eac70 | 2018-08-17 12:10:40 -0500 | [diff] [blame] | 1027 | /* Add the yang-library NETCONF capability as defined in RFC 7950 5.6.4 */ |
| 1028 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?%s%s&module-set-id=%u", |
| 1029 | mod->rev_size ? "revision=" : "", mod->rev_size ? mod->rev[0].date : "", |
| 1030 | ly_ctx_get_module_set_id(ctx)); |
| 1031 | add_cpblt(ctx, str, &cpblts, &size, &count); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1032 | continue; |
| 1033 | } else if (mod->type) { |
| 1034 | /* skip submodules */ |
| 1035 | continue; |
| 1036 | } else if (version == LYS_VERSION_1 && mod->version > version) { |
| 1037 | /* skip YANG 1.1 schemas */ |
| 1038 | continue; |
| 1039 | } else if (version == LYS_VERSION_1_1 && mod->version != version) { |
| 1040 | /* skip YANG 1.0 schemas */ |
| 1041 | continue; |
| 1042 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1043 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1044 | str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, |
| 1045 | mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : ""); |
| 1046 | |
| 1047 | if (mod->features_size) { |
| 1048 | features_count = 0; |
| 1049 | for (i = 0; i < mod->features_size; ++i) { |
| 1050 | if (!(mod->features[i].flags & LYS_FENABLED)) { |
| 1051 | continue; |
| 1052 | } |
| 1053 | if (!features_count) { |
| 1054 | strcat(str, "&features="); |
| 1055 | str_len += 10; |
| 1056 | } |
| 1057 | len = strlen(mod->features[i].name); |
| 1058 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1059 | ERRINT; |
| 1060 | break; |
| 1061 | } |
Radek Krejci | 4e8eadd | 2018-08-21 09:54:27 +0200 | [diff] [blame] | 1062 | if (features_count) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1063 | strcat(str, ","); |
| 1064 | ++str_len; |
| 1065 | } |
| 1066 | strcat(str, mod->features[i].name); |
| 1067 | str_len += len; |
| 1068 | features_count++; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 1069 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1070 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1071 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1072 | if (mod->deviated) { |
| 1073 | strcat(str, "&deviations="); |
| 1074 | str_len += 12; |
| 1075 | dev_count = 0; |
Frank Rimpler | cb9274f | 2019-03-13 16:40:34 +0000 | [diff] [blame] | 1076 | v = 0; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1077 | while ((devmod = ly_ctx_get_module_iter(ctx, &v))) { |
| 1078 | if (devmod == mod) { |
| 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | for (i = 0; i < devmod->deviation_size; ++i) { |
| 1083 | s = strstr(devmod->deviation[i].target_name, mod->name); |
| 1084 | if (s && s[strlen(mod->name)] == ':') { |
| 1085 | /* we have the module deviating the module being processed */ |
| 1086 | len = strlen(devmod->name); |
| 1087 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1088 | ERRINT; |
| 1089 | break; |
| 1090 | } |
| 1091 | if (dev_count) { |
| 1092 | strcat(str, ","); |
| 1093 | ++str_len; |
| 1094 | } |
| 1095 | strcat(str, devmod->name); |
| 1096 | str_len += len; |
| 1097 | dev_count++; |
| 1098 | |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 1106 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1107 | |
| 1108 | /* ending NULL capability */ |
| 1109 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 1110 | |
| 1111 | return cpblts; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1112 | |
| 1113 | error: |
| 1114 | |
| 1115 | free(cpblts); |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1116 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1119 | API const char ** |
| 1120 | nc_server_get_cpblts(struct ly_ctx *ctx) |
| 1121 | { |
| 1122 | return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF); |
| 1123 | } |
| 1124 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1125 | static int |
Michal Vasko | 2cb24b4 | 2017-05-23 14:59:11 +0200 | [diff] [blame] | 1126 | parse_cpblts(struct lyxml_elem *xml, char ***list) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1127 | { |
| 1128 | struct lyxml_elem *cpblt; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1129 | int ver = -1, i = 0; |
| 1130 | const char *cpb_start, *cpb_end; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1131 | |
| 1132 | if (list) { |
| 1133 | /* get the storage for server's capabilities */ |
| 1134 | LY_TREE_FOR(xml->child, cpblt) { |
| 1135 | i++; |
| 1136 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1137 | /* last item remains NULL */ |
| 1138 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1139 | if (!*list) { |
| 1140 | ERRMEM; |
| 1141 | return -1; |
| 1142 | } |
| 1143 | i = 0; |
| 1144 | } |
| 1145 | |
| 1146 | LY_TREE_FOR(xml->child, cpblt) { |
Michal Vasko | 5ce6d7d | 2018-10-12 09:33:33 +0200 | [diff] [blame] | 1147 | if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1148 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 1149 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 1150 | return -1; |
Michal Vasko | 5ce6d7d | 2018-10-12 09:33:33 +0200 | [diff] [blame] | 1151 | } 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] | 1152 | continue; |
| 1153 | } |
| 1154 | |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1155 | /* skip leading/trailing whitespaces */ |
| 1156 | for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start); |
| 1157 | for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end); |
| 1158 | if (!cpb_start[0] || (cpb_end == cpblt->content)) { |
| 1159 | ERR("Empty capability \"%s\" received.", cpblt->content); |
| 1160 | return -1; |
| 1161 | } |
| 1162 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1163 | /* detect NETCONF version */ |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1164 | 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] | 1165 | ver = 0; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1166 | } 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] | 1167 | ver = 1; |
| 1168 | } |
| 1169 | |
| 1170 | /* store capabilities */ |
| 1171 | if (list) { |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1172 | (*list)[i] = strndup(cpb_start, cpb_end - cpb_start); |
| 1173 | if (!(*list)[i]) { |
| 1174 | ERRMEM; |
| 1175 | return -1; |
| 1176 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1177 | i++; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1182 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | return ver; |
| 1186 | } |
| 1187 | |
| 1188 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1189 | nc_send_hello_io(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1190 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1191 | NC_MSG_TYPE ret; |
| 1192 | int i, io_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1193 | const char **cpblts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1194 | uint32_t *sid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1195 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1196 | if (session->side == NC_CLIENT) { |
| 1197 | /* client side hello - send only NETCONF base capabilities */ |
| 1198 | cpblts = malloc(3 * sizeof *cpblts); |
| 1199 | if (!cpblts) { |
| 1200 | ERRMEM; |
| 1201 | return NC_MSG_ERROR; |
| 1202 | } |
| 1203 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 1204 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 1205 | cpblts[2] = NULL; |
| 1206 | |
| 1207 | io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000; |
| 1208 | sid = NULL; |
| 1209 | } else { |
| 1210 | cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1); |
| 1211 | |
| 1212 | io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000; |
| 1213 | sid = &session->id; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1214 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1215 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1216 | 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] | 1217 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1218 | for (i = 0; cpblts[i]; ++i) { |
| 1219 | lydict_remove(session->ctx, cpblts[i]); |
| 1220 | } |
| 1221 | free(cpblts); |
| 1222 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1223 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1227 | nc_recv_client_hello_io(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1228 | { |
| 1229 | struct lyxml_elem *xml = NULL, *node; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1230 | NC_MSG_TYPE msgtype; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1231 | int ver = -1; |
| 1232 | char *str; |
| 1233 | long long int id; |
| 1234 | int flag = 0; |
| 1235 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1236 | 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] | 1237 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1238 | switch (msgtype) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1239 | case NC_MSG_HELLO: |
| 1240 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1241 | LY_TREE_FOR(xml->child, node) { |
| 1242 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 1243 | continue; |
| 1244 | } else if (!strcmp(node->name, "session-id")) { |
| 1245 | if (!node->content || !strlen(node->content)) { |
| 1246 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1247 | goto error; |
| 1248 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1249 | str = NULL; |
| 1250 | id = strtoll(node->content, &str, 10); |
| 1251 | if (*str || id < 1 || id > UINT32_MAX) { |
| 1252 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1253 | goto error; |
| 1254 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1255 | session->id = (uint32_t)id; |
| 1256 | continue; |
| 1257 | } else if (strcmp(node->name, "capabilities")) { |
| 1258 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1259 | goto error; |
| 1260 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1261 | |
| 1262 | if (flag) { |
| 1263 | /* multiple capabilities elements */ |
| 1264 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 1265 | goto error; |
| 1266 | } |
| 1267 | flag = 1; |
| 1268 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1269 | if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1270 | goto error; |
| 1271 | } |
| 1272 | session->version = ver; |
| 1273 | } |
| 1274 | |
| 1275 | if (!session->id) { |
| 1276 | ERR("Missing <session-id> in server's <hello>."); |
| 1277 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1278 | } |
| 1279 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1280 | case NC_MSG_WOULDBLOCK: |
| 1281 | ERR("Server's <hello> timeout elapsed."); |
| 1282 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1283 | case NC_MSG_ERROR: |
| 1284 | /* nothing special, just pass it out */ |
| 1285 | break; |
| 1286 | default: |
| 1287 | ERR("Unexpected message received instead of <hello>."); |
| 1288 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1289 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1293 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1294 | |
| 1295 | return msgtype; |
| 1296 | |
| 1297 | error: |
| 1298 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1299 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1300 | |
| 1301 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 1302 | } |
| 1303 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1304 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1305 | nc_recv_server_hello_io(struct nc_session *session) |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1306 | { |
| 1307 | struct lyxml_elem *xml = NULL, *node; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1308 | NC_MSG_TYPE msgtype; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1309 | int ver = -1; |
| 1310 | int flag = 0; |
| 1311 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1312 | msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ? |
| 1313 | server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1314 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1315 | switch (msgtype) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1316 | case NC_MSG_HELLO: |
| 1317 | /* get know NETCONF version */ |
| 1318 | LY_TREE_FOR(xml->child, node) { |
| 1319 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 1320 | continue; |
| 1321 | } else if (strcmp(node->name, "capabilities")) { |
| 1322 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1323 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1324 | goto cleanup; |
| 1325 | } |
| 1326 | |
| 1327 | if (flag) { |
| 1328 | /* multiple capabilities elements */ |
| 1329 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1330 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1331 | goto cleanup; |
| 1332 | } |
| 1333 | flag = 1; |
| 1334 | |
| 1335 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1336 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1337 | goto cleanup; |
| 1338 | } |
| 1339 | session->version = ver; |
| 1340 | } |
| 1341 | break; |
| 1342 | case NC_MSG_ERROR: |
| 1343 | /* nothing special, just pass it out */ |
| 1344 | break; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1345 | case NC_MSG_WOULDBLOCK: |
| 1346 | ERR("Client's <hello> timeout elapsed."); |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1347 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1348 | default: |
| 1349 | ERR("Unexpected message received instead of <hello>."); |
| 1350 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1351 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | cleanup: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1355 | lyxml_free(session->ctx, xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1356 | return msgtype; |
| 1357 | } |
| 1358 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1359 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1360 | nc_handshake_io(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1361 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1362 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1363 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1364 | type = nc_send_hello_io(session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1365 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1366 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1369 | if (session->side == NC_CLIENT) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1370 | type = nc_recv_client_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1371 | } else { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1372 | type = nc_recv_server_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1373 | } |
| 1374 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1375 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1376 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1377 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1378 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1379 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1380 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1381 | nc_ssh_init(void) |
| 1382 | { |
| 1383 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 1384 | ssh_init(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1385 | } |
| 1386 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1387 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1388 | nc_ssh_destroy(void) |
| 1389 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1390 | FIPS_mode_set(0); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1391 | CONF_modules_unload(1); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1392 | nc_thread_destroy(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1393 | ssh_finalize(); |
| 1394 | } |
| 1395 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1396 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1397 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1398 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1399 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1400 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1401 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1402 | struct CRYPTO_dynlock_value { |
| 1403 | pthread_mutex_t lock; |
| 1404 | }; |
| 1405 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1406 | static struct CRYPTO_dynlock_value * |
| 1407 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 1408 | { |
| 1409 | struct CRYPTO_dynlock_value *value; |
| 1410 | |
| 1411 | value = malloc(sizeof *value); |
| 1412 | if (!value) { |
| 1413 | ERRMEM; |
| 1414 | return NULL; |
| 1415 | } |
| 1416 | pthread_mutex_init(&value->lock, NULL); |
| 1417 | |
| 1418 | return value; |
| 1419 | } |
| 1420 | |
| 1421 | static void |
| 1422 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1423 | { |
| 1424 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 1425 | * I found ignored this fact, what do I know... */ |
| 1426 | if (mode & CRYPTO_LOCK) { |
| 1427 | pthread_mutex_lock(&l->lock); |
| 1428 | } else { |
| 1429 | pthread_mutex_unlock(&l->lock); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | static void |
| 1434 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1435 | { |
| 1436 | pthread_mutex_destroy(&l->lock); |
| 1437 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1438 | } |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1439 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1440 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1441 | #endif /* NC_ENABLED_TLS */ |
| 1442 | |
| 1443 | #if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH) |
| 1444 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1445 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1446 | static pthread_mutex_t *tls_locks; |
| 1447 | |
| 1448 | static void |
| 1449 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 1450 | { |
| 1451 | if (mode & CRYPTO_LOCK) { |
| 1452 | pthread_mutex_lock(tls_locks + n); |
| 1453 | } else { |
| 1454 | pthread_mutex_unlock(tls_locks + n); |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | static void |
| 1459 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 1460 | { |
| 1461 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 1462 | } |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1463 | #endif |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1464 | |
| 1465 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1466 | nc_tls_init(void) |
| 1467 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1468 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1469 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1470 | SSL_library_init(); |
| 1471 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1472 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | f89948c | 2018-01-04 09:19:46 +0100 | [diff] [blame] | 1473 | int i; |
| 1474 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1475 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1476 | if (!tls_locks) { |
| 1477 | ERRMEM; |
| 1478 | return; |
| 1479 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1480 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1481 | pthread_mutex_init(tls_locks + i, NULL); |
| 1482 | } |
| 1483 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1484 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1485 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1486 | |
| 1487 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1488 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1489 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1490 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1491 | } |
| 1492 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1493 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1494 | nc_tls_destroy(void) |
| 1495 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1496 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1497 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1498 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1499 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1500 | ERR_free_strings(); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1501 | #if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1502 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1503 | #elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1504 | SSL_COMP_free_compression_methods(); |
Jan Kundrát | 47e9e1a | 2016-11-21 09:41:59 +0100 | [diff] [blame] | 1505 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1506 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1507 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | f89948c | 2018-01-04 09:19:46 +0100 | [diff] [blame] | 1508 | int i; |
| 1509 | |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1510 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1511 | CRYPTO_set_locking_callback(NULL); |
| 1512 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1513 | pthread_mutex_destroy(tls_locks + i); |
| 1514 | } |
| 1515 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1516 | |
| 1517 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1518 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1519 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1520 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1521 | } |
| 1522 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1523 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1524 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1525 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1526 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1527 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1528 | nc_ssh_tls_init(void) |
| 1529 | { |
| 1530 | SSL_load_error_strings(); |
| 1531 | ERR_load_BIO_strings(); |
| 1532 | SSL_library_init(); |
| 1533 | |
| 1534 | nc_ssh_init(); |
| 1535 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1536 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1537 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1538 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1539 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1540 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1543 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1544 | nc_ssh_tls_destroy(void) |
| 1545 | { |
| 1546 | ERR_free_strings(); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1547 | #if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1548 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1549 | #elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1550 | SSL_COMP_free_compression_methods(); |
Jan Kundrát | 47e9e1a | 2016-11-21 09:41:59 +0100 | [diff] [blame] | 1551 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1552 | |
| 1553 | nc_ssh_destroy(); |
| 1554 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1555 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1556 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1557 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1558 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1559 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1562 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1563 | |
| 1564 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1565 | |
| 1566 | API void |
| 1567 | nc_thread_destroy(void) |
| 1568 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1569 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
| 1570 | //CRYPTO_cleanup_all_ex_data(); |
| 1571 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1572 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1573 | CRYPTO_THREADID crypto_tid; |
| 1574 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1575 | CRYPTO_THREADID_current(&crypto_tid); |
| 1576 | ERR_remove_thread_state(&crypto_tid); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1577 | #endif |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1578 | } |
| 1579 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1580 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 1581 | |
| 1582 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1583 | nc_init(void) |
| 1584 | { |
| 1585 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1586 | nc_ssh_tls_init(); |
| 1587 | #elif defined(NC_ENABLED_SSH) |
| 1588 | nc_ssh_init(); |
| 1589 | #elif defined(NC_ENABLED_TLS) |
| 1590 | nc_tls_init(); |
| 1591 | #endif |
| 1592 | } |
| 1593 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1594 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1595 | nc_destroy(void) |
| 1596 | { |
| 1597 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1598 | nc_ssh_tls_destroy(); |
| 1599 | #elif defined(NC_ENABLED_SSH) |
| 1600 | nc_ssh_destroy(); |
| 1601 | #elif defined(NC_ENABLED_TLS) |
| 1602 | nc_tls_destroy(); |
| 1603 | #endif |
| 1604 | } |