blob: afc89f587cd1dc04de6cc430911f81269dee0ee2 [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 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
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
Radek Krejci206fcd62015-10-07 15:42:48 +020015#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010016#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010017#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020018#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020019#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010020#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020021#include <libyang/libyang.h>
22
Michal Vasko5e228792016-02-03 15:30:24 +010023#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010024#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010025#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010026
Radek Krejci53691be2016-02-22 13:58:37 +010027#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020028
Michal Vasko086311b2016-01-08 09:53:11 +010029# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Radek Krejci53691be2016-02-22 13:58:37 +010031#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020032
Radek Krejci53691be2016-02-22 13:58:37 +010033#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010034
Michal Vasko5e228792016-02-03 15:30:24 +010035# include <openssl/engine.h>
36# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010037# include <openssl/err.h>
38
Radek Krejci53691be2016-02-22 13:58:37 +010039#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010040
Michal Vasko086311b2016-01-08 09:53:11 +010041/* in seconds */
42#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020043
Michal Vasko05ba9df2016-01-13 14:40:27 +010044/* in milliseconds */
45#define NC_CLOSE_REPLY_TIMEOUT 200
46
Michal Vasko086311b2016-01-08 09:53:11 +010047extern struct nc_server_opts server_opts;
48
Radek Krejci7ac16052016-07-15 11:48:18 +020049int
50nc_gettimespec(struct timespec *ts)
51{
Michal Vasko0ef46df2016-09-21 14:03:18 +020052#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020053 return clock_gettime(CLOCK_REALTIME, ts);
54#else
55 int rc;
56 struct timeval tv;
57
58 rc = gettimeofday(&tv, NULL);
59 if (!rc) {
60 ts->tv_sec = (time_t)tv.tv_sec;
61 ts->tv_nsec = 1000L * (long)tv.tv_usec;
62 }
63 return rc;
64#endif
65}
66
Michal Vasko99f251b2017-01-11 11:31:46 +010067/* ts1 < ts2, returns milliseconds */
68uint32_t
69nc_difftimespec(struct timespec *ts1, struct timespec *ts2)
70{
71 uint64_t nsec_diff = 0;
72
73 if (ts1->tv_nsec > ts2->tv_nsec) {
74 ts2->tv_nsec += 1000000000L;
75 --ts2->tv_sec;
76 }
77
78 if (ts1->tv_sec <= ts2->tv_sec) {
79 nsec_diff += (ts2->tv_sec - ts1->tv_sec) * 1000000000L;
80 } else {
81 ERRINT;
82 }
83
84 if (ts1->tv_nsec < ts2->tv_nsec) {
85 nsec_diff += ts2->tv_nsec - ts1->tv_nsec;
86 }
87
88 return (nsec_diff ? nsec_diff / 1000000L : 0);
89}
90
Radek Krejci28472922016-07-15 11:51:16 +020091#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
92int
93pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
94{
95 int rc;
96 struct timespec cur, dur;
97
98 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
99 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
100 nc_gettimespec(&cur);
101
102 if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
103 break;
104 }
105
106 dur.tv_sec = abstime->tv_sec - cur.tv_sec;
107 dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
108 if (dur.tv_nsec < 0) {
109 dur.tv_sec--;
110 dur.tv_nsec += 1000000000;
111 }
112
113 if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
114 dur.tv_sec = 0;
115 dur.tv_nsec = 5000000;
116 }
117
118 nanosleep(&dur, NULL);
119 }
120
121 return rc;
122}
123#endif
124
Michal Vasko96164bf2016-01-21 15:41:58 +0100125/*
126 * @return 1 - success
127 * 0 - timeout
128 * -1 - error
129 */
130int
Michal vasko50cc94f2016-10-04 13:46:20 +0200131nc_timedlock(pthread_mutex_t *lock, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100132{
133 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100134 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100135
136 if (timeout > 0) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200137 nc_gettimespec(&ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100138
Michal Vasko96164bf2016-01-21 15:41:58 +0100139 ts_timeout.tv_sec += timeout / 1000;
140 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
141
142 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100143 } else if (!timeout) {
144 ret = pthread_mutex_trylock(lock);
Michal vasko2f8e4b52016-10-05 13:04:11 +0200145 if (ret == EBUSY) {
146 /* equivalent in this case */
147 ret = ETIMEDOUT;
148 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100149 } else { /* timeout == -1 */
150 ret = pthread_mutex_lock(lock);
151 }
152
153 if (ret == ETIMEDOUT) {
154 /* timeout */
155 return 0;
156 } else if (ret) {
157 /* error */
Michal vasko50cc94f2016-10-04 13:46:20 +0200158 ERR("Mutex lock failed (%s, %s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100159 return -1;
160 }
161
162 /* ok */
163 return 1;
164}
165
Michal Vasko8dadf782016-01-15 10:29:36 +0100166API NC_STATUS
167nc_session_get_status(const struct nc_session *session)
168{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100169 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200170 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100171 return 0;
172 }
173
Michal Vasko8dadf782016-01-15 10:29:36 +0100174 return session->status;
175}
176
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100177API NC_SESSION_TERM_REASON
178nc_session_get_termreason(const struct nc_session *session)
179{
180 if (!session) {
181 ERRARG("session");
182 return 0;
183 }
184
185 return session->term_reason;
186}
187
Michal Vasko8dadf782016-01-15 10:29:36 +0100188API uint32_t
189nc_session_get_id(const struct nc_session *session)
190{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100191 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200192 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100193 return 0;
194 }
195
Michal Vasko8dadf782016-01-15 10:29:36 +0100196 return session->id;
197}
198
Michal Vasko174fe8e2016-02-17 15:38:09 +0100199API int
200nc_session_get_version(const struct nc_session *session)
201{
202 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200203 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100204 return -1;
205 }
206
207 return (session->version == NC_VERSION_10 ? 0 : 1);
208}
209
Michal Vasko8dadf782016-01-15 10:29:36 +0100210API NC_TRANSPORT_IMPL
211nc_session_get_ti(const struct nc_session *session)
212{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100213 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200214 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100215 return 0;
216 }
217
Michal Vasko8dadf782016-01-15 10:29:36 +0100218 return session->ti_type;
219}
220
221API const char *
222nc_session_get_username(const struct nc_session *session)
223{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100224 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200225 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100226 return NULL;
227 }
228
Michal Vasko8dadf782016-01-15 10:29:36 +0100229 return session->username;
230}
231
232API const char *
233nc_session_get_host(const struct nc_session *session)
234{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100235 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200236 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100237 return NULL;
238 }
239
Michal Vasko8dadf782016-01-15 10:29:36 +0100240 return session->host;
241}
242
243API uint16_t
244nc_session_get_port(const struct nc_session *session)
245{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100246 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200247 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100248 return 0;
249 }
250
Michal Vasko8dadf782016-01-15 10:29:36 +0100251 return session->port;
252}
253
Michal Vasko9a25e932016-02-01 10:36:42 +0100254API struct ly_ctx *
255nc_session_get_ctx(const struct nc_session *session)
256{
257 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200258 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100259 return NULL;
260 }
261
262 return session->ctx;
263}
264
Michal Vasko2cc4c682016-03-01 09:16:48 +0100265API void
266nc_session_set_data(struct nc_session *session, void *data)
267{
268 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200269 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100270 return;
271 }
272
273 session->data = data;
274}
275
276API void *
277nc_session_get_data(const struct nc_session *session)
278{
279 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200280 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100281 return NULL;
282 }
283
284 return session->data;
285}
286
Michal Vasko086311b2016-01-08 09:53:11 +0100287NC_MSG_TYPE
288nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200289{
Michal Vasko086311b2016-01-08 09:53:11 +0100290 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200291
Michal Vasko086311b2016-01-08 09:53:11 +0100292 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100293 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
294 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100295 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100296 }
297
Michal Vasko086311b2016-01-08 09:53:11 +0100298 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
299
300 if (r) {
301 return NC_MSG_ERROR;
302 }
303
304 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100305}
306
Radek Krejci695d4fa2015-10-22 13:23:54 +0200307API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100308nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200309{
Michal Vasko9e99f012016-03-03 13:25:20 +0100310 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100311 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100312 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100313 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100314 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100315 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100316 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200317 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100318 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200319 void *p;
320
Michal Vasko428087d2016-01-14 16:04:28 +0100321 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200322 return;
323 }
324
Michal Vasko86d357c2016-03-11 13:46:38 +0100325 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200326 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
327 tid = *session->opts.client.ntf_tid;
328 free((pthread_t *)session->opts.client.ntf_tid);
329 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100330 /* the thread now knows it should quit */
331
332 pthread_join(tid, NULL);
333 }
334
Michal Vaskoadd4c792015-10-26 15:36:58 +0100335 if (session->ti_lock) {
Michal vasko50cc94f2016-10-04 13:46:20 +0200336 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100337 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100338 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100339 } else if (!r) {
340 /* we failed to lock it, too bad */
341 locked = 0;
342 } else {
343 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100344 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200345 } else {
346 ERRINT;
347 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200348 }
349
Michal Vasko9e99f012016-03-03 13:25:20 +0100350 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200351 /* cleanup message queues */
352 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200353 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100354 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200355
Michal Vaskoad611702015-12-03 13:41:51 +0100356 p = contiter;
357 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200358 free(p);
359 }
360
361 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200362 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100363 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200364
Michal Vaskoad611702015-12-03 13:41:51 +0100365 p = contiter;
366 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200367 free(p);
368 }
369
370 /* send closing info to the other side */
371 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
372 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100373 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200374 } else {
375 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100376 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200377 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100378 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100379 case NC_MSG_REPLY:
380 LY_TREE_FOR(rpl->child, child) {
381 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
382 break;
383 }
384 }
385 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100386 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100387 }
Michal Vaskoad611702015-12-03 13:41:51 +0100388 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100389 break;
390 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100391 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100392 break;
393 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100394 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100395 break;
396 default:
397 /* cannot happen */
398 break;
399 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200400 }
401
402 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200403 if (session->opts.client.cpblts) {
404 for (i = 0; session->opts.client.cpblts[i]; i++) {
405 lydict_remove(session->ctx, session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200406 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200407 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200408 }
409 }
410
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100411 if (session->data && data_free) {
412 data_free(session->data);
413 }
414
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200415 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
416 /* CH LOCK */
417 pthread_mutex_lock(session->opts.server.ch_lock);
418 }
419
Michal Vasko86d357c2016-03-11 13:46:38 +0100420 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200421 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200422
423 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
424 pthread_cond_signal(session->opts.server.ch_cond);
425
426 /* CH UNLOCK */
427 pthread_mutex_unlock(session->opts.server.ch_lock);
428 }
429
Michal Vasko428087d2016-01-14 16:04:28 +0100430 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200431
432 /* transport implementation cleanup */
433 switch (session->ti_type) {
434 case NC_TI_FD:
435 /* nothing needed - file descriptors were provided by caller,
436 * so it is up to the caller to close them correctly
437 * TODO use callbacks
438 */
Michal Vasko3512e402016-01-28 16:22:34 +0100439 /* just to avoid compiler warning */
440 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100441 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200442 break;
443
Radek Krejci53691be2016-02-22 13:58:37 +0100444#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200445 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100446 if (connected) {
447 ssh_channel_free(session->ti.libssh.channel);
448 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200449 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
450 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
451 * it.
452 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100453 multisession = 0;
454 if (session->ti.libssh.next) {
455 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
456 if (siter->status != NC_STATUS_STARTING) {
457 multisession = 1;
458 break;
459 }
460 }
461 }
462
463 if (!multisession) {
464 /* it's not multisession yet, but we still need to free the starting sessions */
465 if (session->ti.libssh.next) {
466 do {
467 siter = session->ti.libssh.next;
468 session->ti.libssh.next = siter->ti.libssh.next;
469
470 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100471 lydict_remove(session->ctx, session->username);
472 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100473 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100474 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100475 }
476
477 free(siter);
478 } while (session->ti.libssh.next != session);
479 }
Michal Vasko428087d2016-01-14 16:04:28 +0100480 if (connected) {
481 ssh_disconnect(session->ti.libssh.session);
482 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200483 ssh_free(session->ti.libssh.session);
484 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200485 /* remove the session from the list */
486 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100487 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200488 /* there will be only one session */
489 siter->ti.libssh.next = NULL;
490 } else {
491 /* there are still multiple sessions, keep the ring list */
492 siter->ti.libssh.next = session->ti.libssh.next;
493 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100494 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
495 if (session->flags & NC_SESSION_SSH_MSG_CB) {
496 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
497 if (siter->ti.libssh.next == session) {
498 ERRINT;
499 break;
500 }
501 }
502 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
503 siter->flags |= NC_SESSION_SSH_MSG_CB;
504 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200505 }
506 break;
507#endif
508
Radek Krejci53691be2016-02-22 13:58:37 +0100509#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200510 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100511 if (connected) {
512 SSL_shutdown(session->ti.tls);
513 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200514 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100515
Michal Vasko2e6defd2016-10-07 15:48:15 +0200516 if (session->side == NC_SERVER) {
517 X509_free(session->opts.server.client_cert);
518 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200519 break;
520#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100521 case NC_TI_NONE:
522 ERRINT;
523 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200524 }
Michal Vasko428087d2016-01-14 16:04:28 +0100525
Radek Krejciac6d3472015-10-22 15:47:18 +0200526 lydict_remove(session->ctx, session->username);
527 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200528
529 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100530 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100531 if (locked) {
532 pthread_mutex_unlock(session->ti_lock);
533 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100534 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100535 pthread_mutex_destroy(session->ti_lock);
536 free(session->ti_lock);
537 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200538 }
539
540 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100541 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200542 }
543
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200544 if (session->side == NC_SERVER) {
545 if (session->opts.server.ch_cond) {
546 pthread_cond_destroy(session->opts.server.ch_cond);
547 free(session->opts.server.ch_cond);
548 }
549 if (session->opts.server.ch_lock) {
550 pthread_mutex_destroy(session->opts.server.ch_lock);
551 free(session->opts.server.ch_lock);
552 }
553 }
554
Radek Krejci695d4fa2015-10-22 13:23:54 +0200555 free(session);
556}
557
Michal Vasko086311b2016-01-08 09:53:11 +0100558static void
559add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
560{
Radek Krejci658782b2016-12-04 22:04:55 +0100561 size_t len;
562 int i;
563 char *p;
564
565 if (capab) {
566 /* check if already present */
567 p = strchr(capab, '?');
568 if (p) {
569 len = p - capab;
570 } else {
571 len = strlen(capab);
572 }
573 for (i = 0; i < *count; i++) {
574 if (!strncmp((*cpblts)[i], capab, len)) {
575 /* already present, do not duplicate it */
576 return;
577 }
578 }
579 }
580
581 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100582 if (*count == *size) {
583 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100584 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
585 if (!(*cpblts)) {
586 ERRMEM;
587 return;
588 }
Michal Vasko086311b2016-01-08 09:53:11 +0100589 }
590
591 if (capab) {
592 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
593 } else {
594 (*cpblts)[*count] = NULL;
595 }
596 ++(*count);
597}
598
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200599API const char **
600nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100601{
602 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200603 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 +0100604 const char **cpblts;
605 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200606 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Radek Krejci658782b2016-12-04 22:04:55 +0100607 unsigned int u;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200608#define NC_CPBLT_BUF_LEN 512
609 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100610
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200611 if (!ctx) {
612 ERRARG("ctx");
613 return NULL;
614 }
615
Michal Vasko086311b2016-01-08 09:53:11 +0100616 yanglib = ly_ctx_info(ctx);
617 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200618 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100619 return NULL;
620 }
621
622 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100623 if (!cpblts) {
624 ERRMEM;
625 return NULL;
626 }
Michal Vasko086311b2016-01-08 09:53:11 +0100627 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
628 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
629 count = 2;
630
631 /* capabilities */
632
633 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
634 if (mod) {
635 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100636 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100637 }
638 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100639 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100640 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100641 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100642 }
643 }
644 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100645 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100646 }
647 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100648 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100649 }
650 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100651 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100652 }
653 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100654 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100655 }
656 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100657 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100658 }
659 }
660
661 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
662 if (mod) {
663 if (!server_opts.wd_basic_mode) {
664 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
665 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100666 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100667 switch (server_opts.wd_basic_mode) {
668 case NC_WD_ALL:
669 strcat(str, "?basic-mode=report-all");
670 break;
671 case NC_WD_TRIM:
672 strcat(str, "?basic-mode=trim");
673 break;
674 case NC_WD_EXPLICIT:
675 strcat(str, "?basic-mode=explicit");
676 break;
677 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100678 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100679 break;
680 }
681
682 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200683 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100684 if (server_opts.wd_also_supported & NC_WD_ALL) {
685 strcat(str, "report-all,");
686 }
687 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
688 strcat(str, "report-all-tagged,");
689 }
690 if (server_opts.wd_also_supported & NC_WD_TRIM) {
691 strcat(str, "trim,");
692 }
693 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
694 strcat(str, "explicit,");
695 }
696 str[strlen(str) - 1] = '\0';
697
698 add_cpblt(ctx, str, &cpblts, &size, &count);
699 }
700 }
701 }
702
Radek Krejci658782b2016-12-04 22:04:55 +0100703 /* other capabilities */
704 for (u = 0; u < server_opts.capabilities_count; u++) {
705 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100706 }
707
708 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100709 LY_TREE_FOR(yanglib->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200710 if (!module_set_id) {
711 if (strcmp(child->prev->schema->name, "module-set-id")) {
712 ERRINT;
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200713 free(cpblts);
714 free(deviations);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200715 return NULL;
716 }
717 module_set_id = (struct lyd_node_leaf_list *)child->prev;
718 }
Michal Vasko086311b2016-01-08 09:53:11 +0100719 if (!strcmp(child->schema->name, "module")) {
720 LY_TREE_FOR(child->child, child2) {
721 if (!strcmp(child2->schema->name, "namespace")) {
722 ns = (struct lyd_node_leaf_list *)child2;
723 } else if (!strcmp(child2->schema->name, "name")) {
724 name = (struct lyd_node_leaf_list *)child2;
725 } else if (!strcmp(child2->schema->name, "revision")) {
726 rev = (struct lyd_node_leaf_list *)child2;
727 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100728 features = nc_realloc(features, ++feat_count * sizeof *features);
729 if (!features) {
730 ERRMEM;
731 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200732 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100733 return NULL;
734 }
Michal Vasko086311b2016-01-08 09:53:11 +0100735 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200736 } else if (!strcmp(child2->schema->name, "deviation")) {
737 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
738 if (!deviations) {
739 ERRMEM;
740 free(cpblts);
741 free(features);
742 return NULL;
743 }
Michal Vasko086311b2016-01-08 09:53:11 +0100744 }
745 }
746
747 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100748 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100749 continue;
750 }
751
Radek Krejcidf7ba522016-03-01 16:05:25 +0100752 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200753 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100754 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200755 strcat(str, "&features=");
756 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100757 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200758 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100759 ERRINT;
760 break;
761 }
Michal Vasko086311b2016-01-08 09:53:11 +0100762 if (i) {
763 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100764 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100765 }
766 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100767 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100768 }
769 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200770 if (dev_count) {
771 strcat(str, "&deviations=");
772 str_len += 12;
773 for (i = 0; i < dev_count; ++i) {
774 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
775 ERRINT;
776 break;
777 }
778 if (i) {
779 strcat(str, ",");
780 ++str_len;
781 }
782 strcat(str, deviations[i]->value_str);
783 str_len += strlen(deviations[i]->value_str);
784 }
785 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200786 if (!strcmp(name->value_str, "ietf-yang-library")) {
787 str_len += sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
788 }
Michal Vasko086311b2016-01-08 09:53:11 +0100789
790 add_cpblt(ctx, str, &cpblts, &size, &count);
791
792 ns = NULL;
793 name = NULL;
794 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200795 if (features || feat_count) {
796 free(features);
797 features = NULL;
798 feat_count = 0;
799 }
800 if (deviations || dev_count) {
801 free(deviations);
802 deviations = NULL;
803 dev_count = 0;
804 }
Michal Vasko086311b2016-01-08 09:53:11 +0100805 }
806 }
807
808 lyd_free(yanglib);
809
810 /* ending NULL capability */
811 add_cpblt(ctx, NULL, &cpblts, &size, &count);
812
813 return cpblts;
814}
815
Radek Krejci695d4fa2015-10-22 13:23:54 +0200816static int
817parse_cpblts(struct lyxml_elem *xml, const char ***list)
818{
819 struct lyxml_elem *cpblt;
820 int ver = -1;
821 int i = 0;
822
823 if (list) {
824 /* get the storage for server's capabilities */
825 LY_TREE_FOR(xml->child, cpblt) {
826 i++;
827 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100828 /* last item remains NULL */
829 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200830 if (!*list) {
831 ERRMEM;
832 return -1;
833 }
834 i = 0;
835 }
836
837 LY_TREE_FOR(xml->child, cpblt) {
838 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
839 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
840 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
841 return -1;
842 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
843 continue;
844 }
845
846 /* detect NETCONF version */
847 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
848 ver = 0;
849 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
850 ver = 1;
851 }
852
853 /* store capabilities */
854 if (list) {
855 (*list)[i] = cpblt->content;
856 cpblt->content = NULL;
857 i++;
858 }
859 }
860
861 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100862 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200863 }
864
865 return ver;
866}
867
868static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100869nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100870{
871 int r, i;
872 const char **cpblts;
873
Michal Vasko11d142a2016-01-19 15:58:24 +0100874 /* client side hello - send only NETCONF base capabilities */
875 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100876 if (!cpblts) {
877 ERRMEM;
878 return NC_MSG_ERROR;
879 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100880 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
881 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
882 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100883
Michal Vasko11d142a2016-01-19 15:58:24 +0100884 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100885
Michal Vasko086311b2016-01-08 09:53:11 +0100886 for (i = 0; cpblts[i]; ++i) {
887 lydict_remove(session->ctx, cpblts[i]);
888 }
889 free(cpblts);
890
891 if (r) {
892 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100893 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100894
895 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100896}
897
898static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100899nc_send_server_hello(struct nc_session *session)
900{
901 int r, i;
902 const char **cpblts;
903
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200904 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100905
906 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
907
Michal Vasko11d142a2016-01-19 15:58:24 +0100908 for (i = 0; cpblts[i]; ++i) {
909 lydict_remove(session->ctx, cpblts[i]);
910 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100911 free(cpblts);
912
913 if (r) {
914 return NC_MSG_ERROR;
915 }
916
917 return NC_MSG_HELLO;
918}
919
920static NC_MSG_TYPE
921nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200922{
923 struct lyxml_elem *xml = NULL, *node;
924 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
925 int ver = -1;
926 char *str;
927 long long int id;
928 int flag = 0;
929
Michal Vasko05ba9df2016-01-13 14:40:27 +0100930 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200931
932 switch(msgtype) {
933 case NC_MSG_HELLO:
934 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100935 LY_TREE_FOR(xml->child, node) {
936 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
937 continue;
938 } else if (!strcmp(node->name, "session-id")) {
939 if (!node->content || !strlen(node->content)) {
940 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200941 goto error;
942 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100943 str = NULL;
944 id = strtoll(node->content, &str, 10);
945 if (*str || id < 1 || id > UINT32_MAX) {
946 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200947 goto error;
948 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100949 session->id = (uint32_t)id;
950 continue;
951 } else if (strcmp(node->name, "capabilities")) {
952 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200953 goto error;
954 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100955
956 if (flag) {
957 /* multiple capabilities elements */
958 ERR("Invalid <hello> message (multiple <capabilities> elements).");
959 goto error;
960 }
961 flag = 1;
962
Michal Vasko2e6defd2016-10-07 15:48:15 +0200963 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100964 goto error;
965 }
966 session->version = ver;
967 }
968
969 if (!session->id) {
970 ERR("Missing <session-id> in server's <hello>.");
971 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200972 }
973 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200974 case NC_MSG_WOULDBLOCK:
975 ERR("Server's <hello> timeout elapsed.");
976 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200977 case NC_MSG_ERROR:
978 /* nothing special, just pass it out */
979 break;
980 default:
981 ERR("Unexpected message received instead of <hello>.");
982 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200983 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200984 }
985
986 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100987 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200988
989 return msgtype;
990
991error:
992 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100993 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200994
995 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200996}
997
Michal Vasko11d142a2016-01-19 15:58:24 +0100998static NC_MSG_TYPE
999nc_recv_server_hello(struct nc_session *session)
1000{
1001 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001002 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001003 int ver = -1;
1004 int flag = 0;
1005
Michal Vaskoadb850e2016-01-20 14:06:32 +01001006 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001007
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001008 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001009 case NC_MSG_HELLO:
1010 /* get know NETCONF version */
1011 LY_TREE_FOR(xml->child, node) {
1012 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1013 continue;
1014 } else if (strcmp(node->name, "capabilities")) {
1015 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001016 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001017 goto cleanup;
1018 }
1019
1020 if (flag) {
1021 /* multiple capabilities elements */
1022 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001023 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001024 goto cleanup;
1025 }
1026 flag = 1;
1027
1028 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001029 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001030 goto cleanup;
1031 }
1032 session->version = ver;
1033 }
1034 break;
1035 case NC_MSG_ERROR:
1036 /* nothing special, just pass it out */
1037 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001038 case NC_MSG_WOULDBLOCK:
1039 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001040 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001041 default:
1042 ERR("Unexpected message received instead of <hello>.");
1043 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001044 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001045 }
1046
1047cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001048 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001049
1050 return msgtype;
1051}
1052
Michal Vasko71090fc2016-05-24 16:37:28 +02001053NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001054nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001055{
Michal Vasko086311b2016-01-08 09:53:11 +01001056 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001057
Michal Vasko11d142a2016-01-19 15:58:24 +01001058 if (session->side == NC_CLIENT) {
1059 type = nc_send_client_hello(session);
1060 } else {
1061 type = nc_send_server_hello(session);
1062 }
1063
Michal Vasko086311b2016-01-08 09:53:11 +01001064 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001065 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001066 }
1067
Michal Vasko11d142a2016-01-19 15:58:24 +01001068 if (session->side == NC_CLIENT) {
1069 type = nc_recv_client_hello(session);
1070 } else {
1071 type = nc_recv_server_hello(session);
1072 }
1073
Michal Vasko71090fc2016-05-24 16:37:28 +02001074 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001075}
Michal Vasko086311b2016-01-08 09:53:11 +01001076
Radek Krejci53691be2016-02-22 13:58:37 +01001077#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001078
Michal Vasko8f0c0282016-02-29 10:17:14 +01001079static void
Michal Vasko086311b2016-01-08 09:53:11 +01001080nc_ssh_init(void)
1081{
1082 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1083 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001084}
1085
Michal Vasko8f0c0282016-02-29 10:17:14 +01001086static void
Michal Vasko086311b2016-01-08 09:53:11 +01001087nc_ssh_destroy(void)
1088{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001089 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001090 ENGINE_cleanup();
1091 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001092 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001093 ssh_finalize();
1094}
1095
Radek Krejci53691be2016-02-22 13:58:37 +01001096#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001097
Radek Krejci53691be2016-02-22 13:58:37 +01001098#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001099
Michal Vaskof0c92c02016-01-29 09:41:45 +01001100struct CRYPTO_dynlock_value {
1101 pthread_mutex_t lock;
1102};
1103
Michal Vaskof0c92c02016-01-29 09:41:45 +01001104static struct CRYPTO_dynlock_value *
1105tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1106{
1107 struct CRYPTO_dynlock_value *value;
1108
1109 value = malloc(sizeof *value);
1110 if (!value) {
1111 ERRMEM;
1112 return NULL;
1113 }
1114 pthread_mutex_init(&value->lock, NULL);
1115
1116 return value;
1117}
1118
1119static void
1120tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1121{
1122 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1123 * I found ignored this fact, what do I know... */
1124 if (mode & CRYPTO_LOCK) {
1125 pthread_mutex_lock(&l->lock);
1126 } else {
1127 pthread_mutex_unlock(&l->lock);
1128 }
1129}
1130
1131static void
1132tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1133{
1134 pthread_mutex_destroy(&l->lock);
1135 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001136}
1137
Michal Vasko8f0c0282016-02-29 10:17:14 +01001138#endif /* NC_ENABLED_TLS */
1139
1140#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1141
1142static pthread_mutex_t *tls_locks;
1143
1144static void
1145tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1146{
1147 if (mode & CRYPTO_LOCK) {
1148 pthread_mutex_lock(tls_locks + n);
1149 } else {
1150 pthread_mutex_unlock(tls_locks + n);
1151 }
1152}
1153
1154static void
1155tls_thread_id_func(CRYPTO_THREADID *tid)
1156{
1157 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1158}
1159
1160static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001161nc_tls_init(void)
1162{
1163 int i;
1164
1165 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001166 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001167 SSL_library_init();
1168
1169 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001170 if (!tls_locks) {
1171 ERRMEM;
1172 return;
1173 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001174 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1175 pthread_mutex_init(tls_locks + i, NULL);
1176 }
1177
Michal Vaskof0c92c02016-01-29 09:41:45 +01001178 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001179 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001180
1181 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1182 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1183 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001184}
1185
Michal Vasko8f0c0282016-02-29 10:17:14 +01001186static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001187nc_tls_destroy(void)
1188{
1189 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001190
Michal Vasko8f0c0282016-02-29 10:17:14 +01001191 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001192 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001193 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001194 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001195 ERR_free_strings();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001196#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1197 // no de-init needed
1198#elif OPENSSL_VERSION_NUMBER >= 0x10002000L // >= 1.0.2
1199 SSL_COMP_free_compression_methods();
1200#else
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001201 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001202#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001203
Michal Vaskob6e37262016-02-25 14:49:00 +01001204 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001205 CRYPTO_set_locking_callback(NULL);
1206 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1207 pthread_mutex_destroy(tls_locks + i);
1208 }
1209 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001210
1211 CRYPTO_set_dynlock_create_callback(NULL);
1212 CRYPTO_set_dynlock_lock_callback(NULL);
1213 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001214}
1215
Michal Vasko8f0c0282016-02-29 10:17:14 +01001216#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001217
Radek Krejci53691be2016-02-22 13:58:37 +01001218#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001219
Michal Vasko8f0c0282016-02-29 10:17:14 +01001220static void
Michal Vasko5e228792016-02-03 15:30:24 +01001221nc_ssh_tls_init(void)
1222{
1223 SSL_load_error_strings();
1224 ERR_load_BIO_strings();
1225 SSL_library_init();
1226
1227 nc_ssh_init();
1228
1229 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1230 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1231 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1232}
1233
Michal Vasko8f0c0282016-02-29 10:17:14 +01001234static void
Michal Vasko5e228792016-02-03 15:30:24 +01001235nc_ssh_tls_destroy(void)
1236{
1237 ERR_free_strings();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001238#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1239 // no de-init needed
1240#elif OPENSSL_VERSION_NUMBER >= 0x10002000L // >= 1.0.2
1241 SSL_COMP_free_compression_methods();
1242#else
Michal Vasko5e228792016-02-03 15:30:24 +01001243 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001244#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001245
1246 nc_ssh_destroy();
1247
1248 CRYPTO_set_dynlock_create_callback(NULL);
1249 CRYPTO_set_dynlock_lock_callback(NULL);
1250 CRYPTO_set_dynlock_destroy_callback(NULL);
1251}
1252
Radek Krejci53691be2016-02-22 13:58:37 +01001253#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001254
1255#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1256
1257API void
1258nc_thread_destroy(void)
1259{
1260 CRYPTO_THREADID crypto_tid;
1261
1262 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1263 //CRYPTO_cleanup_all_ex_data();
1264
1265 CRYPTO_THREADID_current(&crypto_tid);
1266 ERR_remove_thread_state(&crypto_tid);
1267}
1268
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001269#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1270
1271void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001272nc_init(void)
1273{
1274#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1275 nc_ssh_tls_init();
1276#elif defined(NC_ENABLED_SSH)
1277 nc_ssh_init();
1278#elif defined(NC_ENABLED_TLS)
1279 nc_tls_init();
1280#endif
1281}
1282
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001283void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001284nc_destroy(void)
1285{
1286#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1287 nc_ssh_tls_destroy();
1288#elif defined(NC_ENABLED_SSH)
1289 nc_ssh_destroy();
1290#elif defined(NC_ENABLED_TLS)
1291 nc_tls_destroy();
1292#endif
1293}