blob: fd0c0ad1ee1f55cbad5a07df7e88821e9de339fa [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
14
Radek Krejci206fcd62015-10-07 15:42:48 +020015#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010016#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010017#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020018#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020019#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010020#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020021#include <libyang/libyang.h>
22
Michal Vasko5e228792016-02-03 15:30:24 +010023#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010024#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010025#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010026
Radek Krejci53691be2016-02-22 13:58:37 +010027#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020028
Michal Vasko086311b2016-01-08 09:53:11 +010029# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Radek Krejci53691be2016-02-22 13:58:37 +010031#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020032
Radek Krejci53691be2016-02-22 13:58:37 +010033#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010034
Michal Vasko5e228792016-02-03 15:30:24 +010035# include <openssl/engine.h>
36# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010037# include <openssl/err.h>
38
Radek Krejci53691be2016-02-22 13:58:37 +010039#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010040
Michal Vasko086311b2016-01-08 09:53:11 +010041/* in seconds */
42#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020043
Michal Vasko05ba9df2016-01-13 14:40:27 +010044/* in milliseconds */
45#define NC_CLOSE_REPLY_TIMEOUT 200
46
Michal Vasko086311b2016-01-08 09:53:11 +010047extern struct nc_server_opts server_opts;
48
Radek Krejci7ac16052016-07-15 11:48:18 +020049int
50nc_gettimespec(struct timespec *ts)
51{
Michal Vasko0ef46df2016-09-21 14:03:18 +020052#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020053 return clock_gettime(CLOCK_REALTIME, ts);
54#else
55 int rc;
56 struct timeval tv;
57
58 rc = gettimeofday(&tv, NULL);
59 if (!rc) {
60 ts->tv_sec = (time_t)tv.tv_sec;
61 ts->tv_nsec = 1000L * (long)tv.tv_usec;
62 }
63 return rc;
64#endif
65}
66
Radek Krejci28472922016-07-15 11:51:16 +020067#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
68int
69pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
70{
71 int rc;
72 struct timespec cur, dur;
73
74 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
75 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
76 nc_gettimespec(&cur);
77
78 if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
79 break;
80 }
81
82 dur.tv_sec = abstime->tv_sec - cur.tv_sec;
83 dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
84 if (dur.tv_nsec < 0) {
85 dur.tv_sec--;
86 dur.tv_nsec += 1000000000;
87 }
88
89 if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
90 dur.tv_sec = 0;
91 dur.tv_nsec = 5000000;
92 }
93
94 nanosleep(&dur, NULL);
95 }
96
97 return rc;
98}
99#endif
100
Michal Vasko96164bf2016-01-21 15:41:58 +0100101/*
102 * @return 1 - success
103 * 0 - timeout
104 * -1 - error
105 */
106int
Michal vasko50cc94f2016-10-04 13:46:20 +0200107nc_timedlock(pthread_mutex_t *lock, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100108{
109 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100110 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100111
112 if (timeout > 0) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200113 nc_gettimespec(&ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100114
Michal Vasko96164bf2016-01-21 15:41:58 +0100115 ts_timeout.tv_sec += timeout / 1000;
116 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
117
118 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100119 } else if (!timeout) {
120 ret = pthread_mutex_trylock(lock);
Michal vasko2f8e4b52016-10-05 13:04:11 +0200121 if (ret == EBUSY) {
122 /* equivalent in this case */
123 ret = ETIMEDOUT;
124 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100125 } else { /* timeout == -1 */
126 ret = pthread_mutex_lock(lock);
127 }
128
129 if (ret == ETIMEDOUT) {
130 /* timeout */
131 return 0;
132 } else if (ret) {
133 /* error */
Michal vasko50cc94f2016-10-04 13:46:20 +0200134 ERR("Mutex lock failed (%s, %s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100135 return -1;
136 }
137
138 /* ok */
139 return 1;
140}
141
Michal Vasko8dadf782016-01-15 10:29:36 +0100142API NC_STATUS
143nc_session_get_status(const struct nc_session *session)
144{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100145 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200146 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100147 return 0;
148 }
149
Michal Vasko8dadf782016-01-15 10:29:36 +0100150 return session->status;
151}
152
153API uint32_t
154nc_session_get_id(const struct nc_session *session)
155{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100156 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200157 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100158 return 0;
159 }
160
Michal Vasko8dadf782016-01-15 10:29:36 +0100161 return session->id;
162}
163
Michal Vasko174fe8e2016-02-17 15:38:09 +0100164API int
165nc_session_get_version(const struct nc_session *session)
166{
167 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200168 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100169 return -1;
170 }
171
172 return (session->version == NC_VERSION_10 ? 0 : 1);
173}
174
Michal Vasko8dadf782016-01-15 10:29:36 +0100175API NC_TRANSPORT_IMPL
176nc_session_get_ti(const struct nc_session *session)
177{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100178 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200179 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100180 return 0;
181 }
182
Michal Vasko8dadf782016-01-15 10:29:36 +0100183 return session->ti_type;
184}
185
186API const char *
187nc_session_get_username(const struct nc_session *session)
188{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100189 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200190 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100191 return NULL;
192 }
193
Michal Vasko8dadf782016-01-15 10:29:36 +0100194 return session->username;
195}
196
197API const char *
198nc_session_get_host(const struct nc_session *session)
199{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100200 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200201 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100202 return NULL;
203 }
204
Michal Vasko8dadf782016-01-15 10:29:36 +0100205 return session->host;
206}
207
208API uint16_t
209nc_session_get_port(const struct nc_session *session)
210{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100211 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200212 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100213 return 0;
214 }
215
Michal Vasko8dadf782016-01-15 10:29:36 +0100216 return session->port;
217}
218
Michal Vasko9a25e932016-02-01 10:36:42 +0100219API struct ly_ctx *
220nc_session_get_ctx(const struct nc_session *session)
221{
222 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200223 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100224 return NULL;
225 }
226
227 return session->ctx;
228}
229
Michal Vasko2cc4c682016-03-01 09:16:48 +0100230API void
231nc_session_set_data(struct nc_session *session, void *data)
232{
233 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200234 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100235 return;
236 }
237
238 session->data = data;
239}
240
241API void *
242nc_session_get_data(const struct nc_session *session)
243{
244 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200245 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100246 return NULL;
247 }
248
249 return session->data;
250}
251
Michal Vasko086311b2016-01-08 09:53:11 +0100252NC_MSG_TYPE
253nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200254{
Michal Vasko086311b2016-01-08 09:53:11 +0100255 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200256
Michal Vasko086311b2016-01-08 09:53:11 +0100257 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100258 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
259 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100260 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100261 }
262
Michal Vasko086311b2016-01-08 09:53:11 +0100263 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
264
265 if (r) {
266 return NC_MSG_ERROR;
267 }
268
269 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100270}
271
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100273nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200274{
Michal Vasko9e99f012016-03-03 13:25:20 +0100275 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100276 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100277 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100278 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100279 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100280 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100281 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200282 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100283 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200284 void *p;
285
Michal Vasko428087d2016-01-14 16:04:28 +0100286 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200287 return;
288 }
289
Michal Vasko86d357c2016-03-11 13:46:38 +0100290 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200291 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
292 tid = *session->opts.client.ntf_tid;
293 free((pthread_t *)session->opts.client.ntf_tid);
294 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100295 /* the thread now knows it should quit */
296
297 pthread_join(tid, NULL);
298 }
299
Michal Vaskoadd4c792015-10-26 15:36:58 +0100300 if (session->ti_lock) {
Michal vasko50cc94f2016-10-04 13:46:20 +0200301 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100302 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100303 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100304 } else if (!r) {
305 /* we failed to lock it, too bad */
306 locked = 0;
307 } else {
308 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100309 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200310 } else {
311 ERRINT;
312 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200313 }
314
Michal Vasko9e99f012016-03-03 13:25:20 +0100315 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200316 /* cleanup message queues */
317 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200318 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100319 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200320
Michal Vaskoad611702015-12-03 13:41:51 +0100321 p = contiter;
322 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200323 free(p);
324 }
325
326 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200327 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100328 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200329
Michal Vaskoad611702015-12-03 13:41:51 +0100330 p = contiter;
331 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200332 free(p);
333 }
334
335 /* send closing info to the other side */
336 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
337 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100338 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200339 } else {
340 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100341 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200342 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100343 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100344 case NC_MSG_REPLY:
345 LY_TREE_FOR(rpl->child, child) {
346 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
347 break;
348 }
349 }
350 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100351 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100352 }
Michal Vaskoad611702015-12-03 13:41:51 +0100353 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100354 break;
355 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100356 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100357 break;
358 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100359 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100360 break;
361 default:
362 /* cannot happen */
363 break;
364 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200365 }
366
367 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200368 if (session->opts.client.cpblts) {
369 for (i = 0; session->opts.client.cpblts[i]; i++) {
370 lydict_remove(session->ctx, session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200371 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200372 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200373 }
374 }
375
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100376 if (session->data && data_free) {
377 data_free(session->data);
378 }
379
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200380 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
381 /* CH LOCK */
382 pthread_mutex_lock(session->opts.server.ch_lock);
383 }
384
Michal Vasko86d357c2016-03-11 13:46:38 +0100385 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200386 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200387
388 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
389 pthread_cond_signal(session->opts.server.ch_cond);
390
391 /* CH UNLOCK */
392 pthread_mutex_unlock(session->opts.server.ch_lock);
393 }
394
Michal Vasko428087d2016-01-14 16:04:28 +0100395 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200396
397 /* transport implementation cleanup */
398 switch (session->ti_type) {
399 case NC_TI_FD:
400 /* nothing needed - file descriptors were provided by caller,
401 * so it is up to the caller to close them correctly
402 * TODO use callbacks
403 */
Michal Vasko3512e402016-01-28 16:22:34 +0100404 /* just to avoid compiler warning */
405 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100406 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200407 break;
408
Radek Krejci53691be2016-02-22 13:58:37 +0100409#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200410 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100411 if (connected) {
412 ssh_channel_free(session->ti.libssh.channel);
413 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200414 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
415 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
416 * it.
417 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100418 multisession = 0;
419 if (session->ti.libssh.next) {
420 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
421 if (siter->status != NC_STATUS_STARTING) {
422 multisession = 1;
423 break;
424 }
425 }
426 }
427
428 if (!multisession) {
429 /* it's not multisession yet, but we still need to free the starting sessions */
430 if (session->ti.libssh.next) {
431 do {
432 siter = session->ti.libssh.next;
433 session->ti.libssh.next = siter->ti.libssh.next;
434
435 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100436 lydict_remove(session->ctx, session->username);
437 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100438 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100439 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100440 }
441
442 free(siter);
443 } while (session->ti.libssh.next != session);
444 }
Michal Vasko428087d2016-01-14 16:04:28 +0100445 if (connected) {
446 ssh_disconnect(session->ti.libssh.session);
447 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200448 ssh_free(session->ti.libssh.session);
449 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200450 /* remove the session from the list */
451 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100452 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200453 /* there will be only one session */
454 siter->ti.libssh.next = NULL;
455 } else {
456 /* there are still multiple sessions, keep the ring list */
457 siter->ti.libssh.next = session->ti.libssh.next;
458 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100459 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
460 if (session->flags & NC_SESSION_SSH_MSG_CB) {
461 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
462 if (siter->ti.libssh.next == session) {
463 ERRINT;
464 break;
465 }
466 }
467 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
468 siter->flags |= NC_SESSION_SSH_MSG_CB;
469 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200470 }
471 break;
472#endif
473
Radek Krejci53691be2016-02-22 13:58:37 +0100474#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200475 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100476 if (connected) {
477 SSL_shutdown(session->ti.tls);
478 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200479 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100480
Michal Vasko2e6defd2016-10-07 15:48:15 +0200481 if (session->side == NC_SERVER) {
482 X509_free(session->opts.server.client_cert);
483 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200484 break;
485#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100486 case NC_TI_NONE:
487 ERRINT;
488 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200489 }
Michal Vasko428087d2016-01-14 16:04:28 +0100490
Radek Krejciac6d3472015-10-22 15:47:18 +0200491 lydict_remove(session->ctx, session->username);
492 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200493
494 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100495 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100496 if (locked) {
497 pthread_mutex_unlock(session->ti_lock);
498 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100499 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100500 pthread_mutex_destroy(session->ti_lock);
501 free(session->ti_lock);
502 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200503 }
504
505 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100506 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200507 }
508
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200509 if (session->side == NC_SERVER) {
510 if (session->opts.server.ch_cond) {
511 pthread_cond_destroy(session->opts.server.ch_cond);
512 free(session->opts.server.ch_cond);
513 }
514 if (session->opts.server.ch_lock) {
515 pthread_mutex_destroy(session->opts.server.ch_lock);
516 free(session->opts.server.ch_lock);
517 }
518 }
519
Radek Krejci695d4fa2015-10-22 13:23:54 +0200520 free(session);
521}
522
Michal Vasko086311b2016-01-08 09:53:11 +0100523static void
524add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
525{
526 if (*count == *size) {
527 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100528 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
529 if (!(*cpblts)) {
530 ERRMEM;
531 return;
532 }
Michal Vasko086311b2016-01-08 09:53:11 +0100533 }
534
535 if (capab) {
536 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
537 } else {
538 (*cpblts)[*count] = NULL;
539 }
540 ++(*count);
541}
542
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200543API const char **
544nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100545{
546 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200547 struct lyd_node_leaf_list **features = NULL, **deviations = NULL, *ns = NULL, *rev = NULL, *name = NULL, *module_set_id = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100548 const char **cpblts;
549 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200550 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200551#define NC_CPBLT_BUF_LEN 512
552 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100553
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200554 if (!ctx) {
555 ERRARG("ctx");
556 return NULL;
557 }
558
Michal Vasko086311b2016-01-08 09:53:11 +0100559 yanglib = ly_ctx_info(ctx);
560 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200561 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100562 return NULL;
563 }
564
565 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100566 if (!cpblts) {
567 ERRMEM;
568 return NULL;
569 }
Michal Vasko086311b2016-01-08 09:53:11 +0100570 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
571 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
572 count = 2;
573
574 /* capabilities */
575
576 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
577 if (mod) {
578 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100579 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100580 }
581 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100582 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100583 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100584 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100585 }
586 }
587 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100588 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100589 }
590 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100591 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100592 }
593 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100594 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100595 }
596 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100597 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100598 }
599 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100600 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100601 }
602 }
603
604 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
605 if (mod) {
606 if (!server_opts.wd_basic_mode) {
607 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
608 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100609 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100610 switch (server_opts.wd_basic_mode) {
611 case NC_WD_ALL:
612 strcat(str, "?basic-mode=report-all");
613 break;
614 case NC_WD_TRIM:
615 strcat(str, "?basic-mode=trim");
616 break;
617 case NC_WD_EXPLICIT:
618 strcat(str, "?basic-mode=explicit");
619 break;
620 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100621 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100622 break;
623 }
624
625 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200626 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100627 if (server_opts.wd_also_supported & NC_WD_ALL) {
628 strcat(str, "report-all,");
629 }
630 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
631 strcat(str, "report-all-tagged,");
632 }
633 if (server_opts.wd_also_supported & NC_WD_TRIM) {
634 strcat(str, "trim,");
635 }
636 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
637 strcat(str, "explicit,");
638 }
639 str[strlen(str) - 1] = '\0';
640
641 add_cpblt(ctx, str, &cpblts, &size, &count);
642 }
643 }
644 }
645
Michal Vasko1a38c862016-01-15 15:50:07 +0100646 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100647 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100648 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100649 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100650 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100651 }
652 }
653
654 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100655 LY_TREE_FOR(yanglib->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200656 if (!module_set_id) {
657 if (strcmp(child->prev->schema->name, "module-set-id")) {
658 ERRINT;
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200659 free(cpblts);
660 free(deviations);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200661 return NULL;
662 }
663 module_set_id = (struct lyd_node_leaf_list *)child->prev;
664 }
Michal Vasko086311b2016-01-08 09:53:11 +0100665 if (!strcmp(child->schema->name, "module")) {
666 LY_TREE_FOR(child->child, child2) {
667 if (!strcmp(child2->schema->name, "namespace")) {
668 ns = (struct lyd_node_leaf_list *)child2;
669 } else if (!strcmp(child2->schema->name, "name")) {
670 name = (struct lyd_node_leaf_list *)child2;
671 } else if (!strcmp(child2->schema->name, "revision")) {
672 rev = (struct lyd_node_leaf_list *)child2;
673 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100674 features = nc_realloc(features, ++feat_count * sizeof *features);
675 if (!features) {
676 ERRMEM;
677 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200678 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100679 return NULL;
680 }
Michal Vasko086311b2016-01-08 09:53:11 +0100681 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200682 } else if (!strcmp(child2->schema->name, "deviation")) {
683 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
684 if (!deviations) {
685 ERRMEM;
686 free(cpblts);
687 free(features);
688 return NULL;
689 }
Michal Vasko086311b2016-01-08 09:53:11 +0100690 }
691 }
692
693 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100694 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100695 continue;
696 }
697
Radek Krejcidf7ba522016-03-01 16:05:25 +0100698 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200699 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100700 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200701 strcat(str, "&features=");
702 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100703 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200704 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100705 ERRINT;
706 break;
707 }
Michal Vasko086311b2016-01-08 09:53:11 +0100708 if (i) {
709 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100710 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100711 }
712 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100713 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100714 }
715 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200716 if (dev_count) {
717 strcat(str, "&deviations=");
718 str_len += 12;
719 for (i = 0; i < dev_count; ++i) {
720 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
721 ERRINT;
722 break;
723 }
724 if (i) {
725 strcat(str, ",");
726 ++str_len;
727 }
728 strcat(str, deviations[i]->value_str);
729 str_len += strlen(deviations[i]->value_str);
730 }
731 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200732 if (!strcmp(name->value_str, "ietf-yang-library")) {
733 str_len += sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
734 }
Michal Vasko086311b2016-01-08 09:53:11 +0100735
736 add_cpblt(ctx, str, &cpblts, &size, &count);
737
738 ns = NULL;
739 name = NULL;
740 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200741 if (features || feat_count) {
742 free(features);
743 features = NULL;
744 feat_count = 0;
745 }
746 if (deviations || dev_count) {
747 free(deviations);
748 deviations = NULL;
749 dev_count = 0;
750 }
Michal Vasko086311b2016-01-08 09:53:11 +0100751 }
752 }
753
754 lyd_free(yanglib);
755
756 /* ending NULL capability */
757 add_cpblt(ctx, NULL, &cpblts, &size, &count);
758
759 return cpblts;
760}
761
Radek Krejci695d4fa2015-10-22 13:23:54 +0200762static int
763parse_cpblts(struct lyxml_elem *xml, const char ***list)
764{
765 struct lyxml_elem *cpblt;
766 int ver = -1;
767 int i = 0;
768
769 if (list) {
770 /* get the storage for server's capabilities */
771 LY_TREE_FOR(xml->child, cpblt) {
772 i++;
773 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100774 /* last item remains NULL */
775 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200776 if (!*list) {
777 ERRMEM;
778 return -1;
779 }
780 i = 0;
781 }
782
783 LY_TREE_FOR(xml->child, cpblt) {
784 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
785 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
786 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
787 return -1;
788 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
789 continue;
790 }
791
792 /* detect NETCONF version */
793 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
794 ver = 0;
795 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
796 ver = 1;
797 }
798
799 /* store capabilities */
800 if (list) {
801 (*list)[i] = cpblt->content;
802 cpblt->content = NULL;
803 i++;
804 }
805 }
806
807 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100808 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200809 }
810
811 return ver;
812}
813
814static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100815nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100816{
817 int r, i;
818 const char **cpblts;
819
Michal Vasko11d142a2016-01-19 15:58:24 +0100820 /* client side hello - send only NETCONF base capabilities */
821 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100822 if (!cpblts) {
823 ERRMEM;
824 return NC_MSG_ERROR;
825 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100826 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
827 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
828 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100829
Michal Vasko11d142a2016-01-19 15:58:24 +0100830 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100831
Michal Vasko086311b2016-01-08 09:53:11 +0100832 for (i = 0; cpblts[i]; ++i) {
833 lydict_remove(session->ctx, cpblts[i]);
834 }
835 free(cpblts);
836
837 if (r) {
838 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100839 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100840
841 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100842}
843
844static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100845nc_send_server_hello(struct nc_session *session)
846{
847 int r, i;
848 const char **cpblts;
849
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200850 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100851
852 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
853
Michal Vasko11d142a2016-01-19 15:58:24 +0100854 for (i = 0; cpblts[i]; ++i) {
855 lydict_remove(session->ctx, cpblts[i]);
856 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100857 free(cpblts);
858
859 if (r) {
860 return NC_MSG_ERROR;
861 }
862
863 return NC_MSG_HELLO;
864}
865
866static NC_MSG_TYPE
867nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200868{
869 struct lyxml_elem *xml = NULL, *node;
870 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
871 int ver = -1;
872 char *str;
873 long long int id;
874 int flag = 0;
875
Michal Vasko05ba9df2016-01-13 14:40:27 +0100876 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200877
878 switch(msgtype) {
879 case NC_MSG_HELLO:
880 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100881 LY_TREE_FOR(xml->child, node) {
882 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
883 continue;
884 } else if (!strcmp(node->name, "session-id")) {
885 if (!node->content || !strlen(node->content)) {
886 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200887 goto error;
888 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100889 str = NULL;
890 id = strtoll(node->content, &str, 10);
891 if (*str || id < 1 || id > UINT32_MAX) {
892 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200893 goto error;
894 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100895 session->id = (uint32_t)id;
896 continue;
897 } else if (strcmp(node->name, "capabilities")) {
898 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200899 goto error;
900 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100901
902 if (flag) {
903 /* multiple capabilities elements */
904 ERR("Invalid <hello> message (multiple <capabilities> elements).");
905 goto error;
906 }
907 flag = 1;
908
Michal Vasko2e6defd2016-10-07 15:48:15 +0200909 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100910 goto error;
911 }
912 session->version = ver;
913 }
914
915 if (!session->id) {
916 ERR("Missing <session-id> in server's <hello>.");
917 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200918 }
919 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200920 case NC_MSG_WOULDBLOCK:
921 ERR("Server's <hello> timeout elapsed.");
922 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200923 case NC_MSG_ERROR:
924 /* nothing special, just pass it out */
925 break;
926 default:
927 ERR("Unexpected message received instead of <hello>.");
928 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200929 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200930 }
931
932 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100933 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200934
935 return msgtype;
936
937error:
938 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100939 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200940
941 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200942}
943
Michal Vasko11d142a2016-01-19 15:58:24 +0100944static NC_MSG_TYPE
945nc_recv_server_hello(struct nc_session *session)
946{
947 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200948 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100949 int ver = -1;
950 int flag = 0;
951
Michal Vaskoadb850e2016-01-20 14:06:32 +0100952 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100953
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100954 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100955 case NC_MSG_HELLO:
956 /* get know NETCONF version */
957 LY_TREE_FOR(xml->child, node) {
958 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
959 continue;
960 } else if (strcmp(node->name, "capabilities")) {
961 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200962 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100963 goto cleanup;
964 }
965
966 if (flag) {
967 /* multiple capabilities elements */
968 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200969 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100970 goto cleanup;
971 }
972 flag = 1;
973
974 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200975 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100976 goto cleanup;
977 }
978 session->version = ver;
979 }
980 break;
981 case NC_MSG_ERROR:
982 /* nothing special, just pass it out */
983 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100984 case NC_MSG_WOULDBLOCK:
985 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100986 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100987 default:
988 ERR("Unexpected message received instead of <hello>.");
989 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200990 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100991 }
992
993cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100994 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100995
996 return msgtype;
997}
998
Michal Vasko71090fc2016-05-24 16:37:28 +0200999NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001000nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001001{
Michal Vasko086311b2016-01-08 09:53:11 +01001002 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001003
Michal Vasko11d142a2016-01-19 15:58:24 +01001004 if (session->side == NC_CLIENT) {
1005 type = nc_send_client_hello(session);
1006 } else {
1007 type = nc_send_server_hello(session);
1008 }
1009
Michal Vasko086311b2016-01-08 09:53:11 +01001010 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001011 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001012 }
1013
Michal Vasko11d142a2016-01-19 15:58:24 +01001014 if (session->side == NC_CLIENT) {
1015 type = nc_recv_client_hello(session);
1016 } else {
1017 type = nc_recv_server_hello(session);
1018 }
1019
Michal Vasko71090fc2016-05-24 16:37:28 +02001020 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001021}
Michal Vasko086311b2016-01-08 09:53:11 +01001022
Radek Krejci53691be2016-02-22 13:58:37 +01001023#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001024
Michal Vasko8f0c0282016-02-29 10:17:14 +01001025static void
Michal Vasko086311b2016-01-08 09:53:11 +01001026nc_ssh_init(void)
1027{
1028 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1029 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001030}
1031
Michal Vasko8f0c0282016-02-29 10:17:14 +01001032static void
Michal Vasko086311b2016-01-08 09:53:11 +01001033nc_ssh_destroy(void)
1034{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001035 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001036 ENGINE_cleanup();
1037 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001038 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001039 ssh_finalize();
1040}
1041
Radek Krejci53691be2016-02-22 13:58:37 +01001042#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001043
Radek Krejci53691be2016-02-22 13:58:37 +01001044#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001045
Michal Vaskof0c92c02016-01-29 09:41:45 +01001046struct CRYPTO_dynlock_value {
1047 pthread_mutex_t lock;
1048};
1049
Michal Vaskof0c92c02016-01-29 09:41:45 +01001050static struct CRYPTO_dynlock_value *
1051tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1052{
1053 struct CRYPTO_dynlock_value *value;
1054
1055 value = malloc(sizeof *value);
1056 if (!value) {
1057 ERRMEM;
1058 return NULL;
1059 }
1060 pthread_mutex_init(&value->lock, NULL);
1061
1062 return value;
1063}
1064
1065static void
1066tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1067{
1068 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1069 * I found ignored this fact, what do I know... */
1070 if (mode & CRYPTO_LOCK) {
1071 pthread_mutex_lock(&l->lock);
1072 } else {
1073 pthread_mutex_unlock(&l->lock);
1074 }
1075}
1076
1077static void
1078tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1079{
1080 pthread_mutex_destroy(&l->lock);
1081 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001082}
1083
Michal Vasko8f0c0282016-02-29 10:17:14 +01001084#endif /* NC_ENABLED_TLS */
1085
1086#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1087
1088static pthread_mutex_t *tls_locks;
1089
1090static void
1091tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1092{
1093 if (mode & CRYPTO_LOCK) {
1094 pthread_mutex_lock(tls_locks + n);
1095 } else {
1096 pthread_mutex_unlock(tls_locks + n);
1097 }
1098}
1099
1100static void
1101tls_thread_id_func(CRYPTO_THREADID *tid)
1102{
1103 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1104}
1105
1106static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001107nc_tls_init(void)
1108{
1109 int i;
1110
1111 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001112 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001113 SSL_library_init();
1114
1115 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001116 if (!tls_locks) {
1117 ERRMEM;
1118 return;
1119 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001120 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1121 pthread_mutex_init(tls_locks + i, NULL);
1122 }
1123
Michal Vaskof0c92c02016-01-29 09:41:45 +01001124 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001125 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001126
1127 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1128 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1129 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001130}
1131
Michal Vasko8f0c0282016-02-29 10:17:14 +01001132static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001133nc_tls_destroy(void)
1134{
1135 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001136
Michal Vasko8f0c0282016-02-29 10:17:14 +01001137 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001138 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001139 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001140 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001141 ERR_free_strings();
1142 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001143
Michal Vaskob6e37262016-02-25 14:49:00 +01001144 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001145 CRYPTO_set_locking_callback(NULL);
1146 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1147 pthread_mutex_destroy(tls_locks + i);
1148 }
1149 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001150
1151 CRYPTO_set_dynlock_create_callback(NULL);
1152 CRYPTO_set_dynlock_lock_callback(NULL);
1153 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001154}
1155
Michal Vasko8f0c0282016-02-29 10:17:14 +01001156#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001157
Radek Krejci53691be2016-02-22 13:58:37 +01001158#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001159
Michal Vasko8f0c0282016-02-29 10:17:14 +01001160static void
Michal Vasko5e228792016-02-03 15:30:24 +01001161nc_ssh_tls_init(void)
1162{
1163 SSL_load_error_strings();
1164 ERR_load_BIO_strings();
1165 SSL_library_init();
1166
1167 nc_ssh_init();
1168
1169 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1170 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1171 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1172}
1173
Michal Vasko8f0c0282016-02-29 10:17:14 +01001174static void
Michal Vasko5e228792016-02-03 15:30:24 +01001175nc_ssh_tls_destroy(void)
1176{
1177 ERR_free_strings();
1178 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1179
1180 nc_ssh_destroy();
1181
1182 CRYPTO_set_dynlock_create_callback(NULL);
1183 CRYPTO_set_dynlock_lock_callback(NULL);
1184 CRYPTO_set_dynlock_destroy_callback(NULL);
1185}
1186
Radek Krejci53691be2016-02-22 13:58:37 +01001187#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001188
1189#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1190
1191API void
1192nc_thread_destroy(void)
1193{
1194 CRYPTO_THREADID crypto_tid;
1195
1196 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1197 //CRYPTO_cleanup_all_ex_data();
1198
1199 CRYPTO_THREADID_current(&crypto_tid);
1200 ERR_remove_thread_state(&crypto_tid);
1201}
1202
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001203#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1204
1205void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001206nc_init(void)
1207{
1208#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1209 nc_ssh_tls_init();
1210#elif defined(NC_ENABLED_SSH)
1211 nc_ssh_init();
1212#elif defined(NC_ENABLED_TLS)
1213 nc_tls_init();
1214#endif
1215}
1216
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001217void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001218nc_destroy(void)
1219{
1220#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1221 nc_ssh_tls_destroy();
1222#elif defined(NC_ENABLED_SSH)
1223 nc_ssh_destroy();
1224#elif defined(NC_ENABLED_TLS)
1225 nc_tls_destroy();
1226#endif
1227}