blob: 80a0a3652dfb2f549cad72ba891c0caa10713422 [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
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200509API const char *
510nc_session_get_path(const struct nc_session *session)
511{
512 if (!session) {
513 ERRARG("session");
514 return NULL;
515 }
516 if (session->ti_type != NC_TI_UNIX)
517 return NULL;
518
519 return session->path;
520}
521
Michal Vasko8dadf782016-01-15 10:29:36 +0100522API uint16_t
523nc_session_get_port(const struct nc_session *session)
524{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100525 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200526 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100527 return 0;
528 }
529
Michal Vasko8dadf782016-01-15 10:29:36 +0100530 return session->port;
531}
532
Michal Vasko9a25e932016-02-01 10:36:42 +0100533API struct ly_ctx *
534nc_session_get_ctx(const struct nc_session *session)
535{
536 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200537 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100538 return NULL;
539 }
540
541 return session->ctx;
542}
543
Michal Vasko2cc4c682016-03-01 09:16:48 +0100544API void
545nc_session_set_data(struct nc_session *session, void *data)
546{
547 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200548 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100549 return;
550 }
551
552 session->data = data;
553}
554
555API void *
556nc_session_get_data(const struct nc_session *session)
557{
558 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200559 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100560 return NULL;
561 }
562
563 return session->data;
564}
565
Michal Vasko086311b2016-01-08 09:53:11 +0100566NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200567nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200568{
Michal Vasko086311b2016-01-08 09:53:11 +0100569 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100570 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
571 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100572 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100573 }
574
Michal Vasko131120a2018-05-29 15:44:02 +0200575 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100576}
577
Radek Krejci695d4fa2015-10-22 13:23:54 +0200578API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100579nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200580{
Michal Vasko131120a2018-05-29 15:44:02 +0200581 int r, i, rpc_locked = 0, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100582 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100583 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100584 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100585 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100586 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200587 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100588 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200589 void *p;
590
Michal Vasko428087d2016-01-14 16:04:28 +0100591 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200592 return;
593 }
594
Michal Vasko86d357c2016-03-11 13:46:38 +0100595 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200596 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200597 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100598 /* the thread now knows it should quit */
Michal Vasko86d357c2016-03-11 13:46:38 +0100599 }
600
Michal Vasko131120a2018-05-29 15:44:02 +0200601 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
602 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100603 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100604 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200605 } else if (r) {
606 rpc_locked = 1;
607 } /* else failed to lock it, too bad */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200608 }
609
Michal Vasko131120a2018-05-29 15:44:02 +0200610 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200611 /* cleanup message queues */
612 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200613 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100614 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200615
Michal Vaskoad611702015-12-03 13:41:51 +0100616 p = contiter;
617 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200618 free(p);
619 }
620
621 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200622 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100623 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200624
Michal Vaskoad611702015-12-03 13:41:51 +0100625 p = contiter;
626 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200627 free(p);
628 }
629
630 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200631 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200632 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100633 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200634 } else {
635 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vasko131120a2018-05-29 15:44:02 +0200636 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200637 lyd_free(close_rpc);
Michal Vasko131120a2018-05-29 15:44:02 +0200638 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100639 case NC_MSG_REPLY:
640 LY_TREE_FOR(rpl->child, child) {
641 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
642 break;
643 }
644 }
645 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100646 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100647 }
Michal Vaskoad611702015-12-03 13:41:51 +0100648 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100649 break;
650 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100651 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100652 break;
653 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100654 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100655 break;
656 default:
657 /* cannot happen */
658 break;
659 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200660 }
661
662 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200663 if (session->opts.client.cpblts) {
664 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200665 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200666 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200667 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200668 }
669 }
670
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100671 if (session->data && data_free) {
672 data_free(session->data);
673 }
674
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200675 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
676 /* CH LOCK */
677 pthread_mutex_lock(session->opts.server.ch_lock);
678 }
679
Michal Vasko86d357c2016-03-11 13:46:38 +0100680 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200681 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200682
683 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
684 pthread_cond_signal(session->opts.server.ch_cond);
685
686 /* CH UNLOCK */
687 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100688
689 /* wait for CH thread to actually wake up */
690 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
691 while (i && (session->flags & NC_SESSION_CALLHOME)) {
692 usleep(NC_TIMEOUT_STEP);
693 --i;
694 }
695 if (session->flags & NC_SESSION_CALLHOME) {
696 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
697 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200698 }
699
Michal Vasko428087d2016-01-14 16:04:28 +0100700 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200701
702 /* transport implementation cleanup */
703 switch (session->ti_type) {
704 case NC_TI_FD:
705 /* nothing needed - file descriptors were provided by caller,
706 * so it is up to the caller to close them correctly
707 * TODO use callbacks
708 */
Michal Vasko3512e402016-01-28 16:22:34 +0100709 /* just to avoid compiler warning */
710 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100711 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200712 break;
713
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200714 case NC_TI_UNIX:
715 sock = session->ti.unixsock.sock;
716 (void)connected;
717 (void)siter;
718 break;
719
Radek Krejci53691be2016-02-22 13:58:37 +0100720#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200721 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100722 if (connected) {
723 ssh_channel_free(session->ti.libssh.channel);
724 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200725 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
726 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200727 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200728 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200729 /* SESSION IO LOCK */
730 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
731
Michal Vasko96164bf2016-01-21 15:41:58 +0100732 multisession = 0;
733 if (session->ti.libssh.next) {
734 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
735 if (siter->status != NC_STATUS_STARTING) {
736 multisession = 1;
737 break;
738 }
739 }
740 }
741
742 if (!multisession) {
743 /* it's not multisession yet, but we still need to free the starting sessions */
744 if (session->ti.libssh.next) {
745 do {
746 siter = session->ti.libssh.next;
747 session->ti.libssh.next = siter->ti.libssh.next;
748
749 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100750 lydict_remove(session->ctx, session->username);
751 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100752 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100753 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100754 }
755
756 free(siter);
757 } while (session->ti.libssh.next != session);
758 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100759 /* remember sock so we can close it */
760 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100761 if (connected) {
762 ssh_disconnect(session->ti.libssh.session);
763 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200764 ssh_free(session->ti.libssh.session);
765 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200766 /* remove the session from the list */
767 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100768 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200769 /* there will be only one session */
770 siter->ti.libssh.next = NULL;
771 } else {
772 /* there are still multiple sessions, keep the ring list */
773 siter->ti.libssh.next = session->ti.libssh.next;
774 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100775 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
776 if (session->flags & NC_SESSION_SSH_MSG_CB) {
777 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
778 if (siter->ti.libssh.next == session) {
779 ERRINT;
780 break;
781 }
782 }
783 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
784 siter->flags |= NC_SESSION_SSH_MSG_CB;
785 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200786 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200787
788 /* SESSION IO UNLOCK */
789 if (r == 1) {
790 nc_session_io_unlock(session, __func__);
791 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200792 break;
793#endif
794
Radek Krejci53691be2016-02-22 13:58:37 +0100795#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200796 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100797 /* remember sock so we can close it */
798 sock = SSL_get_fd(session->ti.tls);
799
Michal Vasko428087d2016-01-14 16:04:28 +0100800 if (connected) {
801 SSL_shutdown(session->ti.tls);
802 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200803 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100804
Michal Vasko2e6defd2016-10-07 15:48:15 +0200805 if (session->side == NC_SERVER) {
806 X509_free(session->opts.server.client_cert);
807 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200808 break;
809#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100810 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100811 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200812 }
Michal Vasko428087d2016-01-14 16:04:28 +0100813
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100814 /* close socket separately */
815 if (sock > -1) {
816 close(sock);
817 }
818
Radek Krejciac6d3472015-10-22 15:47:18 +0200819 lydict_remove(session->ctx, session->username);
820 lydict_remove(session->ctx, session->host);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200821 lydict_remove(session->ctx, session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200822
823 /* final cleanup */
Michal Vasko131120a2018-05-29 15:44:02 +0200824 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
825 if (rpc_locked) {
826 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100827 }
Michal Vasko131120a2018-05-29 15:44:02 +0200828 pthread_mutex_destroy(session->opts.server.rpc_lock);
829 pthread_cond_destroy(session->opts.server.rpc_cond);
830 free(session->opts.server.rpc_lock);
831 free(session->opts.server.rpc_cond);
832 free((int *)session->opts.server.rpc_inuse);
833 }
834
835 if (session->io_lock && !multisession) {
836 pthread_mutex_destroy(session->io_lock);
837 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200838 }
839
840 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100841 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200842 }
843
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200844 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100845 /* free CH synchronization structures if used */
846 if (session->opts.server.ch_cond) {
847 pthread_cond_destroy(session->opts.server.ch_cond);
848 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200849 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100850 if (session->opts.server.ch_lock) {
851 pthread_mutex_destroy(session->opts.server.ch_lock);
852 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200853 }
854 }
855
Radek Krejci695d4fa2015-10-22 13:23:54 +0200856 free(session);
857}
858
Michal Vasko086311b2016-01-08 09:53:11 +0100859static void
860add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
861{
Radek Krejci658782b2016-12-04 22:04:55 +0100862 size_t len;
863 int i;
864 char *p;
865
866 if (capab) {
867 /* check if already present */
868 p = strchr(capab, '?');
869 if (p) {
870 len = p - capab;
871 } else {
872 len = strlen(capab);
873 }
874 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800875 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100876 /* already present, do not duplicate it */
877 return;
878 }
879 }
880 }
881
882 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100883 if (*count == *size) {
884 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100885 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
886 if (!(*cpblts)) {
887 ERRMEM;
888 return;
889 }
Michal Vasko086311b2016-01-08 09:53:11 +0100890 }
891
892 if (capab) {
893 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
894 } else {
895 (*cpblts)[*count] = NULL;
896 }
897 ++(*count);
898}
899
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200900API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200901nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100902{
Michal Vasko086311b2016-01-08 09:53:11 +0100903 const char **cpblts;
Radek Krejci24a18412018-05-16 15:09:10 +0200904 const struct lys_module *mod, *devmod;
905 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
906 unsigned int u, v, module_set_id;
907 char *s;
908#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200909 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100910
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200911 if (!ctx) {
912 ERRARG("ctx");
913 return NULL;
914 }
915
Michal Vasko086311b2016-01-08 09:53:11 +0100916 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100917 if (!cpblts) {
918 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200919 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100920 }
Michal Vasko086311b2016-01-08 09:53:11 +0100921 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
922 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
923 count = 2;
924
925 /* capabilities */
926
Radek Krejci3222b7d2017-09-21 16:04:30 +0200927 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100928 if (mod) {
929 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100930 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100931 }
932 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100933 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100934 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100935 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100936 }
937 }
938 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100939 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100940 }
941 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100942 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100943 }
944 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100945 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100946 }
947 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100948 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100949 }
950 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100951 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100952 }
953 }
954
Radek Krejci3222b7d2017-09-21 16:04:30 +0200955 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100956 if (mod) {
957 if (!server_opts.wd_basic_mode) {
958 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
959 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100960 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100961 switch (server_opts.wd_basic_mode) {
962 case NC_WD_ALL:
963 strcat(str, "?basic-mode=report-all");
964 break;
965 case NC_WD_TRIM:
966 strcat(str, "?basic-mode=trim");
967 break;
968 case NC_WD_EXPLICIT:
969 strcat(str, "?basic-mode=explicit");
970 break;
971 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100972 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100973 break;
974 }
975
976 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200977 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100978 if (server_opts.wd_also_supported & NC_WD_ALL) {
979 strcat(str, "report-all,");
980 }
981 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
982 strcat(str, "report-all-tagged,");
983 }
984 if (server_opts.wd_also_supported & NC_WD_TRIM) {
985 strcat(str, "trim,");
986 }
987 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
988 strcat(str, "explicit,");
989 }
990 str[strlen(str) - 1] = '\0';
991
992 add_cpblt(ctx, str, &cpblts, &size, &count);
993 }
994 }
995 }
996
Radek Krejci658782b2016-12-04 22:04:55 +0100997 /* other capabilities */
998 for (u = 0; u < server_opts.capabilities_count; u++) {
999 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001000 }
1001
1002 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +02001003 u = module_set_id = 0;
1004 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001005 if (!strcmp(mod->name, "ietf-yang-library")) {
Andrew Langefeld50eac702018-08-17 12:10:40 -05001006 /* Add the yang-library NETCONF capability as defined in RFC 7950 5.6.4 */
1007 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?%s%s&module-set-id=%u",
1008 mod->rev_size ? "revision=" : "", mod->rev_size ? mod->rev[0].date : "",
1009 ly_ctx_get_module_set_id(ctx));
1010 add_cpblt(ctx, str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001011 continue;
1012 } else if (mod->type) {
1013 /* skip submodules */
1014 continue;
1015 } else if (version == LYS_VERSION_1 && mod->version > version) {
1016 /* skip YANG 1.1 schemas */
1017 continue;
1018 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
1019 /* skip YANG 1.0 schemas */
1020 continue;
1021 }
Michal Vasko086311b2016-01-08 09:53:11 +01001022
Radek Krejci24a18412018-05-16 15:09:10 +02001023 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
1024 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
1025
1026 if (mod->features_size) {
1027 features_count = 0;
1028 for (i = 0; i < mod->features_size; ++i) {
1029 if (!(mod->features[i].flags & LYS_FENABLED)) {
1030 continue;
1031 }
1032 if (!features_count) {
1033 strcat(str, "&features=");
1034 str_len += 10;
1035 }
1036 len = strlen(mod->features[i].name);
1037 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1038 ERRINT;
1039 break;
1040 }
Radek Krejci4e8eadd2018-08-21 09:54:27 +02001041 if (features_count) {
Radek Krejci24a18412018-05-16 15:09:10 +02001042 strcat(str, ",");
1043 ++str_len;
1044 }
1045 strcat(str, mod->features[i].name);
1046 str_len += len;
1047 features_count++;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001048 }
Michal Vasko086311b2016-01-08 09:53:11 +01001049 }
Michal Vasko086311b2016-01-08 09:53:11 +01001050
Radek Krejci24a18412018-05-16 15:09:10 +02001051 if (mod->deviated) {
1052 strcat(str, "&deviations=");
1053 str_len += 12;
1054 dev_count = 0;
1055 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
1056 if (devmod == mod) {
1057 continue;
1058 }
1059
1060 for (i = 0; i < devmod->deviation_size; ++i) {
1061 s = strstr(devmod->deviation[i].target_name, mod->name);
1062 if (s && s[strlen(mod->name)] == ':') {
1063 /* we have the module deviating the module being processed */
1064 len = strlen(devmod->name);
1065 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1066 ERRINT;
1067 break;
1068 }
1069 if (dev_count) {
1070 strcat(str, ",");
1071 ++str_len;
1072 }
1073 strcat(str, devmod->name);
1074 str_len += len;
1075 dev_count++;
1076
1077 break;
1078 }
1079 }
1080 }
1081 }
1082
1083 add_cpblt(ctx, str, &cpblts, &size, &count);
1084 }
Michal Vasko086311b2016-01-08 09:53:11 +01001085
1086 /* ending NULL capability */
1087 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1088
1089 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001090
1091error:
1092
1093 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001094 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001095}
1096
Radek Krejci24a18412018-05-16 15:09:10 +02001097API const char **
1098nc_server_get_cpblts(struct ly_ctx *ctx)
1099{
1100 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1101}
1102
Radek Krejci695d4fa2015-10-22 13:23:54 +02001103static int
Michal Vasko2cb24b42017-05-23 14:59:11 +02001104parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001105{
1106 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001107 int ver = -1, i = 0;
1108 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001109
1110 if (list) {
1111 /* get the storage for server's capabilities */
1112 LY_TREE_FOR(xml->child, cpblt) {
1113 i++;
1114 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001115 /* last item remains NULL */
1116 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001117 if (!*list) {
1118 ERRMEM;
1119 return -1;
1120 }
1121 i = 0;
1122 }
1123
1124 LY_TREE_FOR(xml->child, cpblt) {
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001125 if (cpblt->name && strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
Radek Krejci695d4fa2015-10-22 13:23:54 +02001126 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1127 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1128 return -1;
Michal Vasko5ce6d7d2018-10-12 09:33:33 +02001129 } else if (!cpblt->name || !cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001130 continue;
1131 }
1132
Michal Vasko156d3272017-04-11 11:46:49 +02001133 /* skip leading/trailing whitespaces */
1134 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1135 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1136 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1137 ERR("Empty capability \"%s\" received.", cpblt->content);
1138 return -1;
1139 }
1140
Radek Krejci695d4fa2015-10-22 13:23:54 +02001141 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001142 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001143 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001144 } 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 +02001145 ver = 1;
1146 }
1147
1148 /* store capabilities */
1149 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001150 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1151 if (!(*list)[i]) {
1152 ERRMEM;
1153 return -1;
1154 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001155 i++;
1156 }
1157 }
1158
1159 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001160 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001161 }
1162
1163 return ver;
1164}
1165
1166static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001167nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001168{
Michal Vasko131120a2018-05-29 15:44:02 +02001169 NC_MSG_TYPE ret;
1170 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001171 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001172 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001173
Michal Vasko131120a2018-05-29 15:44:02 +02001174 if (session->side == NC_CLIENT) {
1175 /* client side hello - send only NETCONF base capabilities */
1176 cpblts = malloc(3 * sizeof *cpblts);
1177 if (!cpblts) {
1178 ERRMEM;
1179 return NC_MSG_ERROR;
1180 }
1181 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1182 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1183 cpblts[2] = NULL;
1184
1185 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1186 sid = NULL;
1187 } else {
1188 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
1189
1190 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1191 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001192 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001193
Michal Vasko131120a2018-05-29 15:44:02 +02001194 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001195
Michal Vasko086311b2016-01-08 09:53:11 +01001196 for (i = 0; cpblts[i]; ++i) {
1197 lydict_remove(session->ctx, cpblts[i]);
1198 }
1199 free(cpblts);
1200
Michal Vasko131120a2018-05-29 15:44:02 +02001201 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001202}
1203
1204static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001205nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001206{
1207 struct lyxml_elem *xml = NULL, *node;
Michal Vasko131120a2018-05-29 15:44:02 +02001208 NC_MSG_TYPE msgtype;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001209 int ver = -1;
1210 char *str;
1211 long long int id;
1212 int flag = 0;
1213
Michal Vasko131120a2018-05-29 15:44:02 +02001214 msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001215
Michal Vasko131120a2018-05-29 15:44:02 +02001216 switch (msgtype) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001217 case NC_MSG_HELLO:
1218 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001219 LY_TREE_FOR(xml->child, node) {
1220 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1221 continue;
1222 } else if (!strcmp(node->name, "session-id")) {
1223 if (!node->content || !strlen(node->content)) {
1224 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001225 goto error;
1226 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001227 str = NULL;
1228 id = strtoll(node->content, &str, 10);
1229 if (*str || id < 1 || id > UINT32_MAX) {
1230 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001231 goto error;
1232 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001233 session->id = (uint32_t)id;
1234 continue;
1235 } else if (strcmp(node->name, "capabilities")) {
1236 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001237 goto error;
1238 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001239
1240 if (flag) {
1241 /* multiple capabilities elements */
1242 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1243 goto error;
1244 }
1245 flag = 1;
1246
Michal Vasko2e6defd2016-10-07 15:48:15 +02001247 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001248 goto error;
1249 }
1250 session->version = ver;
1251 }
1252
1253 if (!session->id) {
1254 ERR("Missing <session-id> in server's <hello>.");
1255 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001256 }
1257 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001258 case NC_MSG_WOULDBLOCK:
1259 ERR("Server's <hello> timeout elapsed.");
1260 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001261 case NC_MSG_ERROR:
1262 /* nothing special, just pass it out */
1263 break;
1264 default:
1265 ERR("Unexpected message received instead of <hello>.");
1266 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001267 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001268 }
1269
1270 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001271 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001272
1273 return msgtype;
1274
1275error:
1276 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001277 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001278
1279 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001280}
1281
Michal Vasko11d142a2016-01-19 15:58:24 +01001282static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001283nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001284{
1285 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001286 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001287 int ver = -1;
1288 int flag = 0;
1289
Michal Vasko131120a2018-05-29 15:44:02 +02001290 msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ?
1291 server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001292
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001293 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001294 case NC_MSG_HELLO:
1295 /* get know NETCONF version */
1296 LY_TREE_FOR(xml->child, node) {
1297 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1298 continue;
1299 } else if (strcmp(node->name, "capabilities")) {
1300 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001301 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001302 goto cleanup;
1303 }
1304
1305 if (flag) {
1306 /* multiple capabilities elements */
1307 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001308 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001309 goto cleanup;
1310 }
1311 flag = 1;
1312
1313 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001314 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001315 goto cleanup;
1316 }
1317 session->version = ver;
1318 }
1319 break;
1320 case NC_MSG_ERROR:
1321 /* nothing special, just pass it out */
1322 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001323 case NC_MSG_WOULDBLOCK:
1324 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001325 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001326 default:
1327 ERR("Unexpected message received instead of <hello>.");
1328 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001329 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001330 }
1331
1332cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001333 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001334 return msgtype;
1335}
1336
Michal Vasko71090fc2016-05-24 16:37:28 +02001337NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001338nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001339{
Michal Vasko086311b2016-01-08 09:53:11 +01001340 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001341
Michal Vasko131120a2018-05-29 15:44:02 +02001342 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001343 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001344 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001345 }
1346
Michal Vasko11d142a2016-01-19 15:58:24 +01001347 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001348 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001349 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001350 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001351 }
1352
Michal Vasko71090fc2016-05-24 16:37:28 +02001353 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001354}
Michal Vasko086311b2016-01-08 09:53:11 +01001355
Radek Krejci53691be2016-02-22 13:58:37 +01001356#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001357
Michal Vasko8f0c0282016-02-29 10:17:14 +01001358static void
Michal Vasko086311b2016-01-08 09:53:11 +01001359nc_ssh_init(void)
1360{
1361 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1362 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001363}
1364
Michal Vasko8f0c0282016-02-29 10:17:14 +01001365static void
Michal Vasko086311b2016-01-08 09:53:11 +01001366nc_ssh_destroy(void)
1367{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001368 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001369 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001370 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001371 ssh_finalize();
1372}
1373
Radek Krejci53691be2016-02-22 13:58:37 +01001374#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001375
Radek Krejci53691be2016-02-22 13:58:37 +01001376#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001377
Michal Vasko770b4362017-02-17 14:44:18 +01001378#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1379
Michal Vaskof0c92c02016-01-29 09:41:45 +01001380struct CRYPTO_dynlock_value {
1381 pthread_mutex_t lock;
1382};
1383
Michal Vaskof0c92c02016-01-29 09:41:45 +01001384static struct CRYPTO_dynlock_value *
1385tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1386{
1387 struct CRYPTO_dynlock_value *value;
1388
1389 value = malloc(sizeof *value);
1390 if (!value) {
1391 ERRMEM;
1392 return NULL;
1393 }
1394 pthread_mutex_init(&value->lock, NULL);
1395
1396 return value;
1397}
1398
1399static void
1400tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1401{
1402 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1403 * I found ignored this fact, what do I know... */
1404 if (mode & CRYPTO_LOCK) {
1405 pthread_mutex_lock(&l->lock);
1406 } else {
1407 pthread_mutex_unlock(&l->lock);
1408 }
1409}
1410
1411static void
1412tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1413{
1414 pthread_mutex_destroy(&l->lock);
1415 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001416}
Michal Vasko770b4362017-02-17 14:44:18 +01001417#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001418
Michal Vasko8f0c0282016-02-29 10:17:14 +01001419#endif /* NC_ENABLED_TLS */
1420
1421#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1422
Michal Vasko770b4362017-02-17 14:44:18 +01001423#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001424static pthread_mutex_t *tls_locks;
1425
1426static void
1427tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1428{
1429 if (mode & CRYPTO_LOCK) {
1430 pthread_mutex_lock(tls_locks + n);
1431 } else {
1432 pthread_mutex_unlock(tls_locks + n);
1433 }
1434}
1435
1436static void
1437tls_thread_id_func(CRYPTO_THREADID *tid)
1438{
1439 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1440}
Michal Vasko770b4362017-02-17 14:44:18 +01001441#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001442
1443static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001444nc_tls_init(void)
1445{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001446 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001447 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001448 SSL_library_init();
1449
Michal Vasko770b4362017-02-17 14:44:18 +01001450#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001451 int i;
1452
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001453 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001454 if (!tls_locks) {
1455 ERRMEM;
1456 return;
1457 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001458 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1459 pthread_mutex_init(tls_locks + i, NULL);
1460 }
1461
Michal Vaskof0c92c02016-01-29 09:41:45 +01001462 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001463 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001464
1465 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1466 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1467 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001468#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001469}
1470
Michal Vasko8f0c0282016-02-29 10:17:14 +01001471static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001472nc_tls_destroy(void)
1473{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001474 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001475 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001476 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001477 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001478 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001479#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001480 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001481#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1482 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001483#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001484
Michal Vasko770b4362017-02-17 14:44:18 +01001485#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001486 int i;
1487
Michal Vaskob6e37262016-02-25 14:49:00 +01001488 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001489 CRYPTO_set_locking_callback(NULL);
1490 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1491 pthread_mutex_destroy(tls_locks + i);
1492 }
1493 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001494
1495 CRYPTO_set_dynlock_create_callback(NULL);
1496 CRYPTO_set_dynlock_lock_callback(NULL);
1497 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001498#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001499}
1500
Michal Vasko8f0c0282016-02-29 10:17:14 +01001501#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001502
Radek Krejci53691be2016-02-22 13:58:37 +01001503#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001504
Michal Vasko8f0c0282016-02-29 10:17:14 +01001505static void
Michal Vasko5e228792016-02-03 15:30:24 +01001506nc_ssh_tls_init(void)
1507{
1508 SSL_load_error_strings();
1509 ERR_load_BIO_strings();
1510 SSL_library_init();
1511
1512 nc_ssh_init();
1513
Michal Vasko770b4362017-02-17 14:44:18 +01001514#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001515 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1516 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1517 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001518#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001519}
1520
Michal Vasko8f0c0282016-02-29 10:17:14 +01001521static void
Michal Vasko5e228792016-02-03 15:30:24 +01001522nc_ssh_tls_destroy(void)
1523{
1524 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001525#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001526 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001527#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1528 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001529#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001530
1531 nc_ssh_destroy();
1532
Michal Vasko770b4362017-02-17 14:44:18 +01001533#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001534 CRYPTO_set_dynlock_create_callback(NULL);
1535 CRYPTO_set_dynlock_lock_callback(NULL);
1536 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001537#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001538}
1539
Radek Krejci53691be2016-02-22 13:58:37 +01001540#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001541
1542#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1543
1544API void
1545nc_thread_destroy(void)
1546{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001547 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1548 //CRYPTO_cleanup_all_ex_data();
1549
Michal Vasko770b4362017-02-17 14:44:18 +01001550#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1551 CRYPTO_THREADID crypto_tid;
1552
Michal Vasko8f0c0282016-02-29 10:17:14 +01001553 CRYPTO_THREADID_current(&crypto_tid);
1554 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001555#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001556}
1557
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001558#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1559
1560void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001561nc_init(void)
1562{
1563#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1564 nc_ssh_tls_init();
1565#elif defined(NC_ENABLED_SSH)
1566 nc_ssh_init();
1567#elif defined(NC_ENABLED_TLS)
1568 nc_tls_init();
1569#endif
1570}
1571
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001572void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001573nc_destroy(void)
1574{
1575#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1576 nc_ssh_tls_destroy();
1577#elif defined(NC_ENABLED_SSH)
1578 nc_ssh_destroy();
1579#elif defined(NC_ENABLED_TLS)
1580 nc_tls_destroy();
1581#endif
1582}