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