blob: 92eeb859fa7b5a3600ea2b53c38f4c1423bbfc71 [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) {
223 sess->opts.server.rpc_lock = malloc(sizeof *sess->opts.server.rpc_lock);
224 sess->opts.server.rpc_cond = malloc(sizeof *sess->opts.server.rpc_cond);
225 sess->opts.server.rpc_inuse = malloc(sizeof *sess->opts.server.rpc_inuse);
226 if (!sess->opts.server.rpc_lock || !sess->opts.server.rpc_cond || !sess->opts.server.rpc_inuse) {
227 goto error;
Michal Vaskoade892d2017-02-22 13:40:35 +0100228 }
Michal Vasko131120a2018-05-29 15:44:02 +0200229 pthread_mutex_init(sess->opts.server.rpc_lock, NULL);
230 pthread_cond_init(sess->opts.server.rpc_cond, NULL);
231 *sess->opts.server.rpc_inuse = 0;
232 }
233
234 if (!shared_ti) {
235 sess->io_lock = malloc(sizeof *sess->io_lock);
236 if (!sess->io_lock) {
237 goto error;
238 }
239 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100240 }
241
242 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200243
244error:
245 if (side == NC_SERVER) {
246 free(sess->opts.server.rpc_lock);
247 free(sess->opts.server.rpc_cond);
248 free((int *)sess->opts.server.rpc_inuse);
249 }
250 free(sess);
251 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100252}
253
Michal Vasko96164bf2016-01-21 15:41:58 +0100254/*
255 * @return 1 - success
256 * 0 - timeout
257 * -1 - error
258 */
259int
Michal Vasko131120a2018-05-29 15:44:02 +0200260nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100261{
262 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100263 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100264
Michal Vasko131120a2018-05-29 15:44:02 +0200265 if (session->side != NC_SERVER) {
266 ERRINT;
267 return -1;
268 }
269
Michal Vasko96164bf2016-01-21 15:41:58 +0100270 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200271 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100272 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100273
Michal Vaskoade892d2017-02-22 13:40:35 +0100274 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200275 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100276 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200277 while (*session->opts.server.rpc_inuse) {
278 ret = pthread_cond_timedwait(session->opts.server.rpc_cond, session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100279 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200280 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100281 break;
282 }
283 }
284 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100285 } else if (!timeout) {
Michal Vasko131120a2018-05-29 15:44:02 +0200286 if (*session->opts.server.rpc_inuse) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100287 /* immediate timeout */
288 return 0;
289 }
290
291 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200292 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100293 if (!ret) {
294 /* be extra careful, someone could have been faster */
Michal Vasko131120a2018-05-29 15:44:02 +0200295 if (*session->opts.server.rpc_inuse) {
296 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100297 return 0;
298 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200299 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100300 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100301 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200302 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100303 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200304 while (*session->opts.server.rpc_inuse) {
305 ret = pthread_cond_wait(session->opts.server.rpc_cond, session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100306 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200307 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100308 break;
309 }
310 }
311 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100312 }
313
Michal Vaskoade892d2017-02-22 13:40:35 +0100314 if (ret) {
315 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
316 /* timeout */
317 return 0;
318 }
319
Michal Vasko96164bf2016-01-21 15:41:58 +0100320 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200321 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100322 return -1;
323 }
324
325 /* ok */
Michal Vasko131120a2018-05-29 15:44:02 +0200326 assert(*session->opts.server.rpc_inuse == 0);
327 *session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100328
329 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200330 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100331 if (ret) {
332 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200333 ERR("%s: faile to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100334 return -1;
335 }
336
337 return 1;
338}
339
340int
Michal Vasko131120a2018-05-29 15:44:02 +0200341nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100342{
343 int ret;
344 struct timespec ts_timeout;
345
Michal Vasko131120a2018-05-29 15:44:02 +0200346 if (session->side != NC_SERVER) {
347 ERRINT;
348 return -1;
349 }
350
351 assert(*session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100352
353 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200354 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100355 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100356
357 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200358 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100359 } else if (!timeout) {
360 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200361 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100362 } else { /* timeout == -1 */
363 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200364 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100365 }
366
367 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
368 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200369 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100370 return -1;
371 } else if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200372 WRN("%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100373 }
374
Michal Vasko131120a2018-05-29 15:44:02 +0200375 *session->opts.server.rpc_inuse = 0;
376 pthread_cond_signal(session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100377
378 if (!ret) {
379 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200380 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100381 if (ret) {
382 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200383 ERR("%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100384 return -1;
385 }
386 }
387
Michal Vasko96164bf2016-01-21 15:41:58 +0100388 return 1;
389}
390
Michal Vasko131120a2018-05-29 15:44:02 +0200391/*
392 * @return 1 - success
393 * 0 - timeout
394 * -1 - error
395 */
396int
397nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
398{
399 int ret;
400 struct timespec ts_timeout;
401
402 if (timeout > 0) {
403 nc_gettimespec_real(&ts_timeout);
404 nc_addtimespec(&ts_timeout, timeout);
405
406 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
407 } else if (!timeout) {
408 ret = pthread_mutex_trylock(session->io_lock);
409 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200410 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200411 }
412
413 if (ret) {
414 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
415 /* timeout */
416 return 0;
417 }
418
419 /* error */
420 ERR("%s: failed to IO lock a session (%s).", func, strerror(ret));
421 return -1;
422 }
423
424 return 1;
425}
426
427int
428nc_session_io_unlock(struct nc_session *session, const char *func)
429{
430 int ret;
431
432 ret = pthread_mutex_unlock(session->io_lock);
433 if (ret) {
434 /* error */
435 ERR("%s: failed to IO unlock a session (%s).", func, strerror(ret));
436 return -1;
437 }
438
439 return 1;
440}
441
Michal Vasko8dadf782016-01-15 10:29:36 +0100442API NC_STATUS
443nc_session_get_status(const struct nc_session *session)
444{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100445 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200446 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200447 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100448 }
449
Michal Vasko8dadf782016-01-15 10:29:36 +0100450 return session->status;
451}
452
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100453API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200454nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100455{
456 if (!session) {
457 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200458 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100459 }
460
461 return session->term_reason;
462}
463
Michal Vasko8dadf782016-01-15 10:29:36 +0100464API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200465nc_session_get_killed_by(const struct nc_session *session)
466{
467 if (!session) {
468 ERRARG("session");
469 return 0;
470 }
471
472 return session->killed_by;
473}
474
475API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100476nc_session_get_id(const struct nc_session *session)
477{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100478 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200479 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100480 return 0;
481 }
482
Michal Vasko8dadf782016-01-15 10:29:36 +0100483 return session->id;
484}
485
Michal Vasko174fe8e2016-02-17 15:38:09 +0100486API int
487nc_session_get_version(const struct nc_session *session)
488{
489 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200490 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100491 return -1;
492 }
493
494 return (session->version == NC_VERSION_10 ? 0 : 1);
495}
496
Michal Vasko8dadf782016-01-15 10:29:36 +0100497API NC_TRANSPORT_IMPL
498nc_session_get_ti(const struct nc_session *session)
499{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100500 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200501 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100502 return 0;
503 }
504
Michal Vasko8dadf782016-01-15 10:29:36 +0100505 return session->ti_type;
506}
507
508API const char *
509nc_session_get_username(const struct nc_session *session)
510{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100511 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200512 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100513 return NULL;
514 }
515
Michal Vasko8dadf782016-01-15 10:29:36 +0100516 return session->username;
517}
518
519API const char *
520nc_session_get_host(const struct nc_session *session)
521{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100522 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200523 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100524 return NULL;
525 }
526
Michal Vasko8dadf782016-01-15 10:29:36 +0100527 return session->host;
528}
529
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200530API const char *
531nc_session_get_path(const struct nc_session *session)
532{
533 if (!session) {
534 ERRARG("session");
535 return NULL;
536 }
537 if (session->ti_type != NC_TI_UNIX)
538 return NULL;
539
540 return session->path;
541}
542
Michal Vasko8dadf782016-01-15 10:29:36 +0100543API uint16_t
544nc_session_get_port(const struct nc_session *session)
545{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100546 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200547 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100548 return 0;
549 }
550
Michal Vasko8dadf782016-01-15 10:29:36 +0100551 return session->port;
552}
553
Michal Vasko9a25e932016-02-01 10:36:42 +0100554API struct ly_ctx *
555nc_session_get_ctx(const struct nc_session *session)
556{
557 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200558 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100559 return NULL;
560 }
561
562 return session->ctx;
563}
564
Michal Vasko2cc4c682016-03-01 09:16:48 +0100565API void
566nc_session_set_data(struct nc_session *session, void *data)
567{
568 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200569 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100570 return;
571 }
572
573 session->data = data;
574}
575
576API void *
577nc_session_get_data(const struct nc_session *session)
578{
579 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200580 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100581 return NULL;
582 }
583
584 return session->data;
585}
586
Michal Vasko086311b2016-01-08 09:53:11 +0100587NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200588nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200589{
Michal Vasko086311b2016-01-08 09:53:11 +0100590 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100591 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
592 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100593 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100594 }
595
Michal Vasko131120a2018-05-29 15:44:02 +0200596 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100597}
598
Radek Krejci695d4fa2015-10-22 13:23:54 +0200599API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100600nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200601{
Michal Vasko131120a2018-05-29 15:44:02 +0200602 int r, i, rpc_locked = 0, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100603 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100604 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100605 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100606 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100607 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200608 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100609 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200610 void *p;
611
Michal Vasko428087d2016-01-14 16:04:28 +0100612 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200613 return;
614 }
615
Michal Vasko86d357c2016-03-11 13:46:38 +0100616 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200617 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200618 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100619 /* the thread now knows it should quit */
Michal Vasko86d357c2016-03-11 13:46:38 +0100620 }
621
Michal Vasko131120a2018-05-29 15:44:02 +0200622 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
623 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100624 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100625 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200626 } else if (r) {
627 rpc_locked = 1;
628 } /* else failed to lock it, too bad */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200629 }
630
Michal Vasko131120a2018-05-29 15:44:02 +0200631 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200632 /* cleanup message queues */
633 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200634 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100635 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200636
Michal Vaskoad611702015-12-03 13:41:51 +0100637 p = contiter;
638 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200639 free(p);
640 }
641
642 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200643 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100644 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200645
Michal Vaskoad611702015-12-03 13:41:51 +0100646 p = contiter;
647 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200648 free(p);
649 }
650
651 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200652 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200653 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100654 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200655 } else {
656 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vasko131120a2018-05-29 15:44:02 +0200657 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200658 lyd_free(close_rpc);
Michal Vasko131120a2018-05-29 15:44:02 +0200659 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100660 case NC_MSG_REPLY:
661 LY_TREE_FOR(rpl->child, child) {
662 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
663 break;
664 }
665 }
666 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100667 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100668 }
Michal Vaskoad611702015-12-03 13:41:51 +0100669 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100670 break;
671 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100672 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100673 break;
674 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100675 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100676 break;
677 default:
678 /* cannot happen */
679 break;
680 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200681 }
682
683 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200684 if (session->opts.client.cpblts) {
685 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200686 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200687 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200688 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200689 }
690 }
691
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100692 if (session->data && data_free) {
693 data_free(session->data);
694 }
695
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200696 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
697 /* CH LOCK */
698 pthread_mutex_lock(session->opts.server.ch_lock);
699 }
700
Michal Vasko86d357c2016-03-11 13:46:38 +0100701 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200702 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200703
704 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
705 pthread_cond_signal(session->opts.server.ch_cond);
706
707 /* CH UNLOCK */
708 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100709
710 /* wait for CH thread to actually wake up */
711 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
712 while (i && (session->flags & NC_SESSION_CALLHOME)) {
713 usleep(NC_TIMEOUT_STEP);
714 --i;
715 }
716 if (session->flags & NC_SESSION_CALLHOME) {
717 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
718 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200719 }
720
Michal Vasko428087d2016-01-14 16:04:28 +0100721 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200722
723 /* transport implementation cleanup */
724 switch (session->ti_type) {
725 case NC_TI_FD:
726 /* nothing needed - file descriptors were provided by caller,
727 * so it is up to the caller to close them correctly
728 * TODO use callbacks
729 */
Michal Vasko3512e402016-01-28 16:22:34 +0100730 /* just to avoid compiler warning */
731 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100732 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200733 break;
734
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200735 case NC_TI_UNIX:
736 sock = session->ti.unixsock.sock;
737 (void)connected;
738 (void)siter;
739 break;
740
Radek Krejci53691be2016-02-22 13:58:37 +0100741#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200742 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100743 if (connected) {
744 ssh_channel_free(session->ti.libssh.channel);
745 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200746 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
747 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200748 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200749 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200750 /* SESSION IO LOCK */
751 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
752
Michal Vasko96164bf2016-01-21 15:41:58 +0100753 multisession = 0;
754 if (session->ti.libssh.next) {
755 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
756 if (siter->status != NC_STATUS_STARTING) {
757 multisession = 1;
758 break;
759 }
760 }
761 }
762
763 if (!multisession) {
764 /* it's not multisession yet, but we still need to free the starting sessions */
765 if (session->ti.libssh.next) {
766 do {
767 siter = session->ti.libssh.next;
768 session->ti.libssh.next = siter->ti.libssh.next;
769
770 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100771 lydict_remove(session->ctx, session->username);
772 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100773 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100774 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100775 }
776
777 free(siter);
778 } while (session->ti.libssh.next != session);
779 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100780 /* remember sock so we can close it */
781 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100782 if (connected) {
783 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200784 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100785 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200786 ssh_free(session->ti.libssh.session);
787 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200788 /* remove the session from the list */
789 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100790 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200791 /* there will be only one session */
792 siter->ti.libssh.next = NULL;
793 } else {
794 /* there are still multiple sessions, keep the ring list */
795 siter->ti.libssh.next = session->ti.libssh.next;
796 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100797 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
798 if (session->flags & NC_SESSION_SSH_MSG_CB) {
799 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
800 if (siter->ti.libssh.next == session) {
801 ERRINT;
802 break;
803 }
804 }
805 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
806 siter->flags |= NC_SESSION_SSH_MSG_CB;
807 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200808 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200809
810 /* SESSION IO UNLOCK */
811 if (r == 1) {
812 nc_session_io_unlock(session, __func__);
813 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200814 break;
815#endif
816
Radek Krejci53691be2016-02-22 13:58:37 +0100817#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200818 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100819 /* remember sock so we can close it */
820 sock = SSL_get_fd(session->ti.tls);
821
Michal Vasko428087d2016-01-14 16:04:28 +0100822 if (connected) {
823 SSL_shutdown(session->ti.tls);
824 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200825 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100826
Michal Vasko2e6defd2016-10-07 15:48:15 +0200827 if (session->side == NC_SERVER) {
828 X509_free(session->opts.server.client_cert);
829 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200830 break;
831#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100832 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100833 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200834 }
Michal Vasko428087d2016-01-14 16:04:28 +0100835
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100836 /* close socket separately */
837 if (sock > -1) {
838 close(sock);
839 }
840
Radek Krejciac6d3472015-10-22 15:47:18 +0200841 lydict_remove(session->ctx, session->username);
842 lydict_remove(session->ctx, session->host);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200843 lydict_remove(session->ctx, session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200844
845 /* final cleanup */
Michal Vasko131120a2018-05-29 15:44:02 +0200846 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
847 if (rpc_locked) {
848 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100849 }
Michal Vasko131120a2018-05-29 15:44:02 +0200850 pthread_mutex_destroy(session->opts.server.rpc_lock);
851 pthread_cond_destroy(session->opts.server.rpc_cond);
852 free(session->opts.server.rpc_lock);
853 free(session->opts.server.rpc_cond);
854 free((int *)session->opts.server.rpc_inuse);
855 }
856
857 if (session->io_lock && !multisession) {
858 pthread_mutex_destroy(session->io_lock);
859 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200860 }
861
862 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100863 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200864 }
865
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200866 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100867 /* free CH synchronization structures if used */
868 if (session->opts.server.ch_cond) {
869 pthread_cond_destroy(session->opts.server.ch_cond);
870 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200871 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100872 if (session->opts.server.ch_lock) {
873 pthread_mutex_destroy(session->opts.server.ch_lock);
874 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200875 }
876 }
877
Radek Krejci695d4fa2015-10-22 13:23:54 +0200878 free(session);
879}
880
Michal Vasko086311b2016-01-08 09:53:11 +0100881static void
882add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
883{
Radek Krejci658782b2016-12-04 22:04:55 +0100884 size_t len;
885 int i;
886 char *p;
887
888 if (capab) {
889 /* check if already present */
890 p = strchr(capab, '?');
891 if (p) {
892 len = p - capab;
893 } else {
894 len = strlen(capab);
895 }
896 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800897 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100898 /* already present, do not duplicate it */
899 return;
900 }
901 }
902 }
903
904 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100905 if (*count == *size) {
906 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100907 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
908 if (!(*cpblts)) {
909 ERRMEM;
910 return;
911 }
Michal Vasko086311b2016-01-08 09:53:11 +0100912 }
913
914 if (capab) {
915 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
916 } else {
917 (*cpblts)[*count] = NULL;
918 }
919 ++(*count);
920}
921
Michal Vaskodafdc742020-03-11 16:15:59 +0100922static struct lys_feature *
923nc_lys_next_feature(struct lys_feature *last, const struct lys_module *ly_mod, int *idx)
924{
925 uint8_t i;
926
927 assert(ly_mod);
928
929 /* find the (sub)module of the last feature */
930 if (last) {
931 for (i = 0; i < ly_mod->inc_size; ++i) {
932 if (*idx >= ly_mod->inc[i].submodule->features_size) {
933 /* not a feature from this submodule, skip */
934 continue;
935 }
936 if (last != ly_mod->inc[i].submodule->features + *idx) {
937 /* feature is not from this submodule */
938 continue;
939 }
940
941 /* we have found the submodule */
942 break;
943 }
944
945 /* feature not found in submodules, it must be in the main module */
946 assert((i < ly_mod->inc_size) || ((*idx < ly_mod->features_size) && (last == ly_mod->features + *idx)));
947
948 /* we want the next feature */
949 ++(*idx);
950 } else {
951 i = 0;
952 *idx = 0;
953 }
954
955 /* find the (sub)module of the next feature */
956 while ((i < ly_mod->inc_size) && (*idx == ly_mod->inc[i].submodule->features_size)) {
957 /* next submodule */
958 ++i;
959 *idx = 0;
960 }
961
962 /* get the next feature */
963 if (i < ly_mod->inc_size) {
964 last = ly_mod->inc[i].submodule->features + *idx;
965 } else if (*idx < ly_mod->features_size) {
966 last = ly_mod->features + *idx;
967 } else {
968 last = NULL;
969 }
970
971 return last;
972}
973
974
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200975API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200976nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100977{
Michal Vaskod5ada122020-03-19 18:28:06 +0100978 const char **cpblts;
Radek Krejci24a18412018-05-16 15:09:10 +0200979 const struct lys_module *mod, *devmod;
Michal Vaskodafdc742020-03-11 16:15:59 +0100980 struct lys_feature *feat;
Radek Krejci24a18412018-05-16 15:09:10 +0200981 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
982 unsigned int u, v, module_set_id;
983 char *s;
984#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200985 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100986
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200987 if (!ctx) {
988 ERRARG("ctx");
989 return NULL;
990 }
991
Michal Vasko086311b2016-01-08 09:53:11 +0100992 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100993 if (!cpblts) {
994 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200995 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100996 }
Michal Vasko086311b2016-01-08 09:53:11 +0100997 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
998 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
999 count = 2;
1000
1001 /* capabilities */
1002
Radek Krejci3222b7d2017-09-21 16:04:30 +02001003 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001004 if (mod) {
1005 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001006 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001007 }
1008 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001009 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001010 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001011 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001012 }
1013 }
1014 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001015 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001016 }
1017 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001018 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001019 }
1020 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001021 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001022 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001023
1024 /* The URL capability must be set manually using nc_server_set_capability()
1025 * because of the need for supported protocols to be included.
1026 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1027 */
mekleoa8de5e92020-02-13 09:05:56 +01001028 // if (lys_features_state(mod, "url") == 1) {
1029 // add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
1030 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001031
Michal Vasko086311b2016-01-08 09:53:11 +01001032 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001033 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001034 }
1035 }
1036
Radek Krejci3222b7d2017-09-21 16:04:30 +02001037 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001038 if (mod) {
1039 if (!server_opts.wd_basic_mode) {
1040 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
1041 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001042 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001043 switch (server_opts.wd_basic_mode) {
1044 case NC_WD_ALL:
1045 strcat(str, "?basic-mode=report-all");
1046 break;
1047 case NC_WD_TRIM:
1048 strcat(str, "?basic-mode=trim");
1049 break;
1050 case NC_WD_EXPLICIT:
1051 strcat(str, "?basic-mode=explicit");
1052 break;
1053 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001054 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001055 break;
1056 }
1057
1058 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001059 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001060 if (server_opts.wd_also_supported & NC_WD_ALL) {
1061 strcat(str, "report-all,");
1062 }
1063 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1064 strcat(str, "report-all-tagged,");
1065 }
1066 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1067 strcat(str, "trim,");
1068 }
1069 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1070 strcat(str, "explicit,");
1071 }
1072 str[strlen(str) - 1] = '\0';
1073
1074 add_cpblt(ctx, str, &cpblts, &size, &count);
1075 }
1076 }
1077 }
1078
Radek Krejci658782b2016-12-04 22:04:55 +01001079 /* other capabilities */
1080 for (u = 0; u < server_opts.capabilities_count; u++) {
1081 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001082 }
1083
1084 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +02001085 u = module_set_id = 0;
1086 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001087 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vaskod5ada122020-03-19 18:28:06 +01001088 if (!mod->rev_size || (strcmp(mod->rev[0].date, "2016-06-21") && strcmp(mod->rev[0].date, "2019-01-04"))) {
1089 ERR("Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
1090 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001091 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001092
Michal Vaskod5ada122020-03-19 18:28:06 +01001093 if (!strcmp(mod->rev[0].date, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001094 /* new one (capab defined in RFC 8526 section 2) */
Michal Vaskod5ada122020-03-19 18:28:06 +01001095 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%u",
1096 mod->rev[0].date, ly_ctx_get_module_set_id(ctx));
1097 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001098 } else {
1099 /* old one (capab defined in RFC 7950 section 5.6.4) */
1100 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%u",
1101 mod->rev[0].date, ly_ctx_get_module_set_id(ctx));
1102 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001103 }
Radek Krejci24a18412018-05-16 15:09:10 +02001104 continue;
1105 } else if (mod->type) {
1106 /* skip submodules */
1107 continue;
1108 } else if (version == LYS_VERSION_1 && mod->version > version) {
1109 /* skip YANG 1.1 schemas */
1110 continue;
1111 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
1112 /* skip YANG 1.0 schemas */
1113 continue;
1114 }
Michal Vasko086311b2016-01-08 09:53:11 +01001115
Radek Krejci24a18412018-05-16 15:09:10 +02001116 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
1117 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
1118
Michal Vaskodafdc742020-03-11 16:15:59 +01001119 features_count = 0;
1120 i = 0;
1121 feat = NULL;
1122 while ((feat = nc_lys_next_feature(feat, mod, &i))) {
1123 if (!(feat->flags & LYS_FENABLED)) {
1124 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001125 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001126 if (!features_count) {
1127 strcat(str, "&features=");
1128 str_len += 10;
1129 }
1130 len = strlen(feat->name);
1131 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1132 ERRINT;
1133 break;
1134 }
1135 if (features_count) {
1136 strcat(str, ",");
1137 ++str_len;
1138 }
1139 strcat(str, feat->name);
1140 str_len += len;
1141 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001142 }
Michal Vasko086311b2016-01-08 09:53:11 +01001143
Radek Krejci24a18412018-05-16 15:09:10 +02001144 if (mod->deviated) {
1145 strcat(str, "&deviations=");
1146 str_len += 12;
1147 dev_count = 0;
Frank Rimplercb9274f2019-03-13 16:40:34 +00001148 v = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001149 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
1150 if (devmod == mod) {
1151 continue;
1152 }
1153
1154 for (i = 0; i < devmod->deviation_size; ++i) {
1155 s = strstr(devmod->deviation[i].target_name, mod->name);
1156 if (s && s[strlen(mod->name)] == ':') {
1157 /* we have the module deviating the module being processed */
1158 len = strlen(devmod->name);
1159 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1160 ERRINT;
1161 break;
1162 }
1163 if (dev_count) {
1164 strcat(str, ",");
1165 ++str_len;
1166 }
1167 strcat(str, devmod->name);
1168 str_len += len;
1169 dev_count++;
1170
1171 break;
1172 }
1173 }
1174 }
1175 }
1176
1177 add_cpblt(ctx, str, &cpblts, &size, &count);
1178 }
Michal Vasko086311b2016-01-08 09:53:11 +01001179
1180 /* ending NULL capability */
1181 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1182
1183 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001184
1185error:
Radek Krejcif906e412017-09-22 14:44:45 +02001186 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001187 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001188}
1189
Radek Krejci24a18412018-05-16 15:09:10 +02001190API const char **
1191nc_server_get_cpblts(struct ly_ctx *ctx)
1192{
1193 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1194}
1195
Radek Krejci695d4fa2015-10-22 13:23:54 +02001196static int
Michal Vasko2cb24b42017-05-23 14:59:11 +02001197parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001198{
1199 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001200 int ver = -1, i = 0;
1201 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001202
1203 if (list) {
1204 /* get the storage for server's capabilities */
1205 LY_TREE_FOR(xml->child, cpblt) {
1206 i++;
1207 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001208 /* last item remains NULL */
1209 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001210 if (!*list) {
1211 ERRMEM;
1212 return -1;
1213 }
1214 i = 0;
1215 }
1216
1217 LY_TREE_FOR(xml->child, cpblt) {
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001218 if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
Radek Krejci695d4fa2015-10-22 13:23:54 +02001219 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1220 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1221 return -1;
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001222 } else if (!cpblt->name || !cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001223 continue;
1224 }
1225
Michal Vasko156d3272017-04-11 11:46:49 +02001226 /* skip leading/trailing whitespaces */
1227 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1228 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1229 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1230 ERR("Empty capability \"%s\" received.", cpblt->content);
1231 return -1;
1232 }
1233
Radek Krejci695d4fa2015-10-22 13:23:54 +02001234 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001235 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001236 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001237 } 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 +02001238 ver = 1;
1239 }
1240
1241 /* store capabilities */
1242 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001243 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1244 if (!(*list)[i]) {
1245 ERRMEM;
1246 return -1;
1247 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001248 i++;
1249 }
1250 }
1251
1252 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001253 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001254 }
1255
1256 return ver;
1257}
1258
1259static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001260nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001261{
Michal Vasko131120a2018-05-29 15:44:02 +02001262 NC_MSG_TYPE ret;
1263 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001264 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001265 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001266
Michal Vasko131120a2018-05-29 15:44:02 +02001267 if (session->side == NC_CLIENT) {
1268 /* client side hello - send only NETCONF base capabilities */
1269 cpblts = malloc(3 * sizeof *cpblts);
1270 if (!cpblts) {
1271 ERRMEM;
1272 return NC_MSG_ERROR;
1273 }
1274 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1275 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1276 cpblts[2] = NULL;
1277
1278 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1279 sid = NULL;
1280 } else {
1281 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
1282
1283 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1284 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001285 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001286
Michal Vasko131120a2018-05-29 15:44:02 +02001287 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001288
Michal Vasko086311b2016-01-08 09:53:11 +01001289 for (i = 0; cpblts[i]; ++i) {
1290 lydict_remove(session->ctx, cpblts[i]);
1291 }
1292 free(cpblts);
1293
Michal Vasko131120a2018-05-29 15:44:02 +02001294 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001295}
1296
1297static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001298nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001299{
1300 struct lyxml_elem *xml = NULL, *node;
Michal Vasko131120a2018-05-29 15:44:02 +02001301 NC_MSG_TYPE msgtype;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001302 int ver = -1;
1303 char *str;
1304 long long int id;
1305 int flag = 0;
1306
Michal Vasko131120a2018-05-29 15:44:02 +02001307 msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001308
Michal Vasko131120a2018-05-29 15:44:02 +02001309 switch (msgtype) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001310 case NC_MSG_HELLO:
1311 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001312 LY_TREE_FOR(xml->child, node) {
1313 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1314 continue;
1315 } else if (!strcmp(node->name, "session-id")) {
1316 if (!node->content || !strlen(node->content)) {
1317 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001318 goto error;
1319 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001320 str = NULL;
1321 id = strtoll(node->content, &str, 10);
1322 if (*str || id < 1 || id > UINT32_MAX) {
1323 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001324 goto error;
1325 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001326 session->id = (uint32_t)id;
1327 continue;
1328 } else if (strcmp(node->name, "capabilities")) {
1329 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001330 goto error;
1331 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001332
1333 if (flag) {
1334 /* multiple capabilities elements */
1335 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1336 goto error;
1337 }
1338 flag = 1;
1339
Michal Vasko2e6defd2016-10-07 15:48:15 +02001340 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001341 goto error;
1342 }
1343 session->version = ver;
1344 }
1345
1346 if (!session->id) {
1347 ERR("Missing <session-id> in server's <hello>.");
1348 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001349 }
1350 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001351 case NC_MSG_WOULDBLOCK:
1352 ERR("Server's <hello> timeout elapsed.");
1353 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001354 case NC_MSG_ERROR:
1355 /* nothing special, just pass it out */
1356 break;
1357 default:
1358 ERR("Unexpected message received instead of <hello>.");
1359 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001360 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001361 }
1362
1363 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001364 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001365
1366 return msgtype;
1367
1368error:
1369 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001370 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001371
1372 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001373}
1374
Michal Vasko11d142a2016-01-19 15:58:24 +01001375static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001376nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001377{
1378 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001379 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001380 int ver = -1;
1381 int flag = 0;
1382
Michal Vasko131120a2018-05-29 15:44:02 +02001383 msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ?
1384 server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001385
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001386 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001387 case NC_MSG_HELLO:
1388 /* get know NETCONF version */
1389 LY_TREE_FOR(xml->child, node) {
1390 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1391 continue;
1392 } else if (strcmp(node->name, "capabilities")) {
1393 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001394 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001395 goto cleanup;
1396 }
1397
1398 if (flag) {
1399 /* multiple capabilities elements */
1400 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001401 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001402 goto cleanup;
1403 }
1404 flag = 1;
1405
1406 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001407 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001408 goto cleanup;
1409 }
1410 session->version = ver;
1411 }
1412 break;
1413 case NC_MSG_ERROR:
1414 /* nothing special, just pass it out */
1415 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001416 case NC_MSG_WOULDBLOCK:
1417 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001418 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001419 default:
1420 ERR("Unexpected message received instead of <hello>.");
1421 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001422 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001423 }
1424
1425cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001426 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001427 return msgtype;
1428}
1429
Michal Vasko71090fc2016-05-24 16:37:28 +02001430NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001431nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001432{
Michal Vasko086311b2016-01-08 09:53:11 +01001433 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001434
Michal Vasko131120a2018-05-29 15:44:02 +02001435 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001436 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001437 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001438 }
1439
Michal Vasko11d142a2016-01-19 15:58:24 +01001440 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001441 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001442 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001443 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001444 }
1445
Michal Vasko71090fc2016-05-24 16:37:28 +02001446 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001447}
Michal Vasko086311b2016-01-08 09:53:11 +01001448
Michal Vasko889162e2019-09-06 14:43:37 +02001449#ifdef NC_ENABLED_SSH
1450
Michal Vasko999b64a2019-07-10 16:06:15 +02001451static void
1452nc_ssh_init(void)
1453{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001454#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001455 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1456 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001457#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001458}
1459
1460static void
1461nc_ssh_destroy(void)
1462{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001463#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001464 FIPS_mode_set(0);
1465 CONF_modules_unload(1);
1466 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001467#endif
1468
Michal Vaskoe1e82632019-09-09 09:18:03 +02001469#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1470 ssh_finalize();
1471#endif
1472}
1473
Michal Vasko889162e2019-09-06 14:43:37 +02001474#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001475
Radek Krejci53691be2016-02-22 13:58:37 +01001476#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001477
Michal Vasko770b4362017-02-17 14:44:18 +01001478#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1479
Michal Vaskof0c92c02016-01-29 09:41:45 +01001480struct CRYPTO_dynlock_value {
1481 pthread_mutex_t lock;
1482};
1483
Michal Vaskof0c92c02016-01-29 09:41:45 +01001484static struct CRYPTO_dynlock_value *
1485tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1486{
1487 struct CRYPTO_dynlock_value *value;
1488
1489 value = malloc(sizeof *value);
1490 if (!value) {
1491 ERRMEM;
1492 return NULL;
1493 }
1494 pthread_mutex_init(&value->lock, NULL);
1495
1496 return value;
1497}
1498
1499static void
1500tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1501{
1502 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1503 * I found ignored this fact, what do I know... */
1504 if (mode & CRYPTO_LOCK) {
1505 pthread_mutex_lock(&l->lock);
1506 } else {
1507 pthread_mutex_unlock(&l->lock);
1508 }
1509}
1510
1511static void
1512tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1513{
1514 pthread_mutex_destroy(&l->lock);
1515 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001516}
Michal Vasko770b4362017-02-17 14:44:18 +01001517#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001518
Michal Vasko8f0c0282016-02-29 10:17:14 +01001519#endif /* NC_ENABLED_TLS */
1520
1521#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1522
Michal Vasko770b4362017-02-17 14:44:18 +01001523#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001524static pthread_mutex_t *tls_locks;
1525
1526static void
1527tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1528{
1529 if (mode & CRYPTO_LOCK) {
1530 pthread_mutex_lock(tls_locks + n);
1531 } else {
1532 pthread_mutex_unlock(tls_locks + n);
1533 }
1534}
1535
1536static void
1537tls_thread_id_func(CRYPTO_THREADID *tid)
1538{
1539 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1540}
Michal Vasko770b4362017-02-17 14:44:18 +01001541#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001542
1543static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001544nc_tls_init(void)
1545{
Rosen Penev4f552d62019-06-26 16:10:43 -07001546#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001547 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001548 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001549 SSL_library_init();
1550
Michal Vaskof89948c2018-01-04 09:19:46 +01001551 int i;
1552
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001553 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001554 if (!tls_locks) {
1555 ERRMEM;
1556 return;
1557 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001558 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1559 pthread_mutex_init(tls_locks + i, NULL);
1560 }
1561
Michal Vaskof0c92c02016-01-29 09:41:45 +01001562 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001563 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001564
1565 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1566 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1567 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001568#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001569}
1570
Michal Vasko8f0c0282016-02-29 10:17:14 +01001571static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001572nc_tls_destroy(void)
1573{
Rosen Penev4f552d62019-06-26 16:10:43 -07001574#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001575 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001576 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001577 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001578 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001579 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001580#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001581 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001582#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1583 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001584#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001585
Michal Vaskof89948c2018-01-04 09:19:46 +01001586 int i;
1587
Michal Vaskob6e37262016-02-25 14:49:00 +01001588 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001589 CRYPTO_set_locking_callback(NULL);
1590 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1591 pthread_mutex_destroy(tls_locks + i);
1592 }
1593 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001594
1595 CRYPTO_set_dynlock_create_callback(NULL);
1596 CRYPTO_set_dynlock_lock_callback(NULL);
1597 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001598#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001599}
1600
Michal Vasko8f0c0282016-02-29 10:17:14 +01001601#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001602
Radek Krejci53691be2016-02-22 13:58:37 +01001603#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001604
Michal Vasko8f0c0282016-02-29 10:17:14 +01001605static void
Michal Vasko5e228792016-02-03 15:30:24 +01001606nc_ssh_tls_init(void)
1607{
Rosen Penev4f552d62019-06-26 16:10:43 -07001608#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001609 SSL_load_error_strings();
1610 ERR_load_BIO_strings();
1611 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001612#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001613
1614 nc_ssh_init();
1615
Michal Vaskoe1e82632019-09-09 09:18:03 +02001616#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001617 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1618 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1619 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001620#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001621}
1622
Michal Vasko8f0c0282016-02-29 10:17:14 +01001623static void
Michal Vasko5e228792016-02-03 15:30:24 +01001624nc_ssh_tls_destroy(void)
1625{
Rosen Penev4f552d62019-06-26 16:10:43 -07001626#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001627 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001628# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001629 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001630# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001631 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001632# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001633#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001634
1635 nc_ssh_destroy();
1636
Michal Vaskoe1e82632019-09-09 09:18:03 +02001637#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001638 CRYPTO_set_dynlock_create_callback(NULL);
1639 CRYPTO_set_dynlock_lock_callback(NULL);
1640 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001641#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001642}
1643
Radek Krejci53691be2016-02-22 13:58:37 +01001644#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001645
1646#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1647
1648API void
1649nc_thread_destroy(void)
1650{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001651 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1652 //CRYPTO_cleanup_all_ex_data();
1653
Michal Vasko770b4362017-02-17 14:44:18 +01001654#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1655 CRYPTO_THREADID crypto_tid;
1656
Michal Vasko8f0c0282016-02-29 10:17:14 +01001657 CRYPTO_THREADID_current(&crypto_tid);
1658 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001659#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001660}
1661
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001662#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1663
1664void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001665nc_init(void)
1666{
1667#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1668 nc_ssh_tls_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001669#elif defined(NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001670 nc_ssh_init();
1671#elif defined(NC_ENABLED_TLS)
1672 nc_tls_init();
1673#endif
1674}
1675
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001676void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001677nc_destroy(void)
1678{
1679#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1680 nc_ssh_tls_destroy();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001681#elif defined(NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001682 nc_ssh_destroy();
1683#elif defined(NC_ENABLED_TLS)
1684 nc_tls_destroy();
1685#endif
1686}