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