blob: 4acaa494bc494e6f92fd23bcb6213e8c515e714a [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
Michal Vaskob85767d2018-03-21 12:21:10 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02007 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
Michal Vaskob85767d2018-03-21 12:21:10 +010014#define _DEFAULT_SOURCE
Radek Krejci206fcd62015-10-07 15:42:48 +020015
Michal Vasko18aeb5d2017-02-17 09:23:56 +010016#include <assert.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020017#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010018#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010019#include <string.h>
Michal Vaskob85767d2018-03-21 12:21:10 +010020#include <unistd.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020021#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020022#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010023#include <time.h>
Michal Vasko156d3272017-04-11 11:46:49 +020024#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020025#include <libyang/libyang.h>
26
Michal Vasko5e228792016-02-03 15:30:24 +010027#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010028#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010029#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010030
Radek Krejci53691be2016-02-22 13:58:37 +010031#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020032
Michal Vasko086311b2016-01-08 09:53:11 +010033# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020034
Radek Krejci53691be2016-02-22 13:58:37 +010035#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020036
Radek Krejci53691be2016-02-22 13:58:37 +010037#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010038
Michal Vasko5e228792016-02-03 15:30:24 +010039# include <openssl/engine.h>
40# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010041# include <openssl/err.h>
42
Radek Krejci53691be2016-02-22 13:58:37 +010043#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044
Michal Vasko086311b2016-01-08 09:53:11 +010045/* in seconds */
46#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080047#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020048
Michal Vasko05ba9df2016-01-13 14:40:27 +010049/* in milliseconds */
50#define NC_CLOSE_REPLY_TIMEOUT 200
51
Michal Vasko086311b2016-01-08 09:53:11 +010052extern struct nc_server_opts server_opts;
53
Radek Krejci7ac16052016-07-15 11:48:18 +020054int
Michal Vasko77a6abe2017-10-05 10:02:20 +020055nc_gettimespec_mono(struct timespec *ts)
56{
57#ifdef CLOCK_MONOTONIC_RAW
58 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
59#elif defined(CLOCK_MONOTONIC)
60 return clock_gettime(CLOCK_MONOTONIC, ts);
61#else
62 /* no monotonic clock available, return realtime */
63 return nc_gettimespec_real(ts);
64#endif
65}
66
67int
68nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020069{
Michal Vasko0ef46df2016-09-21 14:03:18 +020070#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020071 return clock_gettime(CLOCK_REALTIME, ts);
72#else
73 int rc;
74 struct timeval tv;
75
76 rc = gettimeofday(&tv, NULL);
77 if (!rc) {
78 ts->tv_sec = (time_t)tv.tv_sec;
79 ts->tv_nsec = 1000L * (long)tv.tv_usec;
80 }
81 return rc;
82#endif
83}
84
Michal Vasko36c7be82017-02-22 13:37:59 +010085/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
86int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020087nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010088{
Michal Vasko36c7be82017-02-22 13:37:59 +010089 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010090
Michal Vaskoa82bd202017-03-03 14:07:29 +010091 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
92 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010093
94 return (nsec_diff ? nsec_diff / 1000000L : 0);
95}
96
Michal Vasko36c7be82017-02-22 13:37:59 +010097void
98nc_addtimespec(struct timespec *ts, uint32_t msec)
99{
Michal Vasko81c5b302017-03-15 12:10:40 +0100100 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
101
Michal Vasko0dfcd332017-03-03 13:14:39 +0100102 ts->tv_sec += msec / 1000;
103 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100104
Michal Vasko81c5b302017-03-15 12:10:40 +0100105 if (ts->tv_nsec >= 1000000000L) {
106 ++ts->tv_sec;
107 ts->tv_nsec -= 1000000000L;
108 } else if (ts->tv_nsec < 0) {
109 --ts->tv_sec;
110 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100111 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100112
113 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100114}
115
Radek Krejci28472922016-07-15 11:51:16 +0200116#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
117int
118pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
119{
Michal Vasko81c5b302017-03-15 12:10:40 +0100120 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200121 int rc;
122 struct timespec cur, dur;
123
124 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
125 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
Michal Vasko145619f2018-02-16 10:08:45 +0100126 nc_gettimespec_real(&cur);
Radek Krejci28472922016-07-15 11:51:16 +0200127
Michal Vasko81c5b302017-03-15 12:10:40 +0100128 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
129 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200130 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100131 } else if (diff < 5) {
132 /* sleep until timeout */
Michal Vaskobf378cb2018-09-21 15:18:52 +0200133 dur.tv_sec = 0;
134 dur.tv_nsec = (long)diff * 1000000;
Michal Vasko81c5b302017-03-15 12:10:40 +0100135 } else {
136 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200137 dur.tv_sec = 0;
138 dur.tv_nsec = 5000000;
139 }
140
141 nanosleep(&dur, NULL);
142 }
143
144 return rc;
145}
146#endif
147
Michal Vaskoade892d2017-02-22 13:40:35 +0100148struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200149nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100150{
151 struct nc_session *sess;
152
153 sess = calloc(1, sizeof *sess);
154 if (!sess) {
155 return NULL;
156 }
157
Michal Vasko131120a2018-05-29 15:44:02 +0200158 sess->side = side;
159
160 if (side == NC_SERVER) {
161 sess->opts.server.rpc_lock = malloc(sizeof *sess->opts.server.rpc_lock);
162 sess->opts.server.rpc_cond = malloc(sizeof *sess->opts.server.rpc_cond);
163 sess->opts.server.rpc_inuse = malloc(sizeof *sess->opts.server.rpc_inuse);
164 if (!sess->opts.server.rpc_lock || !sess->opts.server.rpc_cond || !sess->opts.server.rpc_inuse) {
165 goto error;
Michal Vaskoade892d2017-02-22 13:40:35 +0100166 }
Michal Vasko131120a2018-05-29 15:44:02 +0200167 pthread_mutex_init(sess->opts.server.rpc_lock, NULL);
168 pthread_cond_init(sess->opts.server.rpc_cond, NULL);
169 *sess->opts.server.rpc_inuse = 0;
170 }
171
172 if (!shared_ti) {
173 sess->io_lock = malloc(sizeof *sess->io_lock);
174 if (!sess->io_lock) {
175 goto error;
176 }
177 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100178 }
179
180 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200181
182error:
183 if (side == NC_SERVER) {
184 free(sess->opts.server.rpc_lock);
185 free(sess->opts.server.rpc_cond);
186 free((int *)sess->opts.server.rpc_inuse);
187 }
188 free(sess);
189 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100190}
191
Michal Vasko96164bf2016-01-21 15:41:58 +0100192/*
193 * @return 1 - success
194 * 0 - timeout
195 * -1 - error
196 */
197int
Michal Vasko131120a2018-05-29 15:44:02 +0200198nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100199{
200 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100201 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100202
Michal Vasko131120a2018-05-29 15:44:02 +0200203 if (session->side != NC_SERVER) {
204 ERRINT;
205 return -1;
206 }
207
Michal Vasko96164bf2016-01-21 15:41:58 +0100208 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200209 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100210 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100211
Michal Vaskoade892d2017-02-22 13:40:35 +0100212 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200213 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100214 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200215 while (*session->opts.server.rpc_inuse) {
216 ret = pthread_cond_timedwait(session->opts.server.rpc_cond, session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100217 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200218 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100219 break;
220 }
221 }
222 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100223 } else if (!timeout) {
Michal Vasko131120a2018-05-29 15:44:02 +0200224 if (*session->opts.server.rpc_inuse) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100225 /* immediate timeout */
226 return 0;
227 }
228
229 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200230 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100231 if (!ret) {
232 /* be extra careful, someone could have been faster */
Michal Vasko131120a2018-05-29 15:44:02 +0200233 if (*session->opts.server.rpc_inuse) {
234 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100235 return 0;
236 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200237 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100238 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100239 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200240 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100241 if (!ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200242 while (*session->opts.server.rpc_inuse) {
243 ret = pthread_cond_wait(session->opts.server.rpc_cond, session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100244 if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200245 pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100246 break;
247 }
248 }
249 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100250 }
251
Michal Vaskoade892d2017-02-22 13:40:35 +0100252 if (ret) {
253 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
254 /* timeout */
255 return 0;
256 }
257
Michal Vasko96164bf2016-01-21 15:41:58 +0100258 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200259 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100260 return -1;
261 }
262
263 /* ok */
Michal Vasko131120a2018-05-29 15:44:02 +0200264 assert(*session->opts.server.rpc_inuse == 0);
265 *session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100266
267 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200268 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100269 if (ret) {
270 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200271 ERR("%s: faile to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100272 return -1;
273 }
274
275 return 1;
276}
277
278int
Michal Vasko131120a2018-05-29 15:44:02 +0200279nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100280{
281 int ret;
282 struct timespec ts_timeout;
283
Michal Vasko131120a2018-05-29 15:44:02 +0200284 if (session->side != NC_SERVER) {
285 ERRINT;
286 return -1;
287 }
288
289 assert(*session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100290
291 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200292 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100293 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100294
295 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200296 ret = pthread_mutex_timedlock(session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100297 } else if (!timeout) {
298 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200299 ret = pthread_mutex_trylock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100300 } else { /* timeout == -1 */
301 /* LOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200302 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100303 }
304
305 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
306 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200307 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100308 return -1;
309 } else if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200310 WRN("%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100311 }
312
Michal Vasko131120a2018-05-29 15:44:02 +0200313 *session->opts.server.rpc_inuse = 0;
314 pthread_cond_signal(session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100315
316 if (!ret) {
317 /* UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200318 ret = pthread_mutex_unlock(session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100319 if (ret) {
320 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200321 ERR("%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100322 return -1;
323 }
324 }
325
Michal Vasko96164bf2016-01-21 15:41:58 +0100326 return 1;
327}
328
Michal Vasko131120a2018-05-29 15:44:02 +0200329/*
330 * @return 1 - success
331 * 0 - timeout
332 * -1 - error
333 */
334int
335nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
336{
337 int ret;
338 struct timespec ts_timeout;
339
340 if (timeout > 0) {
341 nc_gettimespec_real(&ts_timeout);
342 nc_addtimespec(&ts_timeout, timeout);
343
344 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
345 } else if (!timeout) {
346 ret = pthread_mutex_trylock(session->io_lock);
347 } else { /* timeout == -1 */
348 ret = pthread_mutex_lock(session->opts.server.rpc_lock);
349 }
350
351 if (ret) {
352 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
353 /* timeout */
354 return 0;
355 }
356
357 /* error */
358 ERR("%s: failed to IO lock a session (%s).", func, strerror(ret));
359 return -1;
360 }
361
362 return 1;
363}
364
365int
366nc_session_io_unlock(struct nc_session *session, const char *func)
367{
368 int ret;
369
370 ret = pthread_mutex_unlock(session->io_lock);
371 if (ret) {
372 /* error */
373 ERR("%s: failed to IO unlock a session (%s).", func, strerror(ret));
374 return -1;
375 }
376
377 return 1;
378}
379
Michal Vasko8dadf782016-01-15 10:29:36 +0100380API NC_STATUS
381nc_session_get_status(const struct nc_session *session)
382{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100383 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200384 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200385 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100386 }
387
Michal Vasko8dadf782016-01-15 10:29:36 +0100388 return session->status;
389}
390
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100391API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200392nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100393{
394 if (!session) {
395 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200396 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100397 }
398
399 return session->term_reason;
400}
401
Michal Vasko8dadf782016-01-15 10:29:36 +0100402API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200403nc_session_get_killed_by(const struct nc_session *session)
404{
405 if (!session) {
406 ERRARG("session");
407 return 0;
408 }
409
410 return session->killed_by;
411}
412
413API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100414nc_session_get_id(const struct nc_session *session)
415{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100416 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200417 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100418 return 0;
419 }
420
Michal Vasko8dadf782016-01-15 10:29:36 +0100421 return session->id;
422}
423
Michal Vasko174fe8e2016-02-17 15:38:09 +0100424API int
425nc_session_get_version(const struct nc_session *session)
426{
427 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200428 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100429 return -1;
430 }
431
432 return (session->version == NC_VERSION_10 ? 0 : 1);
433}
434
Michal Vasko8dadf782016-01-15 10:29:36 +0100435API NC_TRANSPORT_IMPL
436nc_session_get_ti(const struct nc_session *session)
437{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100438 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200439 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100440 return 0;
441 }
442
Michal Vasko8dadf782016-01-15 10:29:36 +0100443 return session->ti_type;
444}
445
446API const char *
447nc_session_get_username(const struct nc_session *session)
448{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100449 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200450 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100451 return NULL;
452 }
453
Michal Vasko8dadf782016-01-15 10:29:36 +0100454 return session->username;
455}
456
457API const char *
458nc_session_get_host(const struct nc_session *session)
459{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100460 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200461 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100462 return NULL;
463 }
464
Michal Vasko8dadf782016-01-15 10:29:36 +0100465 return session->host;
466}
467
468API uint16_t
469nc_session_get_port(const struct nc_session *session)
470{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100471 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200472 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100473 return 0;
474 }
475
Michal Vasko8dadf782016-01-15 10:29:36 +0100476 return session->port;
477}
478
Michal Vasko9a25e932016-02-01 10:36:42 +0100479API struct ly_ctx *
480nc_session_get_ctx(const struct nc_session *session)
481{
482 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200483 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100484 return NULL;
485 }
486
487 return session->ctx;
488}
489
Michal Vasko2cc4c682016-03-01 09:16:48 +0100490API void
491nc_session_set_data(struct nc_session *session, void *data)
492{
493 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200494 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100495 return;
496 }
497
498 session->data = data;
499}
500
501API void *
502nc_session_get_data(const struct nc_session *session)
503{
504 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200505 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100506 return NULL;
507 }
508
509 return session->data;
510}
511
Michal Vasko086311b2016-01-08 09:53:11 +0100512NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200513nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200514{
Michal Vasko086311b2016-01-08 09:53:11 +0100515 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100516 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
517 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100518 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100519 }
520
Michal Vasko131120a2018-05-29 15:44:02 +0200521 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100522}
523
Radek Krejci695d4fa2015-10-22 13:23:54 +0200524API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100525nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200526{
Michal Vasko131120a2018-05-29 15:44:02 +0200527 int r, i, rpc_locked = 0, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100528 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100529 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100530 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100531 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100532 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200533 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100534 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200535 void *p;
536
Michal Vasko428087d2016-01-14 16:04:28 +0100537 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200538 return;
539 }
540
Michal Vasko86d357c2016-03-11 13:46:38 +0100541 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200542 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200543 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100544 /* the thread now knows it should quit */
Michal Vasko86d357c2016-03-11 13:46:38 +0100545 }
546
Michal Vasko131120a2018-05-29 15:44:02 +0200547 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
548 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100549 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100550 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200551 } else if (r) {
552 rpc_locked = 1;
553 } /* else failed to lock it, too bad */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200554 }
555
Michal Vasko131120a2018-05-29 15:44:02 +0200556 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200557 /* cleanup message queues */
558 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200559 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100560 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200561
Michal Vaskoad611702015-12-03 13:41:51 +0100562 p = contiter;
563 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200564 free(p);
565 }
566
567 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200568 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100569 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200570
Michal Vaskoad611702015-12-03 13:41:51 +0100571 p = contiter;
572 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200573 free(p);
574 }
575
576 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200577 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200578 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100579 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200580 } else {
581 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vasko131120a2018-05-29 15:44:02 +0200582 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200583 lyd_free(close_rpc);
Michal Vasko131120a2018-05-29 15:44:02 +0200584 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100585 case NC_MSG_REPLY:
586 LY_TREE_FOR(rpl->child, child) {
587 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
588 break;
589 }
590 }
591 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100592 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100593 }
Michal Vaskoad611702015-12-03 13:41:51 +0100594 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100595 break;
596 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100597 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100598 break;
599 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100600 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100601 break;
602 default:
603 /* cannot happen */
604 break;
605 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200606 }
607
608 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200609 if (session->opts.client.cpblts) {
610 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200611 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200612 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200613 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200614 }
615 }
616
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100617 if (session->data && data_free) {
618 data_free(session->data);
619 }
620
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200621 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
622 /* CH LOCK */
623 pthread_mutex_lock(session->opts.server.ch_lock);
624 }
625
Michal Vasko86d357c2016-03-11 13:46:38 +0100626 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200627 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200628
629 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
630 pthread_cond_signal(session->opts.server.ch_cond);
631
632 /* CH UNLOCK */
633 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100634
635 /* wait for CH thread to actually wake up */
636 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
637 while (i && (session->flags & NC_SESSION_CALLHOME)) {
638 usleep(NC_TIMEOUT_STEP);
639 --i;
640 }
641 if (session->flags & NC_SESSION_CALLHOME) {
642 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
643 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200644 }
645
Michal Vasko428087d2016-01-14 16:04:28 +0100646 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200647
648 /* transport implementation cleanup */
649 switch (session->ti_type) {
650 case NC_TI_FD:
651 /* nothing needed - file descriptors were provided by caller,
652 * so it is up to the caller to close them correctly
653 * TODO use callbacks
654 */
Michal Vasko3512e402016-01-28 16:22:34 +0100655 /* just to avoid compiler warning */
656 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100657 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200658 break;
659
Radek Krejci53691be2016-02-22 13:58:37 +0100660#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200661 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100662 if (connected) {
663 ssh_channel_free(session->ti.libssh.channel);
664 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200665 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
666 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
667 * it.
668 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100669 multisession = 0;
670 if (session->ti.libssh.next) {
671 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
672 if (siter->status != NC_STATUS_STARTING) {
673 multisession = 1;
674 break;
675 }
676 }
677 }
678
679 if (!multisession) {
680 /* it's not multisession yet, but we still need to free the starting sessions */
681 if (session->ti.libssh.next) {
682 do {
683 siter = session->ti.libssh.next;
684 session->ti.libssh.next = siter->ti.libssh.next;
685
686 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100687 lydict_remove(session->ctx, session->username);
688 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100689 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100690 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100691 }
692
693 free(siter);
694 } while (session->ti.libssh.next != session);
695 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100696 /* remember sock so we can close it */
697 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100698 if (connected) {
699 ssh_disconnect(session->ti.libssh.session);
700 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200701 ssh_free(session->ti.libssh.session);
702 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200703 /* remove the session from the list */
704 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100705 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200706 /* there will be only one session */
707 siter->ti.libssh.next = NULL;
708 } else {
709 /* there are still multiple sessions, keep the ring list */
710 siter->ti.libssh.next = session->ti.libssh.next;
711 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100712 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
713 if (session->flags & NC_SESSION_SSH_MSG_CB) {
714 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
715 if (siter->ti.libssh.next == session) {
716 ERRINT;
717 break;
718 }
719 }
720 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
721 siter->flags |= NC_SESSION_SSH_MSG_CB;
722 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200723 }
724 break;
725#endif
726
Radek Krejci53691be2016-02-22 13:58:37 +0100727#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200728 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100729 /* remember sock so we can close it */
730 sock = SSL_get_fd(session->ti.tls);
731
Michal Vasko428087d2016-01-14 16:04:28 +0100732 if (connected) {
733 SSL_shutdown(session->ti.tls);
734 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200735 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100736
Michal Vasko2e6defd2016-10-07 15:48:15 +0200737 if (session->side == NC_SERVER) {
738 X509_free(session->opts.server.client_cert);
739 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200740 break;
741#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100742 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100743 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200744 }
Michal Vasko428087d2016-01-14 16:04:28 +0100745
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100746 /* close socket separately */
747 if (sock > -1) {
748 close(sock);
749 }
750
Radek Krejciac6d3472015-10-22 15:47:18 +0200751 lydict_remove(session->ctx, session->username);
752 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200753
754 /* final cleanup */
Michal Vasko131120a2018-05-29 15:44:02 +0200755 if ((session->side == NC_SERVER) && session->opts.server.rpc_lock) {
756 if (rpc_locked) {
757 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100758 }
Michal Vasko131120a2018-05-29 15:44:02 +0200759 pthread_mutex_destroy(session->opts.server.rpc_lock);
760 pthread_cond_destroy(session->opts.server.rpc_cond);
761 free(session->opts.server.rpc_lock);
762 free(session->opts.server.rpc_cond);
763 free((int *)session->opts.server.rpc_inuse);
764 }
765
766 if (session->io_lock && !multisession) {
767 pthread_mutex_destroy(session->io_lock);
768 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200769 }
770
771 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100772 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200773 }
774
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200775 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100776 /* free CH synchronization structures if used */
777 if (session->opts.server.ch_cond) {
778 pthread_cond_destroy(session->opts.server.ch_cond);
779 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200780 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100781 if (session->opts.server.ch_lock) {
782 pthread_mutex_destroy(session->opts.server.ch_lock);
783 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200784 }
785 }
786
Radek Krejci695d4fa2015-10-22 13:23:54 +0200787 free(session);
788}
789
Michal Vasko086311b2016-01-08 09:53:11 +0100790static void
791add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
792{
Radek Krejci658782b2016-12-04 22:04:55 +0100793 size_t len;
794 int i;
795 char *p;
796
797 if (capab) {
798 /* check if already present */
799 p = strchr(capab, '?');
800 if (p) {
801 len = p - capab;
802 } else {
803 len = strlen(capab);
804 }
805 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800806 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100807 /* already present, do not duplicate it */
808 return;
809 }
810 }
811 }
812
813 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100814 if (*count == *size) {
815 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100816 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
817 if (!(*cpblts)) {
818 ERRMEM;
819 return;
820 }
Michal Vasko086311b2016-01-08 09:53:11 +0100821 }
822
823 if (capab) {
824 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
825 } else {
826 (*cpblts)[*count] = NULL;
827 }
828 ++(*count);
829}
830
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200831API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200832nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100833{
Michal Vasko086311b2016-01-08 09:53:11 +0100834 const char **cpblts;
Radek Krejci24a18412018-05-16 15:09:10 +0200835 const struct lys_module *mod, *devmod;
836 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
837 unsigned int u, v, module_set_id;
838 char *s;
839#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200840 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100841
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200842 if (!ctx) {
843 ERRARG("ctx");
844 return NULL;
845 }
846
Michal Vasko086311b2016-01-08 09:53:11 +0100847 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100848 if (!cpblts) {
849 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200850 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100851 }
Michal Vasko086311b2016-01-08 09:53:11 +0100852 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
853 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
854 count = 2;
855
856 /* capabilities */
857
Radek Krejci3222b7d2017-09-21 16:04:30 +0200858 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100859 if (mod) {
860 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100861 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100862 }
863 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100864 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100865 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100866 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100867 }
868 }
869 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100870 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100871 }
872 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100873 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100874 }
875 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100876 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100877 }
878 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100879 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100880 }
881 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100882 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100883 }
884 }
885
Radek Krejci3222b7d2017-09-21 16:04:30 +0200886 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100887 if (mod) {
888 if (!server_opts.wd_basic_mode) {
889 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
890 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100891 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100892 switch (server_opts.wd_basic_mode) {
893 case NC_WD_ALL:
894 strcat(str, "?basic-mode=report-all");
895 break;
896 case NC_WD_TRIM:
897 strcat(str, "?basic-mode=trim");
898 break;
899 case NC_WD_EXPLICIT:
900 strcat(str, "?basic-mode=explicit");
901 break;
902 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100903 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100904 break;
905 }
906
907 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200908 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100909 if (server_opts.wd_also_supported & NC_WD_ALL) {
910 strcat(str, "report-all,");
911 }
912 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
913 strcat(str, "report-all-tagged,");
914 }
915 if (server_opts.wd_also_supported & NC_WD_TRIM) {
916 strcat(str, "trim,");
917 }
918 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
919 strcat(str, "explicit,");
920 }
921 str[strlen(str) - 1] = '\0';
922
923 add_cpblt(ctx, str, &cpblts, &size, &count);
924 }
925 }
926 }
927
Radek Krejci658782b2016-12-04 22:04:55 +0100928 /* other capabilities */
929 for (u = 0; u < server_opts.capabilities_count; u++) {
930 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100931 }
932
933 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +0200934 u = module_set_id = 0;
935 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +0200936 if (!strcmp(mod->name, "ietf-yang-library")) {
Andrew Langefeld50eac702018-08-17 12:10:40 -0500937 /* Add the yang-library NETCONF capability as defined in RFC 7950 5.6.4 */
938 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?%s%s&module-set-id=%u",
939 mod->rev_size ? "revision=" : "", mod->rev_size ? mod->rev[0].date : "",
940 ly_ctx_get_module_set_id(ctx));
941 add_cpblt(ctx, str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +0200942 continue;
943 } else if (mod->type) {
944 /* skip submodules */
945 continue;
946 } else if (version == LYS_VERSION_1 && mod->version > version) {
947 /* skip YANG 1.1 schemas */
948 continue;
949 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
950 /* skip YANG 1.0 schemas */
951 continue;
952 }
Michal Vasko086311b2016-01-08 09:53:11 +0100953
Radek Krejci24a18412018-05-16 15:09:10 +0200954 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
955 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
956
957 if (mod->features_size) {
958 features_count = 0;
959 for (i = 0; i < mod->features_size; ++i) {
960 if (!(mod->features[i].flags & LYS_FENABLED)) {
961 continue;
962 }
963 if (!features_count) {
964 strcat(str, "&features=");
965 str_len += 10;
966 }
967 len = strlen(mod->features[i].name);
968 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
969 ERRINT;
970 break;
971 }
Radek Krejci4e8eadd2018-08-21 09:54:27 +0200972 if (features_count) {
Radek Krejci24a18412018-05-16 15:09:10 +0200973 strcat(str, ",");
974 ++str_len;
975 }
976 strcat(str, mod->features[i].name);
977 str_len += len;
978 features_count++;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200979 }
Michal Vasko086311b2016-01-08 09:53:11 +0100980 }
Michal Vasko086311b2016-01-08 09:53:11 +0100981
Radek Krejci24a18412018-05-16 15:09:10 +0200982 if (mod->deviated) {
983 strcat(str, "&deviations=");
984 str_len += 12;
985 dev_count = 0;
986 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
987 if (devmod == mod) {
988 continue;
989 }
990
991 for (i = 0; i < devmod->deviation_size; ++i) {
992 s = strstr(devmod->deviation[i].target_name, mod->name);
993 if (s && s[strlen(mod->name)] == ':') {
994 /* we have the module deviating the module being processed */
995 len = strlen(devmod->name);
996 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
997 ERRINT;
998 break;
999 }
1000 if (dev_count) {
1001 strcat(str, ",");
1002 ++str_len;
1003 }
1004 strcat(str, devmod->name);
1005 str_len += len;
1006 dev_count++;
1007
1008 break;
1009 }
1010 }
1011 }
1012 }
1013
1014 add_cpblt(ctx, str, &cpblts, &size, &count);
1015 }
Michal Vasko086311b2016-01-08 09:53:11 +01001016
1017 /* ending NULL capability */
1018 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1019
1020 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001021
1022error:
1023
1024 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001025 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001026}
1027
Radek Krejci24a18412018-05-16 15:09:10 +02001028API const char **
1029nc_server_get_cpblts(struct ly_ctx *ctx)
1030{
1031 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1032}
1033
Radek Krejci695d4fa2015-10-22 13:23:54 +02001034static int
Michal Vasko2cb24b42017-05-23 14:59:11 +02001035parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001036{
1037 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001038 int ver = -1, i = 0;
1039 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001040
1041 if (list) {
1042 /* get the storage for server's capabilities */
1043 LY_TREE_FOR(xml->child, cpblt) {
1044 i++;
1045 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001046 /* last item remains NULL */
1047 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001048 if (!*list) {
1049 ERRMEM;
1050 return -1;
1051 }
1052 i = 0;
1053 }
1054
1055 LY_TREE_FOR(xml->child, cpblt) {
1056 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
1057 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
1058 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
1059 return -1;
1060 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
1061 continue;
1062 }
1063
Michal Vasko156d3272017-04-11 11:46:49 +02001064 /* skip leading/trailing whitespaces */
1065 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1066 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1067 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1068 ERR("Empty capability \"%s\" received.", cpblt->content);
1069 return -1;
1070 }
1071
Radek Krejci695d4fa2015-10-22 13:23:54 +02001072 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001073 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001074 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001075 } 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 +02001076 ver = 1;
1077 }
1078
1079 /* store capabilities */
1080 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001081 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1082 if (!(*list)[i]) {
1083 ERRMEM;
1084 return -1;
1085 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001086 i++;
1087 }
1088 }
1089
1090 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001091 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001092 }
1093
1094 return ver;
1095}
1096
1097static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001098nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001099{
Michal Vasko131120a2018-05-29 15:44:02 +02001100 NC_MSG_TYPE ret;
1101 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001102 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001103 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001104
Michal Vasko131120a2018-05-29 15:44:02 +02001105 if (session->side == NC_CLIENT) {
1106 /* client side hello - send only NETCONF base capabilities */
1107 cpblts = malloc(3 * sizeof *cpblts);
1108 if (!cpblts) {
1109 ERRMEM;
1110 return NC_MSG_ERROR;
1111 }
1112 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1113 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1114 cpblts[2] = NULL;
1115
1116 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1117 sid = NULL;
1118 } else {
1119 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
1120
1121 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1122 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001123 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001124
Michal Vasko131120a2018-05-29 15:44:02 +02001125 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001126
Michal Vasko086311b2016-01-08 09:53:11 +01001127 for (i = 0; cpblts[i]; ++i) {
1128 lydict_remove(session->ctx, cpblts[i]);
1129 }
1130 free(cpblts);
1131
Michal Vasko131120a2018-05-29 15:44:02 +02001132 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001133}
1134
1135static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001136nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001137{
1138 struct lyxml_elem *xml = NULL, *node;
Michal Vasko131120a2018-05-29 15:44:02 +02001139 NC_MSG_TYPE msgtype;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001140 int ver = -1;
1141 char *str;
1142 long long int id;
1143 int flag = 0;
1144
Michal Vasko131120a2018-05-29 15:44:02 +02001145 msgtype = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001146
Michal Vasko131120a2018-05-29 15:44:02 +02001147 switch (msgtype) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001148 case NC_MSG_HELLO:
1149 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001150 LY_TREE_FOR(xml->child, node) {
1151 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1152 continue;
1153 } else if (!strcmp(node->name, "session-id")) {
1154 if (!node->content || !strlen(node->content)) {
1155 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001156 goto error;
1157 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001158 str = NULL;
1159 id = strtoll(node->content, &str, 10);
1160 if (*str || id < 1 || id > UINT32_MAX) {
1161 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001162 goto error;
1163 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001164 session->id = (uint32_t)id;
1165 continue;
1166 } else if (strcmp(node->name, "capabilities")) {
1167 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001168 goto error;
1169 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001170
1171 if (flag) {
1172 /* multiple capabilities elements */
1173 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1174 goto error;
1175 }
1176 flag = 1;
1177
Michal Vasko2e6defd2016-10-07 15:48:15 +02001178 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001179 goto error;
1180 }
1181 session->version = ver;
1182 }
1183
1184 if (!session->id) {
1185 ERR("Missing <session-id> in server's <hello>.");
1186 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001187 }
1188 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001189 case NC_MSG_WOULDBLOCK:
1190 ERR("Server's <hello> timeout elapsed.");
1191 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001192 case NC_MSG_ERROR:
1193 /* nothing special, just pass it out */
1194 break;
1195 default:
1196 ERR("Unexpected message received instead of <hello>.");
1197 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001198 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001199 }
1200
1201 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001202 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001203
1204 return msgtype;
1205
1206error:
1207 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001208 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001209
1210 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001211}
1212
Michal Vasko11d142a2016-01-19 15:58:24 +01001213static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001214nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001215{
1216 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001217 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001218 int ver = -1;
1219 int flag = 0;
1220
Michal Vasko131120a2018-05-29 15:44:02 +02001221 msgtype = nc_read_msg_poll_io(session, (server_opts.hello_timeout ?
1222 server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001223
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001224 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001225 case NC_MSG_HELLO:
1226 /* get know NETCONF version */
1227 LY_TREE_FOR(xml->child, node) {
1228 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1229 continue;
1230 } else if (strcmp(node->name, "capabilities")) {
1231 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001232 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001233 goto cleanup;
1234 }
1235
1236 if (flag) {
1237 /* multiple capabilities elements */
1238 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001239 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001240 goto cleanup;
1241 }
1242 flag = 1;
1243
1244 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001245 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001246 goto cleanup;
1247 }
1248 session->version = ver;
1249 }
1250 break;
1251 case NC_MSG_ERROR:
1252 /* nothing special, just pass it out */
1253 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001254 case NC_MSG_WOULDBLOCK:
1255 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001256 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001257 default:
1258 ERR("Unexpected message received instead of <hello>.");
1259 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001260 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001261 }
1262
1263cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001264 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001265 return msgtype;
1266}
1267
Michal Vasko71090fc2016-05-24 16:37:28 +02001268NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001269nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001270{
Michal Vasko086311b2016-01-08 09:53:11 +01001271 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001272
Michal Vasko131120a2018-05-29 15:44:02 +02001273 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001274 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001275 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001276 }
1277
Michal Vasko11d142a2016-01-19 15:58:24 +01001278 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001279 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001280 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001281 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001282 }
1283
Michal Vasko71090fc2016-05-24 16:37:28 +02001284 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001285}
Michal Vasko086311b2016-01-08 09:53:11 +01001286
Radek Krejci53691be2016-02-22 13:58:37 +01001287#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001288
Michal Vasko8f0c0282016-02-29 10:17:14 +01001289static void
Michal Vasko086311b2016-01-08 09:53:11 +01001290nc_ssh_init(void)
1291{
1292 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1293 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001294}
1295
Michal Vasko8f0c0282016-02-29 10:17:14 +01001296static void
Michal Vasko086311b2016-01-08 09:53:11 +01001297nc_ssh_destroy(void)
1298{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001299 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001300 ENGINE_cleanup();
1301 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001302 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001303 ssh_finalize();
1304}
1305
Radek Krejci53691be2016-02-22 13:58:37 +01001306#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001307
Radek Krejci53691be2016-02-22 13:58:37 +01001308#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001309
Michal Vasko770b4362017-02-17 14:44:18 +01001310#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1311
Michal Vaskof0c92c02016-01-29 09:41:45 +01001312struct CRYPTO_dynlock_value {
1313 pthread_mutex_t lock;
1314};
1315
Michal Vaskof0c92c02016-01-29 09:41:45 +01001316static struct CRYPTO_dynlock_value *
1317tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1318{
1319 struct CRYPTO_dynlock_value *value;
1320
1321 value = malloc(sizeof *value);
1322 if (!value) {
1323 ERRMEM;
1324 return NULL;
1325 }
1326 pthread_mutex_init(&value->lock, NULL);
1327
1328 return value;
1329}
1330
1331static void
1332tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1333{
1334 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1335 * I found ignored this fact, what do I know... */
1336 if (mode & CRYPTO_LOCK) {
1337 pthread_mutex_lock(&l->lock);
1338 } else {
1339 pthread_mutex_unlock(&l->lock);
1340 }
1341}
1342
1343static void
1344tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1345{
1346 pthread_mutex_destroy(&l->lock);
1347 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001348}
Michal Vasko770b4362017-02-17 14:44:18 +01001349#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001350
Michal Vasko8f0c0282016-02-29 10:17:14 +01001351#endif /* NC_ENABLED_TLS */
1352
1353#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1354
Michal Vasko770b4362017-02-17 14:44:18 +01001355#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001356static pthread_mutex_t *tls_locks;
1357
1358static void
1359tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1360{
1361 if (mode & CRYPTO_LOCK) {
1362 pthread_mutex_lock(tls_locks + n);
1363 } else {
1364 pthread_mutex_unlock(tls_locks + n);
1365 }
1366}
1367
1368static void
1369tls_thread_id_func(CRYPTO_THREADID *tid)
1370{
1371 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1372}
Michal Vasko770b4362017-02-17 14:44:18 +01001373#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001374
1375static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001376nc_tls_init(void)
1377{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001378 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001379 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001380 SSL_library_init();
1381
Michal Vasko770b4362017-02-17 14:44:18 +01001382#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001383 int i;
1384
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001385 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001386 if (!tls_locks) {
1387 ERRMEM;
1388 return;
1389 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001390 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1391 pthread_mutex_init(tls_locks + i, NULL);
1392 }
1393
Michal Vaskof0c92c02016-01-29 09:41:45 +01001394 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001395 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001396
1397 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1398 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1399 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
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 +01001403static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001404nc_tls_destroy(void)
1405{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001406 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001407 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001408 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001409 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001410 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001411#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001412 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001413#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1414 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001415#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001416
Michal Vasko770b4362017-02-17 14:44:18 +01001417#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001418 int i;
1419
Michal Vaskob6e37262016-02-25 14:49:00 +01001420 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001421 CRYPTO_set_locking_callback(NULL);
1422 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1423 pthread_mutex_destroy(tls_locks + i);
1424 }
1425 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001426
1427 CRYPTO_set_dynlock_create_callback(NULL);
1428 CRYPTO_set_dynlock_lock_callback(NULL);
1429 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001430#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001431}
1432
Michal Vasko8f0c0282016-02-29 10:17:14 +01001433#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001434
Radek Krejci53691be2016-02-22 13:58:37 +01001435#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001436
Michal Vasko8f0c0282016-02-29 10:17:14 +01001437static void
Michal Vasko5e228792016-02-03 15:30:24 +01001438nc_ssh_tls_init(void)
1439{
1440 SSL_load_error_strings();
1441 ERR_load_BIO_strings();
1442 SSL_library_init();
1443
1444 nc_ssh_init();
1445
Michal Vasko770b4362017-02-17 14:44:18 +01001446#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001447 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1448 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1449 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001450#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001451}
1452
Michal Vasko8f0c0282016-02-29 10:17:14 +01001453static void
Michal Vasko5e228792016-02-03 15:30:24 +01001454nc_ssh_tls_destroy(void)
1455{
1456 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001457#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001458 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001459#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1460 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001461#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001462
1463 nc_ssh_destroy();
1464
Michal Vasko770b4362017-02-17 14:44:18 +01001465#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001466 CRYPTO_set_dynlock_create_callback(NULL);
1467 CRYPTO_set_dynlock_lock_callback(NULL);
1468 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001469#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001470}
1471
Radek Krejci53691be2016-02-22 13:58:37 +01001472#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001473
1474#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1475
1476API void
1477nc_thread_destroy(void)
1478{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001479 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1480 //CRYPTO_cleanup_all_ex_data();
1481
Michal Vasko770b4362017-02-17 14:44:18 +01001482#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1483 CRYPTO_THREADID crypto_tid;
1484
Michal Vasko8f0c0282016-02-29 10:17:14 +01001485 CRYPTO_THREADID_current(&crypto_tid);
1486 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001487#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001488}
1489
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001490#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1491
1492void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001493nc_init(void)
1494{
1495#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1496 nc_ssh_tls_init();
1497#elif defined(NC_ENABLED_SSH)
1498 nc_ssh_init();
1499#elif defined(NC_ENABLED_TLS)
1500 nc_tls_init();
1501#endif
1502}
1503
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001504void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001505nc_destroy(void)
1506{
1507#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1508 nc_ssh_tls_destroy();
1509#elif defined(NC_ENABLED_SSH)
1510 nc_ssh_destroy();
1511#elif defined(NC_ENABLED_TLS)
1512 nc_tls_destroy();
1513#endif
1514}