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