blob: 9fa313c65cd52b679a21b3c4deb8c9948393a405 [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 *
Michal Vaskob85767d2018-03-21 12:21:10 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02007 *
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 */
Michal Vaskob85767d2018-03-21 12:21:10 +010014#define _DEFAULT_SOURCE
Radek Krejci206fcd62015-10-07 15:42:48 +020015
Michal Vasko18aeb5d2017-02-17 09:23:56 +010016#include <assert.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020017#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010018#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010019#include <string.h>
Michal Vaskob85767d2018-03-21 12:21:10 +010020#include <unistd.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020021#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020022#include <sys/time.h>
Michal Vaskobe52dc22018-10-17 09:28:17 +020023#include <sys/types.h>
24#include <sys/socket.h>
25#include <netinet/in.h>
26#include <netinet/tcp.h>
Michal Vasko58f31552016-01-19 12:39:15 +010027#include <time.h>
Michal Vasko156d3272017-04-11 11:46:49 +020028#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020029#include <libyang/libyang.h>
30
Michal Vasko5e228792016-02-03 15:30:24 +010031#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010032#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010033#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010034
Radek Krejci53691be2016-02-22 13:58:37 +010035#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020036
Michal Vasko086311b2016-01-08 09:53:11 +010037# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020038
Radek Krejci53691be2016-02-22 13:58:37 +010039#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020040
Radek Krejci53691be2016-02-22 13:58:37 +010041#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010042
Michal Vasko5e228792016-02-03 15:30:24 +010043# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044# include <openssl/err.h>
45
Radek Krejci53691be2016-02-22 13:58:37 +010046#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010047
Michal Vasko086311b2016-01-08 09:53:11 +010048/* in seconds */
49#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080050#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020051
Michal Vasko05ba9df2016-01-13 14:40:27 +010052/* in milliseconds */
53#define NC_CLOSE_REPLY_TIMEOUT 200
54
Michal Vasko086311b2016-01-08 09:53:11 +010055extern struct nc_server_opts server_opts;
56
Radek Krejci7ac16052016-07-15 11:48:18 +020057int
Michal Vasko77a6abe2017-10-05 10:02:20 +020058nc_gettimespec_mono(struct timespec *ts)
59{
60#ifdef CLOCK_MONOTONIC_RAW
61 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
62#elif defined(CLOCK_MONOTONIC)
63 return clock_gettime(CLOCK_MONOTONIC, ts);
64#else
65 /* no monotonic clock available, return realtime */
66 return nc_gettimespec_real(ts);
67#endif
68}
69
70int
71nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020072{
Michal Vasko0ef46df2016-09-21 14:03:18 +020073#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020074 return clock_gettime(CLOCK_REALTIME, ts);
75#else
76 int rc;
77 struct timeval tv;
78
79 rc = gettimeofday(&tv, NULL);
80 if (!rc) {
81 ts->tv_sec = (time_t)tv.tv_sec;
82 ts->tv_nsec = 1000L * (long)tv.tv_usec;
83 }
84 return rc;
85#endif
86}
87
Michal Vasko36c7be82017-02-22 13:37:59 +010088/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
89int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020090nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010091{
Michal Vasko36c7be82017-02-22 13:37:59 +010092 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010093
Michal Vaskoa82bd202017-03-03 14:07:29 +010094 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
95 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010096
97 return (nsec_diff ? nsec_diff / 1000000L : 0);
98}
99
Michal Vasko36c7be82017-02-22 13:37:59 +0100100void
101nc_addtimespec(struct timespec *ts, uint32_t msec)
102{
Michal Vasko81c5b302017-03-15 12:10:40 +0100103 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
104
Michal Vasko0dfcd332017-03-03 13:14:39 +0100105 ts->tv_sec += msec / 1000;
106 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100107
Michal Vasko81c5b302017-03-15 12:10:40 +0100108 if (ts->tv_nsec >= 1000000000L) {
109 ++ts->tv_sec;
110 ts->tv_nsec -= 1000000000L;
111 } else if (ts->tv_nsec < 0) {
112 --ts->tv_sec;
113 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100114 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100115
116 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100117}
118
Michal Vaskoddce1212019-05-24 09:58:49 +0200119const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200120nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200121{
122 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200123 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200124 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200125 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200126 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200127 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200128 return "EC";
129 default:
130 break;
131 }
132
133 return NULL;
134}
135
Michal Vaskobe52dc22018-10-17 09:28:17 +0200136int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200137nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200138{
139 int opt;
140
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200141 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200142 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
143 ERR("Could not set SO_KEEPALIVE option (%s).", strerror(errno));
144 return -1;
145 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200146 if (!ka->enabled) {
147 return 0;
148 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200149
150#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200151 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200152 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
153 ERR("Setsockopt failed (%s).", strerror(errno));
154 return -1;
155 }
156#endif
157
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200158#ifdef TCP_KEEPCNT
159 opt = ka->max_probes;
160 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vaskobe52dc22018-10-17 09:28:17 +0200161 ERR("Setsockopt failed (%s).", strerror(errno));
162 return -1;
163 }
164#endif
165
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200166#ifdef TCP_KEEPINTVL
167 opt = ka->probe_interval;
168 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vaskobe52dc22018-10-17 09:28:17 +0200169 ERR("Setsockopt failed (%s).", strerror(errno));
170 return -1;
171 }
172#endif
173
174 return 0;
175}
176
Radek Krejci28472922016-07-15 11:51:16 +0200177#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
178int
179pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
180{
Michal Vasko81c5b302017-03-15 12:10:40 +0100181 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200182 int rc;
183 struct timespec cur, dur;
184
185 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
186 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
Michal Vasko145619f2018-02-16 10:08:45 +0100187 nc_gettimespec_real(&cur);
Radek Krejci28472922016-07-15 11:51:16 +0200188
Michal Vasko81c5b302017-03-15 12:10:40 +0100189 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
190 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200191 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100192 } else if (diff < 5) {
193 /* sleep until timeout */
Michal Vaskobf378cb2018-09-21 15:18:52 +0200194 dur.tv_sec = 0;
195 dur.tv_nsec = (long)diff * 1000000;
Michal Vasko81c5b302017-03-15 12:10:40 +0100196 } else {
197 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200198 dur.tv_sec = 0;
199 dur.tv_nsec = 5000000;
200 }
201
202 nanosleep(&dur, NULL);
203 }
204
205 return rc;
206}
207#endif
208
Michal Vaskoade892d2017-02-22 13:40:35 +0100209struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200210nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100211{
212 struct nc_session *sess;
213
214 sess = calloc(1, sizeof *sess);
215 if (!sess) {
216 return NULL;
217 }
218
Michal Vasko131120a2018-05-29 15:44:02 +0200219 sess->side = side;
220
221 if (side == NC_SERVER) {
222 sess->opts.server.rpc_lock = malloc(sizeof *sess->opts.server.rpc_lock);
223 sess->opts.server.rpc_cond = malloc(sizeof *sess->opts.server.rpc_cond);
224 sess->opts.server.rpc_inuse = malloc(sizeof *sess->opts.server.rpc_inuse);
225 if (!sess->opts.server.rpc_lock || !sess->opts.server.rpc_cond || !sess->opts.server.rpc_inuse) {
226 goto error;
Michal Vaskoade892d2017-02-22 13:40:35 +0100227 }
Michal Vasko131120a2018-05-29 15:44:02 +0200228 pthread_mutex_init(sess->opts.server.rpc_lock, NULL);
229 pthread_cond_init(sess->opts.server.rpc_cond, NULL);
230 *sess->opts.server.rpc_inuse = 0;
231 }
232
233 if (!shared_ti) {
234 sess->io_lock = malloc(sizeof *sess->io_lock);
235 if (!sess->io_lock) {
236 goto error;
237 }
238 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100239 }
240
241 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200242
243error:
244 if (side == NC_SERVER) {
245 free(sess->opts.server.rpc_lock);
246 free(sess->opts.server.rpc_cond);
247 free((int *)sess->opts.server.rpc_inuse);
248 }
249 free(sess);
250 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100251}
252
Michal Vasko96164bf2016-01-21 15:41:58 +0100253/*
254 * @return 1 - success
255 * 0 - timeout
256 * -1 - error
257 */
258int
Michal Vasko131120a2018-05-29 15:44:02 +0200259nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100260{
261 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100262 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100263
Michal Vasko131120a2018-05-29 15:44:02 +0200264 if (session->side != NC_SERVER) {
265 ERRINT;
266 return -1;
267 }
268
Michal Vasko96164bf2016-01-21 15:41:58 +0100269 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200270 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100271 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100272
Michal Vaskoade892d2017-02-22 13:40:35 +0100273 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200274 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100275 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200276 while (*session->opts.server.rpc_inuse) {
277 ret = pthread_cond_timedwait(session->opts.server.rpc_cond, session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100278 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200279 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100280 break;
281 }
282 }
283 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100284 } else if (!timeout) {
Michal Vasko131120a2018-05-29 15:44:02 +0200285 if (*session->opts.server.rpc_inuse) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100286 /* immediate timeout */
287 return 0;
288 }
289
290 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200291 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100292 if (!ret) {
293 /* be extra careful, someone could have been faster */
Michal Vasko131120a2018-05-29 15:44:02 +0200294 if (*session->opts.server.rpc_inuse) {
295 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100296 return 0;
297 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200298 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100299 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100300 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200301 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100302 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200303 while (*session->opts.server.rpc_inuse) {
304 ret = pthread_cond_wait(session->opts.server.rpc_cond, session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100305 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200306 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100307 break;
308 }
309 }
310 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100311 }
312
Michal Vaskoade892d2017-02-22 13:40:35 +0100313 if (ret) {
314 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
315 /* timeout */
316 return 0;
317 }
318
Michal Vasko96164bf2016-01-21 15:41:58 +0100319 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200320 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100321 return -1;
322 }
323
324 /* ok */
Michal Vasko131120a2018-05-29 15:44:02 +0200325 assert(*session->opts.server.rpc_inuse == 0);
326 *session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100327
328 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200329 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100330 if (ret) {
331 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200332 ERR("%s: faile to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100333 return -1;
334 }
335
336 return 1;
337}
338
339int
Michal Vasko131120a2018-05-29 15:44:02 +0200340nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100341{
342 int ret;
343 struct timespec ts_timeout;
344
Michal Vasko131120a2018-05-29 15:44:02 +0200345 if (session->side != NC_SERVER) {
346 ERRINT;
347 return -1;
348 }
349
350 assert(*session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100351
352 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200353 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100354 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100355
356 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200357 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100358 } else if (!timeout) {
359 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200360 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100361 } else { /* timeout == -1 */
362 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200363 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100364 }
365
366 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
367 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200368 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100369 return -1;
370 } else if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200371 WRN("%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100372 }
373
Michal Vasko131120a2018-05-29 15:44:02 +0200374 *session->opts.server.rpc_inuse = 0;
375 pthread_cond_signal(session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100376
377 if (!ret) {
378 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200379 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100380 if (ret) {
381 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200382 ERR("%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100383 return -1;
384 }
385 }
386
Michal Vasko96164bf2016-01-21 15:41:58 +0100387 return 1;
388}
389
Michal Vasko131120a2018-05-29 15:44:02 +0200390/*
391 * @return 1 - success
392 * 0 - timeout
393 * -1 - error
394 */
395int
396nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
397{
398 int ret;
399 struct timespec ts_timeout;
400
401 if (timeout > 0) {
402 nc_gettimespec_real(&ts_timeout);
403 nc_addtimespec(&ts_timeout, timeout);
404
405 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
406 } else if (!timeout) {
407 ret = pthread_mutex_trylock(session->io_lock);
408 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200409 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200410 }
411
412 if (ret) {
413 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
414 /* timeout */
415 return 0;
416 }
417
418 /* error */
419 ERR("%s: failed to IO lock a session (%s).", func, strerror(ret));
420 return -1;
421 }
422
423 return 1;
424}
425
426int
427nc_session_io_unlock(struct nc_session *session, const char *func)
428{
429 int ret;
430
431 ret = pthread_mutex_unlock(session->io_lock);
432 if (ret) {
433 /* error */
434 ERR("%s: failed to IO unlock a session (%s).", func, strerror(ret));
435 return -1;
436 }
437
438 return 1;
439}
440
Michal Vasko8dadf782016-01-15 10:29:36 +0100441API NC_STATUS
442nc_session_get_status(const struct nc_session *session)
443{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100444 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200445 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200446 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100447 }
448
Michal Vasko8dadf782016-01-15 10:29:36 +0100449 return session->status;
450}
451
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100452API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200453nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100454{
455 if (!session) {
456 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200457 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100458 }
459
460 return session->term_reason;
461}
462
Michal Vasko8dadf782016-01-15 10:29:36 +0100463API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200464nc_session_get_killed_by(const struct nc_session *session)
465{
466 if (!session) {
467 ERRARG("session");
468 return 0;
469 }
470
471 return session->killed_by;
472}
473
474API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100475nc_session_get_id(const struct nc_session *session)
476{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100477 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200478 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100479 return 0;
480 }
481
Michal Vasko8dadf782016-01-15 10:29:36 +0100482 return session->id;
483}
484
Michal Vasko174fe8e2016-02-17 15:38:09 +0100485API int
486nc_session_get_version(const struct nc_session *session)
487{
488 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200489 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100490 return -1;
491 }
492
493 return (session->version == NC_VERSION_10 ? 0 : 1);
494}
495
Michal Vasko8dadf782016-01-15 10:29:36 +0100496API NC_TRANSPORT_IMPL
497nc_session_get_ti(const struct nc_session *session)
498{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100499 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200500 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100501 return 0;
502 }
503
Michal Vasko8dadf782016-01-15 10:29:36 +0100504 return session->ti_type;
505}
506
507API const char *
508nc_session_get_username(const struct nc_session *session)
509{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100510 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200511 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100512 return NULL;
513 }
514
Michal Vasko8dadf782016-01-15 10:29:36 +0100515 return session->username;
516}
517
518API const char *
519nc_session_get_host(const struct nc_session *session)
520{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100521 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200522 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100523 return NULL;
524 }
525
Michal Vasko8dadf782016-01-15 10:29:36 +0100526 return session->host;
527}
528
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200529API const char *
530nc_session_get_path(const struct nc_session *session)
531{
532 if (!session) {
533 ERRARG("session");
534 return NULL;
535 }
536 if (session->ti_type != NC_TI_UNIX)
537 return NULL;
538
539 return session->path;
540}
541
Michal Vasko8dadf782016-01-15 10:29:36 +0100542API uint16_t
543nc_session_get_port(const struct nc_session *session)
544{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100545 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200546 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100547 return 0;
548 }
549
Michal Vasko8dadf782016-01-15 10:29:36 +0100550 return session->port;
551}
552
Michal Vasko9a25e932016-02-01 10:36:42 +0100553API struct ly_ctx *
554nc_session_get_ctx(const struct nc_session *session)
555{
556 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200557 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100558 return NULL;
559 }
560
561 return session->ctx;
562}
563
Michal Vasko2cc4c682016-03-01 09:16:48 +0100564API void
565nc_session_set_data(struct nc_session *session, void *data)
566{
567 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200568 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100569 return;
570 }
571
572 session->data = data;
573}
574
575API void *
576nc_session_get_data(const struct nc_session *session)
577{
578 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200579 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100580 return NULL;
581 }
582
583 return session->data;
584}
585
Michal Vasko086311b2016-01-08 09:53:11 +0100586NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200587nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200588{
Michal Vasko086311b2016-01-08 09:53:11 +0100589 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100590 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
591 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100592 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100593 }
594
Michal Vasko131120a2018-05-29 15:44:02 +0200595 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100596}
597
Radek Krejci695d4fa2015-10-22 13:23:54 +0200598API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100599nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200600{
Michal Vasko131120a2018-05-29 15:44:02 +0200601 int r, i, rpc_locked = 0, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100602 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100603 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100604 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100605 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100606 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200607 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100608 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200609 void *p;
610
Michal Vasko428087d2016-01-14 16:04:28 +0100611 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200612 return;
613 }
614
Michal Vasko86d357c2016-03-11 13:46:38 +0100615 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200616 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200617 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100618 /* the thread now knows it should quit */
Michal Vasko86d357c2016-03-11 13:46:38 +0100619 }
620
Michal Vasko131120a2018-05-29 15:44:02 +0200621 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
622 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100623 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100624 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200625 } else if (r) {
626 rpc_locked = 1;
627 } /* else failed to lock it, too bad */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200628 }
629
Michal Vasko131120a2018-05-29 15:44:02 +0200630 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200631 /* cleanup message queues */
632 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200633 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100634 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200635
Michal Vaskoad611702015-12-03 13:41:51 +0100636 p = contiter;
637 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200638 free(p);
639 }
640
641 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200642 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100643 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200644
Michal Vaskoad611702015-12-03 13:41:51 +0100645 p = contiter;
646 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200647 free(p);
648 }
649
650 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200651 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200652 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100653 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200654 } else {
655 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vasko131120a2018-05-29 15:44:02 +0200656 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200657 lyd_free(close_rpc);
Michal Vasko131120a2018-05-29 15:44:02 +0200658 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100659 case NC_MSG_REPLY:
660 LY_TREE_FOR(rpl->child, child) {
661 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
662 break;
663 }
664 }
665 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100666 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100667 }
Michal Vaskoad611702015-12-03 13:41:51 +0100668 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100669 break;
670 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100671 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100672 break;
673 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100674 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100675 break;
676 default:
677 /* cannot happen */
678 break;
679 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200680 }
681
682 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200683 if (session->opts.client.cpblts) {
684 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200685 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200686 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200687 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200688 }
689 }
690
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100691 if (session->data && data_free) {
692 data_free(session->data);
693 }
694
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200695 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
696 /* CH LOCK */
697 pthread_mutex_lock(session->opts.server.ch_lock);
698 }
699
Michal Vasko86d357c2016-03-11 13:46:38 +0100700 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200701 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200702
703 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
704 pthread_cond_signal(session->opts.server.ch_cond);
705
706 /* CH UNLOCK */
707 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100708
709 /* wait for CH thread to actually wake up */
710 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
711 while (i && (session->flags & NC_SESSION_CALLHOME)) {
712 usleep(NC_TIMEOUT_STEP);
713 --i;
714 }
715 if (session->flags & NC_SESSION_CALLHOME) {
716 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
717 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200718 }
719
Michal Vasko428087d2016-01-14 16:04:28 +0100720 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200721
722 /* transport implementation cleanup */
723 switch (session->ti_type) {
724 case NC_TI_FD:
725 /* nothing needed - file descriptors were provided by caller,
726 * so it is up to the caller to close them correctly
727 * TODO use callbacks
728 */
Michal Vasko3512e402016-01-28 16:22:34 +0100729 /* just to avoid compiler warning */
730 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100731 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200732 break;
733
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200734 case NC_TI_UNIX:
735 sock = session->ti.unixsock.sock;
736 (void)connected;
737 (void)siter;
738 break;
739
Radek Krejci53691be2016-02-22 13:58:37 +0100740#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200741 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100742 if (connected) {
743 ssh_channel_free(session->ti.libssh.channel);
744 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200745 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
746 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200747 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200748 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200749 /* SESSION IO LOCK */
750 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
751
Michal Vasko96164bf2016-01-21 15:41:58 +0100752 multisession = 0;
753 if (session->ti.libssh.next) {
754 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
755 if (siter->status != NC_STATUS_STARTING) {
756 multisession = 1;
757 break;
758 }
759 }
760 }
761
762 if (!multisession) {
763 /* it's not multisession yet, but we still need to free the starting sessions */
764 if (session->ti.libssh.next) {
765 do {
766 siter = session->ti.libssh.next;
767 session->ti.libssh.next = siter->ti.libssh.next;
768
769 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100770 lydict_remove(session->ctx, session->username);
771 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100772 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100773 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100774 }
775
776 free(siter);
777 } while (session->ti.libssh.next != session);
778 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100779 /* remember sock so we can close it */
780 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100781 if (connected) {
782 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200783 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100784 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200785 ssh_free(session->ti.libssh.session);
786 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200787 /* remove the session from the list */
788 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100789 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200790 /* there will be only one session */
791 siter->ti.libssh.next = NULL;
792 } else {
793 /* there are still multiple sessions, keep the ring list */
794 siter->ti.libssh.next = session->ti.libssh.next;
795 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100796 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
797 if (session->flags & NC_SESSION_SSH_MSG_CB) {
798 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
799 if (siter->ti.libssh.next == session) {
800 ERRINT;
801 break;
802 }
803 }
804 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
805 siter->flags |= NC_SESSION_SSH_MSG_CB;
806 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200807 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200808
809 /* SESSION IO UNLOCK */
810 if (r == 1) {
811 nc_session_io_unlock(session, __func__);
812 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200813 break;
814#endif
815
Radek Krejci53691be2016-02-22 13:58:37 +0100816#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200817 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100818 /* remember sock so we can close it */
819 sock = SSL_get_fd(session->ti.tls);
820
Michal Vasko428087d2016-01-14 16:04:28 +0100821 if (connected) {
822 SSL_shutdown(session->ti.tls);
823 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200824 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100825
Michal Vasko2e6defd2016-10-07 15:48:15 +0200826 if (session->side == NC_SERVER) {
827 X509_free(session->opts.server.client_cert);
828 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200829 break;
830#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100831 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100832 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200833 }
Michal Vasko428087d2016-01-14 16:04:28 +0100834
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100835 /* close socket separately */
836 if (sock > -1) {
837 close(sock);
838 }
839
Radek Krejciac6d3472015-10-22 15:47:18 +0200840 lydict_remove(session->ctx, session->username);
841 lydict_remove(session->ctx, session->host);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200842 lydict_remove(session->ctx, session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200843
844 /* final cleanup */
Michal Vasko131120a2018-05-29 15:44:02 +0200845 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
846 if (rpc_locked) {
847 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100848 }
Michal Vasko131120a2018-05-29 15:44:02 +0200849 pthread_mutex_destroy(session->opts.server.rpc_lock);
850 pthread_cond_destroy(session->opts.server.rpc_cond);
851 free(session->opts.server.rpc_lock);
852 free(session->opts.server.rpc_cond);
853 free((int *)session->opts.server.rpc_inuse);
854 }
855
856 if (session->io_lock && !multisession) {
857 pthread_mutex_destroy(session->io_lock);
858 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200859 }
860
861 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100862 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200863 }
864
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200865 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100866 /* free CH synchronization structures if used */
867 if (session->opts.server.ch_cond) {
868 pthread_cond_destroy(session->opts.server.ch_cond);
869 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200870 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100871 if (session->opts.server.ch_lock) {
872 pthread_mutex_destroy(session->opts.server.ch_lock);
873 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200874 }
875 }
876
Radek Krejci695d4fa2015-10-22 13:23:54 +0200877 free(session);
878}
879
Michal Vasko086311b2016-01-08 09:53:11 +0100880static void
881add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
882{
Radek Krejci658782b2016-12-04 22:04:55 +0100883 size_t len;
884 int i;
885 char *p;
886
887 if (capab) {
888 /* check if already present */
889 p = strchr(capab, '?');
890 if (p) {
891 len = p - capab;
892 } else {
893 len = strlen(capab);
894 }
895 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800896 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100897 /* already present, do not duplicate it */
898 return;
899 }
900 }
901 }
902
903 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100904 if (*count == *size) {
905 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100906 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
907 if (!(*cpblts)) {
908 ERRMEM;
909 return;
910 }
Michal Vasko086311b2016-01-08 09:53:11 +0100911 }
912
913 if (capab) {
914 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
915 } else {
916 (*cpblts)[*count] = NULL;
917 }
918 ++(*count);
919}
920
Michal Vaskodafdc742020-03-11 16:15:59 +0100921static struct lys_feature *
922nc_lys_next_feature(struct lys_feature *last, const struct lys_module *ly_mod, int *idx)
923{
924 uint8_t i;
925
926 assert(ly_mod);
927
928 /* find the (sub)module of the last feature */
929 if (last) {
930 for (i = 0; i < ly_mod->inc_size; ++i) {
931 if (*idx >= ly_mod->inc[i].submodule->features_size) {
932 /* not a feature from this submodule, skip */
933 continue;
934 }
935 if (last != ly_mod->inc[i].submodule->features + *idx) {
936 /* feature is not from this submodule */
937 continue;
938 }
939
940 /* we have found the submodule */
941 break;
942 }
943
944 /* feature not found in submodules, it must be in the main module */
945 assert((i < ly_mod->inc_size) || ((*idx < ly_mod->features_size) && (last == ly_mod->features + *idx)));
946
947 /* we want the next feature */
948 ++(*idx);
949 } else {
950 i = 0;
951 *idx = 0;
952 }
953
954 /* find the (sub)module of the next feature */
955 while ((i < ly_mod->inc_size) && (*idx == ly_mod->inc[i].submodule->features_size)) {
956 /* next submodule */
957 ++i;
958 *idx = 0;
959 }
960
961 /* get the next feature */
962 if (i < ly_mod->inc_size) {
963 last = ly_mod->inc[i].submodule->features + *idx;
964 } else if (*idx < ly_mod->features_size) {
965 last = ly_mod->features + *idx;
966 } else {
967 last = NULL;
968 }
969
970 return last;
971}
972
973
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200974API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200975nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100976{
Michal Vaskod5ada122020-03-19 18:28:06 +0100977 const char **cpblts;
Radek Krejci24a18412018-05-16 15:09:10 +0200978 const struct lys_module *mod, *devmod;
Michal Vaskodafdc742020-03-11 16:15:59 +0100979 struct lys_feature *feat;
Radek Krejci24a18412018-05-16 15:09:10 +0200980 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
981 unsigned int u, v, module_set_id;
982 char *s;
983#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200984 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100985
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200986 if (!ctx) {
987 ERRARG("ctx");
988 return NULL;
989 }
990
Michal Vasko086311b2016-01-08 09:53:11 +0100991 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100992 if (!cpblts) {
993 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200994 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100995 }
Michal Vasko086311b2016-01-08 09:53:11 +0100996 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
997 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
998 count = 2;
999
1000 /* capabilities */
1001
Radek Krejci3222b7d2017-09-21 16:04:30 +02001002 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001003 if (mod) {
1004 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001005 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001006 }
1007 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001008 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001009 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001010 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001011 }
1012 }
1013 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001014 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001015 }
1016 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001017 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001018 }
1019 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001020 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001021 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001022
1023 /* The URL capability must be set manually using nc_server_set_capability()
1024 * because of the need for supported protocols to be included.
1025 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1026 */
mekleoa8de5e92020-02-13 09:05:56 +01001027 // if (lys_features_state(mod, "url") == 1) {
1028 // add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
1029 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001030
Michal Vasko086311b2016-01-08 09:53:11 +01001031 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001032 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001033 }
1034 }
1035
Radek Krejci3222b7d2017-09-21 16:04:30 +02001036 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001037 if (mod) {
1038 if (!server_opts.wd_basic_mode) {
1039 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
1040 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001041 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001042 switch (server_opts.wd_basic_mode) {
1043 case NC_WD_ALL:
1044 strcat(str, "?basic-mode=report-all");
1045 break;
1046 case NC_WD_TRIM:
1047 strcat(str, "?basic-mode=trim");
1048 break;
1049 case NC_WD_EXPLICIT:
1050 strcat(str, "?basic-mode=explicit");
1051 break;
1052 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001053 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001054 break;
1055 }
1056
1057 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001058 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001059 if (server_opts.wd_also_supported & NC_WD_ALL) {
1060 strcat(str, "report-all,");
1061 }
1062 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1063 strcat(str, "report-all-tagged,");
1064 }
1065 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1066 strcat(str, "trim,");
1067 }
1068 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1069 strcat(str, "explicit,");
1070 }
1071 str[strlen(str) - 1] = '\0';
1072
1073 add_cpblt(ctx, str, &cpblts, &size, &count);
1074 }
1075 }
1076 }
1077
Radek Krejci658782b2016-12-04 22:04:55 +01001078 /* other capabilities */
1079 for (u = 0; u < server_opts.capabilities_count; u++) {
1080 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001081 }
1082
1083 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +02001084 u = module_set_id = 0;
1085 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001086 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vaskod5ada122020-03-19 18:28:06 +01001087 if (!mod->rev_size || (strcmp(mod->rev[0].date, "2016-06-21") && strcmp(mod->rev[0].date, "2019-01-04"))) {
1088 ERR("Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
1089 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001090 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001091
1092 /* the old one is certainly supported (capab defined in RFC 7950 section 5.6.4) */
1093 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%u",
1094 mod->rev[0].date, ly_ctx_get_module_set_id(ctx));
Andrew Langefeld50eac702018-08-17 12:10:40 -05001095 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001096
1097 if (!strcmp(mod->rev[0].date, "2019-01-04")) {
1098 /* new one is supported (capab defined in RFC 8526 section 2) */
1099 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%u",
1100 mod->rev[0].date, ly_ctx_get_module_set_id(ctx));
1101 add_cpblt(ctx, str, &cpblts, &size, &count);
1102 }
Radek Krejci24a18412018-05-16 15:09:10 +02001103 continue;
1104 } else if (mod->type) {
1105 /* skip submodules */
1106 continue;
1107 } else if (version == LYS_VERSION_1 && mod->version > version) {
1108 /* skip YANG 1.1 schemas */
1109 continue;
1110 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
1111 /* skip YANG 1.0 schemas */
1112 continue;
1113 }
Michal Vasko086311b2016-01-08 09:53:11 +01001114
Radek Krejci24a18412018-05-16 15:09:10 +02001115 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
1116 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
1117
Michal Vaskodafdc742020-03-11 16:15:59 +01001118 features_count = 0;
1119 i = 0;
1120 feat = NULL;
1121 while ((feat = nc_lys_next_feature(feat, mod, &i))) {
1122 if (!(feat->flags & LYS_FENABLED)) {
1123 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001124 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001125 if (!features_count) {
1126 strcat(str, "&features=");
1127 str_len += 10;
1128 }
1129 len = strlen(feat->name);
1130 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1131 ERRINT;
1132 break;
1133 }
1134 if (features_count) {
1135 strcat(str, ",");
1136 ++str_len;
1137 }
1138 strcat(str, feat->name);
1139 str_len += len;
1140 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001141 }
Michal Vasko086311b2016-01-08 09:53:11 +01001142
Radek Krejci24a18412018-05-16 15:09:10 +02001143 if (mod->deviated) {
1144 strcat(str, "&deviations=");
1145 str_len += 12;
1146 dev_count = 0;
Frank Rimplercb9274f2019-03-13 16:40:34 +00001147 v = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001148 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
1149 if (devmod == mod) {
1150 continue;
1151 }
1152
1153 for (i = 0; i < devmod->deviation_size; ++i) {
1154 s = strstr(devmod->deviation[i].target_name, mod->name);
1155 if (s && s[strlen(mod->name)] == ':') {
1156 /* we have the module deviating the module being processed */
1157 len = strlen(devmod->name);
1158 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1159 ERRINT;
1160 break;
1161 }
1162 if (dev_count) {
1163 strcat(str, ",");
1164 ++str_len;
1165 }
1166 strcat(str, devmod->name);
1167 str_len += len;
1168 dev_count++;
1169
1170 break;
1171 }
1172 }
1173 }
1174 }
1175
1176 add_cpblt(ctx, str, &cpblts, &size, &count);
1177 }
Michal Vasko086311b2016-01-08 09:53:11 +01001178
1179 /* ending NULL capability */
1180 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1181
1182 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001183
1184error:
Radek Krejcif906e412017-09-22 14:44:45 +02001185 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001186 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001187}
1188
Radek Krejci24a18412018-05-16 15:09:10 +02001189API const char **
1190nc_server_get_cpblts(struct ly_ctx *ctx)
1191{
1192 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1193}
1194
Radek Krejci695d4fa2015-10-22 13:23:54 +02001195static int
Michal Vasko2cb24b42017-05-23 14:59:11 +02001196parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001197{
1198 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001199 int ver = -1, i = 0;
1200 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001201
1202 if (list) {
1203 /* get the storage for server's capabilities */
1204 LY_TREE_FOR(xml->child, cpblt) {
1205 i++;
1206 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001207 /* last item remains NULL */
1208 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001209 if (!*list) {
1210 ERRMEM;
1211 return -1;
1212 }
1213 i = 0;
1214 }
1215
1216 LY_TREE_FOR(xml->child, cpblt) {
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001217 if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
Radek Krejci695d4fa2015-10-22 13:23:54 +02001218 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1219 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1220 return -1;
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001221 } else if (!cpblt->name || !cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001222 continue;
1223 }
1224
Michal Vasko156d3272017-04-11 11:46:49 +02001225 /* skip leading/trailing whitespaces */
1226 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1227 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1228 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1229 ERR("Empty capability \"%s\" received.", cpblt->content);
1230 return -1;
1231 }
1232
Radek Krejci695d4fa2015-10-22 13:23:54 +02001233 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001234 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001235 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001236 } else if (ver < 1 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.1", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001237 ver = 1;
1238 }
1239
1240 /* store capabilities */
1241 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001242 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1243 if (!(*list)[i]) {
1244 ERRMEM;
1245 return -1;
1246 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001247 i++;
1248 }
1249 }
1250
1251 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001252 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001253 }
1254
1255 return ver;
1256}
1257
1258static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001259nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001260{
Michal Vasko131120a2018-05-29 15:44:02 +02001261 NC_MSG_TYPE ret;
1262 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001263 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001264 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001265
Michal Vasko131120a2018-05-29 15:44:02 +02001266 if (session->side == NC_CLIENT) {
1267 /* client side hello - send only NETCONF base capabilities */
1268 cpblts = malloc(3 * sizeof *cpblts);
1269 if (!cpblts) {
1270 ERRMEM;
1271 return NC_MSG_ERROR;
1272 }
1273 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1274 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1275 cpblts[2] = NULL;
1276
1277 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1278 sid = NULL;
1279 } else {
1280 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
1281
1282 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1283 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001284 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001285
Michal Vasko131120a2018-05-29 15:44:02 +02001286 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001287
Michal Vasko086311b2016-01-08 09:53:11 +01001288 for (i = 0; cpblts[i]; ++i) {
1289 lydict_remove(session->ctx, cpblts[i]);
1290 }
1291 free(cpblts);
1292
Michal Vasko131120a2018-05-29 15:44:02 +02001293 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001294}
1295
1296static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001297nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001298{
1299 struct lyxml_elem *xml = NULL, *node;
Michal Vasko131120a2018-05-29 15:44:02 +02001300 NC_MSG_TYPE msgtype;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001301 int ver = -1;
1302 char *str;
1303 long long int id;
1304 int flag = 0;
1305
Michal Vasko131120a2018-05-29 15:44:02 +02001306 msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001307
Michal Vasko131120a2018-05-29 15:44:02 +02001308 switch (msgtype) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001309 case NC_MSG_HELLO:
1310 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001311 LY_TREE_FOR(xml->child, node) {
1312 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1313 continue;
1314 } else if (!strcmp(node->name, "session-id")) {
1315 if (!node->content || !strlen(node->content)) {
1316 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001317 goto error;
1318 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001319 str = NULL;
1320 id = strtoll(node->content, &str, 10);
1321 if (*str || id < 1 || id > UINT32_MAX) {
1322 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001323 goto error;
1324 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001325 session->id = (uint32_t)id;
1326 continue;
1327 } else if (strcmp(node->name, "capabilities")) {
1328 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001329 goto error;
1330 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001331
1332 if (flag) {
1333 /* multiple capabilities elements */
1334 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1335 goto error;
1336 }
1337 flag = 1;
1338
Michal Vasko2e6defd2016-10-07 15:48:15 +02001339 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001340 goto error;
1341 }
1342 session->version = ver;
1343 }
1344
1345 if (!session->id) {
1346 ERR("Missing <session-id> in server's <hello>.");
1347 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001348 }
1349 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001350 case NC_MSG_WOULDBLOCK:
1351 ERR("Server's <hello> timeout elapsed.");
1352 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001353 case NC_MSG_ERROR:
1354 /* nothing special, just pass it out */
1355 break;
1356 default:
1357 ERR("Unexpected message received instead of <hello>.");
1358 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001359 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001360 }
1361
1362 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001363 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001364
1365 return msgtype;
1366
1367error:
1368 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001369 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001370
1371 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001372}
1373
Michal Vasko11d142a2016-01-19 15:58:24 +01001374static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001375nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001376{
1377 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001378 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001379 int ver = -1;
1380 int flag = 0;
1381
Michal Vasko131120a2018-05-29 15:44:02 +02001382 msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ?
1383 server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001384
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001385 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001386 case NC_MSG_HELLO:
1387 /* get know NETCONF version */
1388 LY_TREE_FOR(xml->child, node) {
1389 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1390 continue;
1391 } else if (strcmp(node->name, "capabilities")) {
1392 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001393 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001394 goto cleanup;
1395 }
1396
1397 if (flag) {
1398 /* multiple capabilities elements */
1399 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001400 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001401 goto cleanup;
1402 }
1403 flag = 1;
1404
1405 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001406 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001407 goto cleanup;
1408 }
1409 session->version = ver;
1410 }
1411 break;
1412 case NC_MSG_ERROR:
1413 /* nothing special, just pass it out */
1414 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001415 case NC_MSG_WOULDBLOCK:
1416 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001417 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001418 default:
1419 ERR("Unexpected message received instead of <hello>.");
1420 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001421 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001422 }
1423
1424cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001425 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001426 return msgtype;
1427}
1428
Michal Vasko71090fc2016-05-24 16:37:28 +02001429NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001430nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001431{
Michal Vasko086311b2016-01-08 09:53:11 +01001432 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001433
Michal Vasko131120a2018-05-29 15:44:02 +02001434 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001435 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001436 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001437 }
1438
Michal Vasko11d142a2016-01-19 15:58:24 +01001439 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001440 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001441 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001442 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001443 }
1444
Michal Vasko71090fc2016-05-24 16:37:28 +02001445 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001446}
Michal Vasko086311b2016-01-08 09:53:11 +01001447
Michal Vasko889162e2019-09-06 14:43:37 +02001448#ifdef NC_ENABLED_SSH
1449
Michal Vasko999b64a2019-07-10 16:06:15 +02001450static void
1451nc_ssh_init(void)
1452{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001453#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001454 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1455 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001456#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001457}
1458
1459static void
1460nc_ssh_destroy(void)
1461{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001462#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001463 FIPS_mode_set(0);
1464 CONF_modules_unload(1);
1465 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001466#endif
1467
Michal Vaskoe1e82632019-09-09 09:18:03 +02001468#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1469 ssh_finalize();
1470#endif
1471}
1472
Michal Vasko889162e2019-09-06 14:43:37 +02001473#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001474
Radek Krejci53691be2016-02-22 13:58:37 +01001475#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001476
Michal Vasko770b4362017-02-17 14:44:18 +01001477#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1478
Michal Vaskof0c92c02016-01-29 09:41:45 +01001479struct CRYPTO_dynlock_value {
1480 pthread_mutex_t lock;
1481};
1482
Michal Vaskof0c92c02016-01-29 09:41:45 +01001483static struct CRYPTO_dynlock_value *
1484tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1485{
1486 struct CRYPTO_dynlock_value *value;
1487
1488 value = malloc(sizeof *value);
1489 if (!value) {
1490 ERRMEM;
1491 return NULL;
1492 }
1493 pthread_mutex_init(&value->lock, NULL);
1494
1495 return value;
1496}
1497
1498static void
1499tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1500{
1501 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1502 * I found ignored this fact, what do I know... */
1503 if (mode & CRYPTO_LOCK) {
1504 pthread_mutex_lock(&l->lock);
1505 } else {
1506 pthread_mutex_unlock(&l->lock);
1507 }
1508}
1509
1510static void
1511tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1512{
1513 pthread_mutex_destroy(&l->lock);
1514 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001515}
Michal Vasko770b4362017-02-17 14:44:18 +01001516#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001517
Michal Vasko8f0c0282016-02-29 10:17:14 +01001518#endif /* NC_ENABLED_TLS */
1519
1520#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1521
Michal Vasko770b4362017-02-17 14:44:18 +01001522#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001523static pthread_mutex_t *tls_locks;
1524
1525static void
1526tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1527{
1528 if (mode & CRYPTO_LOCK) {
1529 pthread_mutex_lock(tls_locks + n);
1530 } else {
1531 pthread_mutex_unlock(tls_locks + n);
1532 }
1533}
1534
1535static void
1536tls_thread_id_func(CRYPTO_THREADID *tid)
1537{
1538 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1539}
Michal Vasko770b4362017-02-17 14:44:18 +01001540#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001541
1542static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001543nc_tls_init(void)
1544{
Rosen Penev4f552d62019-06-26 16:10:43 -07001545#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001546 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001547 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001548 SSL_library_init();
1549
Michal Vaskof89948c2018-01-04 09:19:46 +01001550 int i;
1551
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001552 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001553 if (!tls_locks) {
1554 ERRMEM;
1555 return;
1556 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001557 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1558 pthread_mutex_init(tls_locks + i, NULL);
1559 }
1560
Michal Vaskof0c92c02016-01-29 09:41:45 +01001561 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001562 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001563
1564 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1565 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1566 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001567#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001568}
1569
Michal Vasko8f0c0282016-02-29 10:17:14 +01001570static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001571nc_tls_destroy(void)
1572{
Rosen Penev4f552d62019-06-26 16:10:43 -07001573#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001574 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001575 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001576 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001577 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001578 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001579#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001580 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001581#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1582 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001583#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001584
Michal Vaskof89948c2018-01-04 09:19:46 +01001585 int i;
1586
Michal Vaskob6e37262016-02-25 14:49:00 +01001587 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001588 CRYPTO_set_locking_callback(NULL);
1589 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1590 pthread_mutex_destroy(tls_locks + i);
1591 }
1592 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001593
1594 CRYPTO_set_dynlock_create_callback(NULL);
1595 CRYPTO_set_dynlock_lock_callback(NULL);
1596 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001597#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001598}
1599
Michal Vasko8f0c0282016-02-29 10:17:14 +01001600#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001601
Radek Krejci53691be2016-02-22 13:58:37 +01001602#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001603
Michal Vasko8f0c0282016-02-29 10:17:14 +01001604static void
Michal Vasko5e228792016-02-03 15:30:24 +01001605nc_ssh_tls_init(void)
1606{
Rosen Penev4f552d62019-06-26 16:10:43 -07001607#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001608 SSL_load_error_strings();
1609 ERR_load_BIO_strings();
1610 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001611#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001612
1613 nc_ssh_init();
1614
Michal Vaskoe1e82632019-09-09 09:18:03 +02001615#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001616 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1617 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1618 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001619#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001620}
1621
Michal Vasko8f0c0282016-02-29 10:17:14 +01001622static void
Michal Vasko5e228792016-02-03 15:30:24 +01001623nc_ssh_tls_destroy(void)
1624{
Rosen Penev4f552d62019-06-26 16:10:43 -07001625#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001626 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001627# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001628 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001629# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001630 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001631# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001632#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001633
1634 nc_ssh_destroy();
1635
Michal Vaskoe1e82632019-09-09 09:18:03 +02001636#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001637 CRYPTO_set_dynlock_create_callback(NULL);
1638 CRYPTO_set_dynlock_lock_callback(NULL);
1639 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001640#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001641}
1642
Radek Krejci53691be2016-02-22 13:58:37 +01001643#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001644
1645#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1646
1647API void
1648nc_thread_destroy(void)
1649{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001650 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1651 //CRYPTO_cleanup_all_ex_data();
1652
Michal Vasko770b4362017-02-17 14:44:18 +01001653#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1654 CRYPTO_THREADID crypto_tid;
1655
Michal Vasko8f0c0282016-02-29 10:17:14 +01001656 CRYPTO_THREADID_current(&crypto_tid);
1657 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001658#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001659}
1660
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001661#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1662
1663void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001664nc_init(void)
1665{
1666#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1667 nc_ssh_tls_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001668#elif defined(NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001669 nc_ssh_init();
1670#elif defined(NC_ENABLED_TLS)
1671 nc_tls_init();
1672#endif
1673}
1674
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001675void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001676nc_destroy(void)
1677{
1678#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1679 nc_ssh_tls_destroy();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001680#elif defined(NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001681 nc_ssh_destroy();
1682#elif defined(NC_ENABLED_TLS)
1683 nc_tls_destroy();
1684#endif
1685}