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