blob: 73ba4b1e98e7d3c00d2024c51ebf71d9ecd44f97 [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 Vasko4ffa3b22016-05-24 16:36:25 +0200921API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200922nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100923{
Michal Vaskof0fba4e2020-02-14 17:15:31 +0100924 const char **cpblts, *ver;
Radek Krejci24a18412018-05-16 15:09:10 +0200925 const struct lys_module *mod, *devmod;
926 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
927 unsigned int u, v, module_set_id;
928 char *s;
929#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200930 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100931
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200932 if (!ctx) {
933 ERRARG("ctx");
934 return NULL;
935 }
936
Michal Vasko086311b2016-01-08 09:53:11 +0100937 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100938 if (!cpblts) {
939 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200940 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100941 }
Michal Vasko086311b2016-01-08 09:53:11 +0100942 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
943 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
944 count = 2;
945
946 /* capabilities */
947
Radek Krejci3222b7d2017-09-21 16:04:30 +0200948 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100949 if (mod) {
950 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100951 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100952 }
953 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100954 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100955 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100956 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100957 }
958 }
959 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100960 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100961 }
962 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100963 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100964 }
965 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100966 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100967 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +0100968
969 /* The URL capability must be set manually using nc_server_set_capability()
970 * because of the need for supported protocols to be included.
971 * https://tools.ietf.org/html/rfc6241#section-8.8.3
972 */
mekleoa8de5e92020-02-13 09:05:56 +0100973 // if (lys_features_state(mod, "url") == 1) {
974 // add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
975 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +0100976
Michal Vasko086311b2016-01-08 09:53:11 +0100977 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100978 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100979 }
980 }
981
Radek Krejci3222b7d2017-09-21 16:04:30 +0200982 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100983 if (mod) {
984 if (!server_opts.wd_basic_mode) {
985 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
986 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100987 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100988 switch (server_opts.wd_basic_mode) {
989 case NC_WD_ALL:
990 strcat(str, "?basic-mode=report-all");
991 break;
992 case NC_WD_TRIM:
993 strcat(str, "?basic-mode=trim");
994 break;
995 case NC_WD_EXPLICIT:
996 strcat(str, "?basic-mode=explicit");
997 break;
998 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100999 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001000 break;
1001 }
1002
1003 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001004 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001005 if (server_opts.wd_also_supported & NC_WD_ALL) {
1006 strcat(str, "report-all,");
1007 }
1008 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1009 strcat(str, "report-all-tagged,");
1010 }
1011 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1012 strcat(str, "trim,");
1013 }
1014 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1015 strcat(str, "explicit,");
1016 }
1017 str[strlen(str) - 1] = '\0';
1018
1019 add_cpblt(ctx, str, &cpblts, &size, &count);
1020 }
1021 }
1022 }
1023
Radek Krejci658782b2016-12-04 22:04:55 +01001024 /* other capabilities */
1025 for (u = 0; u < server_opts.capabilities_count; u++) {
1026 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001027 }
1028
1029 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +02001030 u = module_set_id = 0;
1031 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001032 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001033 if (mod->rev_size && (strcmp(mod->rev[0].date, "2019-01-04") >= 0)) {
1034 ver = "1.1";
1035 } else {
1036 ver = "1.0";
1037 }
Andrew Langefeld50eac702018-08-17 12:10:40 -05001038 /* Add the yang-library NETCONF capability as defined in RFC 7950 5.6.4 */
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001039 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:%s?%s%s&module-set-id=%u",
1040 ver, mod->rev_size ? "revision=" : "", mod->rev_size ? mod->rev[0].date : "",
Andrew Langefeld50eac702018-08-17 12:10:40 -05001041 ly_ctx_get_module_set_id(ctx));
1042 add_cpblt(ctx, str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001043 continue;
1044 } else if (mod->type) {
1045 /* skip submodules */
1046 continue;
1047 } else if (version == LYS_VERSION_1 && mod->version > version) {
1048 /* skip YANG 1.1 schemas */
1049 continue;
1050 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
1051 /* skip YANG 1.0 schemas */
1052 continue;
1053 }
Michal Vasko086311b2016-01-08 09:53:11 +01001054
Radek Krejci24a18412018-05-16 15:09:10 +02001055 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
1056 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
1057
1058 if (mod->features_size) {
1059 features_count = 0;
1060 for (i = 0; i < mod->features_size; ++i) {
1061 if (!(mod->features[i].flags & LYS_FENABLED)) {
1062 continue;
1063 }
1064 if (!features_count) {
1065 strcat(str, "&features=");
1066 str_len += 10;
1067 }
1068 len = strlen(mod->features[i].name);
1069 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1070 ERRINT;
1071 break;
1072 }
Radek Krejci4e8eadd2018-08-21 09:54:27 +02001073 if (features_count) {
Radek Krejci24a18412018-05-16 15:09:10 +02001074 strcat(str, ",");
1075 ++str_len;
1076 }
1077 strcat(str, mod->features[i].name);
1078 str_len += len;
1079 features_count++;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001080 }
Michal Vasko086311b2016-01-08 09:53:11 +01001081 }
Michal Vasko086311b2016-01-08 09:53:11 +01001082
Radek Krejci24a18412018-05-16 15:09:10 +02001083 if (mod->deviated) {
1084 strcat(str, "&deviations=");
1085 str_len += 12;
1086 dev_count = 0;
Frank Rimplercb9274f2019-03-13 16:40:34 +00001087 v = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001088 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
1089 if (devmod == mod) {
1090 continue;
1091 }
1092
1093 for (i = 0; i < devmod->deviation_size; ++i) {
1094 s = strstr(devmod->deviation[i].target_name, mod->name);
1095 if (s && s[strlen(mod->name)] == ':') {
1096 /* we have the module deviating the module being processed */
1097 len = strlen(devmod->name);
1098 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1099 ERRINT;
1100 break;
1101 }
1102 if (dev_count) {
1103 strcat(str, ",");
1104 ++str_len;
1105 }
1106 strcat(str, devmod->name);
1107 str_len += len;
1108 dev_count++;
1109
1110 break;
1111 }
1112 }
1113 }
1114 }
1115
1116 add_cpblt(ctx, str, &cpblts, &size, &count);
1117 }
Michal Vasko086311b2016-01-08 09:53:11 +01001118
1119 /* ending NULL capability */
1120 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1121
1122 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001123
1124error:
1125
1126 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001127 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001128}
1129
Radek Krejci24a18412018-05-16 15:09:10 +02001130API const char **
1131nc_server_get_cpblts(struct ly_ctx *ctx)
1132{
1133 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1134}
1135
Radek Krejci695d4fa2015-10-22 13:23:54 +02001136static int
Michal Vasko2cb24b42017-05-23 14:59:11 +02001137parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001138{
1139 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001140 int ver = -1, i = 0;
1141 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001142
1143 if (list) {
1144 /* get the storage for server's capabilities */
1145 LY_TREE_FOR(xml->child, cpblt) {
1146 i++;
1147 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001148 /* last item remains NULL */
1149 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001150 if (!*list) {
1151 ERRMEM;
1152 return -1;
1153 }
1154 i = 0;
1155 }
1156
1157 LY_TREE_FOR(xml->child, cpblt) {
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001158 if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
Radek Krejci695d4fa2015-10-22 13:23:54 +02001159 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1160 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1161 return -1;
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001162 } else if (!cpblt->name || !cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001163 continue;
1164 }
1165
Michal Vasko156d3272017-04-11 11:46:49 +02001166 /* skip leading/trailing whitespaces */
1167 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1168 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1169 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1170 ERR("Empty capability \"%s\" received.", cpblt->content);
1171 return -1;
1172 }
1173
Radek Krejci695d4fa2015-10-22 13:23:54 +02001174 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001175 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001176 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001177 } 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 +02001178 ver = 1;
1179 }
1180
1181 /* store capabilities */
1182 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001183 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1184 if (!(*list)[i]) {
1185 ERRMEM;
1186 return -1;
1187 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001188 i++;
1189 }
1190 }
1191
1192 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001193 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001194 }
1195
1196 return ver;
1197}
1198
1199static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001200nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001201{
Michal Vasko131120a2018-05-29 15:44:02 +02001202 NC_MSG_TYPE ret;
1203 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001204 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001205 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001206
Michal Vasko131120a2018-05-29 15:44:02 +02001207 if (session->side == NC_CLIENT) {
1208 /* client side hello - send only NETCONF base capabilities */
1209 cpblts = malloc(3 * sizeof *cpblts);
1210 if (!cpblts) {
1211 ERRMEM;
1212 return NC_MSG_ERROR;
1213 }
1214 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1215 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1216 cpblts[2] = NULL;
1217
1218 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1219 sid = NULL;
1220 } else {
1221 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
1222
1223 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1224 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001225 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001226
Michal Vasko131120a2018-05-29 15:44:02 +02001227 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001228
Michal Vasko086311b2016-01-08 09:53:11 +01001229 for (i = 0; cpblts[i]; ++i) {
1230 lydict_remove(session->ctx, cpblts[i]);
1231 }
1232 free(cpblts);
1233
Michal Vasko131120a2018-05-29 15:44:02 +02001234 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001235}
1236
1237static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001238nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001239{
1240 struct lyxml_elem *xml = NULL, *node;
Michal Vasko131120a2018-05-29 15:44:02 +02001241 NC_MSG_TYPE msgtype;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001242 int ver = -1;
1243 char *str;
1244 long long int id;
1245 int flag = 0;
1246
Michal Vasko131120a2018-05-29 15:44:02 +02001247 msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001248
Michal Vasko131120a2018-05-29 15:44:02 +02001249 switch (msgtype) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001250 case NC_MSG_HELLO:
1251 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001252 LY_TREE_FOR(xml->child, node) {
1253 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1254 continue;
1255 } else if (!strcmp(node->name, "session-id")) {
1256 if (!node->content || !strlen(node->content)) {
1257 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001258 goto error;
1259 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001260 str = NULL;
1261 id = strtoll(node->content, &str, 10);
1262 if (*str || id < 1 || id > UINT32_MAX) {
1263 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001264 goto error;
1265 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001266 session->id = (uint32_t)id;
1267 continue;
1268 } else if (strcmp(node->name, "capabilities")) {
1269 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001270 goto error;
1271 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001272
1273 if (flag) {
1274 /* multiple capabilities elements */
1275 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1276 goto error;
1277 }
1278 flag = 1;
1279
Michal Vasko2e6defd2016-10-07 15:48:15 +02001280 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001281 goto error;
1282 }
1283 session->version = ver;
1284 }
1285
1286 if (!session->id) {
1287 ERR("Missing <session-id> in server's <hello>.");
1288 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001289 }
1290 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001291 case NC_MSG_WOULDBLOCK:
1292 ERR("Server's <hello> timeout elapsed.");
1293 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001294 case NC_MSG_ERROR:
1295 /* nothing special, just pass it out */
1296 break;
1297 default:
1298 ERR("Unexpected message received instead of <hello>.");
1299 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001300 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001301 }
1302
1303 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001304 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001305
1306 return msgtype;
1307
1308error:
1309 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001310 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001311
1312 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001313}
1314
Michal Vasko11d142a2016-01-19 15:58:24 +01001315static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001316nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001317{
1318 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001319 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001320 int ver = -1;
1321 int flag = 0;
1322
Michal Vasko131120a2018-05-29 15:44:02 +02001323 msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ?
1324 server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001325
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001326 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001327 case NC_MSG_HELLO:
1328 /* get know NETCONF version */
1329 LY_TREE_FOR(xml->child, node) {
1330 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1331 continue;
1332 } else if (strcmp(node->name, "capabilities")) {
1333 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001334 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001335 goto cleanup;
1336 }
1337
1338 if (flag) {
1339 /* multiple capabilities elements */
1340 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001341 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001342 goto cleanup;
1343 }
1344 flag = 1;
1345
1346 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001347 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001348 goto cleanup;
1349 }
1350 session->version = ver;
1351 }
1352 break;
1353 case NC_MSG_ERROR:
1354 /* nothing special, just pass it out */
1355 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001356 case NC_MSG_WOULDBLOCK:
1357 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001358 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001359 default:
1360 ERR("Unexpected message received instead of <hello>.");
1361 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001362 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001363 }
1364
1365cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001366 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001367 return msgtype;
1368}
1369
Michal Vasko71090fc2016-05-24 16:37:28 +02001370NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001371nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001372{
Michal Vasko086311b2016-01-08 09:53:11 +01001373 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001374
Michal Vasko131120a2018-05-29 15:44:02 +02001375 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001376 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001377 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001378 }
1379
Michal Vasko11d142a2016-01-19 15:58:24 +01001380 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001381 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001382 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001383 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001384 }
1385
Michal Vasko71090fc2016-05-24 16:37:28 +02001386 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001387}
Michal Vasko086311b2016-01-08 09:53:11 +01001388
Michal Vasko889162e2019-09-06 14:43:37 +02001389#ifdef NC_ENABLED_SSH
1390
Michal Vasko999b64a2019-07-10 16:06:15 +02001391static void
1392nc_ssh_init(void)
1393{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001394#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001395 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1396 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001397#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001398}
1399
1400static void
1401nc_ssh_destroy(void)
1402{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001403#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001404 FIPS_mode_set(0);
1405 CONF_modules_unload(1);
1406 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001407#endif
1408
Michal Vaskoe1e82632019-09-09 09:18:03 +02001409#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1410 ssh_finalize();
1411#endif
1412}
1413
Michal Vasko889162e2019-09-06 14:43:37 +02001414#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001415
Radek Krejci53691be2016-02-22 13:58:37 +01001416#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001417
Michal Vasko770b4362017-02-17 14:44:18 +01001418#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1419
Michal Vaskof0c92c02016-01-29 09:41:45 +01001420struct CRYPTO_dynlock_value {
1421 pthread_mutex_t lock;
1422};
1423
Michal Vaskof0c92c02016-01-29 09:41:45 +01001424static struct CRYPTO_dynlock_value *
1425tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1426{
1427 struct CRYPTO_dynlock_value *value;
1428
1429 value = malloc(sizeof *value);
1430 if (!value) {
1431 ERRMEM;
1432 return NULL;
1433 }
1434 pthread_mutex_init(&value->lock, NULL);
1435
1436 return value;
1437}
1438
1439static void
1440tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1441{
1442 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1443 * I found ignored this fact, what do I know... */
1444 if (mode & CRYPTO_LOCK) {
1445 pthread_mutex_lock(&l->lock);
1446 } else {
1447 pthread_mutex_unlock(&l->lock);
1448 }
1449}
1450
1451static void
1452tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1453{
1454 pthread_mutex_destroy(&l->lock);
1455 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001456}
Michal Vasko770b4362017-02-17 14:44:18 +01001457#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001458
Michal Vasko8f0c0282016-02-29 10:17:14 +01001459#endif /* NC_ENABLED_TLS */
1460
1461#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1462
Michal Vasko770b4362017-02-17 14:44:18 +01001463#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001464static pthread_mutex_t *tls_locks;
1465
1466static void
1467tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1468{
1469 if (mode & CRYPTO_LOCK) {
1470 pthread_mutex_lock(tls_locks + n);
1471 } else {
1472 pthread_mutex_unlock(tls_locks + n);
1473 }
1474}
1475
1476static void
1477tls_thread_id_func(CRYPTO_THREADID *tid)
1478{
1479 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1480}
Michal Vasko770b4362017-02-17 14:44:18 +01001481#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001482
1483static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001484nc_tls_init(void)
1485{
Rosen Penev4f552d62019-06-26 16:10:43 -07001486#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001487 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001488 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001489 SSL_library_init();
1490
Michal Vaskof89948c2018-01-04 09:19:46 +01001491 int i;
1492
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001493 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001494 if (!tls_locks) {
1495 ERRMEM;
1496 return;
1497 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001498 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1499 pthread_mutex_init(tls_locks + i, NULL);
1500 }
1501
Michal Vaskof0c92c02016-01-29 09:41:45 +01001502 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001503 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001504
1505 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1506 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1507 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001508#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001509}
1510
Michal Vasko8f0c0282016-02-29 10:17:14 +01001511static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001512nc_tls_destroy(void)
1513{
Rosen Penev4f552d62019-06-26 16:10:43 -07001514#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001515 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001516 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001517 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001518 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001519 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001520#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001521 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001522#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1523 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001524#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001525
Michal Vaskof89948c2018-01-04 09:19:46 +01001526 int i;
1527
Michal Vaskob6e37262016-02-25 14:49:00 +01001528 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001529 CRYPTO_set_locking_callback(NULL);
1530 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1531 pthread_mutex_destroy(tls_locks + i);
1532 }
1533 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001534
1535 CRYPTO_set_dynlock_create_callback(NULL);
1536 CRYPTO_set_dynlock_lock_callback(NULL);
1537 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001538#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001539}
1540
Michal Vasko8f0c0282016-02-29 10:17:14 +01001541#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001542
Radek Krejci53691be2016-02-22 13:58:37 +01001543#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001544
Michal Vasko8f0c0282016-02-29 10:17:14 +01001545static void
Michal Vasko5e228792016-02-03 15:30:24 +01001546nc_ssh_tls_init(void)
1547{
Rosen Penev4f552d62019-06-26 16:10:43 -07001548#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001549 SSL_load_error_strings();
1550 ERR_load_BIO_strings();
1551 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001552#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001553
1554 nc_ssh_init();
1555
Michal Vaskoe1e82632019-09-09 09:18:03 +02001556#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001557 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1558 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1559 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001560#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001561}
1562
Michal Vasko8f0c0282016-02-29 10:17:14 +01001563static void
Michal Vasko5e228792016-02-03 15:30:24 +01001564nc_ssh_tls_destroy(void)
1565{
Rosen Penev4f552d62019-06-26 16:10:43 -07001566#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001567 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001568# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001569 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001570# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001571 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001572# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001573#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001574
1575 nc_ssh_destroy();
1576
Michal Vaskoe1e82632019-09-09 09:18:03 +02001577#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001578 CRYPTO_set_dynlock_create_callback(NULL);
1579 CRYPTO_set_dynlock_lock_callback(NULL);
1580 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001581#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001582}
1583
Radek Krejci53691be2016-02-22 13:58:37 +01001584#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001585
1586#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1587
1588API void
1589nc_thread_destroy(void)
1590{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001591 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1592 //CRYPTO_cleanup_all_ex_data();
1593
Michal Vasko770b4362017-02-17 14:44:18 +01001594#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1595 CRYPTO_THREADID crypto_tid;
1596
Michal Vasko8f0c0282016-02-29 10:17:14 +01001597 CRYPTO_THREADID_current(&crypto_tid);
1598 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001599#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001600}
1601
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001602#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1603
1604void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001605nc_init(void)
1606{
1607#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1608 nc_ssh_tls_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001609#elif defined(NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001610 nc_ssh_init();
1611#elif defined(NC_ENABLED_TLS)
1612 nc_tls_init();
1613#endif
1614}
1615
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001616void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001617nc_destroy(void)
1618{
1619#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1620 nc_ssh_tls_destroy();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001621#elif defined(NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001622 nc_ssh_destroy();
1623#elif defined(NC_ENABLED_TLS)
1624 nc_tls_destroy();
1625#endif
1626}