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