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) { |
| 124 | nc_gettimespec(&cur); |
| 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); |
| 571 | } |
| 572 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 573 | connected = nc_session_is_connected(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 574 | |
| 575 | /* transport implementation cleanup */ |
| 576 | switch (session->ti_type) { |
| 577 | case NC_TI_FD: |
| 578 | /* nothing needed - file descriptors were provided by caller, |
| 579 | * so it is up to the caller to close them correctly |
| 580 | * TODO use callbacks |
| 581 | */ |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 582 | /* just to avoid compiler warning */ |
| 583 | (void)connected; |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 584 | (void)siter; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 585 | break; |
| 586 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 587 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 588 | case NC_TI_LIBSSH: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 589 | if (connected) { |
| 590 | ssh_channel_free(session->ti.libssh.channel); |
| 591 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 592 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 593 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 594 | * it. |
| 595 | */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 596 | multisession = 0; |
| 597 | if (session->ti.libssh.next) { |
| 598 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 599 | if (siter->status != NC_STATUS_STARTING) { |
| 600 | multisession = 1; |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (!multisession) { |
| 607 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 608 | if (session->ti.libssh.next) { |
| 609 | do { |
| 610 | siter = session->ti.libssh.next; |
| 611 | session->ti.libssh.next = siter->ti.libssh.next; |
| 612 | |
| 613 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 614 | lydict_remove(session->ctx, session->username); |
| 615 | lydict_remove(session->ctx, session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 616 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 617 | ly_ctx_destroy(session->ctx, NULL); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | free(siter); |
| 621 | } while (session->ti.libssh.next != session); |
| 622 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 623 | if (connected) { |
| 624 | ssh_disconnect(session->ti.libssh.session); |
| 625 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 626 | ssh_free(session->ti.libssh.session); |
| 627 | } else { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 628 | /* remove the session from the list */ |
| 629 | 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] | 630 | if (session->ti.libssh.next == siter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 631 | /* there will be only one session */ |
| 632 | siter->ti.libssh.next = NULL; |
| 633 | } else { |
| 634 | /* there are still multiple sessions, keep the ring list */ |
| 635 | siter->ti.libssh.next = session->ti.libssh.next; |
| 636 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 637 | /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */ |
| 638 | if (session->flags & NC_SESSION_SSH_MSG_CB) { |
| 639 | for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) { |
| 640 | if (siter->ti.libssh.next == session) { |
| 641 | ERRINT; |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter); |
| 646 | siter->flags |= NC_SESSION_SSH_MSG_CB; |
| 647 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 648 | } |
| 649 | break; |
| 650 | #endif |
| 651 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 652 | #ifdef NC_ENABLED_TLS |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 653 | case NC_TI_OPENSSL: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 654 | if (connected) { |
| 655 | SSL_shutdown(session->ti.tls); |
| 656 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 657 | SSL_free(session->ti.tls); |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 658 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 659 | if (session->side == NC_SERVER) { |
| 660 | X509_free(session->opts.server.client_cert); |
| 661 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 662 | break; |
| 663 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 664 | case NC_TI_NONE: |
| 665 | ERRINT; |
| 666 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 667 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 668 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 669 | lydict_remove(session->ctx, session->username); |
| 670 | lydict_remove(session->ctx, session->host); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 671 | |
| 672 | /* final cleanup */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 673 | if (session->ti_lock) { |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 674 | if (locked) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 675 | nc_session_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 676 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 677 | if (!multisession) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 678 | pthread_mutex_destroy(session->ti_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 679 | pthread_cond_destroy(session->ti_cond); |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 680 | free(session->ti_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 681 | free(session->ti_cond); |
| 682 | free((int *)session->ti_inuse); |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 683 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 687 | ly_ctx_destroy(session->ctx, NULL); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 690 | if (session->side == NC_SERVER) { |
| 691 | if (session->opts.server.ch_cond) { |
| 692 | pthread_cond_destroy(session->opts.server.ch_cond); |
| 693 | free(session->opts.server.ch_cond); |
| 694 | } |
| 695 | if (session->opts.server.ch_lock) { |
| 696 | pthread_mutex_destroy(session->opts.server.ch_lock); |
| 697 | free(session->opts.server.ch_lock); |
| 698 | } |
| 699 | } |
| 700 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 701 | free(session); |
| 702 | } |
| 703 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 704 | static void |
| 705 | add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count) |
| 706 | { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 707 | size_t len; |
| 708 | int i; |
| 709 | char *p; |
| 710 | |
| 711 | if (capab) { |
| 712 | /* check if already present */ |
| 713 | p = strchr(capab, '?'); |
| 714 | if (p) { |
| 715 | len = p - capab; |
| 716 | } else { |
| 717 | len = strlen(capab); |
| 718 | } |
| 719 | for (i = 0; i < *count; i++) { |
fanchanghu | 5244bf4 | 2017-03-13 16:27:48 +0800 | [diff] [blame] | 720 | 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] | 721 | /* already present, do not duplicate it */ |
| 722 | return; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /* add another capability */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 728 | if (*count == *size) { |
| 729 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 730 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 731 | if (!(*cpblts)) { |
| 732 | ERRMEM; |
| 733 | return; |
| 734 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (capab) { |
| 738 | (*cpblts)[*count] = lydict_insert(ctx, capab, 0); |
| 739 | } else { |
| 740 | (*cpblts)[*count] = NULL; |
| 741 | } |
| 742 | ++(*count); |
| 743 | } |
| 744 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 745 | API const char ** |
| 746 | nc_server_get_cpblts(struct ly_ctx *ctx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 747 | { |
| 748 | struct lyd_node *child, *child2, *yanglib; |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 749 | 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] | 750 | const char **cpblts; |
| 751 | const struct lys_module *mod; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 752 | 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] | 753 | unsigned int u; |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 754 | #define NC_CPBLT_BUF_LEN 512 |
| 755 | char str[NC_CPBLT_BUF_LEN]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 756 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 757 | if (!ctx) { |
| 758 | ERRARG("ctx"); |
| 759 | return NULL; |
| 760 | } |
| 761 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 762 | yanglib = ly_ctx_info(ctx); |
| 763 | if (!yanglib) { |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 764 | ERR("Failed to get ietf-yang-library data from the context."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 765 | return NULL; |
| 766 | } |
| 767 | |
| 768 | cpblts = malloc(size * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 769 | if (!cpblts) { |
| 770 | ERRMEM; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 771 | goto error; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 772 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 773 | cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 774 | cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 775 | count = 2; |
| 776 | |
| 777 | /* capabilities */ |
| 778 | |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 779 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 780 | if (mod) { |
| 781 | if (lys_features_state(mod, "writable-running") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 782 | 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] | 783 | } |
| 784 | if (lys_features_state(mod, "candidate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 785 | 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] | 786 | if (lys_features_state(mod, "confirmed-commit") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 787 | 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] | 788 | } |
| 789 | } |
| 790 | if (lys_features_state(mod, "rollback-on-error") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 791 | 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] | 792 | } |
| 793 | if (lys_features_state(mod, "validate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 794 | 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] | 795 | } |
| 796 | if (lys_features_state(mod, "startup") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 797 | 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] | 798 | } |
| 799 | if (lys_features_state(mod, "url") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 800 | 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] | 801 | } |
| 802 | if (lys_features_state(mod, "xpath") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 803 | 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] | 804 | } |
| 805 | } |
| 806 | |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 807 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 808 | if (mod) { |
| 809 | if (!server_opts.wd_basic_mode) { |
| 810 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 811 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 812 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 813 | switch (server_opts.wd_basic_mode) { |
| 814 | case NC_WD_ALL: |
| 815 | strcat(str, "?basic-mode=report-all"); |
| 816 | break; |
| 817 | case NC_WD_TRIM: |
| 818 | strcat(str, "?basic-mode=trim"); |
| 819 | break; |
| 820 | case NC_WD_EXPLICIT: |
| 821 | strcat(str, "?basic-mode=explicit"); |
| 822 | break; |
| 823 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 824 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 825 | break; |
| 826 | } |
| 827 | |
| 828 | if (server_opts.wd_also_supported) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 829 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 830 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 831 | strcat(str, "report-all,"); |
| 832 | } |
| 833 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 834 | strcat(str, "report-all-tagged,"); |
| 835 | } |
| 836 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 837 | strcat(str, "trim,"); |
| 838 | } |
| 839 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 840 | strcat(str, "explicit,"); |
| 841 | } |
| 842 | str[strlen(str) - 1] = '\0'; |
| 843 | |
| 844 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 849 | /* other capabilities */ |
| 850 | for (u = 0; u < server_opts.capabilities_count; u++) { |
| 851 | add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /* models */ |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 855 | LY_TREE_FOR(yanglib->prev->child, child) { |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 856 | if (!module_set_id) { |
| 857 | if (strcmp(child->prev->schema->name, "module-set-id")) { |
| 858 | ERRINT; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 859 | goto error; |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 860 | } |
| 861 | module_set_id = (struct lyd_node_leaf_list *)child->prev; |
| 862 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 863 | if (!strcmp(child->schema->name, "module")) { |
| 864 | LY_TREE_FOR(child->child, child2) { |
| 865 | if (!strcmp(child2->schema->name, "namespace")) { |
| 866 | ns = (struct lyd_node_leaf_list *)child2; |
| 867 | } else if (!strcmp(child2->schema->name, "name")) { |
| 868 | name = (struct lyd_node_leaf_list *)child2; |
| 869 | } else if (!strcmp(child2->schema->name, "revision")) { |
| 870 | rev = (struct lyd_node_leaf_list *)child2; |
| 871 | } else if (!strcmp(child2->schema->name, "feature")) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 872 | features = nc_realloc(features, ++feat_count * sizeof *features); |
| 873 | if (!features) { |
| 874 | ERRMEM; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 875 | goto error; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 876 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 877 | features[feat_count - 1] = (struct lyd_node_leaf_list *)child2; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 878 | } else if (!strcmp(child2->schema->name, "deviation")) { |
| 879 | deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations); |
| 880 | if (!deviations) { |
| 881 | ERRMEM; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 882 | goto error; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 883 | } |
Hannes Küttner | ebb09ff | 2017-10-16 09:19:39 +0200 | [diff] [blame] | 884 | deviations[dev_count - 1] = (struct lyd_node_leaf_list *)child2; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
| 888 | if (!ns || !name || !rev) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 889 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 890 | continue; |
| 891 | } |
| 892 | |
Radek Krejci | df7ba52 | 2016-03-01 16:05:25 +0100 | [diff] [blame] | 893 | 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] | 894 | rev->value_str[0] ? "&revision=" : "", rev->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 895 | if (feat_count) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 896 | strcat(str, "&features="); |
| 897 | str_len += 10; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 898 | for (i = 0; i < feat_count; ++i) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 899 | 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] | 900 | ERRINT; |
| 901 | break; |
| 902 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 903 | if (i) { |
| 904 | strcat(str, ","); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 905 | ++str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 906 | } |
| 907 | strcat(str, features[i]->value_str); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 908 | str_len += strlen(features[i]->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 909 | } |
| 910 | } |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 911 | if (dev_count) { |
| 912 | strcat(str, "&deviations="); |
| 913 | str_len += 12; |
| 914 | for (i = 0; i < dev_count; ++i) { |
Hannes Küttner | ebb09ff | 2017-10-16 09:19:39 +0200 | [diff] [blame] | 915 | LY_TREE_FOR(((struct lyd_node *)deviations[i])->child, child2) { |
| 916 | if (!strcmp(child2->schema->name, "name")) |
| 917 | break; |
| 918 | } |
| 919 | if (!child2) { |
| 920 | ERRINT; |
| 921 | continue; |
| 922 | } |
| 923 | |
| 924 | 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] | 925 | ERRINT; |
| 926 | break; |
| 927 | } |
| 928 | if (i) { |
| 929 | strcat(str, ","); |
| 930 | ++str_len; |
| 931 | } |
Hannes Küttner | ebb09ff | 2017-10-16 09:19:39 +0200 | [diff] [blame] | 932 | strcat(str, ((struct lyd_node_leaf_list *)child2)->value_str); |
| 933 | str_len += strlen(((struct lyd_node_leaf_list *)child2)->value_str); |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 934 | } |
| 935 | } |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 936 | if (!strcmp(name->value_str, "ietf-yang-library")) { |
Michal Vasko | 7a7d574 | 2017-03-21 15:33:23 +0100 | [diff] [blame] | 937 | 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] | 938 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 939 | |
| 940 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 941 | |
| 942 | ns = NULL; |
| 943 | name = NULL; |
| 944 | rev = NULL; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 945 | if (features || feat_count) { |
| 946 | free(features); |
| 947 | features = NULL; |
| 948 | feat_count = 0; |
| 949 | } |
| 950 | if (deviations || dev_count) { |
| 951 | free(deviations); |
| 952 | deviations = NULL; |
| 953 | dev_count = 0; |
| 954 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 958 | lyd_free_withsiblings(yanglib); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 959 | |
| 960 | /* ending NULL capability */ |
| 961 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 962 | |
| 963 | return cpblts; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 964 | |
| 965 | error: |
| 966 | |
| 967 | free(cpblts); |
| 968 | free(features); |
| 969 | free(deviations); |
| 970 | lyd_free_withsiblings(yanglib); |
| 971 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 972 | } |
| 973 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 974 | static int |
Michal Vasko | 2cb24b4 | 2017-05-23 14:59:11 +0200 | [diff] [blame] | 975 | parse_cpblts(struct lyxml_elem *xml, char ***list) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 976 | { |
| 977 | struct lyxml_elem *cpblt; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 978 | int ver = -1, i = 0; |
| 979 | const char *cpb_start, *cpb_end; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 980 | |
| 981 | if (list) { |
| 982 | /* get the storage for server's capabilities */ |
| 983 | LY_TREE_FOR(xml->child, cpblt) { |
| 984 | i++; |
| 985 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 986 | /* last item remains NULL */ |
| 987 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 988 | if (!*list) { |
| 989 | ERRMEM; |
| 990 | return -1; |
| 991 | } |
| 992 | i = 0; |
| 993 | } |
| 994 | |
| 995 | LY_TREE_FOR(xml->child, cpblt) { |
| 996 | if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
| 997 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 998 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 999 | return -1; |
| 1000 | } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 1001 | continue; |
| 1002 | } |
| 1003 | |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1004 | /* skip leading/trailing whitespaces */ |
| 1005 | for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start); |
| 1006 | for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end); |
| 1007 | if (!cpb_start[0] || (cpb_end == cpblt->content)) { |
| 1008 | ERR("Empty capability \"%s\" received.", cpblt->content); |
| 1009 | return -1; |
| 1010 | } |
| 1011 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1012 | /* detect NETCONF version */ |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1013 | 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] | 1014 | ver = 0; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1015 | } 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] | 1016 | ver = 1; |
| 1017 | } |
| 1018 | |
| 1019 | /* store capabilities */ |
| 1020 | if (list) { |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1021 | (*list)[i] = strndup(cpb_start, cpb_end - cpb_start); |
| 1022 | if (!(*list)[i]) { |
| 1023 | ERRMEM; |
| 1024 | return -1; |
| 1025 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1026 | i++; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1031 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | return ver; |
| 1035 | } |
| 1036 | |
| 1037 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1038 | nc_send_client_hello(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1039 | { |
| 1040 | int r, i; |
| 1041 | const char **cpblts; |
| 1042 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1043 | /* client side hello - send only NETCONF base capabilities */ |
| 1044 | cpblts = malloc(3 * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1045 | if (!cpblts) { |
| 1046 | ERRMEM; |
| 1047 | return NC_MSG_ERROR; |
| 1048 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1049 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 1050 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 1051 | cpblts[2] = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1052 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1053 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1054 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1055 | for (i = 0; cpblts[i]; ++i) { |
| 1056 | lydict_remove(session->ctx, cpblts[i]); |
| 1057 | } |
| 1058 | free(cpblts); |
| 1059 | |
| 1060 | if (r) { |
| 1061 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1062 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1063 | |
| 1064 | return NC_MSG_HELLO; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1068 | nc_send_server_hello(struct nc_session *session) |
| 1069 | { |
| 1070 | int r, i; |
| 1071 | const char **cpblts; |
| 1072 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 1073 | cpblts = nc_server_get_cpblts(session->ctx); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1074 | |
| 1075 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id); |
| 1076 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1077 | for (i = 0; cpblts[i]; ++i) { |
| 1078 | lydict_remove(session->ctx, cpblts[i]); |
| 1079 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1080 | free(cpblts); |
| 1081 | |
| 1082 | if (r) { |
| 1083 | return NC_MSG_ERROR; |
| 1084 | } |
| 1085 | |
| 1086 | return NC_MSG_HELLO; |
| 1087 | } |
| 1088 | |
| 1089 | static NC_MSG_TYPE |
| 1090 | nc_recv_client_hello(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1091 | { |
| 1092 | struct lyxml_elem *xml = NULL, *node; |
| 1093 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 1094 | int ver = -1; |
| 1095 | char *str; |
| 1096 | long long int id; |
| 1097 | int flag = 0; |
| 1098 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1099 | msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1100 | |
| 1101 | switch(msgtype) { |
| 1102 | case NC_MSG_HELLO: |
| 1103 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1104 | LY_TREE_FOR(xml->child, node) { |
| 1105 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 1106 | continue; |
| 1107 | } else if (!strcmp(node->name, "session-id")) { |
| 1108 | if (!node->content || !strlen(node->content)) { |
| 1109 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1110 | goto error; |
| 1111 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1112 | str = NULL; |
| 1113 | id = strtoll(node->content, &str, 10); |
| 1114 | if (*str || id < 1 || id > UINT32_MAX) { |
| 1115 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1116 | goto error; |
| 1117 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1118 | session->id = (uint32_t)id; |
| 1119 | continue; |
| 1120 | } else if (strcmp(node->name, "capabilities")) { |
| 1121 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1122 | goto error; |
| 1123 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1124 | |
| 1125 | if (flag) { |
| 1126 | /* multiple capabilities elements */ |
| 1127 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 1128 | goto error; |
| 1129 | } |
| 1130 | flag = 1; |
| 1131 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1132 | if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1133 | goto error; |
| 1134 | } |
| 1135 | session->version = ver; |
| 1136 | } |
| 1137 | |
| 1138 | if (!session->id) { |
| 1139 | ERR("Missing <session-id> in server's <hello>."); |
| 1140 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1141 | } |
| 1142 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1143 | case NC_MSG_WOULDBLOCK: |
| 1144 | ERR("Server's <hello> timeout elapsed."); |
| 1145 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1146 | case NC_MSG_ERROR: |
| 1147 | /* nothing special, just pass it out */ |
| 1148 | break; |
| 1149 | default: |
| 1150 | ERR("Unexpected message received instead of <hello>."); |
| 1151 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1152 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1156 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1157 | |
| 1158 | return msgtype; |
| 1159 | |
| 1160 | error: |
| 1161 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1162 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1163 | |
| 1164 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1167 | static NC_MSG_TYPE |
| 1168 | nc_recv_server_hello(struct nc_session *session) |
| 1169 | { |
| 1170 | struct lyxml_elem *xml = NULL, *node; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1171 | NC_MSG_TYPE msgtype; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1172 | int ver = -1; |
| 1173 | int flag = 0; |
| 1174 | |
fanchanghu | 75888b6 | 2017-08-01 19:51:20 +0800 | [diff] [blame] | 1175 | 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] | 1176 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1177 | switch (msgtype) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1178 | case NC_MSG_HELLO: |
| 1179 | /* get know NETCONF version */ |
| 1180 | LY_TREE_FOR(xml->child, node) { |
| 1181 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 1182 | continue; |
| 1183 | } else if (strcmp(node->name, "capabilities")) { |
| 1184 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1185 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1186 | goto cleanup; |
| 1187 | } |
| 1188 | |
| 1189 | if (flag) { |
| 1190 | /* multiple capabilities elements */ |
| 1191 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1192 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1193 | goto cleanup; |
| 1194 | } |
| 1195 | flag = 1; |
| 1196 | |
| 1197 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1198 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1199 | goto cleanup; |
| 1200 | } |
| 1201 | session->version = ver; |
| 1202 | } |
| 1203 | break; |
| 1204 | case NC_MSG_ERROR: |
| 1205 | /* nothing special, just pass it out */ |
| 1206 | break; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1207 | case NC_MSG_WOULDBLOCK: |
| 1208 | ERR("Client's <hello> timeout elapsed."); |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1209 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1210 | default: |
| 1211 | ERR("Unexpected message received instead of <hello>."); |
| 1212 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1213 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | cleanup: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1217 | lyxml_free(session->ctx, xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1218 | |
| 1219 | return msgtype; |
| 1220 | } |
| 1221 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1222 | NC_MSG_TYPE |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1223 | nc_handshake(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1224 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1225 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1226 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1227 | if (session->side == NC_CLIENT) { |
| 1228 | type = nc_send_client_hello(session); |
| 1229 | } else { |
| 1230 | type = nc_send_server_hello(session); |
| 1231 | } |
| 1232 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1233 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1234 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1235 | } |
| 1236 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1237 | if (session->side == NC_CLIENT) { |
| 1238 | type = nc_recv_client_hello(session); |
| 1239 | } else { |
| 1240 | type = nc_recv_server_hello(session); |
| 1241 | } |
| 1242 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1243 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1244 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1245 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1246 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1247 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1248 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1249 | nc_ssh_init(void) |
| 1250 | { |
| 1251 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 1252 | ssh_init(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1255 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1256 | nc_ssh_destroy(void) |
| 1257 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1258 | FIPS_mode_set(0); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1259 | ENGINE_cleanup(); |
| 1260 | CONF_modules_unload(1); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1261 | nc_thread_destroy(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1262 | ssh_finalize(); |
| 1263 | } |
| 1264 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1265 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1266 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1267 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1268 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1269 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1270 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1271 | struct CRYPTO_dynlock_value { |
| 1272 | pthread_mutex_t lock; |
| 1273 | }; |
| 1274 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1275 | static struct CRYPTO_dynlock_value * |
| 1276 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 1277 | { |
| 1278 | struct CRYPTO_dynlock_value *value; |
| 1279 | |
| 1280 | value = malloc(sizeof *value); |
| 1281 | if (!value) { |
| 1282 | ERRMEM; |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | pthread_mutex_init(&value->lock, NULL); |
| 1286 | |
| 1287 | return value; |
| 1288 | } |
| 1289 | |
| 1290 | static void |
| 1291 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1292 | { |
| 1293 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 1294 | * I found ignored this fact, what do I know... */ |
| 1295 | if (mode & CRYPTO_LOCK) { |
| 1296 | pthread_mutex_lock(&l->lock); |
| 1297 | } else { |
| 1298 | pthread_mutex_unlock(&l->lock); |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | static void |
| 1303 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1304 | { |
| 1305 | pthread_mutex_destroy(&l->lock); |
| 1306 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1307 | } |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1308 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1309 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1310 | #endif /* NC_ENABLED_TLS */ |
| 1311 | |
| 1312 | #if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH) |
| 1313 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1314 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1315 | static pthread_mutex_t *tls_locks; |
| 1316 | |
| 1317 | static void |
| 1318 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 1319 | { |
| 1320 | if (mode & CRYPTO_LOCK) { |
| 1321 | pthread_mutex_lock(tls_locks + n); |
| 1322 | } else { |
| 1323 | pthread_mutex_unlock(tls_locks + n); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | static void |
| 1328 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 1329 | { |
| 1330 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 1331 | } |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1332 | #endif |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1333 | |
| 1334 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1335 | nc_tls_init(void) |
| 1336 | { |
| 1337 | int i; |
| 1338 | |
| 1339 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1340 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1341 | SSL_library_init(); |
| 1342 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1343 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1344 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1345 | if (!tls_locks) { |
| 1346 | ERRMEM; |
| 1347 | return; |
| 1348 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1349 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1350 | pthread_mutex_init(tls_locks + i, NULL); |
| 1351 | } |
| 1352 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1353 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1354 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1355 | |
| 1356 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1357 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1358 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1359 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1360 | } |
| 1361 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1362 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1363 | nc_tls_destroy(void) |
| 1364 | { |
| 1365 | int i; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1366 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1367 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1368 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1369 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1370 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1371 | ERR_free_strings(); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1372 | #if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1373 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1374 | #elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1375 | SSL_COMP_free_compression_methods(); |
Jan Kundrát | 47e9e1a | 2016-11-21 09:41:59 +0100 | [diff] [blame] | 1376 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1377 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1378 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1379 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1380 | CRYPTO_set_locking_callback(NULL); |
| 1381 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1382 | pthread_mutex_destroy(tls_locks + i); |
| 1383 | } |
| 1384 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1385 | |
| 1386 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1387 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1388 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1389 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1392 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1393 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1394 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1395 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1396 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1397 | nc_ssh_tls_init(void) |
| 1398 | { |
| 1399 | SSL_load_error_strings(); |
| 1400 | ERR_load_BIO_strings(); |
| 1401 | SSL_library_init(); |
| 1402 | |
| 1403 | nc_ssh_init(); |
| 1404 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1405 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1406 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1407 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1408 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1409 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1410 | } |
| 1411 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1412 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1413 | nc_ssh_tls_destroy(void) |
| 1414 | { |
| 1415 | ERR_free_strings(); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1416 | #if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1417 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1418 | #elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1419 | SSL_COMP_free_compression_methods(); |
Jan Kundrát | 47e9e1a | 2016-11-21 09:41:59 +0100 | [diff] [blame] | 1420 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1421 | |
| 1422 | nc_ssh_destroy(); |
| 1423 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1424 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1425 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1426 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1427 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1428 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1429 | } |
| 1430 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1431 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1432 | |
| 1433 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1434 | |
| 1435 | API void |
| 1436 | nc_thread_destroy(void) |
| 1437 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1438 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
| 1439 | //CRYPTO_cleanup_all_ex_data(); |
| 1440 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1441 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1442 | CRYPTO_THREADID crypto_tid; |
| 1443 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1444 | CRYPTO_THREADID_current(&crypto_tid); |
| 1445 | ERR_remove_thread_state(&crypto_tid); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1446 | #endif |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1447 | } |
| 1448 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1449 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 1450 | |
| 1451 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1452 | nc_init(void) |
| 1453 | { |
| 1454 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1455 | nc_ssh_tls_init(); |
| 1456 | #elif defined(NC_ENABLED_SSH) |
| 1457 | nc_ssh_init(); |
| 1458 | #elif defined(NC_ENABLED_TLS) |
| 1459 | nc_tls_init(); |
| 1460 | #endif |
| 1461 | } |
| 1462 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1463 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1464 | nc_destroy(void) |
| 1465 | { |
| 1466 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1467 | nc_ssh_tls_destroy(); |
| 1468 | #elif defined(NC_ENABLED_SSH) |
| 1469 | nc_ssh_destroy(); |
| 1470 | #elif defined(NC_ENABLED_TLS) |
| 1471 | nc_tls_destroy(); |
| 1472 | #endif |
| 1473 | } |