blob: 0b11ee274db5059c135936c4bce5c9d3802dadcc [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 Vasko58f31552016-01-19 12:39:15 +010023#include <time.h>
Michal Vasko156d3272017-04-11 11:46:49 +020024#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020025#include <libyang/libyang.h>
26
Michal Vasko5e228792016-02-03 15:30:24 +010027#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010028#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010029#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010030
Radek Krejci53691be2016-02-22 13:58:37 +010031#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020032
Michal Vasko086311b2016-01-08 09:53:11 +010033# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020034
Radek Krejci53691be2016-02-22 13:58:37 +010035#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020036
Radek Krejci53691be2016-02-22 13:58:37 +010037#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010038
Michal Vasko5e228792016-02-03 15:30:24 +010039# include <openssl/engine.h>
40# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010041# include <openssl/err.h>
42
Radek Krejci53691be2016-02-22 13:58:37 +010043#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044
Michal Vasko086311b2016-01-08 09:53:11 +010045/* in seconds */
46#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080047#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020048
Michal Vasko05ba9df2016-01-13 14:40:27 +010049/* in milliseconds */
50#define NC_CLOSE_REPLY_TIMEOUT 200
51
Michal Vasko086311b2016-01-08 09:53:11 +010052extern struct nc_server_opts server_opts;
53
Radek Krejci7ac16052016-07-15 11:48:18 +020054int
Michal Vasko77a6abe2017-10-05 10:02:20 +020055nc_gettimespec_mono(struct timespec *ts)
56{
57#ifdef CLOCK_MONOTONIC_RAW
58 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
59#elif defined(CLOCK_MONOTONIC)
60 return clock_gettime(CLOCK_MONOTONIC, ts);
61#else
62 /* no monotonic clock available, return realtime */
63 return nc_gettimespec_real(ts);
64#endif
65}
66
67int
68nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020069{
Michal Vasko0ef46df2016-09-21 14:03:18 +020070#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020071 return clock_gettime(CLOCK_REALTIME, ts);
72#else
73 int rc;
74 struct timeval tv;
75
76 rc = gettimeofday(&tv, NULL);
77 if (!rc) {
78 ts->tv_sec = (time_t)tv.tv_sec;
79 ts->tv_nsec = 1000L * (long)tv.tv_usec;
80 }
81 return rc;
82#endif
83}
84
Michal Vasko36c7be82017-02-22 13:37:59 +010085/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
86int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020087nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010088{
Michal Vasko36c7be82017-02-22 13:37:59 +010089 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010090
Michal Vaskoa82bd202017-03-03 14:07:29 +010091 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
92 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010093
94 return (nsec_diff ? nsec_diff / 1000000L : 0);
95}
96
Michal Vasko36c7be82017-02-22 13:37:59 +010097void
98nc_addtimespec(struct timespec *ts, uint32_t msec)
99{
Michal Vasko81c5b302017-03-15 12:10:40 +0100100 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
101
Michal Vasko0dfcd332017-03-03 13:14:39 +0100102 ts->tv_sec += msec / 1000;
103 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100104
Michal Vasko81c5b302017-03-15 12:10:40 +0100105 if (ts->tv_nsec >= 1000000000L) {
106 ++ts->tv_sec;
107 ts->tv_nsec -= 1000000000L;
108 } else if (ts->tv_nsec < 0) {
109 --ts->tv_sec;
110 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100111 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100112
113 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100114}
115
Radek Krejci28472922016-07-15 11:51:16 +0200116#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
117int
118pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
119{
Michal Vasko81c5b302017-03-15 12:10:40 +0100120 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200121 int rc;
122 struct timespec cur, dur;
123
124 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
125 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
Michal Vasko145619f2018-02-16 10:08:45 +0100126 nc_gettimespec_real(&cur);
Radek Krejci28472922016-07-15 11:51:16 +0200127
Michal Vasko81c5b302017-03-15 12:10:40 +0100128 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
129 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200130 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100131 } else if (diff < 5) {
132 /* sleep until timeout */
133 dur = *abstime;
134 } else {
135 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200136 dur.tv_sec = 0;
137 dur.tv_nsec = 5000000;
138 }
139
140 nanosleep(&dur, NULL);
141 }
142
143 return rc;
144}
145#endif
146
Michal Vaskoade892d2017-02-22 13:40:35 +0100147struct nc_session *
148nc_new_session(int not_allocate_ti)
149{
150 struct nc_session *sess;
151
152 sess = calloc(1, sizeof *sess);
153 if (!sess) {
154 return NULL;
155 }
156
157 if (!not_allocate_ti) {
158 sess->ti_lock = malloc(sizeof *sess->ti_lock);
159 sess->ti_cond = malloc(sizeof *sess->ti_cond);
160 sess->ti_inuse = malloc(sizeof *sess->ti_inuse);
161 if (!sess->ti_lock || !sess->ti_cond || !sess->ti_inuse) {
162 free(sess->ti_lock);
163 free(sess->ti_cond);
164 free((int *)sess->ti_inuse);
165 free(sess);
166 return NULL;
167 }
168 }
169
170 return sess;
171}
172
Michal Vasko96164bf2016-01-21 15:41:58 +0100173/*
174 * @return 1 - success
175 * 0 - timeout
176 * -1 - error
177 */
178int
Michal Vaskoade892d2017-02-22 13:40:35 +0100179nc_session_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100180{
181 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100182 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100183
184 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200185 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100186 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100187
Michal Vaskoade892d2017-02-22 13:40:35 +0100188 /* LOCK */
189 ret = pthread_mutex_timedlock(session->ti_lock, &ts_timeout);
190 if (!ret) {
191 while (*session->ti_inuse) {
192 ret = pthread_cond_timedwait(session->ti_cond, session->ti_lock, &ts_timeout);
193 if (ret) {
194 pthread_mutex_unlock(session->ti_lock);
195 break;
196 }
197 }
198 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100199 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100200 if (*session->ti_inuse) {
201 /* immediate timeout */
202 return 0;
203 }
204
205 /* LOCK */
206 ret = pthread_mutex_trylock(session->ti_lock);
207 if (!ret) {
208 /* be extra careful, someone could have been faster */
209 if (*session->ti_inuse) {
210 pthread_mutex_unlock(session->ti_lock);
211 return 0;
212 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200213 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100214 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100215 /* LOCK */
216 ret = pthread_mutex_lock(session->ti_lock);
217 if (!ret) {
218 while (*session->ti_inuse) {
219 ret = pthread_cond_wait(session->ti_cond, session->ti_lock);
220 if (ret) {
221 pthread_mutex_unlock(session->ti_lock);
222 break;
223 }
224 }
225 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100226 }
227
Michal Vaskoade892d2017-02-22 13:40:35 +0100228 if (ret) {
229 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
230 /* timeout */
231 return 0;
232 }
233
Michal Vasko96164bf2016-01-21 15:41:58 +0100234 /* error */
Michal Vaskoade892d2017-02-22 13:40:35 +0100235 ERR("%s: failed to lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100236 return -1;
237 }
238
239 /* ok */
Michal Vaskoade892d2017-02-22 13:40:35 +0100240 assert(*session->ti_inuse == 0);
241 *session->ti_inuse = 1;
242
243 /* UNLOCK */
244 ret = pthread_mutex_unlock(session->ti_lock);
245 if (ret) {
246 /* error */
247 ERR("%s: faile to unlock a session (%s).", func, strerror(ret));
248 return -1;
249 }
250
251 return 1;
252}
253
254int
255nc_session_unlock(struct nc_session *session, int timeout, const char *func)
256{
257 int ret;
258 struct timespec ts_timeout;
259
260 assert(*session->ti_inuse);
261
262 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200263 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100264 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100265
266 /* LOCK */
267 ret = pthread_mutex_timedlock(session->ti_lock, &ts_timeout);
268 } else if (!timeout) {
269 /* LOCK */
270 ret = pthread_mutex_trylock(session->ti_lock);
271 } else { /* timeout == -1 */
272 /* LOCK */
273 ret = pthread_mutex_lock(session->ti_lock);
274 }
275
276 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
277 /* error */
278 ERR("%s: failed to lock a session (%s).", func, strerror(ret));
279 return -1;
280 } else if (ret) {
281 WRN("%s: session lock timeout, should not happen.");
282 }
283
284 *session->ti_inuse = 0;
285 pthread_cond_signal(session->ti_cond);
286
287 if (!ret) {
288 /* UNLOCK */
289 ret = pthread_mutex_unlock(session->ti_lock);
290 if (ret) {
291 /* error */
292 ERR("%s: failed to unlock a session (%s).", func, strerror(ret));
293 return -1;
294 }
295 }
296
Michal Vasko96164bf2016-01-21 15:41:58 +0100297 return 1;
298}
299
Michal Vasko8dadf782016-01-15 10:29:36 +0100300API NC_STATUS
301nc_session_get_status(const struct nc_session *session)
302{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100303 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200304 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200305 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100306 }
307
Michal Vasko8dadf782016-01-15 10:29:36 +0100308 return session->status;
309}
310
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100311API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200312nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100313{
314 if (!session) {
315 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200316 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100317 }
318
319 return session->term_reason;
320}
321
Michal Vasko8dadf782016-01-15 10:29:36 +0100322API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200323nc_session_get_killed_by(const struct nc_session *session)
324{
325 if (!session) {
326 ERRARG("session");
327 return 0;
328 }
329
330 return session->killed_by;
331}
332
333API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100334nc_session_get_id(const struct nc_session *session)
335{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100336 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200337 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100338 return 0;
339 }
340
Michal Vasko8dadf782016-01-15 10:29:36 +0100341 return session->id;
342}
343
Michal Vasko174fe8e2016-02-17 15:38:09 +0100344API int
345nc_session_get_version(const struct nc_session *session)
346{
347 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200348 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100349 return -1;
350 }
351
352 return (session->version == NC_VERSION_10 ? 0 : 1);
353}
354
Michal Vasko8dadf782016-01-15 10:29:36 +0100355API NC_TRANSPORT_IMPL
356nc_session_get_ti(const struct nc_session *session)
357{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100358 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200359 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100360 return 0;
361 }
362
Michal Vasko8dadf782016-01-15 10:29:36 +0100363 return session->ti_type;
364}
365
366API const char *
367nc_session_get_username(const struct nc_session *session)
368{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100369 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200370 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100371 return NULL;
372 }
373
Michal Vasko8dadf782016-01-15 10:29:36 +0100374 return session->username;
375}
376
377API const char *
378nc_session_get_host(const struct nc_session *session)
379{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100380 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200381 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100382 return NULL;
383 }
384
Michal Vasko8dadf782016-01-15 10:29:36 +0100385 return session->host;
386}
387
388API uint16_t
389nc_session_get_port(const struct nc_session *session)
390{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100391 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200392 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100393 return 0;
394 }
395
Michal Vasko8dadf782016-01-15 10:29:36 +0100396 return session->port;
397}
398
Michal Vasko9a25e932016-02-01 10:36:42 +0100399API struct ly_ctx *
400nc_session_get_ctx(const struct nc_session *session)
401{
402 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200403 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100404 return NULL;
405 }
406
407 return session->ctx;
408}
409
Michal Vasko2cc4c682016-03-01 09:16:48 +0100410API void
411nc_session_set_data(struct nc_session *session, void *data)
412{
413 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200414 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100415 return;
416 }
417
418 session->data = data;
419}
420
421API void *
422nc_session_get_data(const struct nc_session *session)
423{
424 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200425 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100426 return NULL;
427 }
428
429 return session->data;
430}
431
Michal Vasko086311b2016-01-08 09:53:11 +0100432NC_MSG_TYPE
433nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200434{
Michal Vasko086311b2016-01-08 09:53:11 +0100435 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200436
Michal Vasko086311b2016-01-08 09:53:11 +0100437 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100438 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
439 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100440 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100441 }
442
Michal Vasko086311b2016-01-08 09:53:11 +0100443 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
444
445 if (r) {
446 return NC_MSG_ERROR;
447 }
448
449 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100450}
451
Radek Krejci695d4fa2015-10-22 13:23:54 +0200452API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100453nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200454{
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100455 int r, i, locked, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100456 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100457 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100458 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100459 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100460 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100461 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200462 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100463 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200464 void *p;
465
Michal Vasko428087d2016-01-14 16:04:28 +0100466 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200467 return;
468 }
469
Michal Vasko86d357c2016-03-11 13:46:38 +0100470 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200471 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
472 tid = *session->opts.client.ntf_tid;
473 free((pthread_t *)session->opts.client.ntf_tid);
474 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100475 /* the thread now knows it should quit */
476
477 pthread_join(tid, NULL);
478 }
479
Michal Vaskoadd4c792015-10-26 15:36:58 +0100480 if (session->ti_lock) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100481 r = nc_session_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100482 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100483 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100484 } else if (!r) {
485 /* we failed to lock it, too bad */
486 locked = 0;
487 } else {
488 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100489 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200490 } else {
491 ERRINT;
492 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200493 }
494
Michal Vasko9e99f012016-03-03 13:25:20 +0100495 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200496 /* cleanup message queues */
497 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200498 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100499 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200500
Michal Vaskoad611702015-12-03 13:41:51 +0100501 p = contiter;
502 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200503 free(p);
504 }
505
506 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200507 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100508 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200509
Michal Vaskoad611702015-12-03 13:41:51 +0100510 p = contiter;
511 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200512 free(p);
513 }
514
515 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200516 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200517 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100518 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200519 } else {
520 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100521 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200522 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100523 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100524 case NC_MSG_REPLY:
525 LY_TREE_FOR(rpl->child, child) {
526 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
527 break;
528 }
529 }
530 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100531 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100532 }
Michal Vaskoad611702015-12-03 13:41:51 +0100533 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100534 break;
535 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100536 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100537 break;
538 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100539 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100540 break;
541 default:
542 /* cannot happen */
543 break;
544 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200545 }
546
547 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200548 if (session->opts.client.cpblts) {
549 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200550 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200551 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200552 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200553 }
554 }
555
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100556 if (session->data && data_free) {
557 data_free(session->data);
558 }
559
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200560 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
561 /* CH LOCK */
562 pthread_mutex_lock(session->opts.server.ch_lock);
563 }
564
Michal Vasko86d357c2016-03-11 13:46:38 +0100565 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200566 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200567
568 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
569 pthread_cond_signal(session->opts.server.ch_cond);
570
571 /* CH UNLOCK */
572 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100573
574 /* wait for CH thread to actually wake up */
575 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
576 while (i && (session->flags & NC_SESSION_CALLHOME)) {
577 usleep(NC_TIMEOUT_STEP);
578 --i;
579 }
580 if (session->flags & NC_SESSION_CALLHOME) {
581 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
582 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200583 }
584
Michal Vasko428087d2016-01-14 16:04:28 +0100585 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200586
587 /* transport implementation cleanup */
588 switch (session->ti_type) {
589 case NC_TI_FD:
590 /* nothing needed - file descriptors were provided by caller,
591 * so it is up to the caller to close them correctly
592 * TODO use callbacks
593 */
Michal Vasko3512e402016-01-28 16:22:34 +0100594 /* just to avoid compiler warning */
595 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100596 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200597 break;
598
Radek Krejci53691be2016-02-22 13:58:37 +0100599#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200600 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100601 if (connected) {
602 ssh_channel_free(session->ti.libssh.channel);
603 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200604 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
605 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
606 * it.
607 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100608 multisession = 0;
609 if (session->ti.libssh.next) {
610 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
611 if (siter->status != NC_STATUS_STARTING) {
612 multisession = 1;
613 break;
614 }
615 }
616 }
617
618 if (!multisession) {
619 /* it's not multisession yet, but we still need to free the starting sessions */
620 if (session->ti.libssh.next) {
621 do {
622 siter = session->ti.libssh.next;
623 session->ti.libssh.next = siter->ti.libssh.next;
624
625 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100626 lydict_remove(session->ctx, session->username);
627 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100628 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100629 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100630 }
631
632 free(siter);
633 } while (session->ti.libssh.next != session);
634 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100635 /* remember sock so we can close it */
636 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100637 if (connected) {
638 ssh_disconnect(session->ti.libssh.session);
639 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200640 ssh_free(session->ti.libssh.session);
641 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200642 /* remove the session from the list */
643 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100644 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200645 /* there will be only one session */
646 siter->ti.libssh.next = NULL;
647 } else {
648 /* there are still multiple sessions, keep the ring list */
649 siter->ti.libssh.next = session->ti.libssh.next;
650 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100651 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
652 if (session->flags & NC_SESSION_SSH_MSG_CB) {
653 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
654 if (siter->ti.libssh.next == session) {
655 ERRINT;
656 break;
657 }
658 }
659 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
660 siter->flags |= NC_SESSION_SSH_MSG_CB;
661 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200662 }
663 break;
664#endif
665
Radek Krejci53691be2016-02-22 13:58:37 +0100666#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200667 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100668 /* remember sock so we can close it */
669 sock = SSL_get_fd(session->ti.tls);
670
Michal Vasko428087d2016-01-14 16:04:28 +0100671 if (connected) {
672 SSL_shutdown(session->ti.tls);
673 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200674 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100675
Michal Vasko2e6defd2016-10-07 15:48:15 +0200676 if (session->side == NC_SERVER) {
677 X509_free(session->opts.server.client_cert);
678 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200679 break;
680#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100681 case NC_TI_NONE:
682 ERRINT;
683 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200684 }
Michal Vasko428087d2016-01-14 16:04:28 +0100685
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100686 /* close socket separately */
687 if (sock > -1) {
688 close(sock);
689 }
690
Radek Krejciac6d3472015-10-22 15:47:18 +0200691 lydict_remove(session->ctx, session->username);
692 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200693
694 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100695 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100696 if (locked) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100697 nc_session_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100698 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100699 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100700 pthread_mutex_destroy(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100701 pthread_cond_destroy(session->ti_cond);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100702 free(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100703 free(session->ti_cond);
704 free((int *)session->ti_inuse);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100705 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200706 }
707
708 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100709 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200710 }
711
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200712 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100713 /* free CH synchronization structures if used */
714 if (session->opts.server.ch_cond) {
715 pthread_cond_destroy(session->opts.server.ch_cond);
716 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200717 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100718 if (session->opts.server.ch_lock) {
719 pthread_mutex_destroy(session->opts.server.ch_lock);
720 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200721 }
722 }
723
Radek Krejci695d4fa2015-10-22 13:23:54 +0200724 free(session);
725}
726
Michal Vasko086311b2016-01-08 09:53:11 +0100727static void
728add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
729{
Radek Krejci658782b2016-12-04 22:04:55 +0100730 size_t len;
731 int i;
732 char *p;
733
734 if (capab) {
735 /* check if already present */
736 p = strchr(capab, '?');
737 if (p) {
738 len = p - capab;
739 } else {
740 len = strlen(capab);
741 }
742 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800743 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100744 /* already present, do not duplicate it */
745 return;
746 }
747 }
748 }
749
750 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100751 if (*count == *size) {
752 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100753 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
754 if (!(*cpblts)) {
755 ERRMEM;
756 return;
757 }
Michal Vasko086311b2016-01-08 09:53:11 +0100758 }
759
760 if (capab) {
761 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
762 } else {
763 (*cpblts)[*count] = NULL;
764 }
765 ++(*count);
766}
767
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200768API const char **
769nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100770{
771 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200772 struct lyd_node_leaf_list **features = NULL, **deviations = NULL, *ns = NULL, *rev = NULL, *name = NULL, *module_set_id = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100773 const char **cpblts;
774 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200775 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Radek Krejci658782b2016-12-04 22:04:55 +0100776 unsigned int u;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200777#define NC_CPBLT_BUF_LEN 512
778 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100779
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200780 if (!ctx) {
781 ERRARG("ctx");
782 return NULL;
783 }
784
Michal Vasko086311b2016-01-08 09:53:11 +0100785 yanglib = ly_ctx_info(ctx);
786 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200787 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100788 return NULL;
789 }
790
791 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100792 if (!cpblts) {
793 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200794 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100795 }
Michal Vasko086311b2016-01-08 09:53:11 +0100796 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
797 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
798 count = 2;
799
800 /* capabilities */
801
Radek Krejci3222b7d2017-09-21 16:04:30 +0200802 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100803 if (mod) {
804 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100805 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100806 }
807 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100808 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100809 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100810 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100811 }
812 }
813 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100814 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100815 }
816 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100817 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100818 }
819 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100820 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100821 }
822 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100823 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100824 }
825 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100826 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100827 }
828 }
829
Radek Krejci3222b7d2017-09-21 16:04:30 +0200830 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100831 if (mod) {
832 if (!server_opts.wd_basic_mode) {
833 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
834 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100835 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100836 switch (server_opts.wd_basic_mode) {
837 case NC_WD_ALL:
838 strcat(str, "?basic-mode=report-all");
839 break;
840 case NC_WD_TRIM:
841 strcat(str, "?basic-mode=trim");
842 break;
843 case NC_WD_EXPLICIT:
844 strcat(str, "?basic-mode=explicit");
845 break;
846 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100847 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100848 break;
849 }
850
851 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200852 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100853 if (server_opts.wd_also_supported & NC_WD_ALL) {
854 strcat(str, "report-all,");
855 }
856 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
857 strcat(str, "report-all-tagged,");
858 }
859 if (server_opts.wd_also_supported & NC_WD_TRIM) {
860 strcat(str, "trim,");
861 }
862 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
863 strcat(str, "explicit,");
864 }
865 str[strlen(str) - 1] = '\0';
866
867 add_cpblt(ctx, str, &cpblts, &size, &count);
868 }
869 }
870 }
871
Radek Krejci658782b2016-12-04 22:04:55 +0100872 /* other capabilities */
873 for (u = 0; u < server_opts.capabilities_count; u++) {
874 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100875 }
876
877 /* models */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200878 LY_TREE_FOR(yanglib->prev->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200879 if (!module_set_id) {
880 if (strcmp(child->prev->schema->name, "module-set-id")) {
881 ERRINT;
Radek Krejcif906e412017-09-22 14:44:45 +0200882 goto error;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200883 }
884 module_set_id = (struct lyd_node_leaf_list *)child->prev;
885 }
Michal Vasko086311b2016-01-08 09:53:11 +0100886 if (!strcmp(child->schema->name, "module")) {
887 LY_TREE_FOR(child->child, child2) {
888 if (!strcmp(child2->schema->name, "namespace")) {
889 ns = (struct lyd_node_leaf_list *)child2;
890 } else if (!strcmp(child2->schema->name, "name")) {
891 name = (struct lyd_node_leaf_list *)child2;
892 } else if (!strcmp(child2->schema->name, "revision")) {
893 rev = (struct lyd_node_leaf_list *)child2;
894 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100895 features = nc_realloc(features, ++feat_count * sizeof *features);
896 if (!features) {
897 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200898 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100899 }
Michal Vasko086311b2016-01-08 09:53:11 +0100900 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200901 } else if (!strcmp(child2->schema->name, "deviation")) {
902 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
903 if (!deviations) {
904 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200905 goto error;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200906 }
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200907 deviations[dev_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vasko086311b2016-01-08 09:53:11 +0100908 }
909 }
910
911 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100912 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100913 continue;
914 }
915
Radek Krejcidf7ba522016-03-01 16:05:25 +0100916 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200917 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100918 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200919 strcat(str, "&features=");
920 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100921 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200922 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100923 ERRINT;
924 break;
925 }
Michal Vasko086311b2016-01-08 09:53:11 +0100926 if (i) {
927 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100928 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100929 }
930 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100931 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100932 }
933 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200934 if (dev_count) {
935 strcat(str, "&deviations=");
936 str_len += 12;
937 for (i = 0; i < dev_count; ++i) {
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200938 LY_TREE_FOR(((struct lyd_node *)deviations[i])->child, child2) {
939 if (!strcmp(child2->schema->name, "name"))
940 break;
941 }
942 if (!child2) {
943 ERRINT;
944 continue;
945 }
946
947 if (str_len + 1 + strlen(((struct lyd_node_leaf_list *)child2)->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200948 ERRINT;
949 break;
950 }
951 if (i) {
952 strcat(str, ",");
953 ++str_len;
954 }
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200955 strcat(str, ((struct lyd_node_leaf_list *)child2)->value_str);
956 str_len += strlen(((struct lyd_node_leaf_list *)child2)->value_str);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200957 }
958 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200959 if (!strcmp(name->value_str, "ietf-yang-library")) {
Michal Vasko7a7d5742017-03-21 15:33:23 +0100960 sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200961 }
Michal Vasko086311b2016-01-08 09:53:11 +0100962
963 add_cpblt(ctx, str, &cpblts, &size, &count);
964
965 ns = NULL;
966 name = NULL;
967 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200968 if (features || feat_count) {
969 free(features);
970 features = NULL;
971 feat_count = 0;
972 }
973 if (deviations || dev_count) {
974 free(deviations);
975 deviations = NULL;
976 dev_count = 0;
977 }
Michal Vasko086311b2016-01-08 09:53:11 +0100978 }
979 }
980
Radek Krejcif906e412017-09-22 14:44:45 +0200981 lyd_free_withsiblings(yanglib);
Michal Vasko086311b2016-01-08 09:53:11 +0100982
983 /* ending NULL capability */
984 add_cpblt(ctx, NULL, &cpblts, &size, &count);
985
986 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +0200987
988error:
989
990 free(cpblts);
991 free(features);
992 free(deviations);
993 lyd_free_withsiblings(yanglib);
994 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100995}
996
Radek Krejci695d4fa2015-10-22 13:23:54 +0200997static int
Michal Vasko2cb24b42017-05-23 14:59:11 +0200998parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200999{
1000 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001001 int ver = -1, i = 0;
1002 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001003
1004 if (list) {
1005 /* get the storage for server's capabilities */
1006 LY_TREE_FOR(xml->child, cpblt) {
1007 i++;
1008 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001009 /* last item remains NULL */
1010 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001011 if (!*list) {
1012 ERRMEM;
1013 return -1;
1014 }
1015 i = 0;
1016 }
1017
1018 LY_TREE_FOR(xml->child, cpblt) {
1019 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
1020 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1021 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1022 return -1;
1023 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
1024 continue;
1025 }
1026
Michal Vasko156d3272017-04-11 11:46:49 +02001027 /* skip leading/trailing whitespaces */
1028 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1029 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1030 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1031 ERR("Empty capability \"%s\" received.", cpblt->content);
1032 return -1;
1033 }
1034
Radek Krejci695d4fa2015-10-22 13:23:54 +02001035 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001036 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001037 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001038 } 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 +02001039 ver = 1;
1040 }
1041
1042 /* store capabilities */
1043 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001044 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1045 if (!(*list)[i]) {
1046 ERRMEM;
1047 return -1;
1048 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001049 i++;
1050 }
1051 }
1052
1053 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001054 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001055 }
1056
1057 return ver;
1058}
1059
1060static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001061nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001062{
1063 int r, i;
1064 const char **cpblts;
1065
Michal Vasko11d142a2016-01-19 15:58:24 +01001066 /* client side hello - send only NETCONF base capabilities */
1067 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001068 if (!cpblts) {
1069 ERRMEM;
1070 return NC_MSG_ERROR;
1071 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001072 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1073 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1074 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001075
Michal Vasko11d142a2016-01-19 15:58:24 +01001076 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001077
Michal Vasko086311b2016-01-08 09:53:11 +01001078 for (i = 0; cpblts[i]; ++i) {
1079 lydict_remove(session->ctx, cpblts[i]);
1080 }
1081 free(cpblts);
1082
1083 if (r) {
1084 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001085 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001086
1087 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +01001088}
1089
1090static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001091nc_send_server_hello(struct nc_session *session)
1092{
1093 int r, i;
1094 const char **cpblts;
1095
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001096 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +01001097
1098 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
1099
Michal Vasko11d142a2016-01-19 15:58:24 +01001100 for (i = 0; cpblts[i]; ++i) {
1101 lydict_remove(session->ctx, cpblts[i]);
1102 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001103 free(cpblts);
1104
1105 if (r) {
1106 return NC_MSG_ERROR;
1107 }
1108
1109 return NC_MSG_HELLO;
1110}
1111
1112static NC_MSG_TYPE
1113nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001114{
1115 struct lyxml_elem *xml = NULL, *node;
1116 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1117 int ver = -1;
1118 char *str;
1119 long long int id;
1120 int flag = 0;
1121
Michal Vasko05ba9df2016-01-13 14:40:27 +01001122 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001123
1124 switch(msgtype) {
1125 case NC_MSG_HELLO:
1126 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001127 LY_TREE_FOR(xml->child, node) {
1128 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1129 continue;
1130 } else if (!strcmp(node->name, "session-id")) {
1131 if (!node->content || !strlen(node->content)) {
1132 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001133 goto error;
1134 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001135 str = NULL;
1136 id = strtoll(node->content, &str, 10);
1137 if (*str || id < 1 || id > UINT32_MAX) {
1138 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001139 goto error;
1140 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001141 session->id = (uint32_t)id;
1142 continue;
1143 } else if (strcmp(node->name, "capabilities")) {
1144 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001145 goto error;
1146 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001147
1148 if (flag) {
1149 /* multiple capabilities elements */
1150 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1151 goto error;
1152 }
1153 flag = 1;
1154
Michal Vasko2e6defd2016-10-07 15:48:15 +02001155 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001156 goto error;
1157 }
1158 session->version = ver;
1159 }
1160
1161 if (!session->id) {
1162 ERR("Missing <session-id> in server's <hello>.");
1163 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001164 }
1165 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001166 case NC_MSG_WOULDBLOCK:
1167 ERR("Server's <hello> timeout elapsed.");
1168 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001169 case NC_MSG_ERROR:
1170 /* nothing special, just pass it out */
1171 break;
1172 default:
1173 ERR("Unexpected message received instead of <hello>.");
1174 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001175 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001176 }
1177
1178 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001179 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001180
1181 return msgtype;
1182
1183error:
1184 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001185 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001186
1187 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001188}
1189
Michal Vasko11d142a2016-01-19 15:58:24 +01001190static NC_MSG_TYPE
1191nc_recv_server_hello(struct nc_session *session)
1192{
1193 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001194 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001195 int ver = -1;
1196 int flag = 0;
1197
fanchanghu75888b62017-08-01 19:51:20 +08001198 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001199
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001200 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001201 case NC_MSG_HELLO:
1202 /* get know NETCONF version */
1203 LY_TREE_FOR(xml->child, node) {
1204 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1205 continue;
1206 } else if (strcmp(node->name, "capabilities")) {
1207 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001208 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001209 goto cleanup;
1210 }
1211
1212 if (flag) {
1213 /* multiple capabilities elements */
1214 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001215 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001216 goto cleanup;
1217 }
1218 flag = 1;
1219
1220 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001221 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001222 goto cleanup;
1223 }
1224 session->version = ver;
1225 }
1226 break;
1227 case NC_MSG_ERROR:
1228 /* nothing special, just pass it out */
1229 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001230 case NC_MSG_WOULDBLOCK:
1231 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001232 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001233 default:
1234 ERR("Unexpected message received instead of <hello>.");
1235 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001236 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001237 }
1238
1239cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001240 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001241
1242 return msgtype;
1243}
1244
Michal Vasko71090fc2016-05-24 16:37:28 +02001245NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001246nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001247{
Michal Vasko086311b2016-01-08 09:53:11 +01001248 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001249
Michal Vasko11d142a2016-01-19 15:58:24 +01001250 if (session->side == NC_CLIENT) {
1251 type = nc_send_client_hello(session);
1252 } else {
1253 type = nc_send_server_hello(session);
1254 }
1255
Michal Vasko086311b2016-01-08 09:53:11 +01001256 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001257 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001258 }
1259
Michal Vasko11d142a2016-01-19 15:58:24 +01001260 if (session->side == NC_CLIENT) {
1261 type = nc_recv_client_hello(session);
1262 } else {
1263 type = nc_recv_server_hello(session);
1264 }
1265
Michal Vasko71090fc2016-05-24 16:37:28 +02001266 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001267}
Michal Vasko086311b2016-01-08 09:53:11 +01001268
Radek Krejci53691be2016-02-22 13:58:37 +01001269#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001270
Michal Vasko8f0c0282016-02-29 10:17:14 +01001271static void
Michal Vasko086311b2016-01-08 09:53:11 +01001272nc_ssh_init(void)
1273{
1274 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1275 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001276}
1277
Michal Vasko8f0c0282016-02-29 10:17:14 +01001278static void
Michal Vasko086311b2016-01-08 09:53:11 +01001279nc_ssh_destroy(void)
1280{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001281 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001282 ENGINE_cleanup();
1283 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001284 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001285 ssh_finalize();
1286}
1287
Radek Krejci53691be2016-02-22 13:58:37 +01001288#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001289
Radek Krejci53691be2016-02-22 13:58:37 +01001290#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001291
Michal Vasko770b4362017-02-17 14:44:18 +01001292#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1293
Michal Vaskof0c92c02016-01-29 09:41:45 +01001294struct CRYPTO_dynlock_value {
1295 pthread_mutex_t lock;
1296};
1297
Michal Vaskof0c92c02016-01-29 09:41:45 +01001298static struct CRYPTO_dynlock_value *
1299tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1300{
1301 struct CRYPTO_dynlock_value *value;
1302
1303 value = malloc(sizeof *value);
1304 if (!value) {
1305 ERRMEM;
1306 return NULL;
1307 }
1308 pthread_mutex_init(&value->lock, NULL);
1309
1310 return value;
1311}
1312
1313static void
1314tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1315{
1316 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1317 * I found ignored this fact, what do I know... */
1318 if (mode & CRYPTO_LOCK) {
1319 pthread_mutex_lock(&l->lock);
1320 } else {
1321 pthread_mutex_unlock(&l->lock);
1322 }
1323}
1324
1325static void
1326tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1327{
1328 pthread_mutex_destroy(&l->lock);
1329 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001330}
Michal Vasko770b4362017-02-17 14:44:18 +01001331#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001332
Michal Vasko8f0c0282016-02-29 10:17:14 +01001333#endif /* NC_ENABLED_TLS */
1334
1335#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1336
Michal Vasko770b4362017-02-17 14:44:18 +01001337#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001338static pthread_mutex_t *tls_locks;
1339
1340static void
1341tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1342{
1343 if (mode & CRYPTO_LOCK) {
1344 pthread_mutex_lock(tls_locks + n);
1345 } else {
1346 pthread_mutex_unlock(tls_locks + n);
1347 }
1348}
1349
1350static void
1351tls_thread_id_func(CRYPTO_THREADID *tid)
1352{
1353 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1354}
Michal Vasko770b4362017-02-17 14:44:18 +01001355#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001356
1357static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001358nc_tls_init(void)
1359{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001360 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001361 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001362 SSL_library_init();
1363
Michal Vasko770b4362017-02-17 14:44:18 +01001364#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001365 int i;
1366
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001367 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001368 if (!tls_locks) {
1369 ERRMEM;
1370 return;
1371 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001372 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1373 pthread_mutex_init(tls_locks + i, NULL);
1374 }
1375
Michal Vaskof0c92c02016-01-29 09:41:45 +01001376 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001377 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001378
1379 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1380 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1381 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001382#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001383}
1384
Michal Vasko8f0c0282016-02-29 10:17:14 +01001385static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001386nc_tls_destroy(void)
1387{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001388 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001389 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001390 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001391 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001392 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001393#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001394 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001395#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1396 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001397#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001398
Michal Vasko770b4362017-02-17 14:44:18 +01001399#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001400 int i;
1401
Michal Vaskob6e37262016-02-25 14:49:00 +01001402 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001403 CRYPTO_set_locking_callback(NULL);
1404 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1405 pthread_mutex_destroy(tls_locks + i);
1406 }
1407 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001408
1409 CRYPTO_set_dynlock_create_callback(NULL);
1410 CRYPTO_set_dynlock_lock_callback(NULL);
1411 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001412#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001413}
1414
Michal Vasko8f0c0282016-02-29 10:17:14 +01001415#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001416
Radek Krejci53691be2016-02-22 13:58:37 +01001417#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001418
Michal Vasko8f0c0282016-02-29 10:17:14 +01001419static void
Michal Vasko5e228792016-02-03 15:30:24 +01001420nc_ssh_tls_init(void)
1421{
1422 SSL_load_error_strings();
1423 ERR_load_BIO_strings();
1424 SSL_library_init();
1425
1426 nc_ssh_init();
1427
Michal Vasko770b4362017-02-17 14:44:18 +01001428#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001429 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1430 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1431 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001432#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001433}
1434
Michal Vasko8f0c0282016-02-29 10:17:14 +01001435static void
Michal Vasko5e228792016-02-03 15:30:24 +01001436nc_ssh_tls_destroy(void)
1437{
1438 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001439#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001440 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001441#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1442 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001443#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001444
1445 nc_ssh_destroy();
1446
Michal Vasko770b4362017-02-17 14:44:18 +01001447#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001448 CRYPTO_set_dynlock_create_callback(NULL);
1449 CRYPTO_set_dynlock_lock_callback(NULL);
1450 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001451#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001452}
1453
Radek Krejci53691be2016-02-22 13:58:37 +01001454#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001455
1456#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1457
1458API void
1459nc_thread_destroy(void)
1460{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001461 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1462 //CRYPTO_cleanup_all_ex_data();
1463
Michal Vasko770b4362017-02-17 14:44:18 +01001464#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1465 CRYPTO_THREADID crypto_tid;
1466
Michal Vasko8f0c0282016-02-29 10:17:14 +01001467 CRYPTO_THREADID_current(&crypto_tid);
1468 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001469#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001470}
1471
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001472#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1473
1474void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001475nc_init(void)
1476{
1477#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1478 nc_ssh_tls_init();
1479#elif defined(NC_ENABLED_SSH)
1480 nc_ssh_init();
1481#elif defined(NC_ENABLED_TLS)
1482 nc_tls_init();
1483#endif
1484}
1485
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001486void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001487nc_destroy(void)
1488{
1489#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1490 nc_ssh_tls_destroy();
1491#elif defined(NC_ENABLED_SSH)
1492 nc_ssh_destroy();
1493#elif defined(NC_ENABLED_TLS)
1494 nc_tls_destroy();
1495#endif
1496}