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