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