blob: fe90fa92aec892ed4701badf90c34ff4d294b2a3 [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 Vaskobe52dc22018-10-17 09:28:17 +0200119int
120nc_sock_enable_keepalive(int sock)
121{
122 int opt;
123
124 opt = 1;
125 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
126 ERR("Could not set SO_KEEPALIVE option (%s).", strerror(errno));
127 return -1;
128 }
129
130#ifdef TCP_KEEPIDLE
131 opt = 1;
132 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
133 ERR("Setsockopt failed (%s).", strerror(errno));
134 return -1;
135 }
136#endif
137
138#ifdef TCP_KEEPINTVL
139 opt = 5;
140 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
141 ERR("Setsockopt failed (%s).", strerror(errno));
142 return -1;
143 }
144#endif
145
146#ifdef TCP_KEEPCNT
147 opt = 10;
148 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
149 ERR("Setsockopt failed (%s).", strerror(errno));
150 return -1;
151 }
152#endif
153
154 return 0;
155}
156
Radek Krejci28472922016-07-15 11:51:16 +0200157#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
158int
159pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
160{
Michal Vasko81c5b302017-03-15 12:10:40 +0100161 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200162 int rc;
163 struct timespec cur, dur;
164
165 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
166 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
Michal Vasko145619f2018-02-16 10:08:45 +0100167 nc_gettimespec_real(&cur);
Radek Krejci28472922016-07-15 11:51:16 +0200168
Michal Vasko81c5b302017-03-15 12:10:40 +0100169 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
170 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200171 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100172 } else if (diff < 5) {
173 /* sleep until timeout */
Michal Vaskobf378cb2018-09-21 15:18:52 +0200174 dur.tv_sec = 0;
175 dur.tv_nsec = (long)diff * 1000000;
Michal Vasko81c5b302017-03-15 12:10:40 +0100176 } else {
177 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200178 dur.tv_sec = 0;
179 dur.tv_nsec = 5000000;
180 }
181
182 nanosleep(&dur, NULL);
183 }
184
185 return rc;
186}
187#endif
188
Michal Vaskoade892d2017-02-22 13:40:35 +0100189struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200190nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100191{
192 struct nc_session *sess;
193
194 sess = calloc(1, sizeof *sess);
195 if (!sess) {
196 return NULL;
197 }
198
Michal Vasko131120a2018-05-29 15:44:02 +0200199 sess->side = side;
200
201 if (side == NC_SERVER) {
202 sess->opts.server.rpc_lock = malloc(sizeof *sess->opts.server.rpc_lock);
203 sess->opts.server.rpc_cond = malloc(sizeof *sess->opts.server.rpc_cond);
204 sess->opts.server.rpc_inuse = malloc(sizeof *sess->opts.server.rpc_inuse);
205 if (!sess->opts.server.rpc_lock || !sess->opts.server.rpc_cond || !sess->opts.server.rpc_inuse) {
206 goto error;
Michal Vaskoade892d2017-02-22 13:40:35 +0100207 }
Michal Vasko131120a2018-05-29 15:44:02 +0200208 pthread_mutex_init(sess->opts.server.rpc_lock, NULL);
209 pthread_cond_init(sess->opts.server.rpc_cond, NULL);
210 *sess->opts.server.rpc_inuse = 0;
211 }
212
213 if (!shared_ti) {
214 sess->io_lock = malloc(sizeof *sess->io_lock);
215 if (!sess->io_lock) {
216 goto error;
217 }
218 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100219 }
220
221 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200222
223error:
224 if (side == NC_SERVER) {
225 free(sess->opts.server.rpc_lock);
226 free(sess->opts.server.rpc_cond);
227 free((int *)sess->opts.server.rpc_inuse);
228 }
229 free(sess);
230 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100231}
232
Michal Vasko96164bf2016-01-21 15:41:58 +0100233/*
234 * @return 1 - success
235 * 0 - timeout
236 * -1 - error
237 */
238int
Michal Vasko131120a2018-05-29 15:44:02 +0200239nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100240{
241 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100242 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100243
Michal Vasko131120a2018-05-29 15:44:02 +0200244 if (session->side != NC_SERVER) {
245 ERRINT;
246 return -1;
247 }
248
Michal Vasko96164bf2016-01-21 15:41:58 +0100249 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200250 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100251 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100252
Michal Vaskoade892d2017-02-22 13:40:35 +0100253 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200254 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100255 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200256 while (*session->opts.server.rpc_inuse) {
257 ret = pthread_cond_timedwait(session->opts.server.rpc_cond, session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100258 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200259 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100260 break;
261 }
262 }
263 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100264 } else if (!timeout) {
Michal Vasko131120a2018-05-29 15:44:02 +0200265 if (*session->opts.server.rpc_inuse) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100266 /* immediate timeout */
267 return 0;
268 }
269
270 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200271 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100272 if (!ret) {
273 /* be extra careful, someone could have been faster */
Michal Vasko131120a2018-05-29 15:44:02 +0200274 if (*session->opts.server.rpc_inuse) {
275 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100276 return 0;
277 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200278 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100279 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100280 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200281 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100282 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200283 while (*session->opts.server.rpc_inuse) {
284 ret = pthread_cond_wait(session->opts.server.rpc_cond, session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100285 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200286 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100287 break;
288 }
289 }
290 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100291 }
292
Michal Vaskoade892d2017-02-22 13:40:35 +0100293 if (ret) {
294 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
295 /* timeout */
296 return 0;
297 }
298
Michal Vasko96164bf2016-01-21 15:41:58 +0100299 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200300 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100301 return -1;
302 }
303
304 /* ok */
Michal Vasko131120a2018-05-29 15:44:02 +0200305 assert(*session->opts.server.rpc_inuse == 0);
306 *session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100307
308 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200309 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100310 if (ret) {
311 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200312 ERR("%s: faile to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100313 return -1;
314 }
315
316 return 1;
317}
318
319int
Michal Vasko131120a2018-05-29 15:44:02 +0200320nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100321{
322 int ret;
323 struct timespec ts_timeout;
324
Michal Vasko131120a2018-05-29 15:44:02 +0200325 if (session->side != NC_SERVER) {
326 ERRINT;
327 return -1;
328 }
329
330 assert(*session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100331
332 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200333 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100334 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100335
336 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200337 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100338 } else if (!timeout) {
339 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200340 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100341 } else { /* timeout == -1 */
342 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200343 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100344 }
345
346 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
347 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200348 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100349 return -1;
350 } else if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200351 WRN("%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100352 }
353
Michal Vasko131120a2018-05-29 15:44:02 +0200354 *session->opts.server.rpc_inuse = 0;
355 pthread_cond_signal(session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100356
357 if (!ret) {
358 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200359 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100360 if (ret) {
361 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200362 ERR("%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100363 return -1;
364 }
365 }
366
Michal Vasko96164bf2016-01-21 15:41:58 +0100367 return 1;
368}
369
Michal Vasko131120a2018-05-29 15:44:02 +0200370/*
371 * @return 1 - success
372 * 0 - timeout
373 * -1 - error
374 */
375int
376nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
377{
378 int ret;
379 struct timespec ts_timeout;
380
381 if (timeout > 0) {
382 nc_gettimespec_real(&ts_timeout);
383 nc_addtimespec(&ts_timeout, timeout);
384
385 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
386 } else if (!timeout) {
387 ret = pthread_mutex_trylock(session->io_lock);
388 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200389 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200390 }
391
392 if (ret) {
393 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
394 /* timeout */
395 return 0;
396 }
397
398 /* error */
399 ERR("%s: failed to IO lock a session (%s).", func, strerror(ret));
400 return -1;
401 }
402
403 return 1;
404}
405
406int
407nc_session_io_unlock(struct nc_session *session, const char *func)
408{
409 int ret;
410
411 ret = pthread_mutex_unlock(session->io_lock);
412 if (ret) {
413 /* error */
414 ERR("%s: failed to IO unlock a session (%s).", func, strerror(ret));
415 return -1;
416 }
417
418 return 1;
419}
420
Michal Vasko8dadf782016-01-15 10:29:36 +0100421API NC_STATUS
422nc_session_get_status(const struct nc_session *session)
423{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100424 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200425 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200426 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100427 }
428
Michal Vasko8dadf782016-01-15 10:29:36 +0100429 return session->status;
430}
431
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100432API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200433nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100434{
435 if (!session) {
436 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200437 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100438 }
439
440 return session->term_reason;
441}
442
Michal Vasko8dadf782016-01-15 10:29:36 +0100443API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200444nc_session_get_killed_by(const struct nc_session *session)
445{
446 if (!session) {
447 ERRARG("session");
448 return 0;
449 }
450
451 return session->killed_by;
452}
453
454API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100455nc_session_get_id(const struct nc_session *session)
456{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100457 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200458 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100459 return 0;
460 }
461
Michal Vasko8dadf782016-01-15 10:29:36 +0100462 return session->id;
463}
464
Michal Vasko174fe8e2016-02-17 15:38:09 +0100465API int
466nc_session_get_version(const struct nc_session *session)
467{
468 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200469 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100470 return -1;
471 }
472
473 return (session->version == NC_VERSION_10 ? 0 : 1);
474}
475
Michal Vasko8dadf782016-01-15 10:29:36 +0100476API NC_TRANSPORT_IMPL
477nc_session_get_ti(const struct nc_session *session)
478{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100479 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200480 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100481 return 0;
482 }
483
Michal Vasko8dadf782016-01-15 10:29:36 +0100484 return session->ti_type;
485}
486
487API const char *
488nc_session_get_username(const struct nc_session *session)
489{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100490 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200491 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100492 return NULL;
493 }
494
Michal Vasko8dadf782016-01-15 10:29:36 +0100495 return session->username;
496}
497
498API const char *
499nc_session_get_host(const struct nc_session *session)
500{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100501 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200502 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100503 return NULL;
504 }
505
Michal Vasko8dadf782016-01-15 10:29:36 +0100506 return session->host;
507}
508
509API uint16_t
510nc_session_get_port(const struct nc_session *session)
511{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100512 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200513 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100514 return 0;
515 }
516
Michal Vasko8dadf782016-01-15 10:29:36 +0100517 return session->port;
518}
519
Michal Vasko9a25e932016-02-01 10:36:42 +0100520API struct ly_ctx *
521nc_session_get_ctx(const struct nc_session *session)
522{
523 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200524 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100525 return NULL;
526 }
527
528 return session->ctx;
529}
530
Michal Vasko2cc4c682016-03-01 09:16:48 +0100531API void
532nc_session_set_data(struct nc_session *session, void *data)
533{
534 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200535 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100536 return;
537 }
538
539 session->data = data;
540}
541
542API void *
543nc_session_get_data(const struct nc_session *session)
544{
545 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200546 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100547 return NULL;
548 }
549
550 return session->data;
551}
552
Michal Vasko086311b2016-01-08 09:53:11 +0100553NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200554nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200555{
Michal Vasko086311b2016-01-08 09:53:11 +0100556 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100557 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
558 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100559 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100560 }
561
Michal Vasko131120a2018-05-29 15:44:02 +0200562 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100563}
564
Radek Krejci695d4fa2015-10-22 13:23:54 +0200565API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100566nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200567{
Michal Vasko131120a2018-05-29 15:44:02 +0200568 int r, i, rpc_locked = 0, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100569 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100570 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100571 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100572 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100573 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200574 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100575 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200576 void *p;
577
Michal Vasko428087d2016-01-14 16:04:28 +0100578 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200579 return;
580 }
581
Michal Vasko86d357c2016-03-11 13:46:38 +0100582 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200583 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200584 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100585 /* the thread now knows it should quit */
Michal Vasko86d357c2016-03-11 13:46:38 +0100586 }
587
Michal Vasko131120a2018-05-29 15:44:02 +0200588 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
589 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100590 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100591 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200592 } else if (r) {
593 rpc_locked = 1;
594 } /* else failed to lock it, too bad */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200595 }
596
Michal Vasko131120a2018-05-29 15:44:02 +0200597 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200598 /* cleanup message queues */
599 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200600 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100601 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200602
Michal Vaskoad611702015-12-03 13:41:51 +0100603 p = contiter;
604 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200605 free(p);
606 }
607
608 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200609 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100610 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200611
Michal Vaskoad611702015-12-03 13:41:51 +0100612 p = contiter;
613 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200614 free(p);
615 }
616
617 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200618 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200619 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100620 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200621 } else {
622 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vasko131120a2018-05-29 15:44:02 +0200623 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200624 lyd_free(close_rpc);
Michal Vasko131120a2018-05-29 15:44:02 +0200625 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100626 case NC_MSG_REPLY:
627 LY_TREE_FOR(rpl->child, child) {
628 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
629 break;
630 }
631 }
632 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100633 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100634 }
Michal Vaskoad611702015-12-03 13:41:51 +0100635 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100636 break;
637 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100638 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100639 break;
640 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100641 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100642 break;
643 default:
644 /* cannot happen */
645 break;
646 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200647 }
648
649 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200650 if (session->opts.client.cpblts) {
651 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200652 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200653 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200654 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200655 }
656 }
657
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100658 if (session->data && data_free) {
659 data_free(session->data);
660 }
661
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200662 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
663 /* CH LOCK */
664 pthread_mutex_lock(session->opts.server.ch_lock);
665 }
666
Michal Vasko86d357c2016-03-11 13:46:38 +0100667 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200668 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200669
670 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
671 pthread_cond_signal(session->opts.server.ch_cond);
672
673 /* CH UNLOCK */
674 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100675
676 /* wait for CH thread to actually wake up */
677 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
678 while (i && (session->flags & NC_SESSION_CALLHOME)) {
679 usleep(NC_TIMEOUT_STEP);
680 --i;
681 }
682 if (session->flags & NC_SESSION_CALLHOME) {
683 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
684 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200685 }
686
Michal Vasko428087d2016-01-14 16:04:28 +0100687 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200688
689 /* transport implementation cleanup */
690 switch (session->ti_type) {
691 case NC_TI_FD:
692 /* nothing needed - file descriptors were provided by caller,
693 * so it is up to the caller to close them correctly
694 * TODO use callbacks
695 */
Michal Vasko3512e402016-01-28 16:22:34 +0100696 /* just to avoid compiler warning */
697 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100698 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200699 break;
700
Radek Krejci53691be2016-02-22 13:58:37 +0100701#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200702 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100703 if (connected) {
704 ssh_channel_free(session->ti.libssh.channel);
705 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200706 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
707 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200708 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200709 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200710 /* SESSION IO LOCK */
711 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
712
Michal Vasko96164bf2016-01-21 15:41:58 +0100713 multisession = 0;
714 if (session->ti.libssh.next) {
715 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
716 if (siter->status != NC_STATUS_STARTING) {
717 multisession = 1;
718 break;
719 }
720 }
721 }
722
723 if (!multisession) {
724 /* it's not multisession yet, but we still need to free the starting sessions */
725 if (session->ti.libssh.next) {
726 do {
727 siter = session->ti.libssh.next;
728 session->ti.libssh.next = siter->ti.libssh.next;
729
730 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100731 lydict_remove(session->ctx, session->username);
732 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100733 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100734 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100735 }
736
737 free(siter);
738 } while (session->ti.libssh.next != session);
739 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100740 /* remember sock so we can close it */
741 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100742 if (connected) {
743 ssh_disconnect(session->ti.libssh.session);
744 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200745 ssh_free(session->ti.libssh.session);
746 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200747 /* remove the session from the list */
748 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100749 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200750 /* there will be only one session */
751 siter->ti.libssh.next = NULL;
752 } else {
753 /* there are still multiple sessions, keep the ring list */
754 siter->ti.libssh.next = session->ti.libssh.next;
755 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100756 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
757 if (session->flags & NC_SESSION_SSH_MSG_CB) {
758 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
759 if (siter->ti.libssh.next == session) {
760 ERRINT;
761 break;
762 }
763 }
764 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
765 siter->flags |= NC_SESSION_SSH_MSG_CB;
766 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200767 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200768
769 /* SESSION IO UNLOCK */
770 if (r == 1) {
771 nc_session_io_unlock(session, __func__);
772 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200773 break;
774#endif
775
Radek Krejci53691be2016-02-22 13:58:37 +0100776#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200777 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100778 /* remember sock so we can close it */
779 sock = SSL_get_fd(session->ti.tls);
780
Michal Vasko428087d2016-01-14 16:04:28 +0100781 if (connected) {
782 SSL_shutdown(session->ti.tls);
783 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200784 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100785
Michal Vasko2e6defd2016-10-07 15:48:15 +0200786 if (session->side == NC_SERVER) {
787 X509_free(session->opts.server.client_cert);
788 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200789 break;
790#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100791 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100792 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200793 }
Michal Vasko428087d2016-01-14 16:04:28 +0100794
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100795 /* close socket separately */
796 if (sock > -1) {
797 close(sock);
798 }
799
Radek Krejciac6d3472015-10-22 15:47:18 +0200800 lydict_remove(session->ctx, session->username);
801 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200802
803 /* final cleanup */
Michal Vasko131120a2018-05-29 15:44:02 +0200804 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
805 if (rpc_locked) {
806 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100807 }
Michal Vasko131120a2018-05-29 15:44:02 +0200808 pthread_mutex_destroy(session->opts.server.rpc_lock);
809 pthread_cond_destroy(session->opts.server.rpc_cond);
810 free(session->opts.server.rpc_lock);
811 free(session->opts.server.rpc_cond);
812 free((int *)session->opts.server.rpc_inuse);
813 }
814
815 if (session->io_lock && !multisession) {
816 pthread_mutex_destroy(session->io_lock);
817 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200818 }
819
820 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100821 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200822 }
823
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200824 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100825 /* free CH synchronization structures if used */
826 if (session->opts.server.ch_cond) {
827 pthread_cond_destroy(session->opts.server.ch_cond);
828 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200829 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100830 if (session->opts.server.ch_lock) {
831 pthread_mutex_destroy(session->opts.server.ch_lock);
832 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200833 }
834 }
835
Radek Krejci695d4fa2015-10-22 13:23:54 +0200836 free(session);
837}
838
Michal Vasko086311b2016-01-08 09:53:11 +0100839static void
840add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
841{
Radek Krejci658782b2016-12-04 22:04:55 +0100842 size_t len;
843 int i;
844 char *p;
845
846 if (capab) {
847 /* check if already present */
848 p = strchr(capab, '?');
849 if (p) {
850 len = p - capab;
851 } else {
852 len = strlen(capab);
853 }
854 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800855 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100856 /* already present, do not duplicate it */
857 return;
858 }
859 }
860 }
861
862 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100863 if (*count == *size) {
864 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100865 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
866 if (!(*cpblts)) {
867 ERRMEM;
868 return;
869 }
Michal Vasko086311b2016-01-08 09:53:11 +0100870 }
871
872 if (capab) {
873 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
874 } else {
875 (*cpblts)[*count] = NULL;
876 }
877 ++(*count);
878}
879
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200880API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200881nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100882{
Michal Vasko086311b2016-01-08 09:53:11 +0100883 const char **cpblts;
Radek Krejci24a18412018-05-16 15:09:10 +0200884 const struct lys_module *mod, *devmod;
885 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
886 unsigned int u, v, module_set_id;
887 char *s;
888#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200889 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100890
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200891 if (!ctx) {
892 ERRARG("ctx");
893 return NULL;
894 }
895
Michal Vasko086311b2016-01-08 09:53:11 +0100896 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100897 if (!cpblts) {
898 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200899 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100900 }
Michal Vasko086311b2016-01-08 09:53:11 +0100901 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
902 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
903 count = 2;
904
905 /* capabilities */
906
Radek Krejci3222b7d2017-09-21 16:04:30 +0200907 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100908 if (mod) {
909 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100910 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100911 }
912 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100913 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100914 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100915 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100916 }
917 }
918 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100919 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100920 }
921 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100922 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100923 }
924 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100925 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100926 }
927 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100928 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100929 }
930 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100931 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100932 }
933 }
934
Radek Krejci3222b7d2017-09-21 16:04:30 +0200935 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100936 if (mod) {
937 if (!server_opts.wd_basic_mode) {
938 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
939 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100940 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100941 switch (server_opts.wd_basic_mode) {
942 case NC_WD_ALL:
943 strcat(str, "?basic-mode=report-all");
944 break;
945 case NC_WD_TRIM:
946 strcat(str, "?basic-mode=trim");
947 break;
948 case NC_WD_EXPLICIT:
949 strcat(str, "?basic-mode=explicit");
950 break;
951 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100952 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100953 break;
954 }
955
956 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200957 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100958 if (server_opts.wd_also_supported & NC_WD_ALL) {
959 strcat(str, "report-all,");
960 }
961 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
962 strcat(str, "report-all-tagged,");
963 }
964 if (server_opts.wd_also_supported & NC_WD_TRIM) {
965 strcat(str, "trim,");
966 }
967 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
968 strcat(str, "explicit,");
969 }
970 str[strlen(str) - 1] = '\0';
971
972 add_cpblt(ctx, str, &cpblts, &size, &count);
973 }
974 }
975 }
976
Radek Krejci658782b2016-12-04 22:04:55 +0100977 /* other capabilities */
978 for (u = 0; u < server_opts.capabilities_count; u++) {
979 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100980 }
981
982 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +0200983 u = module_set_id = 0;
984 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +0200985 if (!strcmp(mod->name, "ietf-yang-library")) {
Andrew Langefeld50eac702018-08-17 12:10:40 -0500986 /* Add the yang-library NETCONF capability as defined in RFC 7950 5.6.4 */
987 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?%s%s&module-set-id=%u",
988 mod->rev_size ? "revision=" : "", mod->rev_size ? mod->rev[0].date : "",
989 ly_ctx_get_module_set_id(ctx));
990 add_cpblt(ctx, str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +0200991 continue;
992 } else if (mod->type) {
993 /* skip submodules */
994 continue;
995 } else if (version == LYS_VERSION_1 && mod->version > version) {
996 /* skip YANG 1.1 schemas */
997 continue;
998 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
999 /* skip YANG 1.0 schemas */
1000 continue;
1001 }
Michal Vasko086311b2016-01-08 09:53:11 +01001002
Radek Krejci24a18412018-05-16 15:09:10 +02001003 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
1004 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
1005
1006 if (mod->features_size) {
1007 features_count = 0;
1008 for (i = 0; i < mod->features_size; ++i) {
1009 if (!(mod->features[i].flags & LYS_FENABLED)) {
1010 continue;
1011 }
1012 if (!features_count) {
1013 strcat(str, "&features=");
1014 str_len += 10;
1015 }
1016 len = strlen(mod->features[i].name);
1017 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1018 ERRINT;
1019 break;
1020 }
Radek Krejci4e8eadd2018-08-21 09:54:27 +02001021 if (features_count) {
Radek Krejci24a18412018-05-16 15:09:10 +02001022 strcat(str, ",");
1023 ++str_len;
1024 }
1025 strcat(str, mod->features[i].name);
1026 str_len += len;
1027 features_count++;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001028 }
Michal Vasko086311b2016-01-08 09:53:11 +01001029 }
Michal Vasko086311b2016-01-08 09:53:11 +01001030
Radek Krejci24a18412018-05-16 15:09:10 +02001031 if (mod->deviated) {
1032 strcat(str, "&deviations=");
1033 str_len += 12;
1034 dev_count = 0;
1035 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
1036 if (devmod == mod) {
1037 continue;
1038 }
1039
1040 for (i = 0; i < devmod->deviation_size; ++i) {
1041 s = strstr(devmod->deviation[i].target_name, mod->name);
1042 if (s && s[strlen(mod->name)] == ':') {
1043 /* we have the module deviating the module being processed */
1044 len = strlen(devmod->name);
1045 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1046 ERRINT;
1047 break;
1048 }
1049 if (dev_count) {
1050 strcat(str, ",");
1051 ++str_len;
1052 }
1053 strcat(str, devmod->name);
1054 str_len += len;
1055 dev_count++;
1056
1057 break;
1058 }
1059 }
1060 }
1061 }
1062
1063 add_cpblt(ctx, str, &cpblts, &size, &count);
1064 }
Michal Vasko086311b2016-01-08 09:53:11 +01001065
1066 /* ending NULL capability */
1067 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1068
1069 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001070
1071error:
1072
1073 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001074 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001075}
1076
Radek Krejci24a18412018-05-16 15:09:10 +02001077API const char **
1078nc_server_get_cpblts(struct ly_ctx *ctx)
1079{
1080 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1081}
1082
Radek Krejci695d4fa2015-10-22 13:23:54 +02001083static int
Michal Vasko2cb24b42017-05-23 14:59:11 +02001084parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001085{
1086 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001087 int ver = -1, i = 0;
1088 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001089
1090 if (list) {
1091 /* get the storage for server's capabilities */
1092 LY_TREE_FOR(xml->child, cpblt) {
1093 i++;
1094 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001095 /* last item remains NULL */
1096 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001097 if (!*list) {
1098 ERRMEM;
1099 return -1;
1100 }
1101 i = 0;
1102 }
1103
1104 LY_TREE_FOR(xml->child, cpblt) {
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001105 if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
Radek Krejci695d4fa2015-10-22 13:23:54 +02001106 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1107 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1108 return -1;
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001109 } else if (!cpblt->name || !cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001110 continue;
1111 }
1112
Michal Vasko156d3272017-04-11 11:46:49 +02001113 /* skip leading/trailing whitespaces */
1114 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1115 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1116 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1117 ERR("Empty capability \"%s\" received.", cpblt->content);
1118 return -1;
1119 }
1120
Radek Krejci695d4fa2015-10-22 13:23:54 +02001121 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001122 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001123 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001124 } 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 +02001125 ver = 1;
1126 }
1127
1128 /* store capabilities */
1129 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001130 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1131 if (!(*list)[i]) {
1132 ERRMEM;
1133 return -1;
1134 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001135 i++;
1136 }
1137 }
1138
1139 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001140 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001141 }
1142
1143 return ver;
1144}
1145
1146static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001147nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001148{
Michal Vasko131120a2018-05-29 15:44:02 +02001149 NC_MSG_TYPE ret;
1150 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001151 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001152 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001153
Michal Vasko131120a2018-05-29 15:44:02 +02001154 if (session->side == NC_CLIENT) {
1155 /* client side hello - send only NETCONF base capabilities */
1156 cpblts = malloc(3 * sizeof *cpblts);
1157 if (!cpblts) {
1158 ERRMEM;
1159 return NC_MSG_ERROR;
1160 }
1161 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1162 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1163 cpblts[2] = NULL;
1164
1165 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1166 sid = NULL;
1167 } else {
1168 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
1169
1170 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1171 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001172 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001173
Michal Vasko131120a2018-05-29 15:44:02 +02001174 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001175
Michal Vasko086311b2016-01-08 09:53:11 +01001176 for (i = 0; cpblts[i]; ++i) {
1177 lydict_remove(session->ctx, cpblts[i]);
1178 }
1179 free(cpblts);
1180
Michal Vasko131120a2018-05-29 15:44:02 +02001181 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001182}
1183
1184static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001185nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001186{
1187 struct lyxml_elem *xml = NULL, *node;
Michal Vasko131120a2018-05-29 15:44:02 +02001188 NC_MSG_TYPE msgtype;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001189 int ver = -1;
1190 char *str;
1191 long long int id;
1192 int flag = 0;
1193
Michal Vasko131120a2018-05-29 15:44:02 +02001194 msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001195
Michal Vasko131120a2018-05-29 15:44:02 +02001196 switch (msgtype) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001197 case NC_MSG_HELLO:
1198 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001199 LY_TREE_FOR(xml->child, node) {
1200 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1201 continue;
1202 } else if (!strcmp(node->name, "session-id")) {
1203 if (!node->content || !strlen(node->content)) {
1204 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001205 goto error;
1206 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001207 str = NULL;
1208 id = strtoll(node->content, &str, 10);
1209 if (*str || id < 1 || id > UINT32_MAX) {
1210 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001211 goto error;
1212 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001213 session->id = (uint32_t)id;
1214 continue;
1215 } else if (strcmp(node->name, "capabilities")) {
1216 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001217 goto error;
1218 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001219
1220 if (flag) {
1221 /* multiple capabilities elements */
1222 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1223 goto error;
1224 }
1225 flag = 1;
1226
Michal Vasko2e6defd2016-10-07 15:48:15 +02001227 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001228 goto error;
1229 }
1230 session->version = ver;
1231 }
1232
1233 if (!session->id) {
1234 ERR("Missing <session-id> in server's <hello>.");
1235 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001236 }
1237 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001238 case NC_MSG_WOULDBLOCK:
1239 ERR("Server's <hello> timeout elapsed.");
1240 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001241 case NC_MSG_ERROR:
1242 /* nothing special, just pass it out */
1243 break;
1244 default:
1245 ERR("Unexpected message received instead of <hello>.");
1246 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001247 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001248 }
1249
1250 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001251 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001252
1253 return msgtype;
1254
1255error:
1256 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001257 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001258
1259 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001260}
1261
Michal Vasko11d142a2016-01-19 15:58:24 +01001262static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001263nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001264{
1265 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001266 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001267 int ver = -1;
1268 int flag = 0;
1269
Michal Vasko131120a2018-05-29 15:44:02 +02001270 msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ?
1271 server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001272
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001273 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001274 case NC_MSG_HELLO:
1275 /* get know NETCONF version */
1276 LY_TREE_FOR(xml->child, node) {
1277 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1278 continue;
1279 } else if (strcmp(node->name, "capabilities")) {
1280 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001281 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001282 goto cleanup;
1283 }
1284
1285 if (flag) {
1286 /* multiple capabilities elements */
1287 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001288 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001289 goto cleanup;
1290 }
1291 flag = 1;
1292
1293 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001294 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001295 goto cleanup;
1296 }
1297 session->version = ver;
1298 }
1299 break;
1300 case NC_MSG_ERROR:
1301 /* nothing special, just pass it out */
1302 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001303 case NC_MSG_WOULDBLOCK:
1304 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001305 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001306 default:
1307 ERR("Unexpected message received instead of <hello>.");
1308 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001309 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001310 }
1311
1312cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001313 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001314 return msgtype;
1315}
1316
Michal Vasko71090fc2016-05-24 16:37:28 +02001317NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001318nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001319{
Michal Vasko086311b2016-01-08 09:53:11 +01001320 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001321
Michal Vasko131120a2018-05-29 15:44:02 +02001322 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001323 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001324 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001325 }
1326
Michal Vasko11d142a2016-01-19 15:58:24 +01001327 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001328 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001329 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001330 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001331 }
1332
Michal Vasko71090fc2016-05-24 16:37:28 +02001333 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001334}
Michal Vasko086311b2016-01-08 09:53:11 +01001335
Radek Krejci53691be2016-02-22 13:58:37 +01001336#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001337
Michal Vasko8f0c0282016-02-29 10:17:14 +01001338static void
Michal Vasko086311b2016-01-08 09:53:11 +01001339nc_ssh_init(void)
1340{
1341 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1342 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001343}
1344
Michal Vasko8f0c0282016-02-29 10:17:14 +01001345static void
Michal Vasko086311b2016-01-08 09:53:11 +01001346nc_ssh_destroy(void)
1347{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001348 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001349 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001350 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001351 ssh_finalize();
1352}
1353
Radek Krejci53691be2016-02-22 13:58:37 +01001354#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001355
Radek Krejci53691be2016-02-22 13:58:37 +01001356#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001357
Michal Vasko770b4362017-02-17 14:44:18 +01001358#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1359
Michal Vaskof0c92c02016-01-29 09:41:45 +01001360struct CRYPTO_dynlock_value {
1361 pthread_mutex_t lock;
1362};
1363
Michal Vaskof0c92c02016-01-29 09:41:45 +01001364static struct CRYPTO_dynlock_value *
1365tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1366{
1367 struct CRYPTO_dynlock_value *value;
1368
1369 value = malloc(sizeof *value);
1370 if (!value) {
1371 ERRMEM;
1372 return NULL;
1373 }
1374 pthread_mutex_init(&value->lock, NULL);
1375
1376 return value;
1377}
1378
1379static void
1380tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1381{
1382 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1383 * I found ignored this fact, what do I know... */
1384 if (mode & CRYPTO_LOCK) {
1385 pthread_mutex_lock(&l->lock);
1386 } else {
1387 pthread_mutex_unlock(&l->lock);
1388 }
1389}
1390
1391static void
1392tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1393{
1394 pthread_mutex_destroy(&l->lock);
1395 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001396}
Michal Vasko770b4362017-02-17 14:44:18 +01001397#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001398
Michal Vasko8f0c0282016-02-29 10:17:14 +01001399#endif /* NC_ENABLED_TLS */
1400
1401#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1402
Michal Vasko770b4362017-02-17 14:44:18 +01001403#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001404static pthread_mutex_t *tls_locks;
1405
1406static void
1407tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1408{
1409 if (mode & CRYPTO_LOCK) {
1410 pthread_mutex_lock(tls_locks + n);
1411 } else {
1412 pthread_mutex_unlock(tls_locks + n);
1413 }
1414}
1415
1416static void
1417tls_thread_id_func(CRYPTO_THREADID *tid)
1418{
1419 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1420}
Michal Vasko770b4362017-02-17 14:44:18 +01001421#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001422
1423static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001424nc_tls_init(void)
1425{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001426 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001427 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001428 SSL_library_init();
1429
Michal Vasko770b4362017-02-17 14:44:18 +01001430#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001431 int i;
1432
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001433 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001434 if (!tls_locks) {
1435 ERRMEM;
1436 return;
1437 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001438 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1439 pthread_mutex_init(tls_locks + i, NULL);
1440 }
1441
Michal Vaskof0c92c02016-01-29 09:41:45 +01001442 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001443 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001444
1445 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1446 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1447 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001448#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001449}
1450
Michal Vasko8f0c0282016-02-29 10:17:14 +01001451static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001452nc_tls_destroy(void)
1453{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001454 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001455 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001456 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001457 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001458 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001459#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001460 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001461#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1462 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001463#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001464
Michal Vasko770b4362017-02-17 14:44:18 +01001465#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001466 int i;
1467
Michal Vaskob6e37262016-02-25 14:49:00 +01001468 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001469 CRYPTO_set_locking_callback(NULL);
1470 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1471 pthread_mutex_destroy(tls_locks + i);
1472 }
1473 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001474
1475 CRYPTO_set_dynlock_create_callback(NULL);
1476 CRYPTO_set_dynlock_lock_callback(NULL);
1477 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001478#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001479}
1480
Michal Vasko8f0c0282016-02-29 10:17:14 +01001481#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001482
Radek Krejci53691be2016-02-22 13:58:37 +01001483#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001484
Michal Vasko8f0c0282016-02-29 10:17:14 +01001485static void
Michal Vasko5e228792016-02-03 15:30:24 +01001486nc_ssh_tls_init(void)
1487{
1488 SSL_load_error_strings();
1489 ERR_load_BIO_strings();
1490 SSL_library_init();
1491
1492 nc_ssh_init();
1493
Michal Vasko770b4362017-02-17 14:44:18 +01001494#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001495 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1496 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1497 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001498#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001499}
1500
Michal Vasko8f0c0282016-02-29 10:17:14 +01001501static void
Michal Vasko5e228792016-02-03 15:30:24 +01001502nc_ssh_tls_destroy(void)
1503{
1504 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001505#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001506 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001507#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1508 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001509#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001510
1511 nc_ssh_destroy();
1512
Michal Vasko770b4362017-02-17 14:44:18 +01001513#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001514 CRYPTO_set_dynlock_create_callback(NULL);
1515 CRYPTO_set_dynlock_lock_callback(NULL);
1516 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001517#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001518}
1519
Radek Krejci53691be2016-02-22 13:58:37 +01001520#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001521
1522#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1523
1524API void
1525nc_thread_destroy(void)
1526{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001527 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1528 //CRYPTO_cleanup_all_ex_data();
1529
Michal Vasko770b4362017-02-17 14:44:18 +01001530#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1531 CRYPTO_THREADID crypto_tid;
1532
Michal Vasko8f0c0282016-02-29 10:17:14 +01001533 CRYPTO_THREADID_current(&crypto_tid);
1534 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001535#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001536}
1537
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001538#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1539
1540void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001541nc_init(void)
1542{
1543#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1544 nc_ssh_tls_init();
1545#elif defined(NC_ENABLED_SSH)
1546 nc_ssh_init();
1547#elif defined(NC_ENABLED_TLS)
1548 nc_tls_init();
1549#endif
1550}
1551
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001552void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001553nc_destroy(void)
1554{
1555#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1556 nc_ssh_tls_destroy();
1557#elif defined(NC_ENABLED_SSH)
1558 nc_ssh_destroy();
1559#elif defined(NC_ENABLED_TLS)
1560 nc_tls_destroy();
1561#endif
1562}