blob: 44dc409954df307dcba4f403ebc396c249c24dac [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 Vasko9e8ac262020-04-07 13:06:45 +020031#include "compat.h"
Michal Vasko5e228792016-02-03 15:30:24 +010032#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010033#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010034#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010035
Radek Krejci53691be2016-02-22 13:58:37 +010036#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020037
Michal Vasko086311b2016-01-08 09:53:11 +010038# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020039
Radek Krejci53691be2016-02-22 13:58:37 +010040#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020041
Radek Krejci53691be2016-02-22 13:58:37 +010042#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010043
Michal Vasko5e228792016-02-03 15:30:24 +010044# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010045# include <openssl/err.h>
46
Radek Krejci53691be2016-02-22 13:58:37 +010047#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010048
Michal Vasko086311b2016-01-08 09:53:11 +010049/* in seconds */
50#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080051#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020052
Michal Vasko05ba9df2016-01-13 14:40:27 +010053/* in milliseconds */
54#define NC_CLOSE_REPLY_TIMEOUT 200
55
Michal Vasko086311b2016-01-08 09:53:11 +010056extern struct nc_server_opts server_opts;
57
Radek Krejci7ac16052016-07-15 11:48:18 +020058int
Michal Vasko77a6abe2017-10-05 10:02:20 +020059nc_gettimespec_mono(struct timespec *ts)
60{
61#ifdef CLOCK_MONOTONIC_RAW
62 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
63#elif defined(CLOCK_MONOTONIC)
64 return clock_gettime(CLOCK_MONOTONIC, ts);
65#else
66 /* no monotonic clock available, return realtime */
67 return nc_gettimespec_real(ts);
68#endif
69}
70
71int
72nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020073{
Michal Vasko0ef46df2016-09-21 14:03:18 +020074#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020075 return clock_gettime(CLOCK_REALTIME, ts);
76#else
77 int rc;
78 struct timeval tv;
79
80 rc = gettimeofday(&tv, NULL);
81 if (!rc) {
82 ts->tv_sec = (time_t)tv.tv_sec;
83 ts->tv_nsec = 1000L * (long)tv.tv_usec;
84 }
85 return rc;
86#endif
87}
88
Michal Vasko36c7be82017-02-22 13:37:59 +010089/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
90int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020091nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010092{
Michal Vasko36c7be82017-02-22 13:37:59 +010093 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010094
Michal Vaskoa82bd202017-03-03 14:07:29 +010095 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
96 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010097
98 return (nsec_diff ? nsec_diff / 1000000L : 0);
99}
100
Michal Vasko36c7be82017-02-22 13:37:59 +0100101void
102nc_addtimespec(struct timespec *ts, uint32_t msec)
103{
Michal Vasko81c5b302017-03-15 12:10:40 +0100104 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
105
Michal Vasko0dfcd332017-03-03 13:14:39 +0100106 ts->tv_sec += msec / 1000;
107 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100108
Michal Vasko81c5b302017-03-15 12:10:40 +0100109 if (ts->tv_nsec >= 1000000000L) {
110 ++ts->tv_sec;
111 ts->tv_nsec -= 1000000000L;
112 } else if (ts->tv_nsec < 0) {
113 --ts->tv_sec;
114 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100115 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100116
117 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100118}
119
Michal Vaskoddce1212019-05-24 09:58:49 +0200120const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200121nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200122{
123 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200124 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200125 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200126 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200127 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200128 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200129 return "EC";
130 default:
131 break;
132 }
133
134 return NULL;
135}
136
Michal Vaskobe52dc22018-10-17 09:28:17 +0200137int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200138nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200139{
140 int opt;
141
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200142 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200143 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
144 ERR("Could not set SO_KEEPALIVE option (%s).", strerror(errno));
145 return -1;
146 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200147 if (!ka->enabled) {
148 return 0;
149 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200150
151#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200152 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200153 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
154 ERR("Setsockopt failed (%s).", strerror(errno));
155 return -1;
156 }
157#endif
158
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200159#ifdef TCP_KEEPCNT
160 opt = ka->max_probes;
161 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vaskobe52dc22018-10-17 09:28:17 +0200162 ERR("Setsockopt failed (%s).", strerror(errno));
163 return -1;
164 }
165#endif
166
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200167#ifdef TCP_KEEPINTVL
168 opt = ka->probe_interval;
169 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vaskobe52dc22018-10-17 09:28:17 +0200170 ERR("Setsockopt failed (%s).", strerror(errno));
171 return -1;
172 }
173#endif
174
175 return 0;
176}
177
Radek Krejci28472922016-07-15 11:51:16 +0200178#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
179int
180pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
181{
Michal Vasko81c5b302017-03-15 12:10:40 +0100182 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200183 int rc;
184 struct timespec cur, dur;
185
186 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
187 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
Michal Vasko145619f2018-02-16 10:08:45 +0100188 nc_gettimespec_real(&cur);
Radek Krejci28472922016-07-15 11:51:16 +0200189
Michal Vasko81c5b302017-03-15 12:10:40 +0100190 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
191 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200192 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100193 } else if (diff < 5) {
194 /* sleep until timeout */
Michal Vaskobf378cb2018-09-21 15:18:52 +0200195 dur.tv_sec = 0;
196 dur.tv_nsec = (long)diff * 1000000;
Michal Vasko81c5b302017-03-15 12:10:40 +0100197 } else {
198 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200199 dur.tv_sec = 0;
200 dur.tv_nsec = 5000000;
201 }
202
203 nanosleep(&dur, NULL);
204 }
205
206 return rc;
207}
208#endif
209
Michal Vaskoade892d2017-02-22 13:40:35 +0100210struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200211nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100212{
213 struct nc_session *sess;
214
215 sess = calloc(1, sizeof *sess);
216 if (!sess) {
217 return NULL;
218 }
219
Michal Vasko131120a2018-05-29 15:44:02 +0200220 sess->side = side;
221
222 if (side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100223 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
224 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
225 sess->opts.server.rpc_inuse = 0;
226
227 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
228 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200229 }
230
231 if (!shared_ti) {
232 sess->io_lock = malloc(sizeof *sess->io_lock);
233 if (!sess->io_lock) {
234 goto error;
235 }
236 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100237 }
238
239 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200240
241error:
Michal Vasko131120a2018-05-29 15:44:02 +0200242 free(sess);
243 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100244}
245
Michal Vasko96164bf2016-01-21 15:41:58 +0100246/*
247 * @return 1 - success
248 * 0 - timeout
249 * -1 - error
250 */
251int
Michal Vasko131120a2018-05-29 15:44:02 +0200252nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100253{
254 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100255 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100256
Michal Vasko131120a2018-05-29 15:44:02 +0200257 if (session->side != NC_SERVER) {
258 ERRINT;
259 return -1;
260 }
261
Michal Vasko96164bf2016-01-21 15:41:58 +0100262 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200263 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100264 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100265
Michal Vaskoade892d2017-02-22 13:40:35 +0100266 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100267 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100268 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100269 while (session->opts.server.rpc_inuse) {
270 ret = pthread_cond_timedwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100271 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100272 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100273 break;
274 }
275 }
276 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100277 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100278 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100279 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100280 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100281 if (session->opts.server.rpc_inuse) {
282 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100283 return 0;
284 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200285 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100286 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100287 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100288 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100289 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100290 while (session->opts.server.rpc_inuse) {
291 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100292 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100293 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100294 break;
295 }
296 }
297 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100298 }
299
Michal Vaskoade892d2017-02-22 13:40:35 +0100300 if (ret) {
301 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
302 /* timeout */
303 return 0;
304 }
305
Michal Vasko96164bf2016-01-21 15:41:58 +0100306 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200307 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100308 return -1;
309 }
310
311 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100312 assert(session->opts.server.rpc_inuse == 0);
313 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100314
315 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100316 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100317 if (ret) {
318 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200319 ERR("%s: faile to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100320 return -1;
321 }
322
323 return 1;
324}
325
326int
Michal Vasko131120a2018-05-29 15:44:02 +0200327nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100328{
329 int ret;
330 struct timespec ts_timeout;
331
Michal Vasko131120a2018-05-29 15:44:02 +0200332 if (session->side != NC_SERVER) {
333 ERRINT;
334 return -1;
335 }
336
Michal Vaskoacf98472021-02-04 15:33:57 +0100337 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100338
339 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200340 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100341 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100342
343 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100344 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100345 } else if (!timeout) {
346 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100347 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100348 } else { /* timeout == -1 */
349 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100350 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100351 }
352
353 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
354 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200355 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100356 return -1;
357 } else if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200358 WRN("%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100359 }
360
Michal Vaskoacf98472021-02-04 15:33:57 +0100361 session->opts.server.rpc_inuse = 0;
362 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100363
364 if (!ret) {
365 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100366 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100367 if (ret) {
368 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200369 ERR("%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100370 return -1;
371 }
372 }
373
Michal Vasko96164bf2016-01-21 15:41:58 +0100374 return 1;
375}
376
Michal Vasko131120a2018-05-29 15:44:02 +0200377/*
378 * @return 1 - success
379 * 0 - timeout
380 * -1 - error
381 */
382int
383nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
384{
385 int ret;
386 struct timespec ts_timeout;
387
388 if (timeout > 0) {
389 nc_gettimespec_real(&ts_timeout);
390 nc_addtimespec(&ts_timeout, timeout);
391
392 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
393 } else if (!timeout) {
394 ret = pthread_mutex_trylock(session->io_lock);
395 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200396 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200397 }
398
399 if (ret) {
400 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
401 /* timeout */
402 return 0;
403 }
404
405 /* error */
406 ERR("%s: failed to IO lock a session (%s).", func, strerror(ret));
407 return -1;
408 }
409
410 return 1;
411}
412
413int
414nc_session_io_unlock(struct nc_session *session, const char *func)
415{
416 int ret;
417
418 ret = pthread_mutex_unlock(session->io_lock);
419 if (ret) {
420 /* error */
421 ERR("%s: failed to IO unlock a session (%s).", func, strerror(ret));
422 return -1;
423 }
424
425 return 1;
426}
427
Michal Vasko8dadf782016-01-15 10:29:36 +0100428API NC_STATUS
429nc_session_get_status(const struct nc_session *session)
430{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100431 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200432 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200433 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100434 }
435
Michal Vasko8dadf782016-01-15 10:29:36 +0100436 return session->status;
437}
438
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100439API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200440nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100441{
442 if (!session) {
443 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200444 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100445 }
446
447 return session->term_reason;
448}
449
Michal Vasko8dadf782016-01-15 10:29:36 +0100450API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200451nc_session_get_killed_by(const struct nc_session *session)
452{
453 if (!session) {
454 ERRARG("session");
455 return 0;
456 }
457
458 return session->killed_by;
459}
460
461API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100462nc_session_get_id(const struct nc_session *session)
463{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100464 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200465 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100466 return 0;
467 }
468
Michal Vasko8dadf782016-01-15 10:29:36 +0100469 return session->id;
470}
471
Michal Vasko174fe8e2016-02-17 15:38:09 +0100472API int
473nc_session_get_version(const struct nc_session *session)
474{
475 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200476 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100477 return -1;
478 }
479
480 return (session->version == NC_VERSION_10 ? 0 : 1);
481}
482
Michal Vasko8dadf782016-01-15 10:29:36 +0100483API NC_TRANSPORT_IMPL
484nc_session_get_ti(const struct nc_session *session)
485{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100486 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200487 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100488 return 0;
489 }
490
Michal Vasko8dadf782016-01-15 10:29:36 +0100491 return session->ti_type;
492}
493
494API const char *
495nc_session_get_username(const struct nc_session *session)
496{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100497 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200498 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100499 return NULL;
500 }
501
Michal Vasko8dadf782016-01-15 10:29:36 +0100502 return session->username;
503}
504
505API const char *
506nc_session_get_host(const struct nc_session *session)
507{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100508 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200509 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100510 return NULL;
511 }
512
Michal Vasko8dadf782016-01-15 10:29:36 +0100513 return session->host;
514}
515
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200516API const char *
517nc_session_get_path(const struct nc_session *session)
518{
519 if (!session) {
520 ERRARG("session");
521 return NULL;
522 }
523 if (session->ti_type != NC_TI_UNIX)
524 return NULL;
525
526 return session->path;
527}
528
Michal Vasko8dadf782016-01-15 10:29:36 +0100529API uint16_t
530nc_session_get_port(const struct nc_session *session)
531{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100532 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200533 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100534 return 0;
535 }
536
Michal Vasko8dadf782016-01-15 10:29:36 +0100537 return session->port;
538}
539
Michal Vasko9a25e932016-02-01 10:36:42 +0100540API struct ly_ctx *
541nc_session_get_ctx(const struct nc_session *session)
542{
543 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200544 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100545 return NULL;
546 }
547
548 return session->ctx;
549}
550
Michal Vasko2cc4c682016-03-01 09:16:48 +0100551API void
552nc_session_set_data(struct nc_session *session, void *data)
553{
554 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200555 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100556 return;
557 }
558
559 session->data = data;
560}
561
562API void *
563nc_session_get_data(const struct nc_session *session)
564{
565 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200566 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100567 return NULL;
568 }
569
570 return session->data;
571}
572
Michal Vasko086311b2016-01-08 09:53:11 +0100573NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200574nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200575{
Michal Vasko086311b2016-01-08 09:53:11 +0100576 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100577 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
578 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100579 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100580 }
581
Michal Vasko131120a2018-05-29 15:44:02 +0200582 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100583}
584
Radek Krejci695d4fa2015-10-22 13:23:54 +0200585API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100586nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200587{
Michal Vasko131120a2018-05-29 15:44:02 +0200588 int r, i, rpc_locked = 0, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100589 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100590 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100591 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100592 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100593 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200594 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100595 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200596 void *p;
597
Michal Vasko428087d2016-01-14 16:04:28 +0100598 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200599 return;
600 }
601
Michal Vasko86d357c2016-03-11 13:46:38 +0100602 /* stop notifications loop if any */
Michal Vasko7f1fa3c2020-09-08 16:30:41 +0200603 if ((session->side == NC_CLIENT) && ATOMIC_LOAD(session->opts.client.ntf_tid)) {
Michal Vaskob639e9c2020-09-09 09:51:48 +0200604 ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)NULL);
Michal Vasko86d357c2016-03-11 13:46:38 +0100605 /* the thread now knows it should quit */
Michal Vasko86d357c2016-03-11 13:46:38 +0100606 }
607
Michal Vaskoacf98472021-02-04 15:33:57 +0100608 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200609 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100610 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100611 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200612 } else if (r) {
613 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100614 } else {
615 /* else failed to lock it, too bad */
616 ERR("Session %u: freeing a session while an RPC is being processed.", session->id);
617 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200618 }
619
Michal Vasko8c247822020-09-07 13:23:23 +0200620 if (session->side == NC_CLIENT) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200621 /* cleanup message queues */
622 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200623 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100624 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200625
Michal Vaskoad611702015-12-03 13:41:51 +0100626 p = contiter;
627 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200628 free(p);
629 }
630
631 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200632 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100633 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200634
Michal Vaskoad611702015-12-03 13:41:51 +0100635 p = contiter;
636 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200637 free(p);
638 }
639
Michal Vasko8c247822020-09-07 13:23:23 +0200640 if (session->status == NC_STATUS_RUNNING) {
641 /* send closing info to the other side */
642 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
643 if (!ietfnc) {
644 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
645 } else {
646 close_rpc = lyd_new(NULL, ietfnc, "close-session");
647 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
648 lyd_free(close_rpc);
649 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
650 case NC_MSG_REPLY:
651 LY_TREE_FOR(rpl->child, child) {
652 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
653 break;
654 }
Michal Vaskofad6e912015-10-26 15:37:22 +0100655 }
Michal Vasko8c247822020-09-07 13:23:23 +0200656 if (!child) {
657 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
658 }
659 lyxml_free(session->ctx, rpl);
660 break;
661 case NC_MSG_WOULDBLOCK:
662 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
663 break;
664 case NC_MSG_ERROR:
665 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
666 break;
667 default:
668 /* cannot happen */
669 break;
Michal Vaskofad6e912015-10-26 15:37:22 +0100670 }
Michal Vaskofad6e912015-10-26 15:37:22 +0100671 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200672 }
673
674 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200675 if (session->opts.client.cpblts) {
676 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200677 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200678 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200679 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200680 }
681 }
682
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100683 if (session->data && data_free) {
684 data_free(session->data);
685 }
686
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200687 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
688 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100689 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200690 }
691
Michal Vasko86d357c2016-03-11 13:46:38 +0100692 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200693 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200694
695 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100696 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200697
698 /* CH UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100699 pthread_mutex_unlock(&session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100700
701 /* wait for CH thread to actually wake up */
702 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
703 while (i && (session->flags & NC_SESSION_CALLHOME)) {
704 usleep(NC_TIMEOUT_STEP);
705 --i;
706 }
707 if (session->flags & NC_SESSION_CALLHOME) {
708 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
709 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200710 }
711
Michal Vasko428087d2016-01-14 16:04:28 +0100712 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200713
714 /* transport implementation cleanup */
715 switch (session->ti_type) {
716 case NC_TI_FD:
717 /* nothing needed - file descriptors were provided by caller,
718 * so it is up to the caller to close them correctly
719 * TODO use callbacks
720 */
Michal Vasko3512e402016-01-28 16:22:34 +0100721 /* just to avoid compiler warning */
722 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100723 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200724 break;
725
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200726 case NC_TI_UNIX:
727 sock = session->ti.unixsock.sock;
728 (void)connected;
729 (void)siter;
730 break;
731
Radek Krejci53691be2016-02-22 13:58:37 +0100732#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200733 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100734 if (connected) {
735 ssh_channel_free(session->ti.libssh.channel);
736 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200737 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
738 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200739 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200740 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200741 /* SESSION IO LOCK */
742 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
743
Michal Vasko96164bf2016-01-21 15:41:58 +0100744 multisession = 0;
745 if (session->ti.libssh.next) {
746 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
747 if (siter->status != NC_STATUS_STARTING) {
748 multisession = 1;
749 break;
750 }
751 }
752 }
753
754 if (!multisession) {
755 /* it's not multisession yet, but we still need to free the starting sessions */
756 if (session->ti.libssh.next) {
757 do {
758 siter = session->ti.libssh.next;
759 session->ti.libssh.next = siter->ti.libssh.next;
760
761 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100762 lydict_remove(session->ctx, session->username);
763 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100764 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100765 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100766 }
767
768 free(siter);
769 } while (session->ti.libssh.next != session);
770 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100771 /* remember sock so we can close it */
772 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100773 if (connected) {
774 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200775 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100776 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200777 ssh_free(session->ti.libssh.session);
778 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200779 /* remove the session from the list */
780 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100781 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200782 /* there will be only one session */
783 siter->ti.libssh.next = NULL;
784 } else {
785 /* there are still multiple sessions, keep the ring list */
786 siter->ti.libssh.next = session->ti.libssh.next;
787 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100788 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
789 if (session->flags & NC_SESSION_SSH_MSG_CB) {
Michal Vasko5e0edd82020-07-29 15:26:13 +0200790 siter = session->ti.libssh.next;
791 while (siter && siter->status != NC_STATUS_RUNNING) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100792 if (siter->ti.libssh.next == session) {
793 ERRINT;
794 break;
795 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200796 siter = siter->ti.libssh.next;
Michal Vasko96164bf2016-01-21 15:41:58 +0100797 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200798 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
799 * we set session to NULL because we do not expect any new message to arrive */
Michal Vasko96164bf2016-01-21 15:41:58 +0100800 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200801 if (siter) {
802 siter->flags |= NC_SESSION_SSH_MSG_CB;
803 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100804 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200805 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200806
807 /* SESSION IO UNLOCK */
808 if (r == 1) {
809 nc_session_io_unlock(session, __func__);
810 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200811 break;
812#endif
813
Radek Krejci53691be2016-02-22 13:58:37 +0100814#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200815 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100816 /* remember sock so we can close it */
817 sock = SSL_get_fd(session->ti.tls);
818
Michal Vasko428087d2016-01-14 16:04:28 +0100819 if (connected) {
820 SSL_shutdown(session->ti.tls);
821 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200822 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100823
Michal Vasko2e6defd2016-10-07 15:48:15 +0200824 if (session->side == NC_SERVER) {
825 X509_free(session->opts.server.client_cert);
826 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200827 break;
828#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100829 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100830 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200831 }
Michal Vasko428087d2016-01-14 16:04:28 +0100832
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100833 /* close socket separately */
834 if (sock > -1) {
835 close(sock);
836 }
837
Radek Krejciac6d3472015-10-22 15:47:18 +0200838 lydict_remove(session->ctx, session->username);
839 lydict_remove(session->ctx, session->host);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200840 lydict_remove(session->ctx, session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200841
842 /* final cleanup */
Michal Vaskoacf98472021-02-04 15:33:57 +0100843 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200844 if (rpc_locked) {
845 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100846 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100847 pthread_mutex_destroy(&session->opts.server.rpc_lock);
848 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200849 }
850
851 if (session->io_lock && !multisession) {
852 pthread_mutex_destroy(session->io_lock);
853 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200854 }
855
856 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100857 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200858 }
859
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200860 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100861 /* free CH synchronization structures */
862 pthread_cond_destroy(&session->opts.server.ch_cond);
863 pthread_mutex_destroy(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200864 }
865
Radek Krejci695d4fa2015-10-22 13:23:54 +0200866 free(session);
867}
868
Michal Vasko086311b2016-01-08 09:53:11 +0100869static void
870add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
871{
Radek Krejci658782b2016-12-04 22:04:55 +0100872 size_t len;
873 int i;
874 char *p;
875
876 if (capab) {
877 /* check if already present */
878 p = strchr(capab, '?');
879 if (p) {
880 len = p - capab;
881 } else {
882 len = strlen(capab);
883 }
884 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800885 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100886 /* already present, do not duplicate it */
887 return;
888 }
889 }
890 }
891
892 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100893 if (*count == *size) {
894 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100895 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
896 if (!(*cpblts)) {
897 ERRMEM;
898 return;
899 }
Michal Vasko086311b2016-01-08 09:53:11 +0100900 }
901
902 if (capab) {
903 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
904 } else {
905 (*cpblts)[*count] = NULL;
906 }
907 ++(*count);
908}
909
Michal Vaskodafdc742020-03-11 16:15:59 +0100910static struct lys_feature *
911nc_lys_next_feature(struct lys_feature *last, const struct lys_module *ly_mod, int *idx)
912{
913 uint8_t i;
914
915 assert(ly_mod);
916
917 /* find the (sub)module of the last feature */
918 if (last) {
919 for (i = 0; i < ly_mod->inc_size; ++i) {
920 if (*idx >= ly_mod->inc[i].submodule->features_size) {
921 /* not a feature from this submodule, skip */
922 continue;
923 }
924 if (last != ly_mod->inc[i].submodule->features + *idx) {
925 /* feature is not from this submodule */
926 continue;
927 }
928
929 /* we have found the submodule */
930 break;
931 }
932
933 /* feature not found in submodules, it must be in the main module */
934 assert((i < ly_mod->inc_size) || ((*idx < ly_mod->features_size) && (last == ly_mod->features + *idx)));
935
936 /* we want the next feature */
937 ++(*idx);
938 } else {
939 i = 0;
940 *idx = 0;
941 }
942
943 /* find the (sub)module of the next feature */
944 while ((i < ly_mod->inc_size) && (*idx == ly_mod->inc[i].submodule->features_size)) {
945 /* next submodule */
946 ++i;
947 *idx = 0;
948 }
949
950 /* get the next feature */
951 if (i < ly_mod->inc_size) {
952 last = ly_mod->inc[i].submodule->features + *idx;
953 } else if (*idx < ly_mod->features_size) {
954 last = ly_mod->features + *idx;
955 } else {
956 last = NULL;
957 }
958
959 return last;
960}
961
962
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200963API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200964nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100965{
Michal Vaskod5ada122020-03-19 18:28:06 +0100966 const char **cpblts;
Radek Krejci24a18412018-05-16 15:09:10 +0200967 const struct lys_module *mod, *devmod;
Michal Vaskodafdc742020-03-11 16:15:59 +0100968 struct lys_feature *feat;
Radek Krejci24a18412018-05-16 15:09:10 +0200969 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
970 unsigned int u, v, module_set_id;
971 char *s;
972#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200973 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100974
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200975 if (!ctx) {
976 ERRARG("ctx");
977 return NULL;
978 }
979
Michal Vasko086311b2016-01-08 09:53:11 +0100980 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100981 if (!cpblts) {
982 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200983 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100984 }
Michal Vasko086311b2016-01-08 09:53:11 +0100985 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
986 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
987 count = 2;
988
989 /* capabilities */
990
Radek Krejci3222b7d2017-09-21 16:04:30 +0200991 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100992 if (mod) {
993 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100994 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100995 }
996 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100997 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100998 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100999 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001000 }
1001 }
1002 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001003 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001004 }
1005 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001006 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001007 }
1008 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001009 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001010 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001011
1012 /* The URL capability must be set manually using nc_server_set_capability()
1013 * because of the need for supported protocols to be included.
1014 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1015 */
mekleoa8de5e92020-02-13 09:05:56 +01001016 // if (lys_features_state(mod, "url") == 1) {
1017 // add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
1018 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001019
Michal Vasko086311b2016-01-08 09:53:11 +01001020 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001021 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001022 }
1023 }
1024
Radek Krejci3222b7d2017-09-21 16:04:30 +02001025 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001026 if (mod) {
1027 if (!server_opts.wd_basic_mode) {
1028 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
1029 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001030 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001031 switch (server_opts.wd_basic_mode) {
1032 case NC_WD_ALL:
1033 strcat(str, "?basic-mode=report-all");
1034 break;
1035 case NC_WD_TRIM:
1036 strcat(str, "?basic-mode=trim");
1037 break;
1038 case NC_WD_EXPLICIT:
1039 strcat(str, "?basic-mode=explicit");
1040 break;
1041 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001042 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001043 break;
1044 }
1045
1046 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001047 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001048 if (server_opts.wd_also_supported & NC_WD_ALL) {
1049 strcat(str, "report-all,");
1050 }
1051 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1052 strcat(str, "report-all-tagged,");
1053 }
1054 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1055 strcat(str, "trim,");
1056 }
1057 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1058 strcat(str, "explicit,");
1059 }
1060 str[strlen(str) - 1] = '\0';
1061
1062 add_cpblt(ctx, str, &cpblts, &size, &count);
1063 }
1064 }
1065 }
1066
Radek Krejci658782b2016-12-04 22:04:55 +01001067 /* other capabilities */
1068 for (u = 0; u < server_opts.capabilities_count; u++) {
1069 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001070 }
1071
1072 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +02001073 u = module_set_id = 0;
1074 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001075 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vaskod5ada122020-03-19 18:28:06 +01001076 if (!mod->rev_size || (strcmp(mod->rev[0].date, "2016-06-21") && strcmp(mod->rev[0].date, "2019-01-04"))) {
1077 ERR("Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
1078 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001079 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001080
Michal Vaskod5ada122020-03-19 18:28:06 +01001081 if (!strcmp(mod->rev[0].date, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001082 /* new one (capab defined in RFC 8526 section 2) */
Michal Vaskod5ada122020-03-19 18:28:06 +01001083 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%u",
1084 mod->rev[0].date, ly_ctx_get_module_set_id(ctx));
1085 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001086 } else {
1087 /* old one (capab defined in RFC 7950 section 5.6.4) */
1088 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%u",
1089 mod->rev[0].date, ly_ctx_get_module_set_id(ctx));
1090 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001091 }
Radek Krejci24a18412018-05-16 15:09:10 +02001092 continue;
1093 } else if (mod->type) {
1094 /* skip submodules */
1095 continue;
1096 } else if (version == LYS_VERSION_1 && mod->version > version) {
1097 /* skip YANG 1.1 schemas */
1098 continue;
1099 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
1100 /* skip YANG 1.0 schemas */
1101 continue;
1102 }
Michal Vasko086311b2016-01-08 09:53:11 +01001103
Radek Krejci24a18412018-05-16 15:09:10 +02001104 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
1105 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
1106
Michal Vaskodafdc742020-03-11 16:15:59 +01001107 features_count = 0;
1108 i = 0;
1109 feat = NULL;
1110 while ((feat = nc_lys_next_feature(feat, mod, &i))) {
1111 if (!(feat->flags & LYS_FENABLED)) {
1112 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001113 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001114 if (!features_count) {
1115 strcat(str, "&features=");
1116 str_len += 10;
1117 }
1118 len = strlen(feat->name);
1119 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1120 ERRINT;
1121 break;
1122 }
1123 if (features_count) {
1124 strcat(str, ",");
1125 ++str_len;
1126 }
1127 strcat(str, feat->name);
1128 str_len += len;
1129 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001130 }
Michal Vasko086311b2016-01-08 09:53:11 +01001131
Radek Krejci24a18412018-05-16 15:09:10 +02001132 if (mod->deviated) {
1133 strcat(str, "&deviations=");
1134 str_len += 12;
1135 dev_count = 0;
Frank Rimplercb9274f2019-03-13 16:40:34 +00001136 v = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001137 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
1138 if (devmod == mod) {
1139 continue;
1140 }
1141
1142 for (i = 0; i < devmod->deviation_size; ++i) {
1143 s = strstr(devmod->deviation[i].target_name, mod->name);
1144 if (s && s[strlen(mod->name)] == ':') {
1145 /* we have the module deviating the module being processed */
1146 len = strlen(devmod->name);
1147 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1148 ERRINT;
1149 break;
1150 }
1151 if (dev_count) {
1152 strcat(str, ",");
1153 ++str_len;
1154 }
1155 strcat(str, devmod->name);
1156 str_len += len;
1157 dev_count++;
1158
1159 break;
1160 }
1161 }
1162 }
1163 }
1164
1165 add_cpblt(ctx, str, &cpblts, &size, &count);
1166 }
Michal Vasko086311b2016-01-08 09:53:11 +01001167
1168 /* ending NULL capability */
1169 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1170
1171 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001172
1173error:
Radek Krejcif906e412017-09-22 14:44:45 +02001174 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001175 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001176}
1177
Radek Krejci24a18412018-05-16 15:09:10 +02001178API const char **
1179nc_server_get_cpblts(struct ly_ctx *ctx)
1180{
1181 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1182}
1183
Radek Krejci695d4fa2015-10-22 13:23:54 +02001184static int
Michal Vasko2cb24b42017-05-23 14:59:11 +02001185parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001186{
1187 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001188 int ver = -1, i = 0;
1189 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001190
1191 if (list) {
1192 /* get the storage for server's capabilities */
1193 LY_TREE_FOR(xml->child, cpblt) {
1194 i++;
1195 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001196 /* last item remains NULL */
1197 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001198 if (!*list) {
1199 ERRMEM;
1200 return -1;
1201 }
1202 i = 0;
1203 }
1204
1205 LY_TREE_FOR(xml->child, cpblt) {
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001206 if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
Radek Krejci695d4fa2015-10-22 13:23:54 +02001207 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1208 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1209 return -1;
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001210 } else if (!cpblt->name || !cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001211 continue;
1212 }
1213
Michal Vasko156d3272017-04-11 11:46:49 +02001214 /* skip leading/trailing whitespaces */
1215 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1216 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1217 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1218 ERR("Empty capability \"%s\" received.", cpblt->content);
1219 return -1;
1220 }
1221
Radek Krejci695d4fa2015-10-22 13:23:54 +02001222 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001223 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001224 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001225 } 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 +02001226 ver = 1;
1227 }
1228
1229 /* store capabilities */
1230 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001231 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1232 if (!(*list)[i]) {
1233 ERRMEM;
1234 return -1;
1235 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001236 i++;
1237 }
1238 }
1239
1240 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001241 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001242 }
1243
1244 return ver;
1245}
1246
1247static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001248nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001249{
Michal Vasko131120a2018-05-29 15:44:02 +02001250 NC_MSG_TYPE ret;
1251 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001252 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001253 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001254
Michal Vasko131120a2018-05-29 15:44:02 +02001255 if (session->side == NC_CLIENT) {
1256 /* client side hello - send only NETCONF base capabilities */
1257 cpblts = malloc(3 * sizeof *cpblts);
1258 if (!cpblts) {
1259 ERRMEM;
1260 return NC_MSG_ERROR;
1261 }
1262 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1263 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1264 cpblts[2] = NULL;
1265
1266 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1267 sid = NULL;
1268 } else {
1269 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001270 if (!cpblts) {
1271 return NC_MSG_ERROR;
1272 }
Michal Vasko131120a2018-05-29 15:44:02 +02001273
1274 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1275 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001276 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001277
Michal Vasko131120a2018-05-29 15:44:02 +02001278 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001279
Michal Vasko086311b2016-01-08 09:53:11 +01001280 for (i = 0; cpblts[i]; ++i) {
1281 lydict_remove(session->ctx, cpblts[i]);
1282 }
1283 free(cpblts);
1284
Michal Vasko131120a2018-05-29 15:44:02 +02001285 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001286}
1287
1288static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001289nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001290{
1291 struct lyxml_elem *xml = NULL, *node;
Michal Vasko131120a2018-05-29 15:44:02 +02001292 NC_MSG_TYPE msgtype;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001293 int ver = -1;
1294 char *str;
1295 long long int id;
1296 int flag = 0;
1297
Michal Vasko131120a2018-05-29 15:44:02 +02001298 msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001299
Michal Vasko131120a2018-05-29 15:44:02 +02001300 switch (msgtype) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001301 case NC_MSG_HELLO:
1302 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001303 LY_TREE_FOR(xml->child, node) {
1304 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1305 continue;
1306 } else if (!strcmp(node->name, "session-id")) {
1307 if (!node->content || !strlen(node->content)) {
1308 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001309 goto error;
1310 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001311 str = NULL;
1312 id = strtoll(node->content, &str, 10);
1313 if (*str || id < 1 || id > UINT32_MAX) {
1314 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001315 goto error;
1316 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001317 session->id = (uint32_t)id;
1318 continue;
1319 } else if (strcmp(node->name, "capabilities")) {
1320 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001321 goto error;
1322 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001323
1324 if (flag) {
1325 /* multiple capabilities elements */
1326 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1327 goto error;
1328 }
1329 flag = 1;
1330
Michal Vasko2e6defd2016-10-07 15:48:15 +02001331 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001332 goto error;
1333 }
1334 session->version = ver;
1335 }
1336
1337 if (!session->id) {
1338 ERR("Missing <session-id> in server's <hello>.");
1339 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001340 }
1341 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001342 case NC_MSG_WOULDBLOCK:
1343 ERR("Server's <hello> timeout elapsed.");
1344 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001345 case NC_MSG_ERROR:
1346 /* nothing special, just pass it out */
1347 break;
1348 default:
1349 ERR("Unexpected message received instead of <hello>.");
1350 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001351 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001352 }
1353
1354 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001355 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001356
1357 return msgtype;
1358
1359error:
1360 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001361 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001362
1363 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001364}
1365
Michal Vasko11d142a2016-01-19 15:58:24 +01001366static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001367nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001368{
1369 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001370 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001371 int ver = -1;
1372 int flag = 0;
1373
Michal Vasko131120a2018-05-29 15:44:02 +02001374 msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ?
1375 server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001376
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001377 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001378 case NC_MSG_HELLO:
1379 /* get know NETCONF version */
1380 LY_TREE_FOR(xml->child, node) {
1381 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1382 continue;
1383 } else if (strcmp(node->name, "capabilities")) {
1384 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001385 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001386 goto cleanup;
1387 }
1388
1389 if (flag) {
1390 /* multiple capabilities elements */
1391 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001392 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001393 goto cleanup;
1394 }
1395 flag = 1;
1396
1397 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001398 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001399 goto cleanup;
1400 }
1401 session->version = ver;
1402 }
1403 break;
1404 case NC_MSG_ERROR:
1405 /* nothing special, just pass it out */
1406 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001407 case NC_MSG_WOULDBLOCK:
1408 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001409 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001410 default:
1411 ERR("Unexpected message received instead of <hello>.");
1412 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001413 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001414 }
1415
1416cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001417 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001418 return msgtype;
1419}
1420
Michal Vasko71090fc2016-05-24 16:37:28 +02001421NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001422nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001423{
Michal Vasko086311b2016-01-08 09:53:11 +01001424 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001425
Michal Vasko131120a2018-05-29 15:44:02 +02001426 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001427 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001428 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001429 }
1430
Michal Vasko11d142a2016-01-19 15:58:24 +01001431 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001432 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001433 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001434 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001435 }
1436
Michal Vasko71090fc2016-05-24 16:37:28 +02001437 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001438}
Michal Vasko086311b2016-01-08 09:53:11 +01001439
Michal Vasko889162e2019-09-06 14:43:37 +02001440#ifdef NC_ENABLED_SSH
1441
Michal Vasko999b64a2019-07-10 16:06:15 +02001442static void
1443nc_ssh_init(void)
1444{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001445#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001446 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1447 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001448#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001449}
1450
1451static void
1452nc_ssh_destroy(void)
1453{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001454#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001455 FIPS_mode_set(0);
1456 CONF_modules_unload(1);
1457 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001458#endif
1459
Michal Vaskoe1e82632019-09-09 09:18:03 +02001460#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1461 ssh_finalize();
1462#endif
1463}
1464
Michal Vasko889162e2019-09-06 14:43:37 +02001465#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001466
Radek Krejci53691be2016-02-22 13:58:37 +01001467#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001468
Michal Vasko770b4362017-02-17 14:44:18 +01001469#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1470
Michal Vaskof0c92c02016-01-29 09:41:45 +01001471struct CRYPTO_dynlock_value {
1472 pthread_mutex_t lock;
1473};
1474
Michal Vaskof0c92c02016-01-29 09:41:45 +01001475static struct CRYPTO_dynlock_value *
1476tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1477{
1478 struct CRYPTO_dynlock_value *value;
1479
1480 value = malloc(sizeof *value);
1481 if (!value) {
1482 ERRMEM;
1483 return NULL;
1484 }
1485 pthread_mutex_init(&value->lock, NULL);
1486
1487 return value;
1488}
1489
1490static void
1491tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1492{
1493 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1494 * I found ignored this fact, what do I know... */
1495 if (mode & CRYPTO_LOCK) {
1496 pthread_mutex_lock(&l->lock);
1497 } else {
1498 pthread_mutex_unlock(&l->lock);
1499 }
1500}
1501
1502static void
1503tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1504{
1505 pthread_mutex_destroy(&l->lock);
1506 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001507}
Michal Vasko770b4362017-02-17 14:44:18 +01001508#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001509
Michal Vasko8f0c0282016-02-29 10:17:14 +01001510#endif /* NC_ENABLED_TLS */
1511
1512#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1513
Michal Vasko770b4362017-02-17 14:44:18 +01001514#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001515static pthread_mutex_t *tls_locks;
1516
1517static void
1518tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1519{
1520 if (mode & CRYPTO_LOCK) {
1521 pthread_mutex_lock(tls_locks + n);
1522 } else {
1523 pthread_mutex_unlock(tls_locks + n);
1524 }
1525}
1526
1527static void
1528tls_thread_id_func(CRYPTO_THREADID *tid)
1529{
1530 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1531}
Michal Vasko770b4362017-02-17 14:44:18 +01001532#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001533
1534static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001535nc_tls_init(void)
1536{
Rosen Penev4f552d62019-06-26 16:10:43 -07001537#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001538 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001539 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001540 SSL_library_init();
1541
Michal Vaskof89948c2018-01-04 09:19:46 +01001542 int i;
1543
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001544 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001545 if (!tls_locks) {
1546 ERRMEM;
1547 return;
1548 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001549 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1550 pthread_mutex_init(tls_locks + i, NULL);
1551 }
1552
Michal Vaskof0c92c02016-01-29 09:41:45 +01001553 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001554 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001555
1556 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1557 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1558 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001559#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001560}
1561
Michal Vasko8f0c0282016-02-29 10:17:14 +01001562static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001563nc_tls_destroy(void)
1564{
Rosen Penev4f552d62019-06-26 16:10:43 -07001565#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001566 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001567 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001568 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001569 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001570 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001571#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001572 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001573#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1574 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001575#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001576
Michal Vaskof89948c2018-01-04 09:19:46 +01001577 int i;
1578
Michal Vaskob6e37262016-02-25 14:49:00 +01001579 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001580 CRYPTO_set_locking_callback(NULL);
1581 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1582 pthread_mutex_destroy(tls_locks + i);
1583 }
1584 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001585
1586 CRYPTO_set_dynlock_create_callback(NULL);
1587 CRYPTO_set_dynlock_lock_callback(NULL);
1588 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001589#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001590}
1591
Michal Vasko8f0c0282016-02-29 10:17:14 +01001592#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001593
Radek Krejci53691be2016-02-22 13:58:37 +01001594#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001595
Michal Vasko8f0c0282016-02-29 10:17:14 +01001596static void
Michal Vasko5e228792016-02-03 15:30:24 +01001597nc_ssh_tls_init(void)
1598{
Rosen Penev4f552d62019-06-26 16:10:43 -07001599#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001600 SSL_load_error_strings();
1601 ERR_load_BIO_strings();
1602 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001603#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001604
1605 nc_ssh_init();
1606
Michal Vaskoe1e82632019-09-09 09:18:03 +02001607#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001608 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1609 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1610 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001611#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001612}
1613
Michal Vasko8f0c0282016-02-29 10:17:14 +01001614static void
Michal Vasko5e228792016-02-03 15:30:24 +01001615nc_ssh_tls_destroy(void)
1616{
Rosen Penev4f552d62019-06-26 16:10:43 -07001617#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001618 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001619# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001620 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001621# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001622 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001623# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001624#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001625
1626 nc_ssh_destroy();
1627
Michal Vaskoe1e82632019-09-09 09:18:03 +02001628#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001629 CRYPTO_set_dynlock_create_callback(NULL);
1630 CRYPTO_set_dynlock_lock_callback(NULL);
1631 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001632#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001633}
1634
Radek Krejci53691be2016-02-22 13:58:37 +01001635#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001636
1637#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1638
1639API void
1640nc_thread_destroy(void)
1641{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001642 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1643 //CRYPTO_cleanup_all_ex_data();
1644
Michal Vasko770b4362017-02-17 14:44:18 +01001645#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1646 CRYPTO_THREADID crypto_tid;
1647
Michal Vasko8f0c0282016-02-29 10:17:14 +01001648 CRYPTO_THREADID_current(&crypto_tid);
1649 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001650#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001651}
1652
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001653#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1654
1655void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001656nc_init(void)
1657{
1658#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1659 nc_ssh_tls_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001660#elif defined(NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001661 nc_ssh_init();
1662#elif defined(NC_ENABLED_TLS)
1663 nc_tls_init();
1664#endif
1665}
1666
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001667void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001668nc_destroy(void)
1669{
1670#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1671 nc_ssh_tls_destroy();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001672#elif defined(NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001673 nc_ssh_destroy();
1674#elif defined(NC_ENABLED_TLS)
1675 nc_tls_destroy();
1676#endif
1677}