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 */ |
Radek Krejci | da8f58d | 2016-03-03 13:10:21 +0100 | [diff] [blame^] | 88 | ERR("Mutex lock failed (%s).", strerror(ret)); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 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 |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 258 | nc_session_free(struct nc_session *session, void (*data_free)(void *)) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 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 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 354 | if (session->data && data_free) { |
| 355 | data_free(session->data); |
| 356 | } |
| 357 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 358 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 359 | connected = nc_session_is_connected(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 360 | |
| 361 | /* transport implementation cleanup */ |
| 362 | switch (session->ti_type) { |
| 363 | case NC_TI_FD: |
| 364 | /* nothing needed - file descriptors were provided by caller, |
| 365 | * so it is up to the caller to close them correctly |
| 366 | * TODO use callbacks |
| 367 | */ |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 368 | /* just to avoid compiler warning */ |
| 369 | (void)connected; |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 370 | (void)siter; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 371 | break; |
| 372 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 373 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 374 | case NC_TI_LIBSSH: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 375 | if (connected) { |
| 376 | ssh_channel_free(session->ti.libssh.channel); |
| 377 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 378 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 379 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 380 | * it. |
| 381 | */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 382 | multisession = 0; |
| 383 | if (session->ti.libssh.next) { |
| 384 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 385 | if (siter->status != NC_STATUS_STARTING) { |
| 386 | multisession = 1; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (!multisession) { |
| 393 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 394 | if (session->ti.libssh.next) { |
| 395 | do { |
| 396 | siter = session->ti.libssh.next; |
| 397 | session->ti.libssh.next = siter->ti.libssh.next; |
| 398 | |
| 399 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 400 | lydict_remove(session->ctx, session->username); |
| 401 | lydict_remove(session->ctx, session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 402 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 403 | ly_ctx_destroy(session->ctx, NULL); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | free(siter); |
| 407 | } while (session->ti.libssh.next != session); |
| 408 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 409 | if (connected) { |
| 410 | ssh_disconnect(session->ti.libssh.session); |
| 411 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 412 | ssh_free(session->ti.libssh.session); |
| 413 | } else { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 414 | /* remove the session from the list */ |
| 415 | 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] | 416 | if (session->ti.libssh.next == siter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 417 | /* there will be only one session */ |
| 418 | siter->ti.libssh.next = NULL; |
| 419 | } else { |
| 420 | /* there are still multiple sessions, keep the ring list */ |
| 421 | siter->ti.libssh.next = session->ti.libssh.next; |
| 422 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 423 | /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */ |
| 424 | if (session->flags & NC_SESSION_SSH_MSG_CB) { |
| 425 | for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) { |
| 426 | if (siter->ti.libssh.next == session) { |
| 427 | ERRINT; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter); |
| 432 | siter->flags |= NC_SESSION_SSH_MSG_CB; |
| 433 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 434 | } |
| 435 | break; |
| 436 | #endif |
| 437 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 438 | #ifdef NC_ENABLED_TLS |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 439 | case NC_TI_OPENSSL: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 440 | if (connected) { |
| 441 | SSL_shutdown(session->ti.tls); |
| 442 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 443 | SSL_free(session->ti.tls); |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 444 | |
| 445 | X509_free(session->tls_cert); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 446 | break; |
| 447 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 448 | case NC_TI_NONE: |
| 449 | ERRINT; |
| 450 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 451 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 452 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 453 | lydict_remove(session->ctx, session->username); |
| 454 | lydict_remove(session->ctx, session->host); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 455 | |
| 456 | /* final cleanup */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 457 | if (session->ti_lock) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 458 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 459 | if (!multisession) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 460 | pthread_mutex_destroy(session->ti_lock); |
| 461 | free(session->ti_lock); |
| 462 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 466 | ly_ctx_destroy(session->ctx, NULL); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | free(session); |
| 470 | } |
| 471 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 472 | static void |
| 473 | add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count) |
| 474 | { |
| 475 | if (*count == *size) { |
| 476 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 477 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 478 | if (!(*cpblts)) { |
| 479 | ERRMEM; |
| 480 | return; |
| 481 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (capab) { |
| 485 | (*cpblts)[*count] = lydict_insert(ctx, capab, 0); |
| 486 | } else { |
| 487 | (*cpblts)[*count] = NULL; |
| 488 | } |
| 489 | ++(*count); |
| 490 | } |
| 491 | |
| 492 | static const char ** |
| 493 | create_cpblts(struct ly_ctx *ctx) |
| 494 | { |
| 495 | struct lyd_node *child, *child2, *yanglib; |
| 496 | struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL; |
| 497 | const char **cpblts; |
| 498 | const struct lys_module *mod; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 499 | int size = 10, count, feat_count = 0, i, str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 500 | char str[512]; |
| 501 | |
| 502 | yanglib = ly_ctx_info(ctx); |
| 503 | if (!yanglib) { |
| 504 | return NULL; |
| 505 | } |
| 506 | |
| 507 | cpblts = malloc(size * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 508 | if (!cpblts) { |
| 509 | ERRMEM; |
| 510 | return NULL; |
| 511 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 512 | cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 513 | cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 514 | count = 2; |
| 515 | |
| 516 | /* capabilities */ |
| 517 | |
| 518 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 519 | if (mod) { |
| 520 | if (lys_features_state(mod, "writable-running") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 521 | 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] | 522 | } |
| 523 | if (lys_features_state(mod, "candidate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 524 | 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] | 525 | if (lys_features_state(mod, "confirmed-commit") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 526 | 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] | 527 | } |
| 528 | } |
| 529 | if (lys_features_state(mod, "rollback-on-error") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 530 | 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] | 531 | } |
| 532 | if (lys_features_state(mod, "validate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 533 | 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] | 534 | } |
| 535 | if (lys_features_state(mod, "startup") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 536 | 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] | 537 | } |
| 538 | if (lys_features_state(mod, "url") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 539 | 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] | 540 | } |
| 541 | if (lys_features_state(mod, "xpath") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 542 | 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] | 543 | } |
| 544 | } |
| 545 | |
| 546 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL); |
| 547 | if (mod) { |
| 548 | if (!server_opts.wd_basic_mode) { |
| 549 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 550 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 551 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 552 | switch (server_opts.wd_basic_mode) { |
| 553 | case NC_WD_ALL: |
| 554 | strcat(str, "?basic-mode=report-all"); |
| 555 | break; |
| 556 | case NC_WD_TRIM: |
| 557 | strcat(str, "?basic-mode=trim"); |
| 558 | break; |
| 559 | case NC_WD_EXPLICIT: |
| 560 | strcat(str, "?basic-mode=explicit"); |
| 561 | break; |
| 562 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 563 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | |
| 567 | if (server_opts.wd_also_supported) { |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame] | 568 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 569 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 570 | strcat(str, "report-all,"); |
| 571 | } |
| 572 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 573 | strcat(str, "report-all-tagged,"); |
| 574 | } |
| 575 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 576 | strcat(str, "trim,"); |
| 577 | } |
| 578 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 579 | strcat(str, "explicit,"); |
| 580 | } |
| 581 | str[strlen(str) - 1] = '\0'; |
| 582 | |
| 583 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 588 | mod = ly_ctx_get_module(ctx, "nc-notifications", NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 589 | if (mod) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 590 | 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] | 591 | if (server_opts.interleave_capab) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 592 | 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] | 593 | } |
| 594 | } |
| 595 | |
| 596 | /* models */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 597 | LY_TREE_FOR(yanglib->child, child) { |
| 598 | if (!strcmp(child->schema->name, "module")) { |
| 599 | LY_TREE_FOR(child->child, child2) { |
| 600 | if (!strcmp(child2->schema->name, "namespace")) { |
| 601 | ns = (struct lyd_node_leaf_list *)child2; |
| 602 | } else if (!strcmp(child2->schema->name, "name")) { |
| 603 | name = (struct lyd_node_leaf_list *)child2; |
| 604 | } else if (!strcmp(child2->schema->name, "revision")) { |
| 605 | rev = (struct lyd_node_leaf_list *)child2; |
| 606 | } else if (!strcmp(child2->schema->name, "feature")) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 607 | features = nc_realloc(features, ++feat_count * sizeof *features); |
| 608 | if (!features) { |
| 609 | ERRMEM; |
| 610 | free(cpblts); |
| 611 | return NULL; |
| 612 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 613 | features[feat_count - 1] = (struct lyd_node_leaf_list *)child2; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (!ns || !name || !rev) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 618 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 619 | continue; |
| 620 | } |
| 621 | |
Radek Krejci | df7ba52 | 2016-03-01 16:05:25 +0100 | [diff] [blame] | 622 | str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str, |
| 623 | rev->value_str[0] ? "&revision=" : "", rev->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 624 | if (feat_count) { |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame] | 625 | strcat(str, "&features="); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 626 | str_len += 14; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 627 | for (i = 0; i < feat_count; ++i) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 628 | if (str_len + 1 + strlen(features[i]->value_str) >= 512) { |
| 629 | ERRINT; |
| 630 | break; |
| 631 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 632 | if (i) { |
| 633 | strcat(str, ","); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 634 | ++str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 635 | } |
| 636 | strcat(str, features[i]->value_str); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 637 | str_len += strlen(features[i]->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
| 641 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 642 | |
| 643 | ns = NULL; |
| 644 | name = NULL; |
| 645 | rev = NULL; |
| 646 | free(features); |
| 647 | features = NULL; |
| 648 | feat_count = 0; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | lyd_free(yanglib); |
| 653 | |
| 654 | /* ending NULL capability */ |
| 655 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 656 | |
| 657 | return cpblts; |
| 658 | } |
| 659 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 660 | static int |
| 661 | parse_cpblts(struct lyxml_elem *xml, const char ***list) |
| 662 | { |
| 663 | struct lyxml_elem *cpblt; |
| 664 | int ver = -1; |
| 665 | int i = 0; |
| 666 | |
| 667 | if (list) { |
| 668 | /* get the storage for server's capabilities */ |
| 669 | LY_TREE_FOR(xml->child, cpblt) { |
| 670 | i++; |
| 671 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 672 | /* last item remains NULL */ |
| 673 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 674 | if (!*list) { |
| 675 | ERRMEM; |
| 676 | return -1; |
| 677 | } |
| 678 | i = 0; |
| 679 | } |
| 680 | |
| 681 | LY_TREE_FOR(xml->child, cpblt) { |
| 682 | if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
| 683 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 684 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 685 | return -1; |
| 686 | } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | /* detect NETCONF version */ |
| 691 | if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) { |
| 692 | ver = 0; |
| 693 | } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) { |
| 694 | ver = 1; |
| 695 | } |
| 696 | |
| 697 | /* store capabilities */ |
| 698 | if (list) { |
| 699 | (*list)[i] = cpblt->content; |
| 700 | cpblt->content = NULL; |
| 701 | i++; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 706 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | return ver; |
| 710 | } |
| 711 | |
| 712 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 713 | nc_send_client_hello(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 714 | { |
| 715 | int r, i; |
| 716 | const char **cpblts; |
| 717 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 718 | /* client side hello - send only NETCONF base capabilities */ |
| 719 | cpblts = malloc(3 * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 720 | if (!cpblts) { |
| 721 | ERRMEM; |
| 722 | return NC_MSG_ERROR; |
| 723 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 724 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 725 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 726 | cpblts[2] = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 727 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 728 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 729 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 730 | for (i = 0; cpblts[i]; ++i) { |
| 731 | lydict_remove(session->ctx, cpblts[i]); |
| 732 | } |
| 733 | free(cpblts); |
| 734 | |
| 735 | if (r) { |
| 736 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 737 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 738 | |
| 739 | return NC_MSG_HELLO; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 743 | nc_send_server_hello(struct nc_session *session) |
| 744 | { |
| 745 | int r, i; |
| 746 | const char **cpblts; |
| 747 | |
| 748 | cpblts = create_cpblts(session->ctx); |
| 749 | |
| 750 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id); |
| 751 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 752 | for (i = 0; cpblts[i]; ++i) { |
| 753 | lydict_remove(session->ctx, cpblts[i]); |
| 754 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 755 | free(cpblts); |
| 756 | |
| 757 | if (r) { |
| 758 | return NC_MSG_ERROR; |
| 759 | } |
| 760 | |
| 761 | return NC_MSG_HELLO; |
| 762 | } |
| 763 | |
| 764 | static NC_MSG_TYPE |
| 765 | nc_recv_client_hello(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 766 | { |
| 767 | struct lyxml_elem *xml = NULL, *node; |
| 768 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 769 | int ver = -1; |
| 770 | char *str; |
| 771 | long long int id; |
| 772 | int flag = 0; |
| 773 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 774 | msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 775 | |
| 776 | switch(msgtype) { |
| 777 | case NC_MSG_HELLO: |
| 778 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 779 | LY_TREE_FOR(xml->child, node) { |
| 780 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 781 | continue; |
| 782 | } else if (!strcmp(node->name, "session-id")) { |
| 783 | if (!node->content || !strlen(node->content)) { |
| 784 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 785 | goto error; |
| 786 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 787 | str = NULL; |
| 788 | id = strtoll(node->content, &str, 10); |
| 789 | if (*str || id < 1 || id > UINT32_MAX) { |
| 790 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 791 | goto error; |
| 792 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 793 | session->id = (uint32_t)id; |
| 794 | continue; |
| 795 | } else if (strcmp(node->name, "capabilities")) { |
| 796 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 797 | goto error; |
| 798 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 799 | |
| 800 | if (flag) { |
| 801 | /* multiple capabilities elements */ |
| 802 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 803 | goto error; |
| 804 | } |
| 805 | flag = 1; |
| 806 | |
| 807 | if ((ver = parse_cpblts(node, &session->cpblts)) < 0) { |
| 808 | goto error; |
| 809 | } |
| 810 | session->version = ver; |
| 811 | } |
| 812 | |
| 813 | if (!session->id) { |
| 814 | ERR("Missing <session-id> in server's <hello>."); |
| 815 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 816 | } |
| 817 | break; |
| 818 | case NC_MSG_ERROR: |
| 819 | /* nothing special, just pass it out */ |
| 820 | break; |
| 821 | default: |
| 822 | ERR("Unexpected message received instead of <hello>."); |
| 823 | msgtype = NC_MSG_ERROR; |
| 824 | } |
| 825 | |
| 826 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 827 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 828 | |
| 829 | return msgtype; |
| 830 | |
| 831 | error: |
| 832 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 833 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 834 | |
| 835 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 838 | static NC_MSG_TYPE |
| 839 | nc_recv_server_hello(struct nc_session *session) |
| 840 | { |
| 841 | struct lyxml_elem *xml = NULL, *node; |
| 842 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 843 | int ver = -1; |
| 844 | int flag = 0; |
| 845 | |
Michal Vasko | adb850e | 2016-01-20 14:06:32 +0100 | [diff] [blame] | 846 | 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] | 847 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 848 | switch (msgtype) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 849 | case NC_MSG_HELLO: |
| 850 | /* get know NETCONF version */ |
| 851 | LY_TREE_FOR(xml->child, node) { |
| 852 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 853 | continue; |
| 854 | } else if (strcmp(node->name, "capabilities")) { |
| 855 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
| 856 | msgtype = NC_MSG_ERROR; |
| 857 | goto cleanup; |
| 858 | } |
| 859 | |
| 860 | if (flag) { |
| 861 | /* multiple capabilities elements */ |
| 862 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 863 | msgtype = NC_MSG_ERROR; |
| 864 | goto cleanup; |
| 865 | } |
| 866 | flag = 1; |
| 867 | |
| 868 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
| 869 | msgtype = NC_MSG_ERROR; |
| 870 | goto cleanup; |
| 871 | } |
| 872 | session->version = ver; |
| 873 | } |
| 874 | break; |
| 875 | case NC_MSG_ERROR: |
| 876 | /* nothing special, just pass it out */ |
| 877 | break; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 878 | case NC_MSG_WOULDBLOCK: |
| 879 | ERR("Client's <hello> timeout elapsed."); |
| 880 | msgtype = NC_MSG_ERROR; |
| 881 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 882 | default: |
| 883 | ERR("Unexpected message received instead of <hello>."); |
| 884 | msgtype = NC_MSG_ERROR; |
| 885 | } |
| 886 | |
| 887 | cleanup: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 888 | lyxml_free(session->ctx, xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 889 | |
| 890 | return msgtype; |
| 891 | } |
| 892 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 893 | int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 894 | nc_handshake(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 895 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 896 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 897 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 898 | if (session->side == NC_CLIENT) { |
| 899 | type = nc_send_client_hello(session); |
| 900 | } else { |
| 901 | type = nc_send_server_hello(session); |
| 902 | } |
| 903 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 904 | if (type != NC_MSG_HELLO) { |
| 905 | return 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 908 | if (session->side == NC_CLIENT) { |
| 909 | type = nc_recv_client_hello(session); |
| 910 | } else { |
| 911 | type = nc_recv_server_hello(session); |
| 912 | } |
| 913 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 914 | if (type != NC_MSG_HELLO) { |
| 915 | return 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 918 | return 0; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 919 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 920 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 921 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 922 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 923 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 924 | nc_ssh_init(void) |
| 925 | { |
| 926 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 927 | ssh_init(); |
| 928 | ssh_set_log_level(verbose_level); |
| 929 | } |
| 930 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 931 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 932 | nc_ssh_destroy(void) |
| 933 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 934 | FIPS_mode_set(0); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 935 | ENGINE_cleanup(); |
| 936 | CONF_modules_unload(1); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 937 | nc_thread_destroy(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 938 | ssh_finalize(); |
| 939 | } |
| 940 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 941 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 942 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 943 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 944 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 945 | struct CRYPTO_dynlock_value { |
| 946 | pthread_mutex_t lock; |
| 947 | }; |
| 948 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 949 | static struct CRYPTO_dynlock_value * |
| 950 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 951 | { |
| 952 | struct CRYPTO_dynlock_value *value; |
| 953 | |
| 954 | value = malloc(sizeof *value); |
| 955 | if (!value) { |
| 956 | ERRMEM; |
| 957 | return NULL; |
| 958 | } |
| 959 | pthread_mutex_init(&value->lock, NULL); |
| 960 | |
| 961 | return value; |
| 962 | } |
| 963 | |
| 964 | static void |
| 965 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 966 | { |
| 967 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 968 | * I found ignored this fact, what do I know... */ |
| 969 | if (mode & CRYPTO_LOCK) { |
| 970 | pthread_mutex_lock(&l->lock); |
| 971 | } else { |
| 972 | pthread_mutex_unlock(&l->lock); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | static void |
| 977 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 978 | { |
| 979 | pthread_mutex_destroy(&l->lock); |
| 980 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 981 | } |
| 982 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 983 | #endif /* NC_ENABLED_TLS */ |
| 984 | |
| 985 | #if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH) |
| 986 | |
| 987 | static pthread_mutex_t *tls_locks; |
| 988 | |
| 989 | static void |
| 990 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 991 | { |
| 992 | if (mode & CRYPTO_LOCK) { |
| 993 | pthread_mutex_lock(tls_locks + n); |
| 994 | } else { |
| 995 | pthread_mutex_unlock(tls_locks + n); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | static void |
| 1000 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 1001 | { |
| 1002 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 1003 | } |
| 1004 | |
| 1005 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1006 | nc_tls_init(void) |
| 1007 | { |
| 1008 | int i; |
| 1009 | |
| 1010 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1011 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1012 | SSL_library_init(); |
| 1013 | |
| 1014 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1015 | if (!tls_locks) { |
| 1016 | ERRMEM; |
| 1017 | return; |
| 1018 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1019 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1020 | pthread_mutex_init(tls_locks + i, NULL); |
| 1021 | } |
| 1022 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1023 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1024 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1025 | |
| 1026 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1027 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1028 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1029 | } |
| 1030 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1031 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1032 | nc_tls_destroy(void) |
| 1033 | { |
| 1034 | int i; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1035 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1036 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1037 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1038 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1039 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1040 | ERR_free_strings(); |
| 1041 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1042 | |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1043 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1044 | CRYPTO_set_locking_callback(NULL); |
| 1045 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1046 | pthread_mutex_destroy(tls_locks + i); |
| 1047 | } |
| 1048 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1049 | |
| 1050 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1051 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1052 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1055 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1056 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1057 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1058 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1059 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1060 | nc_ssh_tls_init(void) |
| 1061 | { |
| 1062 | SSL_load_error_strings(); |
| 1063 | ERR_load_BIO_strings(); |
| 1064 | SSL_library_init(); |
| 1065 | |
| 1066 | nc_ssh_init(); |
| 1067 | |
| 1068 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1069 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1070 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
| 1071 | } |
| 1072 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1073 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1074 | nc_ssh_tls_destroy(void) |
| 1075 | { |
| 1076 | ERR_free_strings(); |
| 1077 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
| 1078 | |
| 1079 | nc_ssh_destroy(); |
| 1080 | |
| 1081 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1082 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1083 | CRYPTO_set_dynlock_destroy_callback(NULL); |
| 1084 | } |
| 1085 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1086 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1087 | |
| 1088 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1089 | |
| 1090 | API void |
| 1091 | nc_thread_destroy(void) |
| 1092 | { |
| 1093 | CRYPTO_THREADID crypto_tid; |
| 1094 | |
| 1095 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
| 1096 | //CRYPTO_cleanup_all_ex_data(); |
| 1097 | |
| 1098 | CRYPTO_THREADID_current(&crypto_tid); |
| 1099 | ERR_remove_thread_state(&crypto_tid); |
| 1100 | } |
| 1101 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1102 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 1103 | |
| 1104 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1105 | nc_init(void) |
| 1106 | { |
| 1107 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1108 | nc_ssh_tls_init(); |
| 1109 | #elif defined(NC_ENABLED_SSH) |
| 1110 | nc_ssh_init(); |
| 1111 | #elif defined(NC_ENABLED_TLS) |
| 1112 | nc_tls_init(); |
| 1113 | #endif |
| 1114 | } |
| 1115 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1116 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1117 | nc_destroy(void) |
| 1118 | { |
| 1119 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1120 | nc_ssh_tls_destroy(); |
| 1121 | #elif defined(NC_ENABLED_SSH) |
| 1122 | nc_ssh_destroy(); |
| 1123 | #elif defined(NC_ENABLED_TLS) |
| 1124 | nc_tls_destroy(); |
| 1125 | #endif |
| 1126 | } |