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 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Radek Krejci | 952eb86 | 2016-01-08 14:22:55 +0100 | [diff] [blame] | 16 | #include <stdlib.h> |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 17 | #include <string.h> |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 18 | #include <pthread.h> |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 19 | #include <time.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 20 | #include <libyang/libyang.h> |
| 21 | |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 22 | #include "session.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 23 | #include "libnetconf.h" |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 24 | #include "session_server.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 25 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 26 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 27 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 28 | # include <libssh/libssh.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 29 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 30 | #endif /* NC_ENABLED_SSH */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 31 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 32 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 33 | |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 34 | # include <openssl/engine.h> |
| 35 | # include <openssl/conf.h> |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 36 | # include <openssl/err.h> |
| 37 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 38 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 39 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 40 | /* in seconds */ |
| 41 | #define NC_CLIENT_HELLO_TIMEOUT 60 |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 42 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 43 | /* in milliseconds */ |
| 44 | #define NC_CLOSE_REPLY_TIMEOUT 200 |
| 45 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 46 | extern struct nc_server_opts server_opts; |
| 47 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 48 | /* |
| 49 | * @return 1 - success |
| 50 | * 0 - timeout |
| 51 | * -1 - error |
| 52 | */ |
| 53 | int |
| 54 | nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed) |
| 55 | { |
| 56 | int ret; |
| 57 | struct timespec ts_timeout, ts_old, ts_new; |
| 58 | |
| 59 | if (timeout > 0) { |
| 60 | clock_gettime(CLOCK_REALTIME, &ts_timeout); |
| 61 | |
| 62 | if (elapsed) { |
| 63 | ts_old = ts_timeout; |
| 64 | } |
| 65 | |
| 66 | ts_timeout.tv_sec += timeout / 1000; |
| 67 | ts_timeout.tv_nsec += (timeout % 1000) * 1000000; |
| 68 | |
| 69 | ret = pthread_mutex_timedlock(lock, &ts_timeout); |
| 70 | |
| 71 | if (elapsed) { |
| 72 | clock_gettime(CLOCK_REALTIME, &ts_new); |
| 73 | |
| 74 | *elapsed += (ts_new.tv_sec - ts_old.tv_sec) * 1000; |
| 75 | *elapsed += (ts_new.tv_nsec - ts_old.tv_nsec) / 1000000; |
| 76 | } |
| 77 | } else if (!timeout) { |
| 78 | ret = pthread_mutex_trylock(lock); |
| 79 | } else { /* timeout == -1 */ |
| 80 | ret = pthread_mutex_lock(lock); |
| 81 | } |
| 82 | |
| 83 | if (ret == ETIMEDOUT) { |
| 84 | /* timeout */ |
| 85 | return 0; |
| 86 | } else if (ret) { |
| 87 | /* error */ |
| 88 | ERR("Mutex lock failed (%s).", strerror(errno)); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | /* ok */ |
| 93 | return 1; |
| 94 | } |
| 95 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 96 | API NC_STATUS |
| 97 | nc_session_get_status(const struct nc_session *session) |
| 98 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 99 | if (!session) { |
| 100 | ERRARG; |
| 101 | return 0; |
| 102 | } |
| 103 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 104 | return session->status; |
| 105 | } |
| 106 | |
| 107 | API uint32_t |
| 108 | nc_session_get_id(const struct nc_session *session) |
| 109 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 110 | if (!session) { |
| 111 | ERRARG; |
| 112 | return 0; |
| 113 | } |
| 114 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 115 | return session->id; |
| 116 | } |
| 117 | |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 118 | API int |
| 119 | nc_session_get_version(const struct nc_session *session) |
| 120 | { |
| 121 | if (!session) { |
| 122 | ERRARG; |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | return (session->version == NC_VERSION_10 ? 0 : 1); |
| 127 | } |
| 128 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 129 | API NC_TRANSPORT_IMPL |
| 130 | nc_session_get_ti(const struct nc_session *session) |
| 131 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 132 | if (!session) { |
| 133 | ERRARG; |
| 134 | return 0; |
| 135 | } |
| 136 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 137 | return session->ti_type; |
| 138 | } |
| 139 | |
| 140 | API const char * |
| 141 | nc_session_get_username(const struct nc_session *session) |
| 142 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 143 | if (!session) { |
| 144 | ERRARG; |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 148 | return session->username; |
| 149 | } |
| 150 | |
| 151 | API const char * |
| 152 | nc_session_get_host(const struct nc_session *session) |
| 153 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 154 | if (!session) { |
| 155 | ERRARG; |
| 156 | return NULL; |
| 157 | } |
| 158 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 159 | return session->host; |
| 160 | } |
| 161 | |
| 162 | API uint16_t |
| 163 | nc_session_get_port(const struct nc_session *session) |
| 164 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 165 | if (!session) { |
| 166 | ERRARG; |
| 167 | return 0; |
| 168 | } |
| 169 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 170 | return session->port; |
| 171 | } |
| 172 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 173 | API struct ly_ctx * |
| 174 | nc_session_get_ctx(const struct nc_session *session) |
| 175 | { |
| 176 | if (!session) { |
| 177 | ERRARG; |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | return session->ctx; |
| 182 | } |
| 183 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 184 | API const char ** |
| 185 | nc_session_get_cpblts(const struct nc_session *session) |
| 186 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 187 | if (!session) { |
| 188 | ERRARG; |
| 189 | return NULL; |
| 190 | } |
| 191 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 192 | return session->cpblts; |
| 193 | } |
| 194 | |
| 195 | API const char * |
| 196 | nc_session_cpblt(const struct nc_session *session, const char *capab) |
| 197 | { |
| 198 | int i, len; |
| 199 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 200 | if (!session || !capab) { |
| 201 | ERRARG; |
| 202 | return NULL; |
| 203 | } |
| 204 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 205 | len = strlen(capab); |
| 206 | for (i = 0; session->cpblts[i]; ++i) { |
| 207 | if (!strncmp(session->cpblts[i], capab, len)) { |
| 208 | return session->cpblts[i]; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return NULL; |
| 213 | } |
| 214 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 215 | API void |
| 216 | nc_session_set_data(struct nc_session *session, void *data) |
| 217 | { |
| 218 | if (!session) { |
| 219 | ERRARG; |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | session->data = data; |
| 224 | } |
| 225 | |
| 226 | API void * |
| 227 | nc_session_get_data(const struct nc_session *session) |
| 228 | { |
| 229 | if (!session) { |
| 230 | ERRARG; |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | return session->data; |
| 235 | } |
| 236 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 237 | NC_MSG_TYPE |
| 238 | nc_send_msg(struct nc_session *session, struct lyd_node *op) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 239 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 240 | int r; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 241 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 242 | if (session->ctx != op->schema->module->ctx) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 243 | ERR("Session %u: RPC \"%s\" was created in different context than that of the session.", |
| 244 | session->id, op->schema->name); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 245 | return NC_MSG_ERROR; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 248 | r = nc_write_msg(session, NC_MSG_RPC, op, NULL); |
| 249 | |
| 250 | if (r) { |
| 251 | return NC_MSG_ERROR; |
| 252 | } |
| 253 | |
| 254 | return NC_MSG_RPC; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 257 | API void |
| 258 | nc_session_free(struct nc_session *session) |
| 259 | { |
| 260 | int r, i; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 261 | int connected; /* flag to indicate whether the transport socket is still connected */ |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 262 | 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] | 263 | pthread_t tid; |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 264 | struct nc_session *siter; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 265 | struct nc_msg_cont *contiter; |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 266 | struct lyxml_elem *rpl, *child; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 267 | struct lyd_node *close_rpc; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 268 | const struct lys_module *ietfnc; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 269 | void *p; |
| 270 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 271 | if (!session || (session->status == NC_STATUS_CLOSING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | |
| 275 | /* mark session for closing */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 276 | if (session->ti_lock) { |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 277 | r = nc_timedlock(session->ti_lock, -1, NULL); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 278 | if (r == -1) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 279 | return; |
| 280 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* stop notifications loop if any */ |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 284 | if (session->ntf_tid) { |
| 285 | tid = *session->ntf_tid; |
| 286 | free((pthread_t *)session->ntf_tid); |
| 287 | session->ntf_tid = NULL; |
| 288 | /* the thread now knows it should quit */ |
| 289 | |
| 290 | pthread_join(tid, NULL); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 293 | if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 294 | /* cleanup message queues */ |
| 295 | /* notifications */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 296 | for (contiter = session->notifs; contiter; ) { |
| 297 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 298 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 299 | p = contiter; |
| 300 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 301 | free(p); |
| 302 | } |
| 303 | |
| 304 | /* rpc replies */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 305 | for (contiter = session->replies; contiter; ) { |
| 306 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 307 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 308 | p = contiter; |
| 309 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 310 | free(p); |
| 311 | } |
| 312 | |
| 313 | /* send closing info to the other side */ |
| 314 | ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL); |
| 315 | if (!ietfnc) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 316 | 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] | 317 | } else { |
| 318 | close_rpc = lyd_new(NULL, ietfnc, "close-session"); |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 319 | nc_send_msg(session, close_rpc); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 320 | lyd_free(close_rpc); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 321 | switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) { |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 322 | case NC_MSG_REPLY: |
| 323 | LY_TREE_FOR(rpl->child, child) { |
| 324 | if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) { |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | if (!child) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 329 | 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] | 330 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 331 | lyxml_free(session->ctx, rpl); |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 332 | break; |
| 333 | case NC_MSG_WOULDBLOCK: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 334 | 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] | 335 | break; |
| 336 | case NC_MSG_ERROR: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 337 | 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] | 338 | break; |
| 339 | default: |
| 340 | /* cannot happen */ |
| 341 | break; |
| 342 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* list of server's capabilities */ |
| 346 | if (session->cpblts) { |
| 347 | for (i = 0; session->cpblts[i]; i++) { |
| 348 | lydict_remove(session->ctx, session->cpblts[i]); |
| 349 | } |
| 350 | free(session->cpblts); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 355 | connected = nc_session_is_connected(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 356 | |
| 357 | /* transport implementation cleanup */ |
| 358 | switch (session->ti_type) { |
| 359 | case NC_TI_FD: |
| 360 | /* nothing needed - file descriptors were provided by caller, |
| 361 | * so it is up to the caller to close them correctly |
| 362 | * TODO use callbacks |
| 363 | */ |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 364 | /* just to avoid compiler warning */ |
| 365 | (void)connected; |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 366 | (void)siter; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 367 | break; |
| 368 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 369 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 370 | case NC_TI_LIBSSH: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 371 | if (connected) { |
| 372 | ssh_channel_free(session->ti.libssh.channel); |
| 373 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 374 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 375 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 376 | * it. |
| 377 | */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 378 | multisession = 0; |
| 379 | if (session->ti.libssh.next) { |
| 380 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 381 | if (siter->status != NC_STATUS_STARTING) { |
| 382 | multisession = 1; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (!multisession) { |
| 389 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 390 | if (session->ti.libssh.next) { |
| 391 | do { |
| 392 | siter = session->ti.libssh.next; |
| 393 | session->ti.libssh.next = siter->ti.libssh.next; |
| 394 | |
| 395 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 396 | lydict_remove(session->ctx, session->username); |
| 397 | lydict_remove(session->ctx, session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 398 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 399 | ly_ctx_destroy(session->ctx, NULL); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | free(siter); |
| 403 | } while (session->ti.libssh.next != session); |
| 404 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 405 | if (connected) { |
| 406 | ssh_disconnect(session->ti.libssh.session); |
| 407 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 408 | ssh_free(session->ti.libssh.session); |
| 409 | } else { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 410 | /* remove the session from the list */ |
| 411 | 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] | 412 | if (session->ti.libssh.next == siter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 413 | /* there will be only one session */ |
| 414 | siter->ti.libssh.next = NULL; |
| 415 | } else { |
| 416 | /* there are still multiple sessions, keep the ring list */ |
| 417 | siter->ti.libssh.next = session->ti.libssh.next; |
| 418 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 419 | /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */ |
| 420 | if (session->flags & NC_SESSION_SSH_MSG_CB) { |
| 421 | for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) { |
| 422 | if (siter->ti.libssh.next == session) { |
| 423 | ERRINT; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter); |
| 428 | siter->flags |= NC_SESSION_SSH_MSG_CB; |
| 429 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 430 | } |
| 431 | break; |
| 432 | #endif |
| 433 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 434 | #ifdef NC_ENABLED_TLS |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 435 | case NC_TI_OPENSSL: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 436 | if (connected) { |
| 437 | SSL_shutdown(session->ti.tls); |
| 438 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 439 | SSL_free(session->ti.tls); |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 440 | |
| 441 | X509_free(session->tls_cert); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 442 | break; |
| 443 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 444 | case NC_TI_NONE: |
| 445 | ERRINT; |
| 446 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 447 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 448 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 449 | lydict_remove(session->ctx, session->username); |
| 450 | lydict_remove(session->ctx, session->host); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 451 | |
| 452 | /* final cleanup */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 453 | if (session->ti_lock) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 454 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 455 | if (!multisession) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 456 | pthread_mutex_destroy(session->ti_lock); |
| 457 | free(session->ti_lock); |
| 458 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 462 | ly_ctx_destroy(session->ctx, NULL); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | free(session); |
| 466 | } |
| 467 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 468 | static void |
| 469 | add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count) |
| 470 | { |
| 471 | if (*count == *size) { |
| 472 | *size += 5; |
| 473 | *cpblts = realloc(*cpblts, *size * sizeof **cpblts); |
| 474 | } |
| 475 | |
| 476 | if (capab) { |
| 477 | (*cpblts)[*count] = lydict_insert(ctx, capab, 0); |
| 478 | } else { |
| 479 | (*cpblts)[*count] = NULL; |
| 480 | } |
| 481 | ++(*count); |
| 482 | } |
| 483 | |
| 484 | static const char ** |
| 485 | create_cpblts(struct ly_ctx *ctx) |
| 486 | { |
| 487 | struct lyd_node *child, *child2, *yanglib; |
| 488 | struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL; |
| 489 | const char **cpblts; |
| 490 | const struct lys_module *mod; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 491 | int size = 10, count, feat_count = 0, i, str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 492 | char str[512]; |
| 493 | |
| 494 | yanglib = ly_ctx_info(ctx); |
| 495 | if (!yanglib) { |
| 496 | return NULL; |
| 497 | } |
| 498 | |
| 499 | cpblts = malloc(size * sizeof *cpblts); |
| 500 | cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 501 | cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 502 | count = 2; |
| 503 | |
| 504 | /* capabilities */ |
| 505 | |
| 506 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 507 | if (mod) { |
| 508 | if (lys_features_state(mod, "writable-running") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 509 | 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] | 510 | } |
| 511 | if (lys_features_state(mod, "candidate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 512 | 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] | 513 | if (lys_features_state(mod, "confirmed-commit") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 514 | 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] | 515 | } |
| 516 | } |
| 517 | if (lys_features_state(mod, "rollback-on-error") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 518 | 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] | 519 | } |
| 520 | if (lys_features_state(mod, "validate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 521 | 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] | 522 | } |
| 523 | if (lys_features_state(mod, "startup") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 524 | 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] | 525 | } |
| 526 | if (lys_features_state(mod, "url") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 527 | 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] | 528 | } |
| 529 | if (lys_features_state(mod, "xpath") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 530 | 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] | 531 | } |
| 532 | } |
| 533 | |
| 534 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL); |
| 535 | if (mod) { |
| 536 | if (!server_opts.wd_basic_mode) { |
| 537 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 538 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 539 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 540 | switch (server_opts.wd_basic_mode) { |
| 541 | case NC_WD_ALL: |
| 542 | strcat(str, "?basic-mode=report-all"); |
| 543 | break; |
| 544 | case NC_WD_TRIM: |
| 545 | strcat(str, "?basic-mode=trim"); |
| 546 | break; |
| 547 | case NC_WD_EXPLICIT: |
| 548 | strcat(str, "?basic-mode=explicit"); |
| 549 | break; |
| 550 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 551 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 552 | break; |
| 553 | } |
| 554 | |
| 555 | if (server_opts.wd_also_supported) { |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame] | 556 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 557 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 558 | strcat(str, "report-all,"); |
| 559 | } |
| 560 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 561 | strcat(str, "report-all-tagged,"); |
| 562 | } |
| 563 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 564 | strcat(str, "trim,"); |
| 565 | } |
| 566 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 567 | strcat(str, "explicit,"); |
| 568 | } |
| 569 | str[strlen(str) - 1] = '\0'; |
| 570 | |
| 571 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 576 | mod = ly_ctx_get_module(ctx, "nc-notifications", NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 577 | if (mod) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 578 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 579 | if (server_opts.interleave_capab) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 580 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | /* models */ |
| 585 | |
| 586 | LY_TREE_FOR(yanglib->child, child) { |
| 587 | if (!strcmp(child->schema->name, "module")) { |
| 588 | LY_TREE_FOR(child->child, child2) { |
| 589 | if (!strcmp(child2->schema->name, "namespace")) { |
| 590 | ns = (struct lyd_node_leaf_list *)child2; |
| 591 | } else if (!strcmp(child2->schema->name, "name")) { |
| 592 | name = (struct lyd_node_leaf_list *)child2; |
| 593 | } else if (!strcmp(child2->schema->name, "revision")) { |
| 594 | rev = (struct lyd_node_leaf_list *)child2; |
| 595 | } else if (!strcmp(child2->schema->name, "feature")) { |
| 596 | features = realloc(features, feat_count++ * sizeof *features); |
| 597 | features[feat_count - 1] = (struct lyd_node_leaf_list *)child2; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if (!ns || !name || !rev) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 602 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 603 | continue; |
| 604 | } |
| 605 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 606 | str_len = sprintf(str, "%s?module=%s&revision=%s", ns->value_str, name->value_str, rev->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 607 | if (feat_count) { |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame] | 608 | strcat(str, "&features="); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 609 | str_len += 14; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 610 | for (i = 0; i < feat_count; ++i) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 611 | if (str_len + 1 + strlen(features[i]->value_str) >= 512) { |
| 612 | ERRINT; |
| 613 | break; |
| 614 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 615 | if (i) { |
| 616 | strcat(str, ","); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 617 | ++str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 618 | } |
| 619 | strcat(str, features[i]->value_str); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 620 | str_len += strlen(features[i]->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 625 | |
| 626 | ns = NULL; |
| 627 | name = NULL; |
| 628 | rev = NULL; |
| 629 | free(features); |
| 630 | features = NULL; |
| 631 | feat_count = 0; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | lyd_free(yanglib); |
| 636 | |
| 637 | /* ending NULL capability */ |
| 638 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 639 | |
| 640 | return cpblts; |
| 641 | } |
| 642 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 643 | static int |
| 644 | parse_cpblts(struct lyxml_elem *xml, const char ***list) |
| 645 | { |
| 646 | struct lyxml_elem *cpblt; |
| 647 | int ver = -1; |
| 648 | int i = 0; |
| 649 | |
| 650 | if (list) { |
| 651 | /* get the storage for server's capabilities */ |
| 652 | LY_TREE_FOR(xml->child, cpblt) { |
| 653 | i++; |
| 654 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 655 | /* last item remains NULL */ |
| 656 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 657 | if (!*list) { |
| 658 | ERRMEM; |
| 659 | return -1; |
| 660 | } |
| 661 | i = 0; |
| 662 | } |
| 663 | |
| 664 | LY_TREE_FOR(xml->child, cpblt) { |
| 665 | if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
| 666 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 667 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 668 | return -1; |
| 669 | } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 670 | continue; |
| 671 | } |
| 672 | |
| 673 | /* detect NETCONF version */ |
| 674 | if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) { |
| 675 | ver = 0; |
| 676 | } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) { |
| 677 | ver = 1; |
| 678 | } |
| 679 | |
| 680 | /* store capabilities */ |
| 681 | if (list) { |
| 682 | (*list)[i] = cpblt->content; |
| 683 | cpblt->content = NULL; |
| 684 | i++; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 689 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | return ver; |
| 693 | } |
| 694 | |
| 695 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 696 | nc_send_client_hello(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 697 | { |
| 698 | int r, i; |
| 699 | const char **cpblts; |
| 700 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 701 | /* client side hello - send only NETCONF base capabilities */ |
| 702 | cpblts = malloc(3 * sizeof *cpblts); |
| 703 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 704 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 705 | cpblts[2] = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 706 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 707 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 708 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 709 | for (i = 0; cpblts[i]; ++i) { |
| 710 | lydict_remove(session->ctx, cpblts[i]); |
| 711 | } |
| 712 | free(cpblts); |
| 713 | |
| 714 | if (r) { |
| 715 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 716 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 717 | |
| 718 | return NC_MSG_HELLO; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 722 | nc_send_server_hello(struct nc_session *session) |
| 723 | { |
| 724 | int r, i; |
| 725 | const char **cpblts; |
| 726 | |
| 727 | cpblts = create_cpblts(session->ctx); |
| 728 | |
| 729 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id); |
| 730 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 731 | for (i = 0; cpblts[i]; ++i) { |
| 732 | lydict_remove(session->ctx, cpblts[i]); |
| 733 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 734 | free(cpblts); |
| 735 | |
| 736 | if (r) { |
| 737 | return NC_MSG_ERROR; |
| 738 | } |
| 739 | |
| 740 | return NC_MSG_HELLO; |
| 741 | } |
| 742 | |
| 743 | static NC_MSG_TYPE |
| 744 | nc_recv_client_hello(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 745 | { |
| 746 | struct lyxml_elem *xml = NULL, *node; |
| 747 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 748 | int ver = -1; |
| 749 | char *str; |
| 750 | long long int id; |
| 751 | int flag = 0; |
| 752 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 753 | msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 754 | |
| 755 | switch(msgtype) { |
| 756 | case NC_MSG_HELLO: |
| 757 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 758 | LY_TREE_FOR(xml->child, node) { |
| 759 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 760 | continue; |
| 761 | } else if (!strcmp(node->name, "session-id")) { |
| 762 | if (!node->content || !strlen(node->content)) { |
| 763 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 764 | goto error; |
| 765 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 766 | str = NULL; |
| 767 | id = strtoll(node->content, &str, 10); |
| 768 | if (*str || id < 1 || id > UINT32_MAX) { |
| 769 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 770 | goto error; |
| 771 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 772 | session->id = (uint32_t)id; |
| 773 | continue; |
| 774 | } else if (strcmp(node->name, "capabilities")) { |
| 775 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 776 | goto error; |
| 777 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 778 | |
| 779 | if (flag) { |
| 780 | /* multiple capabilities elements */ |
| 781 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 782 | goto error; |
| 783 | } |
| 784 | flag = 1; |
| 785 | |
| 786 | if ((ver = parse_cpblts(node, &session->cpblts)) < 0) { |
| 787 | goto error; |
| 788 | } |
| 789 | session->version = ver; |
| 790 | } |
| 791 | |
| 792 | if (!session->id) { |
| 793 | ERR("Missing <session-id> in server's <hello>."); |
| 794 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 795 | } |
| 796 | break; |
| 797 | case NC_MSG_ERROR: |
| 798 | /* nothing special, just pass it out */ |
| 799 | break; |
| 800 | default: |
| 801 | ERR("Unexpected message received instead of <hello>."); |
| 802 | msgtype = NC_MSG_ERROR; |
| 803 | } |
| 804 | |
| 805 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 806 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 807 | |
| 808 | return msgtype; |
| 809 | |
| 810 | error: |
| 811 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 812 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 813 | |
| 814 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 815 | } |
| 816 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 817 | static NC_MSG_TYPE |
| 818 | nc_recv_server_hello(struct nc_session *session) |
| 819 | { |
| 820 | struct lyxml_elem *xml = NULL, *node; |
| 821 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 822 | int ver = -1; |
| 823 | int flag = 0; |
| 824 | |
Michal Vasko | adb850e | 2016-01-20 14:06:32 +0100 | [diff] [blame] | 825 | msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 826 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 827 | switch (msgtype) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 828 | case NC_MSG_HELLO: |
| 829 | /* get know NETCONF version */ |
| 830 | LY_TREE_FOR(xml->child, node) { |
| 831 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 832 | continue; |
| 833 | } else if (strcmp(node->name, "capabilities")) { |
| 834 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
| 835 | msgtype = NC_MSG_ERROR; |
| 836 | goto cleanup; |
| 837 | } |
| 838 | |
| 839 | if (flag) { |
| 840 | /* multiple capabilities elements */ |
| 841 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 842 | msgtype = NC_MSG_ERROR; |
| 843 | goto cleanup; |
| 844 | } |
| 845 | flag = 1; |
| 846 | |
| 847 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
| 848 | msgtype = NC_MSG_ERROR; |
| 849 | goto cleanup; |
| 850 | } |
| 851 | session->version = ver; |
| 852 | } |
| 853 | break; |
| 854 | case NC_MSG_ERROR: |
| 855 | /* nothing special, just pass it out */ |
| 856 | break; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 857 | case NC_MSG_WOULDBLOCK: |
| 858 | ERR("Client's <hello> timeout elapsed."); |
| 859 | msgtype = NC_MSG_ERROR; |
| 860 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 861 | default: |
| 862 | ERR("Unexpected message received instead of <hello>."); |
| 863 | msgtype = NC_MSG_ERROR; |
| 864 | } |
| 865 | |
| 866 | cleanup: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 867 | lyxml_free(session->ctx, xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 868 | |
| 869 | return msgtype; |
| 870 | } |
| 871 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 872 | int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 873 | nc_handshake(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 874 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 875 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 876 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 877 | if (session->side == NC_CLIENT) { |
| 878 | type = nc_send_client_hello(session); |
| 879 | } else { |
| 880 | type = nc_send_server_hello(session); |
| 881 | } |
| 882 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 883 | if (type != NC_MSG_HELLO) { |
| 884 | return 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 885 | } |
| 886 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 887 | if (session->side == NC_CLIENT) { |
| 888 | type = nc_recv_client_hello(session); |
| 889 | } else { |
| 890 | type = nc_recv_server_hello(session); |
| 891 | } |
| 892 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 893 | if (type != NC_MSG_HELLO) { |
| 894 | return 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 895 | } |
| 896 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 897 | return 0; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 898 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 899 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 900 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 901 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 902 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 903 | nc_ssh_init(void) |
| 904 | { |
| 905 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 906 | ssh_init(); |
| 907 | ssh_set_log_level(verbose_level); |
| 908 | } |
| 909 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 910 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 911 | nc_ssh_destroy(void) |
| 912 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 913 | FIPS_mode_set(0); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 914 | ENGINE_cleanup(); |
| 915 | CONF_modules_unload(1); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 916 | nc_thread_destroy(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 917 | ssh_finalize(); |
| 918 | } |
| 919 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 920 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 921 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 922 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 923 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 924 | struct CRYPTO_dynlock_value { |
| 925 | pthread_mutex_t lock; |
| 926 | }; |
| 927 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 928 | static struct CRYPTO_dynlock_value * |
| 929 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 930 | { |
| 931 | struct CRYPTO_dynlock_value *value; |
| 932 | |
| 933 | value = malloc(sizeof *value); |
| 934 | if (!value) { |
| 935 | ERRMEM; |
| 936 | return NULL; |
| 937 | } |
| 938 | pthread_mutex_init(&value->lock, NULL); |
| 939 | |
| 940 | return value; |
| 941 | } |
| 942 | |
| 943 | static void |
| 944 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 945 | { |
| 946 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 947 | * I found ignored this fact, what do I know... */ |
| 948 | if (mode & CRYPTO_LOCK) { |
| 949 | pthread_mutex_lock(&l->lock); |
| 950 | } else { |
| 951 | pthread_mutex_unlock(&l->lock); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | static void |
| 956 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 957 | { |
| 958 | pthread_mutex_destroy(&l->lock); |
| 959 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 960 | } |
| 961 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 962 | #endif /* NC_ENABLED_TLS */ |
| 963 | |
| 964 | #if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH) |
| 965 | |
| 966 | static pthread_mutex_t *tls_locks; |
| 967 | |
| 968 | static void |
| 969 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 970 | { |
| 971 | if (mode & CRYPTO_LOCK) { |
| 972 | pthread_mutex_lock(tls_locks + n); |
| 973 | } else { |
| 974 | pthread_mutex_unlock(tls_locks + n); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | static void |
| 979 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 980 | { |
| 981 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 982 | } |
| 983 | |
| 984 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 985 | nc_tls_init(void) |
| 986 | { |
| 987 | int i; |
| 988 | |
| 989 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 990 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 991 | SSL_library_init(); |
| 992 | |
| 993 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
| 994 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 995 | pthread_mutex_init(tls_locks + i, NULL); |
| 996 | } |
| 997 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 998 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 999 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1000 | |
| 1001 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1002 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1003 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1006 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1007 | nc_tls_destroy(void) |
| 1008 | { |
| 1009 | int i; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1010 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1011 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1012 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1013 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1014 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1015 | ERR_free_strings(); |
| 1016 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1017 | |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1018 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1019 | CRYPTO_set_locking_callback(NULL); |
| 1020 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1021 | pthread_mutex_destroy(tls_locks + i); |
| 1022 | } |
| 1023 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1024 | |
| 1025 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1026 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1027 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1030 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1031 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1032 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1033 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1034 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1035 | nc_ssh_tls_init(void) |
| 1036 | { |
| 1037 | SSL_load_error_strings(); |
| 1038 | ERR_load_BIO_strings(); |
| 1039 | SSL_library_init(); |
| 1040 | |
| 1041 | nc_ssh_init(); |
| 1042 | |
| 1043 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1044 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1045 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
| 1046 | } |
| 1047 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1048 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1049 | nc_ssh_tls_destroy(void) |
| 1050 | { |
| 1051 | ERR_free_strings(); |
| 1052 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
| 1053 | |
| 1054 | nc_ssh_destroy(); |
| 1055 | |
| 1056 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1057 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1058 | CRYPTO_set_dynlock_destroy_callback(NULL); |
| 1059 | } |
| 1060 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1061 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1062 | |
| 1063 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1064 | |
| 1065 | API void |
| 1066 | nc_thread_destroy(void) |
| 1067 | { |
| 1068 | CRYPTO_THREADID crypto_tid; |
| 1069 | |
| 1070 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
| 1071 | //CRYPTO_cleanup_all_ex_data(); |
| 1072 | |
| 1073 | CRYPTO_THREADID_current(&crypto_tid); |
| 1074 | ERR_remove_thread_state(&crypto_tid); |
| 1075 | } |
| 1076 | |
| 1077 | API void |
| 1078 | nc_init(void) |
| 1079 | { |
| 1080 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1081 | nc_ssh_tls_init(); |
| 1082 | #elif defined(NC_ENABLED_SSH) |
| 1083 | nc_ssh_init(); |
| 1084 | #elif defined(NC_ENABLED_TLS) |
| 1085 | nc_tls_init(); |
| 1086 | #endif |
| 1087 | } |
| 1088 | |
| 1089 | API void |
| 1090 | nc_destroy(void) |
| 1091 | { |
| 1092 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1093 | nc_ssh_tls_destroy(); |
| 1094 | #elif defined(NC_ENABLED_SSH) |
| 1095 | nc_ssh_destroy(); |
| 1096 | #elif defined(NC_ENABLED_TLS) |
| 1097 | nc_tls_destroy(); |
| 1098 | #endif |
| 1099 | } |
| 1100 | |
| 1101 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |