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