blob: 5aa1bda879c41ea1f475f1a5d9c1ec98e63a4165 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
Michal Vaskob85767d2018-03-21 12:21:10 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02007 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
Michal Vaskob85767d2018-03-21 12:21:10 +010014#define _DEFAULT_SOURCE
Radek Krejci206fcd62015-10-07 15:42:48 +020015
Michal Vasko18aeb5d2017-02-17 09:23:56 +010016#include <assert.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020017#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010018#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010019#include <string.h>
Michal Vaskob85767d2018-03-21 12:21:10 +010020#include <unistd.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020021#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020022#include <sys/time.h>
Michal Vaskobe52dc22018-10-17 09:28:17 +020023#include <sys/types.h>
24#include <sys/socket.h>
25#include <netinet/in.h>
26#include <netinet/tcp.h>
Michal Vasko58f31552016-01-19 12:39:15 +010027#include <time.h>
Michal Vasko156d3272017-04-11 11:46:49 +020028#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020029#include <libyang/libyang.h>
30
Michal Vasko5e228792016-02-03 15:30:24 +010031#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010032#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010033#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010034
Radek Krejci53691be2016-02-22 13:58:37 +010035#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020036
Michal Vasko086311b2016-01-08 09:53:11 +010037# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020038
Radek Krejci53691be2016-02-22 13:58:37 +010039#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020040
Radek Krejci53691be2016-02-22 13:58:37 +010041#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010042
Michal Vasko5e228792016-02-03 15:30:24 +010043# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044# include <openssl/err.h>
45
Radek Krejci53691be2016-02-22 13:58:37 +010046#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010047
Michal Vasko086311b2016-01-08 09:53:11 +010048/* in seconds */
49#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080050#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020051
Michal Vasko05ba9df2016-01-13 14:40:27 +010052/* in milliseconds */
53#define NC_CLOSE_REPLY_TIMEOUT 200
54
Michal Vasko086311b2016-01-08 09:53:11 +010055extern struct nc_server_opts server_opts;
56
Radek Krejci7ac16052016-07-15 11:48:18 +020057int
Michal Vasko77a6abe2017-10-05 10:02:20 +020058nc_gettimespec_mono(struct timespec *ts)
59{
60#ifdef CLOCK_MONOTONIC_RAW
61 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
62#elif defined(CLOCK_MONOTONIC)
63 return clock_gettime(CLOCK_MONOTONIC, ts);
64#else
65 /* no monotonic clock available, return realtime */
66 return nc_gettimespec_real(ts);
67#endif
68}
69
70int
71nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020072{
Michal Vasko0ef46df2016-09-21 14:03:18 +020073#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020074 return clock_gettime(CLOCK_REALTIME, ts);
75#else
76 int rc;
77 struct timeval tv;
78
79 rc = gettimeofday(&tv, NULL);
80 if (!rc) {
81 ts->tv_sec = (time_t)tv.tv_sec;
82 ts->tv_nsec = 1000L * (long)tv.tv_usec;
83 }
84 return rc;
85#endif
86}
87
Michal Vasko36c7be82017-02-22 13:37:59 +010088/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
89int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020090nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010091{
Michal Vasko36c7be82017-02-22 13:37:59 +010092 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010093
Michal Vaskoa82bd202017-03-03 14:07:29 +010094 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
95 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010096
97 return (nsec_diff ? nsec_diff / 1000000L : 0);
98}
99
Michal Vasko36c7be82017-02-22 13:37:59 +0100100void
101nc_addtimespec(struct timespec *ts, uint32_t msec)
102{
Michal Vasko81c5b302017-03-15 12:10:40 +0100103 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
104
Michal Vasko0dfcd332017-03-03 13:14:39 +0100105 ts->tv_sec += msec / 1000;
106 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100107
Michal Vasko81c5b302017-03-15 12:10:40 +0100108 if (ts->tv_nsec >= 1000000000L) {
109 ++ts->tv_sec;
110 ts->tv_nsec -= 1000000000L;
111 } else if (ts->tv_nsec < 0) {
112 --ts->tv_sec;
113 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100114 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100115
116 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100117}
118
Michal Vaskoddce1212019-05-24 09:58:49 +0200119const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200120nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200121{
122 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200123 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200124 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200125 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200126 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200127 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200128 return "EC";
129 default:
130 break;
131 }
132
133 return NULL;
134}
135
Michal Vaskobe52dc22018-10-17 09:28:17 +0200136int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200137nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200138{
139 int opt;
140
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200141 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200142 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
143 ERR("Could not set SO_KEEPALIVE option (%s).", strerror(errno));
144 return -1;
145 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200146 if (!ka->enabled) {
147 return 0;
148 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200149
150#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200151 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200152 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
153 ERR("Setsockopt failed (%s).", strerror(errno));
154 return -1;
155 }
156#endif
157
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200158#ifdef TCP_KEEPCNT
159 opt = ka->max_probes;
160 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vaskobe52dc22018-10-17 09:28:17 +0200161 ERR("Setsockopt failed (%s).", strerror(errno));
162 return -1;
163 }
164#endif
165
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200166#ifdef TCP_KEEPINTVL
167 opt = ka->probe_interval;
168 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vaskobe52dc22018-10-17 09:28:17 +0200169 ERR("Setsockopt failed (%s).", strerror(errno));
170 return -1;
171 }
172#endif
173
174 return 0;
175}
176
Radek Krejci28472922016-07-15 11:51:16 +0200177#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
178int
179pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
180{
Michal Vasko81c5b302017-03-15 12:10:40 +0100181 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200182 int rc;
183 struct timespec cur, dur;
184
185 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
186 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
Michal Vasko145619f2018-02-16 10:08:45 +0100187 nc_gettimespec_real(&cur);
Radek Krejci28472922016-07-15 11:51:16 +0200188
Michal Vasko81c5b302017-03-15 12:10:40 +0100189 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
190 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200191 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100192 } else if (diff < 5) {
193 /* sleep until timeout */
Michal Vaskobf378cb2018-09-21 15:18:52 +0200194 dur.tv_sec = 0;
195 dur.tv_nsec = (long)diff * 1000000;
Michal Vasko81c5b302017-03-15 12:10:40 +0100196 } else {
197 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200198 dur.tv_sec = 0;
199 dur.tv_nsec = 5000000;
200 }
201
202 nanosleep(&dur, NULL);
203 }
204
205 return rc;
206}
207#endif
208
Michal Vaskoade892d2017-02-22 13:40:35 +0100209struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200210nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100211{
212 struct nc_session *sess;
213
214 sess = calloc(1, sizeof *sess);
215 if (!sess) {
216 return NULL;
217 }
218
Michal Vasko131120a2018-05-29 15:44:02 +0200219 sess->side = side;
220
221 if (side == NC_SERVER) {
222 sess->opts.server.rpc_lock = malloc(sizeof *sess->opts.server.rpc_lock);
223 sess->opts.server.rpc_cond = malloc(sizeof *sess->opts.server.rpc_cond);
224 sess->opts.server.rpc_inuse = malloc(sizeof *sess->opts.server.rpc_inuse);
225 if (!sess->opts.server.rpc_lock || !sess->opts.server.rpc_cond || !sess->opts.server.rpc_inuse) {
226 goto error;
Michal Vaskoade892d2017-02-22 13:40:35 +0100227 }
Michal Vasko131120a2018-05-29 15:44:02 +0200228 pthread_mutex_init(sess->opts.server.rpc_lock, NULL);
229 pthread_cond_init(sess->opts.server.rpc_cond, NULL);
230 *sess->opts.server.rpc_inuse = 0;
231 }
232
233 if (!shared_ti) {
234 sess->io_lock = malloc(sizeof *sess->io_lock);
235 if (!sess->io_lock) {
236 goto error;
237 }
238 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100239 }
240
241 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200242
243error:
244 if (side == NC_SERVER) {
245 free(sess->opts.server.rpc_lock);
246 free(sess->opts.server.rpc_cond);
247 free((int *)sess->opts.server.rpc_inuse);
248 }
249 free(sess);
250 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100251}
252
Michal Vasko96164bf2016-01-21 15:41:58 +0100253/*
254 * @return 1 - success
255 * 0 - timeout
256 * -1 - error
257 */
258int
Michal Vasko131120a2018-05-29 15:44:02 +0200259nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100260{
261 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100262 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100263
Michal Vasko131120a2018-05-29 15:44:02 +0200264 if (session->side != NC_SERVER) {
265 ERRINT;
266 return -1;
267 }
268
Michal Vasko96164bf2016-01-21 15:41:58 +0100269 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200270 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100271 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100272
Michal Vaskoade892d2017-02-22 13:40:35 +0100273 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200274 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100275 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200276 while (*session->opts.server.rpc_inuse) {
277 ret = pthread_cond_timedwait(session->opts.server.rpc_cond, session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100278 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200279 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100280 break;
281 }
282 }
283 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100284 } else if (!timeout) {
Michal Vasko131120a2018-05-29 15:44:02 +0200285 if (*session->opts.server.rpc_inuse) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100286 /* immediate timeout */
287 return 0;
288 }
289
290 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200291 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100292 if (!ret) {
293 /* be extra careful, someone could have been faster */
Michal Vasko131120a2018-05-29 15:44:02 +0200294 if (*session->opts.server.rpc_inuse) {
295 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100296 return 0;
297 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200298 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100299 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100300 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200301 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100302 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200303 while (*session->opts.server.rpc_inuse) {
304 ret = pthread_cond_wait(session->opts.server.rpc_cond, session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100305 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200306 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100307 break;
308 }
309 }
310 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100311 }
312
Michal Vaskoade892d2017-02-22 13:40:35 +0100313 if (ret) {
314 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
315 /* timeout */
316 return 0;
317 }
318
Michal Vasko96164bf2016-01-21 15:41:58 +0100319 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200320 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100321 return -1;
322 }
323
324 /* ok */
Michal Vasko131120a2018-05-29 15:44:02 +0200325 assert(*session->opts.server.rpc_inuse == 0);
326 *session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100327
328 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200329 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100330 if (ret) {
331 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200332 ERR("%s: faile to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100333 return -1;
334 }
335
336 return 1;
337}
338
339int
Michal Vasko131120a2018-05-29 15:44:02 +0200340nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100341{
342 int ret;
343 struct timespec ts_timeout;
344
Michal Vasko131120a2018-05-29 15:44:02 +0200345 if (session->side != NC_SERVER) {
346 ERRINT;
347 return -1;
348 }
349
350 assert(*session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100351
352 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200353 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100354 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100355
356 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200357 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100358 } else if (!timeout) {
359 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200360 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100361 } else { /* timeout == -1 */
362 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200363 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100364 }
365
366 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
367 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200368 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100369 return -1;
370 } else if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200371 WRN("%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100372 }
373
Michal Vasko131120a2018-05-29 15:44:02 +0200374 *session->opts.server.rpc_inuse = 0;
375 pthread_cond_signal(session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100376
377 if (!ret) {
378 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200379 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100380 if (ret) {
381 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200382 ERR("%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100383 return -1;
384 }
385 }
386
Michal Vasko96164bf2016-01-21 15:41:58 +0100387 return 1;
388}
389
Michal Vasko131120a2018-05-29 15:44:02 +0200390/*
391 * @return 1 - success
392 * 0 - timeout
393 * -1 - error
394 */
395int
396nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
397{
398 int ret;
399 struct timespec ts_timeout;
400
401 if (timeout > 0) {
402 nc_gettimespec_real(&ts_timeout);
403 nc_addtimespec(&ts_timeout, timeout);
404
405 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
406 } else if (!timeout) {
407 ret = pthread_mutex_trylock(session->io_lock);
408 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200409 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200410 }
411
412 if (ret) {
413 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
414 /* timeout */
415 return 0;
416 }
417
418 /* error */
419 ERR("%s: failed to IO lock a session (%s).", func, strerror(ret));
420 return -1;
421 }
422
423 return 1;
424}
425
426int
427nc_session_io_unlock(struct nc_session *session, const char *func)
428{
429 int ret;
430
431 ret = pthread_mutex_unlock(session->io_lock);
432 if (ret) {
433 /* error */
434 ERR("%s: failed to IO unlock a session (%s).", func, strerror(ret));
435 return -1;
436 }
437
438 return 1;
439}
440
Michal Vasko8dadf782016-01-15 10:29:36 +0100441API NC_STATUS
442nc_session_get_status(const struct nc_session *session)
443{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100444 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200445 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200446 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100447 }
448
Michal Vasko8dadf782016-01-15 10:29:36 +0100449 return session->status;
450}
451
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100452API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200453nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100454{
455 if (!session) {
456 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200457 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100458 }
459
460 return session->term_reason;
461}
462
Michal Vasko8dadf782016-01-15 10:29:36 +0100463API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200464nc_session_get_killed_by(const struct nc_session *session)
465{
466 if (!session) {
467 ERRARG("session");
468 return 0;
469 }
470
471 return session->killed_by;
472}
473
474API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100475nc_session_get_id(const struct nc_session *session)
476{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100477 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200478 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100479 return 0;
480 }
481
Michal Vasko8dadf782016-01-15 10:29:36 +0100482 return session->id;
483}
484
Michal Vasko174fe8e2016-02-17 15:38:09 +0100485API int
486nc_session_get_version(const struct nc_session *session)
487{
488 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200489 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100490 return -1;
491 }
492
493 return (session->version == NC_VERSION_10 ? 0 : 1);
494}
495
Michal Vasko8dadf782016-01-15 10:29:36 +0100496API NC_TRANSPORT_IMPL
497nc_session_get_ti(const struct nc_session *session)
498{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100499 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200500 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100501 return 0;
502 }
503
Michal Vasko8dadf782016-01-15 10:29:36 +0100504 return session->ti_type;
505}
506
507API const char *
508nc_session_get_username(const struct nc_session *session)
509{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100510 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200511 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100512 return NULL;
513 }
514
Michal Vasko8dadf782016-01-15 10:29:36 +0100515 return session->username;
516}
517
518API const char *
519nc_session_get_host(const struct nc_session *session)
520{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100521 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200522 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100523 return NULL;
524 }
525
Michal Vasko8dadf782016-01-15 10:29:36 +0100526 return session->host;
527}
528
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200529API const char *
530nc_session_get_path(const struct nc_session *session)
531{
532 if (!session) {
533 ERRARG("session");
534 return NULL;
535 }
536 if (session->ti_type != NC_TI_UNIX)
537 return NULL;
538
539 return session->path;
540}
541
Michal Vasko8dadf782016-01-15 10:29:36 +0100542API uint16_t
543nc_session_get_port(const struct nc_session *session)
544{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100545 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200546 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100547 return 0;
548 }
549
Michal Vasko8dadf782016-01-15 10:29:36 +0100550 return session->port;
551}
552
Michal Vasko9a25e932016-02-01 10:36:42 +0100553API struct ly_ctx *
554nc_session_get_ctx(const struct nc_session *session)
555{
556 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200557 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100558 return NULL;
559 }
560
561 return session->ctx;
562}
563
Michal Vasko2cc4c682016-03-01 09:16:48 +0100564API void
565nc_session_set_data(struct nc_session *session, void *data)
566{
567 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200568 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100569 return;
570 }
571
572 session->data = data;
573}
574
575API void *
576nc_session_get_data(const struct nc_session *session)
577{
578 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200579 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100580 return NULL;
581 }
582
583 return session->data;
584}
585
Michal Vasko086311b2016-01-08 09:53:11 +0100586NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200587nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200588{
Michal Vasko086311b2016-01-08 09:53:11 +0100589 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100590 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
591 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100592 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100593 }
594
Michal Vasko131120a2018-05-29 15:44:02 +0200595 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100596}
597
Radek Krejci695d4fa2015-10-22 13:23:54 +0200598API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100599nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200600{
Michal Vasko131120a2018-05-29 15:44:02 +0200601 int r, i, rpc_locked = 0, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100602 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100603 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100604 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100605 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100606 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200607 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100608 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200609 void *p;
610
Michal Vasko428087d2016-01-14 16:04:28 +0100611 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200612 return;
613 }
614
Michal Vasko86d357c2016-03-11 13:46:38 +0100615 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200616 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200617 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100618 /* the thread now knows it should quit */
Michal Vasko86d357c2016-03-11 13:46:38 +0100619 }
620
Michal Vasko131120a2018-05-29 15:44:02 +0200621 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
622 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100623 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100624 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200625 } else if (r) {
626 rpc_locked = 1;
627 } /* else failed to lock it, too bad */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200628 }
629
Michal Vasko131120a2018-05-29 15:44:02 +0200630 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200631 /* cleanup message queues */
632 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200633 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100634 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200635
Michal Vaskoad611702015-12-03 13:41:51 +0100636 p = contiter;
637 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200638 free(p);
639 }
640
641 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200642 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100643 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200644
Michal Vaskoad611702015-12-03 13:41:51 +0100645 p = contiter;
646 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200647 free(p);
648 }
649
650 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200651 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200652 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100653 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200654 } else {
655 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vasko131120a2018-05-29 15:44:02 +0200656 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200657 lyd_free(close_rpc);
Michal Vasko131120a2018-05-29 15:44:02 +0200658 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100659 case NC_MSG_REPLY:
660 LY_TREE_FOR(rpl->child, child) {
661 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
662 break;
663 }
664 }
665 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100666 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100667 }
Michal Vaskoad611702015-12-03 13:41:51 +0100668 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100669 break;
670 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100671 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100672 break;
673 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100674 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100675 break;
676 default:
677 /* cannot happen */
678 break;
679 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200680 }
681
682 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200683 if (session->opts.client.cpblts) {
684 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200685 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200686 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200687 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200688 }
689 }
690
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100691 if (session->data && data_free) {
692 data_free(session->data);
693 }
694
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200695 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
696 /* CH LOCK */
697 pthread_mutex_lock(session->opts.server.ch_lock);
698 }
699
Michal Vasko86d357c2016-03-11 13:46:38 +0100700 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200701 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200702
703 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
704 pthread_cond_signal(session->opts.server.ch_cond);
705
706 /* CH UNLOCK */
707 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100708
709 /* wait for CH thread to actually wake up */
710 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
711 while (i && (session->flags & NC_SESSION_CALLHOME)) {
712 usleep(NC_TIMEOUT_STEP);
713 --i;
714 }
715 if (session->flags & NC_SESSION_CALLHOME) {
716 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
717 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200718 }
719
Michal Vasko428087d2016-01-14 16:04:28 +0100720 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200721
722 /* transport implementation cleanup */
723 switch (session->ti_type) {
724 case NC_TI_FD:
725 /* nothing needed - file descriptors were provided by caller,
726 * so it is up to the caller to close them correctly
727 * TODO use callbacks
728 */
Michal Vasko3512e402016-01-28 16:22:34 +0100729 /* just to avoid compiler warning */
730 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100731 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200732 break;
733
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200734 case NC_TI_UNIX:
735 sock = session->ti.unixsock.sock;
736 (void)connected;
737 (void)siter;
738 break;
739
Radek Krejci53691be2016-02-22 13:58:37 +0100740#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200741 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100742 if (connected) {
743 ssh_channel_free(session->ti.libssh.channel);
744 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200745 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
746 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200747 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200748 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200749 /* SESSION IO LOCK */
750 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
751
Michal Vasko96164bf2016-01-21 15:41:58 +0100752 multisession = 0;
753 if (session->ti.libssh.next) {
754 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
755 if (siter->status != NC_STATUS_STARTING) {
756 multisession = 1;
757 break;
758 }
759 }
760 }
761
762 if (!multisession) {
763 /* it's not multisession yet, but we still need to free the starting sessions */
764 if (session->ti.libssh.next) {
765 do {
766 siter = session->ti.libssh.next;
767 session->ti.libssh.next = siter->ti.libssh.next;
768
769 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100770 lydict_remove(session->ctx, session->username);
771 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100772 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100773 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100774 }
775
776 free(siter);
777 } while (session->ti.libssh.next != session);
778 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100779 /* remember sock so we can close it */
780 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100781 if (connected) {
782 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200783 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100784 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200785 ssh_free(session->ti.libssh.session);
786 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200787 /* remove the session from the list */
788 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100789 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200790 /* there will be only one session */
791 siter->ti.libssh.next = NULL;
792 } else {
793 /* there are still multiple sessions, keep the ring list */
794 siter->ti.libssh.next = session->ti.libssh.next;
795 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100796 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
797 if (session->flags & NC_SESSION_SSH_MSG_CB) {
798 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
799 if (siter->ti.libssh.next == session) {
800 ERRINT;
801 break;
802 }
803 }
804 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
805 siter->flags |= NC_SESSION_SSH_MSG_CB;
806 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200807 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200808
809 /* SESSION IO UNLOCK */
810 if (r == 1) {
811 nc_session_io_unlock(session, __func__);
812 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200813 break;
814#endif
815
Radek Krejci53691be2016-02-22 13:58:37 +0100816#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200817 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100818 /* remember sock so we can close it */
819 sock = SSL_get_fd(session->ti.tls);
820
Michal Vasko428087d2016-01-14 16:04:28 +0100821 if (connected) {
822 SSL_shutdown(session->ti.tls);
823 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200824 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100825
Michal Vasko2e6defd2016-10-07 15:48:15 +0200826 if (session->side == NC_SERVER) {
827 X509_free(session->opts.server.client_cert);
828 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200829 break;
830#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100831 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100832 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200833 }
Michal Vasko428087d2016-01-14 16:04:28 +0100834
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100835 /* close socket separately */
836 if (sock > -1) {
837 close(sock);
838 }
839
Radek Krejciac6d3472015-10-22 15:47:18 +0200840 lydict_remove(session->ctx, session->username);
841 lydict_remove(session->ctx, session->host);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200842 lydict_remove(session->ctx, session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200843
844 /* final cleanup */
Michal Vasko131120a2018-05-29 15:44:02 +0200845 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
846 if (rpc_locked) {
847 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100848 }
Michal Vasko131120a2018-05-29 15:44:02 +0200849 pthread_mutex_destroy(session->opts.server.rpc_lock);
850 pthread_cond_destroy(session->opts.server.rpc_cond);
851 free(session->opts.server.rpc_lock);
852 free(session->opts.server.rpc_cond);
853 free((int *)session->opts.server.rpc_inuse);
854 }
855
856 if (session->io_lock && !multisession) {
857 pthread_mutex_destroy(session->io_lock);
858 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200859 }
860
861 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100862 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200863 }
864
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200865 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100866 /* free CH synchronization structures if used */
867 if (session->opts.server.ch_cond) {
868 pthread_cond_destroy(session->opts.server.ch_cond);
869 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200870 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100871 if (session->opts.server.ch_lock) {
872 pthread_mutex_destroy(session->opts.server.ch_lock);
873 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200874 }
875 }
876
Radek Krejci695d4fa2015-10-22 13:23:54 +0200877 free(session);
878}
879
Michal Vasko086311b2016-01-08 09:53:11 +0100880static void
881add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
882{
Radek Krejci658782b2016-12-04 22:04:55 +0100883 size_t len;
884 int i;
885 char *p;
886
887 if (capab) {
888 /* check if already present */
889 p = strchr(capab, '?');
890 if (p) {
891 len = p - capab;
892 } else {
893 len = strlen(capab);
894 }
895 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800896 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100897 /* already present, do not duplicate it */
898 return;
899 }
900 }
901 }
902
903 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100904 if (*count == *size) {
905 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100906 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
907 if (!(*cpblts)) {
908 ERRMEM;
909 return;
910 }
Michal Vasko086311b2016-01-08 09:53:11 +0100911 }
912
913 if (capab) {
914 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
915 } else {
916 (*cpblts)[*count] = NULL;
917 }
918 ++(*count);
919}
920
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200921API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200922nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100923{
Michal Vasko086311b2016-01-08 09:53:11 +0100924 const char **cpblts;
Radek Krejci24a18412018-05-16 15:09:10 +0200925 const struct lys_module *mod, *devmod;
926 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
927 unsigned int u, v, module_set_id;
928 char *s;
929#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200930 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100931
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200932 if (!ctx) {
933 ERRARG("ctx");
934 return NULL;
935 }
936
Michal Vasko086311b2016-01-08 09:53:11 +0100937 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100938 if (!cpblts) {
939 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200940 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100941 }
Michal Vasko086311b2016-01-08 09:53:11 +0100942 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
943 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
944 count = 2;
945
946 /* capabilities */
947
Radek Krejci3222b7d2017-09-21 16:04:30 +0200948 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100949 if (mod) {
950 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100951 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100952 }
953 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100954 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100955 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100956 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100957 }
958 }
959 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100960 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100961 }
962 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100963 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100964 }
965 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100966 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100967 }
968 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100969 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100970 }
971 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100972 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100973 }
974 }
975
Radek Krejci3222b7d2017-09-21 16:04:30 +0200976 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100977 if (mod) {
978 if (!server_opts.wd_basic_mode) {
979 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
980 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100981 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100982 switch (server_opts.wd_basic_mode) {
983 case NC_WD_ALL:
984 strcat(str, "?basic-mode=report-all");
985 break;
986 case NC_WD_TRIM:
987 strcat(str, "?basic-mode=trim");
988 break;
989 case NC_WD_EXPLICIT:
990 strcat(str, "?basic-mode=explicit");
991 break;
992 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100993 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100994 break;
995 }
996
997 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200998 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100999 if (server_opts.wd_also_supported & NC_WD_ALL) {
1000 strcat(str, "report-all,");
1001 }
1002 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1003 strcat(str, "report-all-tagged,");
1004 }
1005 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1006 strcat(str, "trim,");
1007 }
1008 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1009 strcat(str, "explicit,");
1010 }
1011 str[strlen(str) - 1] = '\0';
1012
1013 add_cpblt(ctx, str, &cpblts, &size, &count);
1014 }
1015 }
1016 }
1017
Radek Krejci658782b2016-12-04 22:04:55 +01001018 /* other capabilities */
1019 for (u = 0; u < server_opts.capabilities_count; u++) {
1020 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001021 }
1022
1023 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +02001024 u = module_set_id = 0;
1025 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001026 if (!strcmp(mod->name, "ietf-yang-library")) {
Andrew Langefeld50eac702018-08-17 12:10:40 -05001027 /* Add the yang-library NETCONF capability as defined in RFC 7950 5.6.4 */
1028 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?%s%s&module-set-id=%u",
1029 mod->rev_size ? "revision=" : "", mod->rev_size ? mod->rev[0].date : "",
1030 ly_ctx_get_module_set_id(ctx));
1031 add_cpblt(ctx, str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001032 continue;
1033 } else if (mod->type) {
1034 /* skip submodules */
1035 continue;
1036 } else if (version == LYS_VERSION_1 && mod->version > version) {
1037 /* skip YANG 1.1 schemas */
1038 continue;
1039 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
1040 /* skip YANG 1.0 schemas */
1041 continue;
1042 }
Michal Vasko086311b2016-01-08 09:53:11 +01001043
Radek Krejci24a18412018-05-16 15:09:10 +02001044 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
1045 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
1046
1047 if (mod->features_size) {
1048 features_count = 0;
1049 for (i = 0; i < mod->features_size; ++i) {
1050 if (!(mod->features[i].flags & LYS_FENABLED)) {
1051 continue;
1052 }
1053 if (!features_count) {
1054 strcat(str, "&features=");
1055 str_len += 10;
1056 }
1057 len = strlen(mod->features[i].name);
1058 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1059 ERRINT;
1060 break;
1061 }
Radek Krejci4e8eadd2018-08-21 09:54:27 +02001062 if (features_count) {
Radek Krejci24a18412018-05-16 15:09:10 +02001063 strcat(str, ",");
1064 ++str_len;
1065 }
1066 strcat(str, mod->features[i].name);
1067 str_len += len;
1068 features_count++;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001069 }
Michal Vasko086311b2016-01-08 09:53:11 +01001070 }
Michal Vasko086311b2016-01-08 09:53:11 +01001071
Radek Krejci24a18412018-05-16 15:09:10 +02001072 if (mod->deviated) {
1073 strcat(str, "&deviations=");
1074 str_len += 12;
1075 dev_count = 0;
Frank Rimplercb9274f2019-03-13 16:40:34 +00001076 v = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001077 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
1078 if (devmod == mod) {
1079 continue;
1080 }
1081
1082 for (i = 0; i < devmod->deviation_size; ++i) {
1083 s = strstr(devmod->deviation[i].target_name, mod->name);
1084 if (s && s[strlen(mod->name)] == ':') {
1085 /* we have the module deviating the module being processed */
1086 len = strlen(devmod->name);
1087 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1088 ERRINT;
1089 break;
1090 }
1091 if (dev_count) {
1092 strcat(str, ",");
1093 ++str_len;
1094 }
1095 strcat(str, devmod->name);
1096 str_len += len;
1097 dev_count++;
1098
1099 break;
1100 }
1101 }
1102 }
1103 }
1104
1105 add_cpblt(ctx, str, &cpblts, &size, &count);
1106 }
Michal Vasko086311b2016-01-08 09:53:11 +01001107
1108 /* ending NULL capability */
1109 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1110
1111 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001112
1113error:
1114
1115 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001116 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001117}
1118
Radek Krejci24a18412018-05-16 15:09:10 +02001119API const char **
1120nc_server_get_cpblts(struct ly_ctx *ctx)
1121{
1122 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1123}
1124
Radek Krejci695d4fa2015-10-22 13:23:54 +02001125static int
Michal Vasko2cb24b42017-05-23 14:59:11 +02001126parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001127{
1128 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001129 int ver = -1, i = 0;
1130 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001131
1132 if (list) {
1133 /* get the storage for server's capabilities */
1134 LY_TREE_FOR(xml->child, cpblt) {
1135 i++;
1136 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001137 /* last item remains NULL */
1138 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001139 if (!*list) {
1140 ERRMEM;
1141 return -1;
1142 }
1143 i = 0;
1144 }
1145
1146 LY_TREE_FOR(xml->child, cpblt) {
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001147 if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
Radek Krejci695d4fa2015-10-22 13:23:54 +02001148 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1149 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1150 return -1;
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001151 } else if (!cpblt->name || !cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001152 continue;
1153 }
1154
Michal Vasko156d3272017-04-11 11:46:49 +02001155 /* skip leading/trailing whitespaces */
1156 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1157 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1158 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1159 ERR("Empty capability \"%s\" received.", cpblt->content);
1160 return -1;
1161 }
1162
Radek Krejci695d4fa2015-10-22 13:23:54 +02001163 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001164 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001165 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001166 } 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 +02001167 ver = 1;
1168 }
1169
1170 /* store capabilities */
1171 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001172 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1173 if (!(*list)[i]) {
1174 ERRMEM;
1175 return -1;
1176 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001177 i++;
1178 }
1179 }
1180
1181 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001182 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001183 }
1184
1185 return ver;
1186}
1187
1188static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001189nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001190{
Michal Vasko131120a2018-05-29 15:44:02 +02001191 NC_MSG_TYPE ret;
1192 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001193 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001194 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001195
Michal Vasko131120a2018-05-29 15:44:02 +02001196 if (session->side == NC_CLIENT) {
1197 /* client side hello - send only NETCONF base capabilities */
1198 cpblts = malloc(3 * sizeof *cpblts);
1199 if (!cpblts) {
1200 ERRMEM;
1201 return NC_MSG_ERROR;
1202 }
1203 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1204 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1205 cpblts[2] = NULL;
1206
1207 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1208 sid = NULL;
1209 } else {
1210 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
1211
1212 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1213 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001214 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001215
Michal Vasko131120a2018-05-29 15:44:02 +02001216 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001217
Michal Vasko086311b2016-01-08 09:53:11 +01001218 for (i = 0; cpblts[i]; ++i) {
1219 lydict_remove(session->ctx, cpblts[i]);
1220 }
1221 free(cpblts);
1222
Michal Vasko131120a2018-05-29 15:44:02 +02001223 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001224}
1225
1226static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001227nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001228{
1229 struct lyxml_elem *xml = NULL, *node;
Michal Vasko131120a2018-05-29 15:44:02 +02001230 NC_MSG_TYPE msgtype;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001231 int ver = -1;
1232 char *str;
1233 long long int id;
1234 int flag = 0;
1235
Michal Vasko131120a2018-05-29 15:44:02 +02001236 msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001237
Michal Vasko131120a2018-05-29 15:44:02 +02001238 switch (msgtype) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001239 case NC_MSG_HELLO:
1240 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001241 LY_TREE_FOR(xml->child, node) {
1242 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1243 continue;
1244 } else if (!strcmp(node->name, "session-id")) {
1245 if (!node->content || !strlen(node->content)) {
1246 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001247 goto error;
1248 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001249 str = NULL;
1250 id = strtoll(node->content, &str, 10);
1251 if (*str || id < 1 || id > UINT32_MAX) {
1252 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001253 goto error;
1254 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001255 session->id = (uint32_t)id;
1256 continue;
1257 } else if (strcmp(node->name, "capabilities")) {
1258 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001259 goto error;
1260 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001261
1262 if (flag) {
1263 /* multiple capabilities elements */
1264 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1265 goto error;
1266 }
1267 flag = 1;
1268
Michal Vasko2e6defd2016-10-07 15:48:15 +02001269 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001270 goto error;
1271 }
1272 session->version = ver;
1273 }
1274
1275 if (!session->id) {
1276 ERR("Missing <session-id> in server's <hello>.");
1277 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001278 }
1279 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001280 case NC_MSG_WOULDBLOCK:
1281 ERR("Server's <hello> timeout elapsed.");
1282 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001283 case NC_MSG_ERROR:
1284 /* nothing special, just pass it out */
1285 break;
1286 default:
1287 ERR("Unexpected message received instead of <hello>.");
1288 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001289 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001290 }
1291
1292 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001293 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001294
1295 return msgtype;
1296
1297error:
1298 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001299 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001300
1301 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001302}
1303
Michal Vasko11d142a2016-01-19 15:58:24 +01001304static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001305nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001306{
1307 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001308 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001309 int ver = -1;
1310 int flag = 0;
1311
Michal Vasko131120a2018-05-29 15:44:02 +02001312 msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ?
1313 server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001314
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001315 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001316 case NC_MSG_HELLO:
1317 /* get know NETCONF version */
1318 LY_TREE_FOR(xml->child, node) {
1319 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1320 continue;
1321 } else if (strcmp(node->name, "capabilities")) {
1322 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001323 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001324 goto cleanup;
1325 }
1326
1327 if (flag) {
1328 /* multiple capabilities elements */
1329 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001330 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001331 goto cleanup;
1332 }
1333 flag = 1;
1334
1335 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001336 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001337 goto cleanup;
1338 }
1339 session->version = ver;
1340 }
1341 break;
1342 case NC_MSG_ERROR:
1343 /* nothing special, just pass it out */
1344 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001345 case NC_MSG_WOULDBLOCK:
1346 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001347 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001348 default:
1349 ERR("Unexpected message received instead of <hello>.");
1350 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001351 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001352 }
1353
1354cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001355 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001356 return msgtype;
1357}
1358
Michal Vasko71090fc2016-05-24 16:37:28 +02001359NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001360nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001361{
Michal Vasko086311b2016-01-08 09:53:11 +01001362 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001363
Michal Vasko131120a2018-05-29 15:44:02 +02001364 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001365 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001366 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001367 }
1368
Michal Vasko11d142a2016-01-19 15:58:24 +01001369 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001370 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001371 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001372 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001373 }
1374
Michal Vasko71090fc2016-05-24 16:37:28 +02001375 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001376}
Michal Vasko086311b2016-01-08 09:53:11 +01001377
Radek Krejci53691be2016-02-22 13:58:37 +01001378#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001379
Michal Vasko8f0c0282016-02-29 10:17:14 +01001380static void
Michal Vasko086311b2016-01-08 09:53:11 +01001381nc_ssh_init(void)
1382{
1383 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1384 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001385}
1386
Michal Vasko8f0c0282016-02-29 10:17:14 +01001387static void
Michal Vasko086311b2016-01-08 09:53:11 +01001388nc_ssh_destroy(void)
1389{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001390 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001391 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001392 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001393 ssh_finalize();
1394}
1395
Radek Krejci53691be2016-02-22 13:58:37 +01001396#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001397
Radek Krejci53691be2016-02-22 13:58:37 +01001398#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001399
Michal Vasko770b4362017-02-17 14:44:18 +01001400#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1401
Michal Vaskof0c92c02016-01-29 09:41:45 +01001402struct CRYPTO_dynlock_value {
1403 pthread_mutex_t lock;
1404};
1405
Michal Vaskof0c92c02016-01-29 09:41:45 +01001406static struct CRYPTO_dynlock_value *
1407tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1408{
1409 struct CRYPTO_dynlock_value *value;
1410
1411 value = malloc(sizeof *value);
1412 if (!value) {
1413 ERRMEM;
1414 return NULL;
1415 }
1416 pthread_mutex_init(&value->lock, NULL);
1417
1418 return value;
1419}
1420
1421static void
1422tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1423{
1424 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1425 * I found ignored this fact, what do I know... */
1426 if (mode & CRYPTO_LOCK) {
1427 pthread_mutex_lock(&l->lock);
1428 } else {
1429 pthread_mutex_unlock(&l->lock);
1430 }
1431}
1432
1433static void
1434tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1435{
1436 pthread_mutex_destroy(&l->lock);
1437 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001438}
Michal Vasko770b4362017-02-17 14:44:18 +01001439#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001440
Michal Vasko8f0c0282016-02-29 10:17:14 +01001441#endif /* NC_ENABLED_TLS */
1442
1443#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1444
Michal Vasko770b4362017-02-17 14:44:18 +01001445#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001446static pthread_mutex_t *tls_locks;
1447
1448static void
1449tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1450{
1451 if (mode & CRYPTO_LOCK) {
1452 pthread_mutex_lock(tls_locks + n);
1453 } else {
1454 pthread_mutex_unlock(tls_locks + n);
1455 }
1456}
1457
1458static void
1459tls_thread_id_func(CRYPTO_THREADID *tid)
1460{
1461 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1462}
Michal Vasko770b4362017-02-17 14:44:18 +01001463#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001464
1465static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001466nc_tls_init(void)
1467{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001468 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001469 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001470 SSL_library_init();
1471
Michal Vasko770b4362017-02-17 14:44:18 +01001472#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001473 int i;
1474
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001475 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001476 if (!tls_locks) {
1477 ERRMEM;
1478 return;
1479 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001480 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1481 pthread_mutex_init(tls_locks + i, NULL);
1482 }
1483
Michal Vaskof0c92c02016-01-29 09:41:45 +01001484 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001485 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001486
1487 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1488 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1489 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001490#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001491}
1492
Michal Vasko8f0c0282016-02-29 10:17:14 +01001493static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001494nc_tls_destroy(void)
1495{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001496 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001497 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001498 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001499 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001500 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001501#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001502 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001503#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1504 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001505#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001506
Michal Vasko770b4362017-02-17 14:44:18 +01001507#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001508 int i;
1509
Michal Vaskob6e37262016-02-25 14:49:00 +01001510 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001511 CRYPTO_set_locking_callback(NULL);
1512 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1513 pthread_mutex_destroy(tls_locks + i);
1514 }
1515 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001516
1517 CRYPTO_set_dynlock_create_callback(NULL);
1518 CRYPTO_set_dynlock_lock_callback(NULL);
1519 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001520#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001521}
1522
Michal Vasko8f0c0282016-02-29 10:17:14 +01001523#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001524
Radek Krejci53691be2016-02-22 13:58:37 +01001525#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001526
Michal Vasko8f0c0282016-02-29 10:17:14 +01001527static void
Michal Vasko5e228792016-02-03 15:30:24 +01001528nc_ssh_tls_init(void)
1529{
1530 SSL_load_error_strings();
1531 ERR_load_BIO_strings();
1532 SSL_library_init();
1533
1534 nc_ssh_init();
1535
Michal Vasko770b4362017-02-17 14:44:18 +01001536#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001537 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1538 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1539 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001540#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001541}
1542
Michal Vasko8f0c0282016-02-29 10:17:14 +01001543static void
Michal Vasko5e228792016-02-03 15:30:24 +01001544nc_ssh_tls_destroy(void)
1545{
1546 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001547#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001548 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001549#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1550 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001551#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001552
1553 nc_ssh_destroy();
1554
Michal Vasko770b4362017-02-17 14:44:18 +01001555#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001556 CRYPTO_set_dynlock_create_callback(NULL);
1557 CRYPTO_set_dynlock_lock_callback(NULL);
1558 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001559#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001560}
1561
Radek Krejci53691be2016-02-22 13:58:37 +01001562#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001563
1564#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1565
1566API void
1567nc_thread_destroy(void)
1568{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001569 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1570 //CRYPTO_cleanup_all_ex_data();
1571
Michal Vasko770b4362017-02-17 14:44:18 +01001572#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1573 CRYPTO_THREADID crypto_tid;
1574
Michal Vasko8f0c0282016-02-29 10:17:14 +01001575 CRYPTO_THREADID_current(&crypto_tid);
1576 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001577#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001578}
1579
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001580#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1581
1582void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001583nc_init(void)
1584{
1585#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1586 nc_ssh_tls_init();
1587#elif defined(NC_ENABLED_SSH)
1588 nc_ssh_init();
1589#elif defined(NC_ENABLED_TLS)
1590 nc_tls_init();
1591#endif
1592}
1593
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001594void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001595nc_destroy(void)
1596{
1597#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1598 nc_ssh_tls_destroy();
1599#elif defined(NC_ENABLED_SSH)
1600 nc_ssh_destroy();
1601#elif defined(NC_ENABLED_TLS)
1602 nc_tls_destroy();
1603#endif
1604}