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