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