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