blob: 0a8db0eac9c0f7d14d2df27f6dba17297dcc0f14 [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 Vasko18aeb5d2017-02-17 09:23:56 +01006 * Copyright (c) 2015 - 2017 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 */
14
Michal Vasko18aeb5d2017-02-17 09:23:56 +010015#include <assert.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020016#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010017#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010018#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020019#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020020#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010021#include <time.h>
Michal Vasko156d3272017-04-11 11:46:49 +020022#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020023#include <libyang/libyang.h>
24
Michal Vasko5e228792016-02-03 15:30:24 +010025#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010026#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010027#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010028
Radek Krejci53691be2016-02-22 13:58:37 +010029#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Michal Vasko086311b2016-01-08 09:53:11 +010031# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020032
Radek Krejci53691be2016-02-22 13:58:37 +010033#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020034
Radek Krejci53691be2016-02-22 13:58:37 +010035#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010036
Michal Vasko5e228792016-02-03 15:30:24 +010037# include <openssl/engine.h>
38# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010039# include <openssl/err.h>
40
Radek Krejci53691be2016-02-22 13:58:37 +010041#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010042
Michal Vasko086311b2016-01-08 09:53:11 +010043/* in seconds */
44#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080045#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020046
Michal Vasko05ba9df2016-01-13 14:40:27 +010047/* in milliseconds */
48#define NC_CLOSE_REPLY_TIMEOUT 200
49
Michal Vasko086311b2016-01-08 09:53:11 +010050extern struct nc_server_opts server_opts;
51
Radek Krejci7ac16052016-07-15 11:48:18 +020052int
Michal Vasko77a6abe2017-10-05 10:02:20 +020053nc_gettimespec_mono(struct timespec *ts)
54{
55#ifdef CLOCK_MONOTONIC_RAW
56 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
57#elif defined(CLOCK_MONOTONIC)
58 return clock_gettime(CLOCK_MONOTONIC, ts);
59#else
60 /* no monotonic clock available, return realtime */
61 return nc_gettimespec_real(ts);
62#endif
63}
64
65int
66nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020067{
Michal Vasko0ef46df2016-09-21 14:03:18 +020068#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020069 return clock_gettime(CLOCK_REALTIME, ts);
70#else
71 int rc;
72 struct timeval tv;
73
74 rc = gettimeofday(&tv, NULL);
75 if (!rc) {
76 ts->tv_sec = (time_t)tv.tv_sec;
77 ts->tv_nsec = 1000L * (long)tv.tv_usec;
78 }
79 return rc;
80#endif
81}
82
Michal Vasko36c7be82017-02-22 13:37:59 +010083/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
84int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020085nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010086{
Michal Vasko36c7be82017-02-22 13:37:59 +010087 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010088
Michal Vaskoa82bd202017-03-03 14:07:29 +010089 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
90 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010091
92 return (nsec_diff ? nsec_diff / 1000000L : 0);
93}
94
Michal Vasko36c7be82017-02-22 13:37:59 +010095void
96nc_addtimespec(struct timespec *ts, uint32_t msec)
97{
Michal Vasko81c5b302017-03-15 12:10:40 +010098 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
99
Michal Vasko0dfcd332017-03-03 13:14:39 +0100100 ts->tv_sec += msec / 1000;
101 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100102
Michal Vasko81c5b302017-03-15 12:10:40 +0100103 if (ts->tv_nsec >= 1000000000L) {
104 ++ts->tv_sec;
105 ts->tv_nsec -= 1000000000L;
106 } else if (ts->tv_nsec < 0) {
107 --ts->tv_sec;
108 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100109 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100110
111 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100112}
113
Radek Krejci28472922016-07-15 11:51:16 +0200114#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
115int
116pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
117{
Michal Vasko81c5b302017-03-15 12:10:40 +0100118 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200119 int rc;
120 struct timespec cur, dur;
121
122 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
123 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
Michal Vasko145619f2018-02-16 10:08:45 +0100124 nc_gettimespec_real(&cur);
Radek Krejci28472922016-07-15 11:51:16 +0200125
Michal Vasko81c5b302017-03-15 12:10:40 +0100126 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
127 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200128 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100129 } else if (diff < 5) {
130 /* sleep until timeout */
131 dur = *abstime;
132 } else {
133 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200134 dur.tv_sec = 0;
135 dur.tv_nsec = 5000000;
136 }
137
138 nanosleep(&dur, NULL);
139 }
140
141 return rc;
142}
143#endif
144
Michal Vaskoade892d2017-02-22 13:40:35 +0100145struct nc_session *
146nc_new_session(int not_allocate_ti)
147{
148 struct nc_session *sess;
149
150 sess = calloc(1, sizeof *sess);
151 if (!sess) {
152 return NULL;
153 }
154
155 if (!not_allocate_ti) {
156 sess->ti_lock = malloc(sizeof *sess->ti_lock);
157 sess->ti_cond = malloc(sizeof *sess->ti_cond);
158 sess->ti_inuse = malloc(sizeof *sess->ti_inuse);
159 if (!sess->ti_lock || !sess->ti_cond || !sess->ti_inuse) {
160 free(sess->ti_lock);
161 free(sess->ti_cond);
162 free((int *)sess->ti_inuse);
163 free(sess);
164 return NULL;
165 }
166 }
167
168 return sess;
169}
170
Michal Vasko96164bf2016-01-21 15:41:58 +0100171/*
172 * @return 1 - success
173 * 0 - timeout
174 * -1 - error
175 */
176int
Michal Vaskoade892d2017-02-22 13:40:35 +0100177nc_session_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100178{
179 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100180 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100181
182 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200183 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100184 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100185
Michal Vaskoade892d2017-02-22 13:40:35 +0100186 /* LOCK */
187 ret = pthread_mutex_timedlock(session->ti_lock, &ts_timeout);
188 if (!ret) {
189 while (*session->ti_inuse) {
190 ret = pthread_cond_timedwait(session->ti_cond, session->ti_lock, &ts_timeout);
191 if (ret) {
192 pthread_mutex_unlock(session->ti_lock);
193 break;
194 }
195 }
196 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100197 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100198 if (*session->ti_inuse) {
199 /* immediate timeout */
200 return 0;
201 }
202
203 /* LOCK */
204 ret = pthread_mutex_trylock(session->ti_lock);
205 if (!ret) {
206 /* be extra careful, someone could have been faster */
207 if (*session->ti_inuse) {
208 pthread_mutex_unlock(session->ti_lock);
209 return 0;
210 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200211 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100212 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100213 /* LOCK */
214 ret = pthread_mutex_lock(session->ti_lock);
215 if (!ret) {
216 while (*session->ti_inuse) {
217 ret = pthread_cond_wait(session->ti_cond, session->ti_lock);
218 if (ret) {
219 pthread_mutex_unlock(session->ti_lock);
220 break;
221 }
222 }
223 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100224 }
225
Michal Vaskoade892d2017-02-22 13:40:35 +0100226 if (ret) {
227 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
228 /* timeout */
229 return 0;
230 }
231
Michal Vasko96164bf2016-01-21 15:41:58 +0100232 /* error */
Michal Vaskoade892d2017-02-22 13:40:35 +0100233 ERR("%s: failed to lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100234 return -1;
235 }
236
237 /* ok */
Michal Vaskoade892d2017-02-22 13:40:35 +0100238 assert(*session->ti_inuse == 0);
239 *session->ti_inuse = 1;
240
241 /* UNLOCK */
242 ret = pthread_mutex_unlock(session->ti_lock);
243 if (ret) {
244 /* error */
245 ERR("%s: faile to unlock a session (%s).", func, strerror(ret));
246 return -1;
247 }
248
249 return 1;
250}
251
252int
253nc_session_unlock(struct nc_session *session, int timeout, const char *func)
254{
255 int ret;
256 struct timespec ts_timeout;
257
258 assert(*session->ti_inuse);
259
260 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200261 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100262 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100263
264 /* LOCK */
265 ret = pthread_mutex_timedlock(session->ti_lock, &ts_timeout);
266 } else if (!timeout) {
267 /* LOCK */
268 ret = pthread_mutex_trylock(session->ti_lock);
269 } else { /* timeout == -1 */
270 /* LOCK */
271 ret = pthread_mutex_lock(session->ti_lock);
272 }
273
274 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
275 /* error */
276 ERR("%s: failed to lock a session (%s).", func, strerror(ret));
277 return -1;
278 } else if (ret) {
279 WRN("%s: session lock timeout, should not happen.");
280 }
281
282 *session->ti_inuse = 0;
283 pthread_cond_signal(session->ti_cond);
284
285 if (!ret) {
286 /* UNLOCK */
287 ret = pthread_mutex_unlock(session->ti_lock);
288 if (ret) {
289 /* error */
290 ERR("%s: failed to unlock a session (%s).", func, strerror(ret));
291 return -1;
292 }
293 }
294
Michal Vasko96164bf2016-01-21 15:41:58 +0100295 return 1;
296}
297
Michal Vasko8dadf782016-01-15 10:29:36 +0100298API NC_STATUS
299nc_session_get_status(const struct nc_session *session)
300{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100301 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200302 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200303 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100304 }
305
Michal Vasko8dadf782016-01-15 10:29:36 +0100306 return session->status;
307}
308
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100309API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200310nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100311{
312 if (!session) {
313 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200314 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100315 }
316
317 return session->term_reason;
318}
319
Michal Vasko8dadf782016-01-15 10:29:36 +0100320API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200321nc_session_get_killed_by(const struct nc_session *session)
322{
323 if (!session) {
324 ERRARG("session");
325 return 0;
326 }
327
328 return session->killed_by;
329}
330
331API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100332nc_session_get_id(const struct nc_session *session)
333{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100334 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200335 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100336 return 0;
337 }
338
Michal Vasko8dadf782016-01-15 10:29:36 +0100339 return session->id;
340}
341
Michal Vasko174fe8e2016-02-17 15:38:09 +0100342API int
343nc_session_get_version(const struct nc_session *session)
344{
345 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200346 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100347 return -1;
348 }
349
350 return (session->version == NC_VERSION_10 ? 0 : 1);
351}
352
Michal Vasko8dadf782016-01-15 10:29:36 +0100353API NC_TRANSPORT_IMPL
354nc_session_get_ti(const struct nc_session *session)
355{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100356 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200357 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100358 return 0;
359 }
360
Michal Vasko8dadf782016-01-15 10:29:36 +0100361 return session->ti_type;
362}
363
364API const char *
365nc_session_get_username(const struct nc_session *session)
366{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100367 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200368 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100369 return NULL;
370 }
371
Michal Vasko8dadf782016-01-15 10:29:36 +0100372 return session->username;
373}
374
375API const char *
376nc_session_get_host(const struct nc_session *session)
377{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100378 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200379 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100380 return NULL;
381 }
382
Michal Vasko8dadf782016-01-15 10:29:36 +0100383 return session->host;
384}
385
386API uint16_t
387nc_session_get_port(const struct nc_session *session)
388{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100389 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200390 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100391 return 0;
392 }
393
Michal Vasko8dadf782016-01-15 10:29:36 +0100394 return session->port;
395}
396
Michal Vasko9a25e932016-02-01 10:36:42 +0100397API struct ly_ctx *
398nc_session_get_ctx(const struct nc_session *session)
399{
400 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200401 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100402 return NULL;
403 }
404
405 return session->ctx;
406}
407
Michal Vasko2cc4c682016-03-01 09:16:48 +0100408API void
409nc_session_set_data(struct nc_session *session, void *data)
410{
411 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200412 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100413 return;
414 }
415
416 session->data = data;
417}
418
419API void *
420nc_session_get_data(const struct nc_session *session)
421{
422 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200423 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100424 return NULL;
425 }
426
427 return session->data;
428}
429
Michal Vasko086311b2016-01-08 09:53:11 +0100430NC_MSG_TYPE
431nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200432{
Michal Vasko086311b2016-01-08 09:53:11 +0100433 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200434
Michal Vasko086311b2016-01-08 09:53:11 +0100435 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100436 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
437 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100438 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100439 }
440
Michal Vasko086311b2016-01-08 09:53:11 +0100441 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
442
443 if (r) {
444 return NC_MSG_ERROR;
445 }
446
447 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100448}
449
Radek Krejci695d4fa2015-10-22 13:23:54 +0200450API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100451nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200452{
Michal Vasko9e99f012016-03-03 13:25:20 +0100453 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100454 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100455 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100456 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100457 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100458 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100459 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200460 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100461 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200462 void *p;
463
Michal Vasko428087d2016-01-14 16:04:28 +0100464 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200465 return;
466 }
467
Michal Vasko86d357c2016-03-11 13:46:38 +0100468 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200469 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
470 tid = *session->opts.client.ntf_tid;
471 free((pthread_t *)session->opts.client.ntf_tid);
472 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100473 /* the thread now knows it should quit */
474
475 pthread_join(tid, NULL);
476 }
477
Michal Vaskoadd4c792015-10-26 15:36:58 +0100478 if (session->ti_lock) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100479 r = nc_session_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100480 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100481 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100482 } else if (!r) {
483 /* we failed to lock it, too bad */
484 locked = 0;
485 } else {
486 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100487 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200488 } else {
489 ERRINT;
490 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200491 }
492
Michal Vasko9e99f012016-03-03 13:25:20 +0100493 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200494 /* cleanup message queues */
495 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200496 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100497 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200498
Michal Vaskoad611702015-12-03 13:41:51 +0100499 p = contiter;
500 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200501 free(p);
502 }
503
504 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200505 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100506 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200507
Michal Vaskoad611702015-12-03 13:41:51 +0100508 p = contiter;
509 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200510 free(p);
511 }
512
513 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200514 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200515 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100516 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200517 } else {
518 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100519 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200520 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100521 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100522 case NC_MSG_REPLY:
523 LY_TREE_FOR(rpl->child, child) {
524 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
525 break;
526 }
527 }
528 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100529 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100530 }
Michal Vaskoad611702015-12-03 13:41:51 +0100531 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100532 break;
533 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100534 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100535 break;
536 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100537 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100538 break;
539 default:
540 /* cannot happen */
541 break;
542 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200543 }
544
545 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200546 if (session->opts.client.cpblts) {
547 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200548 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200549 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200550 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200551 }
552 }
553
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100554 if (session->data && data_free) {
555 data_free(session->data);
556 }
557
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200558 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
559 /* CH LOCK */
560 pthread_mutex_lock(session->opts.server.ch_lock);
561 }
562
Michal Vasko86d357c2016-03-11 13:46:38 +0100563 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200564 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200565
566 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
567 pthread_cond_signal(session->opts.server.ch_cond);
568
569 /* CH UNLOCK */
570 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100571
572 /* wait for CH thread to actually wake up */
573 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
574 while (i && (session->flags & NC_SESSION_CALLHOME)) {
575 usleep(NC_TIMEOUT_STEP);
576 --i;
577 }
578 if (session->flags & NC_SESSION_CALLHOME) {
579 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
580 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200581 }
582
Michal Vasko428087d2016-01-14 16:04:28 +0100583 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200584
585 /* transport implementation cleanup */
586 switch (session->ti_type) {
587 case NC_TI_FD:
588 /* nothing needed - file descriptors were provided by caller,
589 * so it is up to the caller to close them correctly
590 * TODO use callbacks
591 */
Michal Vasko3512e402016-01-28 16:22:34 +0100592 /* just to avoid compiler warning */
593 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100594 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200595 break;
596
Radek Krejci53691be2016-02-22 13:58:37 +0100597#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200598 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100599 if (connected) {
600 ssh_channel_free(session->ti.libssh.channel);
601 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200602 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
603 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
604 * it.
605 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100606 multisession = 0;
607 if (session->ti.libssh.next) {
608 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
609 if (siter->status != NC_STATUS_STARTING) {
610 multisession = 1;
611 break;
612 }
613 }
614 }
615
616 if (!multisession) {
617 /* it's not multisession yet, but we still need to free the starting sessions */
618 if (session->ti.libssh.next) {
619 do {
620 siter = session->ti.libssh.next;
621 session->ti.libssh.next = siter->ti.libssh.next;
622
623 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100624 lydict_remove(session->ctx, session->username);
625 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100626 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100627 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100628 }
629
630 free(siter);
631 } while (session->ti.libssh.next != session);
632 }
Michal Vasko428087d2016-01-14 16:04:28 +0100633 if (connected) {
634 ssh_disconnect(session->ti.libssh.session);
635 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200636 ssh_free(session->ti.libssh.session);
637 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200638 /* remove the session from the list */
639 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100640 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200641 /* there will be only one session */
642 siter->ti.libssh.next = NULL;
643 } else {
644 /* there are still multiple sessions, keep the ring list */
645 siter->ti.libssh.next = session->ti.libssh.next;
646 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100647 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
648 if (session->flags & NC_SESSION_SSH_MSG_CB) {
649 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
650 if (siter->ti.libssh.next == session) {
651 ERRINT;
652 break;
653 }
654 }
655 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
656 siter->flags |= NC_SESSION_SSH_MSG_CB;
657 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200658 }
659 break;
660#endif
661
Radek Krejci53691be2016-02-22 13:58:37 +0100662#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200663 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100664 if (connected) {
665 SSL_shutdown(session->ti.tls);
666 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200667 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100668
Michal Vasko2e6defd2016-10-07 15:48:15 +0200669 if (session->side == NC_SERVER) {
670 X509_free(session->opts.server.client_cert);
671 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200672 break;
673#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100674 case NC_TI_NONE:
675 ERRINT;
676 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200677 }
Michal Vasko428087d2016-01-14 16:04:28 +0100678
Radek Krejciac6d3472015-10-22 15:47:18 +0200679 lydict_remove(session->ctx, session->username);
680 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200681
682 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100683 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100684 if (locked) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100685 nc_session_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100686 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100687 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100688 pthread_mutex_destroy(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100689 pthread_cond_destroy(session->ti_cond);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100690 free(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100691 free(session->ti_cond);
692 free((int *)session->ti_inuse);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100693 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200694 }
695
696 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100697 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200698 }
699
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200700 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100701 /* free CH synchronization structures if used */
702 if (session->opts.server.ch_cond) {
703 pthread_cond_destroy(session->opts.server.ch_cond);
704 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200705 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100706 if (session->opts.server.ch_lock) {
707 pthread_mutex_destroy(session->opts.server.ch_lock);
708 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200709 }
710 }
711
Radek Krejci695d4fa2015-10-22 13:23:54 +0200712 free(session);
713}
714
Michal Vasko086311b2016-01-08 09:53:11 +0100715static void
716add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
717{
Radek Krejci658782b2016-12-04 22:04:55 +0100718 size_t len;
719 int i;
720 char *p;
721
722 if (capab) {
723 /* check if already present */
724 p = strchr(capab, '?');
725 if (p) {
726 len = p - capab;
727 } else {
728 len = strlen(capab);
729 }
730 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800731 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100732 /* already present, do not duplicate it */
733 return;
734 }
735 }
736 }
737
738 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100739 if (*count == *size) {
740 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100741 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
742 if (!(*cpblts)) {
743 ERRMEM;
744 return;
745 }
Michal Vasko086311b2016-01-08 09:53:11 +0100746 }
747
748 if (capab) {
749 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
750 } else {
751 (*cpblts)[*count] = NULL;
752 }
753 ++(*count);
754}
755
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200756API const char **
757nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100758{
759 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200760 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 +0100761 const char **cpblts;
762 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200763 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Radek Krejci658782b2016-12-04 22:04:55 +0100764 unsigned int u;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200765#define NC_CPBLT_BUF_LEN 512
766 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100767
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200768 if (!ctx) {
769 ERRARG("ctx");
770 return NULL;
771 }
772
Michal Vasko086311b2016-01-08 09:53:11 +0100773 yanglib = ly_ctx_info(ctx);
774 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200775 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100776 return NULL;
777 }
778
779 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100780 if (!cpblts) {
781 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200782 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100783 }
Michal Vasko086311b2016-01-08 09:53:11 +0100784 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
785 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
786 count = 2;
787
788 /* capabilities */
789
Radek Krejci3222b7d2017-09-21 16:04:30 +0200790 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100791 if (mod) {
792 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100793 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100794 }
795 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100796 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100797 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100798 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100799 }
800 }
801 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100802 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100803 }
804 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100805 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100806 }
807 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100808 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100809 }
810 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100811 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100812 }
813 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100814 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100815 }
816 }
817
Radek Krejci3222b7d2017-09-21 16:04:30 +0200818 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100819 if (mod) {
820 if (!server_opts.wd_basic_mode) {
821 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
822 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100823 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100824 switch (server_opts.wd_basic_mode) {
825 case NC_WD_ALL:
826 strcat(str, "?basic-mode=report-all");
827 break;
828 case NC_WD_TRIM:
829 strcat(str, "?basic-mode=trim");
830 break;
831 case NC_WD_EXPLICIT:
832 strcat(str, "?basic-mode=explicit");
833 break;
834 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100835 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100836 break;
837 }
838
839 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200840 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100841 if (server_opts.wd_also_supported & NC_WD_ALL) {
842 strcat(str, "report-all,");
843 }
844 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
845 strcat(str, "report-all-tagged,");
846 }
847 if (server_opts.wd_also_supported & NC_WD_TRIM) {
848 strcat(str, "trim,");
849 }
850 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
851 strcat(str, "explicit,");
852 }
853 str[strlen(str) - 1] = '\0';
854
855 add_cpblt(ctx, str, &cpblts, &size, &count);
856 }
857 }
858 }
859
Radek Krejci658782b2016-12-04 22:04:55 +0100860 /* other capabilities */
861 for (u = 0; u < server_opts.capabilities_count; u++) {
862 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100863 }
864
865 /* models */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200866 LY_TREE_FOR(yanglib->prev->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200867 if (!module_set_id) {
868 if (strcmp(child->prev->schema->name, "module-set-id")) {
869 ERRINT;
Radek Krejcif906e412017-09-22 14:44:45 +0200870 goto error;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200871 }
872 module_set_id = (struct lyd_node_leaf_list *)child->prev;
873 }
Michal Vasko086311b2016-01-08 09:53:11 +0100874 if (!strcmp(child->schema->name, "module")) {
875 LY_TREE_FOR(child->child, child2) {
876 if (!strcmp(child2->schema->name, "namespace")) {
877 ns = (struct lyd_node_leaf_list *)child2;
878 } else if (!strcmp(child2->schema->name, "name")) {
879 name = (struct lyd_node_leaf_list *)child2;
880 } else if (!strcmp(child2->schema->name, "revision")) {
881 rev = (struct lyd_node_leaf_list *)child2;
882 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100883 features = nc_realloc(features, ++feat_count * sizeof *features);
884 if (!features) {
885 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200886 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100887 }
Michal Vasko086311b2016-01-08 09:53:11 +0100888 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200889 } else if (!strcmp(child2->schema->name, "deviation")) {
890 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
891 if (!deviations) {
892 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200893 goto error;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200894 }
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200895 deviations[dev_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vasko086311b2016-01-08 09:53:11 +0100896 }
897 }
898
899 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100900 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100901 continue;
902 }
903
Radek Krejcidf7ba522016-03-01 16:05:25 +0100904 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200905 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100906 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200907 strcat(str, "&features=");
908 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100909 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200910 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100911 ERRINT;
912 break;
913 }
Michal Vasko086311b2016-01-08 09:53:11 +0100914 if (i) {
915 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100916 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100917 }
918 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100919 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100920 }
921 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200922 if (dev_count) {
923 strcat(str, "&deviations=");
924 str_len += 12;
925 for (i = 0; i < dev_count; ++i) {
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200926 LY_TREE_FOR(((struct lyd_node *)deviations[i])->child, child2) {
927 if (!strcmp(child2->schema->name, "name"))
928 break;
929 }
930 if (!child2) {
931 ERRINT;
932 continue;
933 }
934
935 if (str_len + 1 + strlen(((struct lyd_node_leaf_list *)child2)->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200936 ERRINT;
937 break;
938 }
939 if (i) {
940 strcat(str, ",");
941 ++str_len;
942 }
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200943 strcat(str, ((struct lyd_node_leaf_list *)child2)->value_str);
944 str_len += strlen(((struct lyd_node_leaf_list *)child2)->value_str);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200945 }
946 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200947 if (!strcmp(name->value_str, "ietf-yang-library")) {
Michal Vasko7a7d5742017-03-21 15:33:23 +0100948 sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200949 }
Michal Vasko086311b2016-01-08 09:53:11 +0100950
951 add_cpblt(ctx, str, &cpblts, &size, &count);
952
953 ns = NULL;
954 name = NULL;
955 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200956 if (features || feat_count) {
957 free(features);
958 features = NULL;
959 feat_count = 0;
960 }
961 if (deviations || dev_count) {
962 free(deviations);
963 deviations = NULL;
964 dev_count = 0;
965 }
Michal Vasko086311b2016-01-08 09:53:11 +0100966 }
967 }
968
Radek Krejcif906e412017-09-22 14:44:45 +0200969 lyd_free_withsiblings(yanglib);
Michal Vasko086311b2016-01-08 09:53:11 +0100970
971 /* ending NULL capability */
972 add_cpblt(ctx, NULL, &cpblts, &size, &count);
973
974 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +0200975
976error:
977
978 free(cpblts);
979 free(features);
980 free(deviations);
981 lyd_free_withsiblings(yanglib);
982 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100983}
984
Radek Krejci695d4fa2015-10-22 13:23:54 +0200985static int
Michal Vasko2cb24b42017-05-23 14:59:11 +0200986parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200987{
988 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +0200989 int ver = -1, i = 0;
990 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200991
992 if (list) {
993 /* get the storage for server's capabilities */
994 LY_TREE_FOR(xml->child, cpblt) {
995 i++;
996 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100997 /* last item remains NULL */
998 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200999 if (!*list) {
1000 ERRMEM;
1001 return -1;
1002 }
1003 i = 0;
1004 }
1005
1006 LY_TREE_FOR(xml->child, cpblt) {
1007 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
1008 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1009 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1010 return -1;
1011 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
1012 continue;
1013 }
1014
Michal Vasko156d3272017-04-11 11:46:49 +02001015 /* skip leading/trailing whitespaces */
1016 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1017 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1018 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1019 ERR("Empty capability \"%s\" received.", cpblt->content);
1020 return -1;
1021 }
1022
Radek Krejci695d4fa2015-10-22 13:23:54 +02001023 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001024 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001025 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001026 } 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 +02001027 ver = 1;
1028 }
1029
1030 /* store capabilities */
1031 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001032 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1033 if (!(*list)[i]) {
1034 ERRMEM;
1035 return -1;
1036 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001037 i++;
1038 }
1039 }
1040
1041 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001042 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001043 }
1044
1045 return ver;
1046}
1047
1048static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001049nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001050{
1051 int r, i;
1052 const char **cpblts;
1053
Michal Vasko11d142a2016-01-19 15:58:24 +01001054 /* client side hello - send only NETCONF base capabilities */
1055 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001056 if (!cpblts) {
1057 ERRMEM;
1058 return NC_MSG_ERROR;
1059 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001060 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1061 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1062 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001063
Michal Vasko11d142a2016-01-19 15:58:24 +01001064 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001065
Michal Vasko086311b2016-01-08 09:53:11 +01001066 for (i = 0; cpblts[i]; ++i) {
1067 lydict_remove(session->ctx, cpblts[i]);
1068 }
1069 free(cpblts);
1070
1071 if (r) {
1072 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001073 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001074
1075 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +01001076}
1077
1078static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001079nc_send_server_hello(struct nc_session *session)
1080{
1081 int r, i;
1082 const char **cpblts;
1083
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001084 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +01001085
1086 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
1087
Michal Vasko11d142a2016-01-19 15:58:24 +01001088 for (i = 0; cpblts[i]; ++i) {
1089 lydict_remove(session->ctx, cpblts[i]);
1090 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001091 free(cpblts);
1092
1093 if (r) {
1094 return NC_MSG_ERROR;
1095 }
1096
1097 return NC_MSG_HELLO;
1098}
1099
1100static NC_MSG_TYPE
1101nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001102{
1103 struct lyxml_elem *xml = NULL, *node;
1104 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1105 int ver = -1;
1106 char *str;
1107 long long int id;
1108 int flag = 0;
1109
Michal Vasko05ba9df2016-01-13 14:40:27 +01001110 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001111
1112 switch(msgtype) {
1113 case NC_MSG_HELLO:
1114 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001115 LY_TREE_FOR(xml->child, node) {
1116 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1117 continue;
1118 } else if (!strcmp(node->name, "session-id")) {
1119 if (!node->content || !strlen(node->content)) {
1120 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001121 goto error;
1122 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001123 str = NULL;
1124 id = strtoll(node->content, &str, 10);
1125 if (*str || id < 1 || id > UINT32_MAX) {
1126 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001127 goto error;
1128 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001129 session->id = (uint32_t)id;
1130 continue;
1131 } else if (strcmp(node->name, "capabilities")) {
1132 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001133 goto error;
1134 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001135
1136 if (flag) {
1137 /* multiple capabilities elements */
1138 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1139 goto error;
1140 }
1141 flag = 1;
1142
Michal Vasko2e6defd2016-10-07 15:48:15 +02001143 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001144 goto error;
1145 }
1146 session->version = ver;
1147 }
1148
1149 if (!session->id) {
1150 ERR("Missing <session-id> in server's <hello>.");
1151 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001152 }
1153 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001154 case NC_MSG_WOULDBLOCK:
1155 ERR("Server's <hello> timeout elapsed.");
1156 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001157 case NC_MSG_ERROR:
1158 /* nothing special, just pass it out */
1159 break;
1160 default:
1161 ERR("Unexpected message received instead of <hello>.");
1162 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001163 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001164 }
1165
1166 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001167 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001168
1169 return msgtype;
1170
1171error:
1172 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001173 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001174
1175 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001176}
1177
Michal Vasko11d142a2016-01-19 15:58:24 +01001178static NC_MSG_TYPE
1179nc_recv_server_hello(struct nc_session *session)
1180{
1181 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001182 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001183 int ver = -1;
1184 int flag = 0;
1185
fanchanghu75888b62017-08-01 19:51:20 +08001186 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 +01001187
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001188 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001189 case NC_MSG_HELLO:
1190 /* get know NETCONF version */
1191 LY_TREE_FOR(xml->child, node) {
1192 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1193 continue;
1194 } else if (strcmp(node->name, "capabilities")) {
1195 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001196 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001197 goto cleanup;
1198 }
1199
1200 if (flag) {
1201 /* multiple capabilities elements */
1202 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001203 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001204 goto cleanup;
1205 }
1206 flag = 1;
1207
1208 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001209 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001210 goto cleanup;
1211 }
1212 session->version = ver;
1213 }
1214 break;
1215 case NC_MSG_ERROR:
1216 /* nothing special, just pass it out */
1217 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001218 case NC_MSG_WOULDBLOCK:
1219 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001220 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001221 default:
1222 ERR("Unexpected message received instead of <hello>.");
1223 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001224 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001225 }
1226
1227cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001228 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001229
1230 return msgtype;
1231}
1232
Michal Vasko71090fc2016-05-24 16:37:28 +02001233NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001234nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001235{
Michal Vasko086311b2016-01-08 09:53:11 +01001236 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001237
Michal Vasko11d142a2016-01-19 15:58:24 +01001238 if (session->side == NC_CLIENT) {
1239 type = nc_send_client_hello(session);
1240 } else {
1241 type = nc_send_server_hello(session);
1242 }
1243
Michal Vasko086311b2016-01-08 09:53:11 +01001244 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001245 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001246 }
1247
Michal Vasko11d142a2016-01-19 15:58:24 +01001248 if (session->side == NC_CLIENT) {
1249 type = nc_recv_client_hello(session);
1250 } else {
1251 type = nc_recv_server_hello(session);
1252 }
1253
Michal Vasko71090fc2016-05-24 16:37:28 +02001254 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001255}
Michal Vasko086311b2016-01-08 09:53:11 +01001256
Radek Krejci53691be2016-02-22 13:58:37 +01001257#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001258
Michal Vasko8f0c0282016-02-29 10:17:14 +01001259static void
Michal Vasko086311b2016-01-08 09:53:11 +01001260nc_ssh_init(void)
1261{
1262 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1263 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001264}
1265
Michal Vasko8f0c0282016-02-29 10:17:14 +01001266static void
Michal Vasko086311b2016-01-08 09:53:11 +01001267nc_ssh_destroy(void)
1268{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001269 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001270 ENGINE_cleanup();
1271 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001272 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001273 ssh_finalize();
1274}
1275
Radek Krejci53691be2016-02-22 13:58:37 +01001276#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001277
Radek Krejci53691be2016-02-22 13:58:37 +01001278#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001279
Michal Vasko770b4362017-02-17 14:44:18 +01001280#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1281
Michal Vaskof0c92c02016-01-29 09:41:45 +01001282struct CRYPTO_dynlock_value {
1283 pthread_mutex_t lock;
1284};
1285
Michal Vaskof0c92c02016-01-29 09:41:45 +01001286static struct CRYPTO_dynlock_value *
1287tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1288{
1289 struct CRYPTO_dynlock_value *value;
1290
1291 value = malloc(sizeof *value);
1292 if (!value) {
1293 ERRMEM;
1294 return NULL;
1295 }
1296 pthread_mutex_init(&value->lock, NULL);
1297
1298 return value;
1299}
1300
1301static void
1302tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1303{
1304 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1305 * I found ignored this fact, what do I know... */
1306 if (mode & CRYPTO_LOCK) {
1307 pthread_mutex_lock(&l->lock);
1308 } else {
1309 pthread_mutex_unlock(&l->lock);
1310 }
1311}
1312
1313static void
1314tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1315{
1316 pthread_mutex_destroy(&l->lock);
1317 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001318}
Michal Vasko770b4362017-02-17 14:44:18 +01001319#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001320
Michal Vasko8f0c0282016-02-29 10:17:14 +01001321#endif /* NC_ENABLED_TLS */
1322
1323#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1324
Michal Vasko770b4362017-02-17 14:44:18 +01001325#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001326static pthread_mutex_t *tls_locks;
1327
1328static void
1329tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1330{
1331 if (mode & CRYPTO_LOCK) {
1332 pthread_mutex_lock(tls_locks + n);
1333 } else {
1334 pthread_mutex_unlock(tls_locks + n);
1335 }
1336}
1337
1338static void
1339tls_thread_id_func(CRYPTO_THREADID *tid)
1340{
1341 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1342}
Michal Vasko770b4362017-02-17 14:44:18 +01001343#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001344
1345static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001346nc_tls_init(void)
1347{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001348 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001349 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001350 SSL_library_init();
1351
Michal Vasko770b4362017-02-17 14:44:18 +01001352#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001353 int i;
1354
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001355 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001356 if (!tls_locks) {
1357 ERRMEM;
1358 return;
1359 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001360 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1361 pthread_mutex_init(tls_locks + i, NULL);
1362 }
1363
Michal Vaskof0c92c02016-01-29 09:41:45 +01001364 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001365 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001366
1367 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1368 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1369 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001370#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001371}
1372
Michal Vasko8f0c0282016-02-29 10:17:14 +01001373static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001374nc_tls_destroy(void)
1375{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001376 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001377 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001378 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001379 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001380 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001381#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001382 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001383#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1384 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001385#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001386
Michal Vasko770b4362017-02-17 14:44:18 +01001387#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001388 int i;
1389
Michal Vaskob6e37262016-02-25 14:49:00 +01001390 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001391 CRYPTO_set_locking_callback(NULL);
1392 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1393 pthread_mutex_destroy(tls_locks + i);
1394 }
1395 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001396
1397 CRYPTO_set_dynlock_create_callback(NULL);
1398 CRYPTO_set_dynlock_lock_callback(NULL);
1399 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001400#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001401}
1402
Michal Vasko8f0c0282016-02-29 10:17:14 +01001403#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001404
Radek Krejci53691be2016-02-22 13:58:37 +01001405#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001406
Michal Vasko8f0c0282016-02-29 10:17:14 +01001407static void
Michal Vasko5e228792016-02-03 15:30:24 +01001408nc_ssh_tls_init(void)
1409{
1410 SSL_load_error_strings();
1411 ERR_load_BIO_strings();
1412 SSL_library_init();
1413
1414 nc_ssh_init();
1415
Michal Vasko770b4362017-02-17 14:44:18 +01001416#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001417 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1418 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1419 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001420#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001421}
1422
Michal Vasko8f0c0282016-02-29 10:17:14 +01001423static void
Michal Vasko5e228792016-02-03 15:30:24 +01001424nc_ssh_tls_destroy(void)
1425{
1426 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001427#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001428 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001429#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1430 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001431#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001432
1433 nc_ssh_destroy();
1434
Michal Vasko770b4362017-02-17 14:44:18 +01001435#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001436 CRYPTO_set_dynlock_create_callback(NULL);
1437 CRYPTO_set_dynlock_lock_callback(NULL);
1438 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001439#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001440}
1441
Radek Krejci53691be2016-02-22 13:58:37 +01001442#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001443
1444#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1445
1446API void
1447nc_thread_destroy(void)
1448{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001449 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1450 //CRYPTO_cleanup_all_ex_data();
1451
Michal Vasko770b4362017-02-17 14:44:18 +01001452#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1453 CRYPTO_THREADID crypto_tid;
1454
Michal Vasko8f0c0282016-02-29 10:17:14 +01001455 CRYPTO_THREADID_current(&crypto_tid);
1456 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001457#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001458}
1459
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001460#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1461
1462void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001463nc_init(void)
1464{
1465#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1466 nc_ssh_tls_init();
1467#elif defined(NC_ENABLED_SSH)
1468 nc_ssh_init();
1469#elif defined(NC_ENABLED_TLS)
1470 nc_tls_init();
1471#endif
1472}
1473
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001474void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001475nc_destroy(void)
1476{
1477#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1478 nc_ssh_tls_destroy();
1479#elif defined(NC_ENABLED_SSH)
1480 nc_ssh_destroy();
1481#elif defined(NC_ENABLED_TLS)
1482 nc_tls_destroy();
1483#endif
1484}