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