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