blob: edc15143322bca6b54e1bc4430375a0042bdcd5b [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);
571 }
572
Michal Vasko428087d2016-01-14 16:04:28 +0100573 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200574
575 /* transport implementation cleanup */
576 switch (session->ti_type) {
577 case NC_TI_FD:
578 /* nothing needed - file descriptors were provided by caller,
579 * so it is up to the caller to close them correctly
580 * TODO use callbacks
581 */
Michal Vasko3512e402016-01-28 16:22:34 +0100582 /* just to avoid compiler warning */
583 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100584 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200585 break;
586
Radek Krejci53691be2016-02-22 13:58:37 +0100587#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200588 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100589 if (connected) {
590 ssh_channel_free(session->ti.libssh.channel);
591 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200592 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
593 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
594 * it.
595 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100596 multisession = 0;
597 if (session->ti.libssh.next) {
598 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
599 if (siter->status != NC_STATUS_STARTING) {
600 multisession = 1;
601 break;
602 }
603 }
604 }
605
606 if (!multisession) {
607 /* it's not multisession yet, but we still need to free the starting sessions */
608 if (session->ti.libssh.next) {
609 do {
610 siter = session->ti.libssh.next;
611 session->ti.libssh.next = siter->ti.libssh.next;
612
613 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100614 lydict_remove(session->ctx, session->username);
615 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100616 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100617 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100618 }
619
620 free(siter);
621 } while (session->ti.libssh.next != session);
622 }
Michal Vasko428087d2016-01-14 16:04:28 +0100623 if (connected) {
624 ssh_disconnect(session->ti.libssh.session);
625 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200626 ssh_free(session->ti.libssh.session);
627 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200628 /* remove the session from the list */
629 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100630 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200631 /* there will be only one session */
632 siter->ti.libssh.next = NULL;
633 } else {
634 /* there are still multiple sessions, keep the ring list */
635 siter->ti.libssh.next = session->ti.libssh.next;
636 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100637 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
638 if (session->flags & NC_SESSION_SSH_MSG_CB) {
639 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
640 if (siter->ti.libssh.next == session) {
641 ERRINT;
642 break;
643 }
644 }
645 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
646 siter->flags |= NC_SESSION_SSH_MSG_CB;
647 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200648 }
649 break;
650#endif
651
Radek Krejci53691be2016-02-22 13:58:37 +0100652#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200653 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100654 if (connected) {
655 SSL_shutdown(session->ti.tls);
656 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200657 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100658
Michal Vasko2e6defd2016-10-07 15:48:15 +0200659 if (session->side == NC_SERVER) {
660 X509_free(session->opts.server.client_cert);
661 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200662 break;
663#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100664 case NC_TI_NONE:
665 ERRINT;
666 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200667 }
Michal Vasko428087d2016-01-14 16:04:28 +0100668
Radek Krejciac6d3472015-10-22 15:47:18 +0200669 lydict_remove(session->ctx, session->username);
670 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200671
672 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100673 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100674 if (locked) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100675 nc_session_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100676 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100677 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100678 pthread_mutex_destroy(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100679 pthread_cond_destroy(session->ti_cond);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100680 free(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100681 free(session->ti_cond);
682 free((int *)session->ti_inuse);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100683 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200684 }
685
686 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100687 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200688 }
689
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200690 if (session->side == NC_SERVER) {
Michal Vasko2e39ed92018-03-12 13:51:44 +0100691 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
692 while (i && (session->flags & NC_SESSION_CALLHOME)) {
693 usleep(NC_TIMEOUT_STEP);
694 --i;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200695 }
Michal Vasko2e39ed92018-03-12 13:51:44 +0100696 if (session->flags & NC_SESSION_CALLHOME) {
697 ERR("Session %u: Call Home thread failed to exit in a timely manner, fatal synchronization problem.", session->id);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200698 }
699 }
700
Radek Krejci695d4fa2015-10-22 13:23:54 +0200701 free(session);
702}
703
Michal Vasko086311b2016-01-08 09:53:11 +0100704static void
705add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
706{
Radek Krejci658782b2016-12-04 22:04:55 +0100707 size_t len;
708 int i;
709 char *p;
710
711 if (capab) {
712 /* check if already present */
713 p = strchr(capab, '?');
714 if (p) {
715 len = p - capab;
716 } else {
717 len = strlen(capab);
718 }
719 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800720 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100721 /* already present, do not duplicate it */
722 return;
723 }
724 }
725 }
726
727 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100728 if (*count == *size) {
729 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100730 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
731 if (!(*cpblts)) {
732 ERRMEM;
733 return;
734 }
Michal Vasko086311b2016-01-08 09:53:11 +0100735 }
736
737 if (capab) {
738 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
739 } else {
740 (*cpblts)[*count] = NULL;
741 }
742 ++(*count);
743}
744
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200745API const char **
746nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100747{
748 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200749 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 +0100750 const char **cpblts;
751 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200752 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Radek Krejci658782b2016-12-04 22:04:55 +0100753 unsigned int u;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200754#define NC_CPBLT_BUF_LEN 512
755 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100756
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200757 if (!ctx) {
758 ERRARG("ctx");
759 return NULL;
760 }
761
Michal Vasko086311b2016-01-08 09:53:11 +0100762 yanglib = ly_ctx_info(ctx);
763 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200764 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100765 return NULL;
766 }
767
768 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100769 if (!cpblts) {
770 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200771 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100772 }
Michal Vasko086311b2016-01-08 09:53:11 +0100773 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
774 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
775 count = 2;
776
777 /* capabilities */
778
Radek Krejci3222b7d2017-09-21 16:04:30 +0200779 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100780 if (mod) {
781 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100782 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100783 }
784 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100785 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100786 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100787 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100788 }
789 }
790 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100791 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100792 }
793 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100794 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100795 }
796 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100797 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100798 }
799 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100800 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100801 }
802 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100803 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100804 }
805 }
806
Radek Krejci3222b7d2017-09-21 16:04:30 +0200807 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100808 if (mod) {
809 if (!server_opts.wd_basic_mode) {
810 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
811 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100812 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100813 switch (server_opts.wd_basic_mode) {
814 case NC_WD_ALL:
815 strcat(str, "?basic-mode=report-all");
816 break;
817 case NC_WD_TRIM:
818 strcat(str, "?basic-mode=trim");
819 break;
820 case NC_WD_EXPLICIT:
821 strcat(str, "?basic-mode=explicit");
822 break;
823 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100824 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100825 break;
826 }
827
828 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200829 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100830 if (server_opts.wd_also_supported & NC_WD_ALL) {
831 strcat(str, "report-all,");
832 }
833 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
834 strcat(str, "report-all-tagged,");
835 }
836 if (server_opts.wd_also_supported & NC_WD_TRIM) {
837 strcat(str, "trim,");
838 }
839 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
840 strcat(str, "explicit,");
841 }
842 str[strlen(str) - 1] = '\0';
843
844 add_cpblt(ctx, str, &cpblts, &size, &count);
845 }
846 }
847 }
848
Radek Krejci658782b2016-12-04 22:04:55 +0100849 /* other capabilities */
850 for (u = 0; u < server_opts.capabilities_count; u++) {
851 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100852 }
853
854 /* models */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200855 LY_TREE_FOR(yanglib->prev->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200856 if (!module_set_id) {
857 if (strcmp(child->prev->schema->name, "module-set-id")) {
858 ERRINT;
Radek Krejcif906e412017-09-22 14:44:45 +0200859 goto error;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200860 }
861 module_set_id = (struct lyd_node_leaf_list *)child->prev;
862 }
Michal Vasko086311b2016-01-08 09:53:11 +0100863 if (!strcmp(child->schema->name, "module")) {
864 LY_TREE_FOR(child->child, child2) {
865 if (!strcmp(child2->schema->name, "namespace")) {
866 ns = (struct lyd_node_leaf_list *)child2;
867 } else if (!strcmp(child2->schema->name, "name")) {
868 name = (struct lyd_node_leaf_list *)child2;
869 } else if (!strcmp(child2->schema->name, "revision")) {
870 rev = (struct lyd_node_leaf_list *)child2;
871 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100872 features = nc_realloc(features, ++feat_count * sizeof *features);
873 if (!features) {
874 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200875 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100876 }
Michal Vasko086311b2016-01-08 09:53:11 +0100877 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200878 } else if (!strcmp(child2->schema->name, "deviation")) {
879 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
880 if (!deviations) {
881 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200882 goto error;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200883 }
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200884 deviations[dev_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vasko086311b2016-01-08 09:53:11 +0100885 }
886 }
887
888 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100889 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100890 continue;
891 }
892
Radek Krejcidf7ba522016-03-01 16:05:25 +0100893 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200894 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100895 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200896 strcat(str, "&features=");
897 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100898 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200899 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100900 ERRINT;
901 break;
902 }
Michal Vasko086311b2016-01-08 09:53:11 +0100903 if (i) {
904 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100905 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100906 }
907 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100908 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100909 }
910 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200911 if (dev_count) {
912 strcat(str, "&deviations=");
913 str_len += 12;
914 for (i = 0; i < dev_count; ++i) {
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200915 LY_TREE_FOR(((struct lyd_node *)deviations[i])->child, child2) {
916 if (!strcmp(child2->schema->name, "name"))
917 break;
918 }
919 if (!child2) {
920 ERRINT;
921 continue;
922 }
923
924 if (str_len + 1 + strlen(((struct lyd_node_leaf_list *)child2)->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200925 ERRINT;
926 break;
927 }
928 if (i) {
929 strcat(str, ",");
930 ++str_len;
931 }
Hannes Küttnerebb09ff2017-10-16 09:19:39 +0200932 strcat(str, ((struct lyd_node_leaf_list *)child2)->value_str);
933 str_len += strlen(((struct lyd_node_leaf_list *)child2)->value_str);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200934 }
935 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200936 if (!strcmp(name->value_str, "ietf-yang-library")) {
Michal Vasko7a7d5742017-03-21 15:33:23 +0100937 sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200938 }
Michal Vasko086311b2016-01-08 09:53:11 +0100939
940 add_cpblt(ctx, str, &cpblts, &size, &count);
941
942 ns = NULL;
943 name = NULL;
944 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200945 if (features || feat_count) {
946 free(features);
947 features = NULL;
948 feat_count = 0;
949 }
950 if (deviations || dev_count) {
951 free(deviations);
952 deviations = NULL;
953 dev_count = 0;
954 }
Michal Vasko086311b2016-01-08 09:53:11 +0100955 }
956 }
957
Radek Krejcif906e412017-09-22 14:44:45 +0200958 lyd_free_withsiblings(yanglib);
Michal Vasko086311b2016-01-08 09:53:11 +0100959
960 /* ending NULL capability */
961 add_cpblt(ctx, NULL, &cpblts, &size, &count);
962
963 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +0200964
965error:
966
967 free(cpblts);
968 free(features);
969 free(deviations);
970 lyd_free_withsiblings(yanglib);
971 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100972}
973
Radek Krejci695d4fa2015-10-22 13:23:54 +0200974static int
Michal Vasko2cb24b42017-05-23 14:59:11 +0200975parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200976{
977 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +0200978 int ver = -1, i = 0;
979 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200980
981 if (list) {
982 /* get the storage for server's capabilities */
983 LY_TREE_FOR(xml->child, cpblt) {
984 i++;
985 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100986 /* last item remains NULL */
987 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200988 if (!*list) {
989 ERRMEM;
990 return -1;
991 }
992 i = 0;
993 }
994
995 LY_TREE_FOR(xml->child, cpblt) {
996 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
997 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
998 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
999 return -1;
1000 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
1001 continue;
1002 }
1003
Michal Vasko156d3272017-04-11 11:46:49 +02001004 /* skip leading/trailing whitespaces */
1005 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1006 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1007 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1008 ERR("Empty capability \"%s\" received.", cpblt->content);
1009 return -1;
1010 }
1011
Radek Krejci695d4fa2015-10-22 13:23:54 +02001012 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001013 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001014 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001015 } 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 +02001016 ver = 1;
1017 }
1018
1019 /* store capabilities */
1020 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001021 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1022 if (!(*list)[i]) {
1023 ERRMEM;
1024 return -1;
1025 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001026 i++;
1027 }
1028 }
1029
1030 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001031 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001032 }
1033
1034 return ver;
1035}
1036
1037static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001038nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001039{
1040 int r, i;
1041 const char **cpblts;
1042
Michal Vasko11d142a2016-01-19 15:58:24 +01001043 /* client side hello - send only NETCONF base capabilities */
1044 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001045 if (!cpblts) {
1046 ERRMEM;
1047 return NC_MSG_ERROR;
1048 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001049 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1050 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1051 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001052
Michal Vasko11d142a2016-01-19 15:58:24 +01001053 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001054
Michal Vasko086311b2016-01-08 09:53:11 +01001055 for (i = 0; cpblts[i]; ++i) {
1056 lydict_remove(session->ctx, cpblts[i]);
1057 }
1058 free(cpblts);
1059
1060 if (r) {
1061 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001062 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001063
1064 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +01001065}
1066
1067static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001068nc_send_server_hello(struct nc_session *session)
1069{
1070 int r, i;
1071 const char **cpblts;
1072
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001073 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +01001074
1075 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
1076
Michal Vasko11d142a2016-01-19 15:58:24 +01001077 for (i = 0; cpblts[i]; ++i) {
1078 lydict_remove(session->ctx, cpblts[i]);
1079 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001080 free(cpblts);
1081
1082 if (r) {
1083 return NC_MSG_ERROR;
1084 }
1085
1086 return NC_MSG_HELLO;
1087}
1088
1089static NC_MSG_TYPE
1090nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001091{
1092 struct lyxml_elem *xml = NULL, *node;
1093 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1094 int ver = -1;
1095 char *str;
1096 long long int id;
1097 int flag = 0;
1098
Michal Vasko05ba9df2016-01-13 14:40:27 +01001099 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001100
1101 switch(msgtype) {
1102 case NC_MSG_HELLO:
1103 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001104 LY_TREE_FOR(xml->child, node) {
1105 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1106 continue;
1107 } else if (!strcmp(node->name, "session-id")) {
1108 if (!node->content || !strlen(node->content)) {
1109 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001110 goto error;
1111 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001112 str = NULL;
1113 id = strtoll(node->content, &str, 10);
1114 if (*str || id < 1 || id > UINT32_MAX) {
1115 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001116 goto error;
1117 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001118 session->id = (uint32_t)id;
1119 continue;
1120 } else if (strcmp(node->name, "capabilities")) {
1121 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001122 goto error;
1123 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001124
1125 if (flag) {
1126 /* multiple capabilities elements */
1127 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1128 goto error;
1129 }
1130 flag = 1;
1131
Michal Vasko2e6defd2016-10-07 15:48:15 +02001132 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001133 goto error;
1134 }
1135 session->version = ver;
1136 }
1137
1138 if (!session->id) {
1139 ERR("Missing <session-id> in server's <hello>.");
1140 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001141 }
1142 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001143 case NC_MSG_WOULDBLOCK:
1144 ERR("Server's <hello> timeout elapsed.");
1145 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001146 case NC_MSG_ERROR:
1147 /* nothing special, just pass it out */
1148 break;
1149 default:
1150 ERR("Unexpected message received instead of <hello>.");
1151 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001152 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001153 }
1154
1155 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001156 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001157
1158 return msgtype;
1159
1160error:
1161 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001162 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001163
1164 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001165}
1166
Michal Vasko11d142a2016-01-19 15:58:24 +01001167static NC_MSG_TYPE
1168nc_recv_server_hello(struct nc_session *session)
1169{
1170 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001171 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001172 int ver = -1;
1173 int flag = 0;
1174
fanchanghu75888b62017-08-01 19:51:20 +08001175 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 +01001176
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001177 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001178 case NC_MSG_HELLO:
1179 /* get know NETCONF version */
1180 LY_TREE_FOR(xml->child, node) {
1181 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1182 continue;
1183 } else if (strcmp(node->name, "capabilities")) {
1184 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001185 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001186 goto cleanup;
1187 }
1188
1189 if (flag) {
1190 /* multiple capabilities elements */
1191 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001192 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001193 goto cleanup;
1194 }
1195 flag = 1;
1196
1197 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001198 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001199 goto cleanup;
1200 }
1201 session->version = ver;
1202 }
1203 break;
1204 case NC_MSG_ERROR:
1205 /* nothing special, just pass it out */
1206 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001207 case NC_MSG_WOULDBLOCK:
1208 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001209 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001210 default:
1211 ERR("Unexpected message received instead of <hello>.");
1212 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001213 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001214 }
1215
1216cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001217 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001218
1219 return msgtype;
1220}
1221
Michal Vasko71090fc2016-05-24 16:37:28 +02001222NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001223nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001224{
Michal Vasko086311b2016-01-08 09:53:11 +01001225 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001226
Michal Vasko11d142a2016-01-19 15:58:24 +01001227 if (session->side == NC_CLIENT) {
1228 type = nc_send_client_hello(session);
1229 } else {
1230 type = nc_send_server_hello(session);
1231 }
1232
Michal Vasko086311b2016-01-08 09:53:11 +01001233 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001234 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001235 }
1236
Michal Vasko11d142a2016-01-19 15:58:24 +01001237 if (session->side == NC_CLIENT) {
1238 type = nc_recv_client_hello(session);
1239 } else {
1240 type = nc_recv_server_hello(session);
1241 }
1242
Michal Vasko71090fc2016-05-24 16:37:28 +02001243 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001244}
Michal Vasko086311b2016-01-08 09:53:11 +01001245
Radek Krejci53691be2016-02-22 13:58:37 +01001246#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001247
Michal Vasko8f0c0282016-02-29 10:17:14 +01001248static void
Michal Vasko086311b2016-01-08 09:53:11 +01001249nc_ssh_init(void)
1250{
1251 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1252 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001253}
1254
Michal Vasko8f0c0282016-02-29 10:17:14 +01001255static void
Michal Vasko086311b2016-01-08 09:53:11 +01001256nc_ssh_destroy(void)
1257{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001258 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001259 ENGINE_cleanup();
1260 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001261 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001262 ssh_finalize();
1263}
1264
Radek Krejci53691be2016-02-22 13:58:37 +01001265#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001266
Radek Krejci53691be2016-02-22 13:58:37 +01001267#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001268
Michal Vasko770b4362017-02-17 14:44:18 +01001269#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1270
Michal Vaskof0c92c02016-01-29 09:41:45 +01001271struct CRYPTO_dynlock_value {
1272 pthread_mutex_t lock;
1273};
1274
Michal Vaskof0c92c02016-01-29 09:41:45 +01001275static struct CRYPTO_dynlock_value *
1276tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1277{
1278 struct CRYPTO_dynlock_value *value;
1279
1280 value = malloc(sizeof *value);
1281 if (!value) {
1282 ERRMEM;
1283 return NULL;
1284 }
1285 pthread_mutex_init(&value->lock, NULL);
1286
1287 return value;
1288}
1289
1290static void
1291tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1292{
1293 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1294 * I found ignored this fact, what do I know... */
1295 if (mode & CRYPTO_LOCK) {
1296 pthread_mutex_lock(&l->lock);
1297 } else {
1298 pthread_mutex_unlock(&l->lock);
1299 }
1300}
1301
1302static void
1303tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1304{
1305 pthread_mutex_destroy(&l->lock);
1306 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001307}
Michal Vasko770b4362017-02-17 14:44:18 +01001308#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001309
Michal Vasko8f0c0282016-02-29 10:17:14 +01001310#endif /* NC_ENABLED_TLS */
1311
1312#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1313
Michal Vasko770b4362017-02-17 14:44:18 +01001314#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001315static pthread_mutex_t *tls_locks;
1316
1317static void
1318tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1319{
1320 if (mode & CRYPTO_LOCK) {
1321 pthread_mutex_lock(tls_locks + n);
1322 } else {
1323 pthread_mutex_unlock(tls_locks + n);
1324 }
1325}
1326
1327static void
1328tls_thread_id_func(CRYPTO_THREADID *tid)
1329{
1330 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1331}
Michal Vasko770b4362017-02-17 14:44:18 +01001332#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001333
1334static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001335nc_tls_init(void)
1336{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001337 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001338 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001339 SSL_library_init();
1340
Michal Vasko770b4362017-02-17 14:44:18 +01001341#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001342 int i;
1343
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001344 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001345 if (!tls_locks) {
1346 ERRMEM;
1347 return;
1348 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001349 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1350 pthread_mutex_init(tls_locks + i, NULL);
1351 }
1352
Michal Vaskof0c92c02016-01-29 09:41:45 +01001353 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001354 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001355
1356 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1357 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1358 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001359#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001360}
1361
Michal Vasko8f0c0282016-02-29 10:17:14 +01001362static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001363nc_tls_destroy(void)
1364{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001365 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001366 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001367 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001368 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001369 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001370#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001371 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001372#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1373 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001374#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001375
Michal Vasko770b4362017-02-17 14:44:18 +01001376#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001377 int i;
1378
Michal Vaskob6e37262016-02-25 14:49:00 +01001379 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001380 CRYPTO_set_locking_callback(NULL);
1381 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1382 pthread_mutex_destroy(tls_locks + i);
1383 }
1384 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001385
1386 CRYPTO_set_dynlock_create_callback(NULL);
1387 CRYPTO_set_dynlock_lock_callback(NULL);
1388 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001389#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001390}
1391
Michal Vasko8f0c0282016-02-29 10:17:14 +01001392#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001393
Radek Krejci53691be2016-02-22 13:58:37 +01001394#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001395
Michal Vasko8f0c0282016-02-29 10:17:14 +01001396static void
Michal Vasko5e228792016-02-03 15:30:24 +01001397nc_ssh_tls_init(void)
1398{
1399 SSL_load_error_strings();
1400 ERR_load_BIO_strings();
1401 SSL_library_init();
1402
1403 nc_ssh_init();
1404
Michal Vasko770b4362017-02-17 14:44:18 +01001405#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001406 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1407 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1408 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001409#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001410}
1411
Michal Vasko8f0c0282016-02-29 10:17:14 +01001412static void
Michal Vasko5e228792016-02-03 15:30:24 +01001413nc_ssh_tls_destroy(void)
1414{
1415 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001416#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001417 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001418#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1419 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001420#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001421
1422 nc_ssh_destroy();
1423
Michal Vasko770b4362017-02-17 14:44:18 +01001424#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001425 CRYPTO_set_dynlock_create_callback(NULL);
1426 CRYPTO_set_dynlock_lock_callback(NULL);
1427 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001428#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001429}
1430
Radek Krejci53691be2016-02-22 13:58:37 +01001431#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001432
1433#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1434
1435API void
1436nc_thread_destroy(void)
1437{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001438 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1439 //CRYPTO_cleanup_all_ex_data();
1440
Michal Vasko770b4362017-02-17 14:44:18 +01001441#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1442 CRYPTO_THREADID crypto_tid;
1443
Michal Vasko8f0c0282016-02-29 10:17:14 +01001444 CRYPTO_THREADID_current(&crypto_tid);
1445 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001446#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001447}
1448
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001449#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1450
1451void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001452nc_init(void)
1453{
1454#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1455 nc_ssh_tls_init();
1456#elif defined(NC_ENABLED_SSH)
1457 nc_ssh_init();
1458#elif defined(NC_ENABLED_TLS)
1459 nc_tls_init();
1460#endif
1461}
1462
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001463void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001464nc_destroy(void)
1465{
1466#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1467 nc_ssh_tls_destroy();
1468#elif defined(NC_ENABLED_SSH)
1469 nc_ssh_destroy();
1470#elif defined(NC_ENABLED_TLS)
1471 nc_tls_destroy();
1472#endif
1473}