blob: 49f93f4abc0fd9c4127a4e161f6d33a137e58711 [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
Radek Krejci28472922016-07-15 11:51:16 +020067#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
68int
69pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
70{
71 int rc;
72 struct timespec cur, dur;
73
74 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
75 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
76 nc_gettimespec(&cur);
77
78 if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
79 break;
80 }
81
82 dur.tv_sec = abstime->tv_sec - cur.tv_sec;
83 dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
84 if (dur.tv_nsec < 0) {
85 dur.tv_sec--;
86 dur.tv_nsec += 1000000000;
87 }
88
89 if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
90 dur.tv_sec = 0;
91 dur.tv_nsec = 5000000;
92 }
93
94 nanosleep(&dur, NULL);
95 }
96
97 return rc;
98}
99#endif
100
Michal Vasko96164bf2016-01-21 15:41:58 +0100101/*
102 * @return 1 - success
103 * 0 - timeout
104 * -1 - error
105 */
106int
Michal vasko50cc94f2016-10-04 13:46:20 +0200107nc_timedlock(pthread_mutex_t *lock, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100108{
109 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100110 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100111
112 if (timeout > 0) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200113 nc_gettimespec(&ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100114
Michal Vasko96164bf2016-01-21 15:41:58 +0100115 ts_timeout.tv_sec += timeout / 1000;
116 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
117
118 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100119 } else if (!timeout) {
120 ret = pthread_mutex_trylock(lock);
Michal vasko2f8e4b52016-10-05 13:04:11 +0200121 if (ret == EBUSY) {
122 /* equivalent in this case */
123 ret = ETIMEDOUT;
124 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100125 } else { /* timeout == -1 */
126 ret = pthread_mutex_lock(lock);
127 }
128
129 if (ret == ETIMEDOUT) {
130 /* timeout */
131 return 0;
132 } else if (ret) {
133 /* error */
Michal vasko50cc94f2016-10-04 13:46:20 +0200134 ERR("Mutex lock failed (%s, %s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100135 return -1;
136 }
137
138 /* ok */
139 return 1;
140}
141
Michal Vasko8dadf782016-01-15 10:29:36 +0100142API NC_STATUS
143nc_session_get_status(const struct nc_session *session)
144{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100145 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200146 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100147 return 0;
148 }
149
Michal Vasko8dadf782016-01-15 10:29:36 +0100150 return session->status;
151}
152
153API uint32_t
154nc_session_get_id(const struct nc_session *session)
155{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100156 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200157 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100158 return 0;
159 }
160
Michal Vasko8dadf782016-01-15 10:29:36 +0100161 return session->id;
162}
163
Michal Vasko174fe8e2016-02-17 15:38:09 +0100164API int
165nc_session_get_version(const struct nc_session *session)
166{
167 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200168 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100169 return -1;
170 }
171
172 return (session->version == NC_VERSION_10 ? 0 : 1);
173}
174
Michal Vasko8dadf782016-01-15 10:29:36 +0100175API NC_TRANSPORT_IMPL
176nc_session_get_ti(const struct nc_session *session)
177{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100178 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200179 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100180 return 0;
181 }
182
Michal Vasko8dadf782016-01-15 10:29:36 +0100183 return session->ti_type;
184}
185
186API const char *
187nc_session_get_username(const struct nc_session *session)
188{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100189 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200190 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100191 return NULL;
192 }
193
Michal Vasko8dadf782016-01-15 10:29:36 +0100194 return session->username;
195}
196
197API const char *
198nc_session_get_host(const struct nc_session *session)
199{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100200 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200201 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100202 return NULL;
203 }
204
Michal Vasko8dadf782016-01-15 10:29:36 +0100205 return session->host;
206}
207
208API uint16_t
209nc_session_get_port(const struct nc_session *session)
210{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100211 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200212 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100213 return 0;
214 }
215
Michal Vasko8dadf782016-01-15 10:29:36 +0100216 return session->port;
217}
218
Michal Vasko9a25e932016-02-01 10:36:42 +0100219API struct ly_ctx *
220nc_session_get_ctx(const struct nc_session *session)
221{
222 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200223 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100224 return NULL;
225 }
226
227 return session->ctx;
228}
229
Michal Vasko2cc4c682016-03-01 09:16:48 +0100230API void
231nc_session_set_data(struct nc_session *session, void *data)
232{
233 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200234 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100235 return;
236 }
237
238 session->data = data;
239}
240
241API void *
242nc_session_get_data(const struct nc_session *session)
243{
244 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200245 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100246 return NULL;
247 }
248
249 return session->data;
250}
251
Michal Vasko086311b2016-01-08 09:53:11 +0100252NC_MSG_TYPE
253nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200254{
Michal Vasko086311b2016-01-08 09:53:11 +0100255 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200256
Michal Vasko086311b2016-01-08 09:53:11 +0100257 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100258 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
259 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100260 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100261 }
262
Michal Vasko086311b2016-01-08 09:53:11 +0100263 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
264
265 if (r) {
266 return NC_MSG_ERROR;
267 }
268
269 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100270}
271
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100273nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200274{
Michal Vasko9e99f012016-03-03 13:25:20 +0100275 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100276 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100277 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100278 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100279 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100280 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100281 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200282 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100283 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200284 void *p;
285
Michal Vasko428087d2016-01-14 16:04:28 +0100286 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200287 return;
288 }
289
Michal Vasko86d357c2016-03-11 13:46:38 +0100290 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200291 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
292 tid = *session->opts.client.ntf_tid;
293 free((pthread_t *)session->opts.client.ntf_tid);
294 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100295 /* the thread now knows it should quit */
296
297 pthread_join(tid, NULL);
298 }
299
Michal Vaskoadd4c792015-10-26 15:36:58 +0100300 if (session->ti_lock) {
Michal vasko50cc94f2016-10-04 13:46:20 +0200301 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100302 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100303 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100304 } else if (!r) {
305 /* we failed to lock it, too bad */
306 locked = 0;
307 } else {
308 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100309 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200310 } else {
311 ERRINT;
312 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200313 }
314
Michal Vasko9e99f012016-03-03 13:25:20 +0100315 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200316 /* cleanup message queues */
317 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200318 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100319 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200320
Michal Vaskoad611702015-12-03 13:41:51 +0100321 p = contiter;
322 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200323 free(p);
324 }
325
326 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200327 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100328 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200329
Michal Vaskoad611702015-12-03 13:41:51 +0100330 p = contiter;
331 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200332 free(p);
333 }
334
335 /* send closing info to the other side */
336 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
337 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100338 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200339 } else {
340 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100341 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200342 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100343 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100344 case NC_MSG_REPLY:
345 LY_TREE_FOR(rpl->child, child) {
346 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
347 break;
348 }
349 }
350 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100351 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100352 }
Michal Vaskoad611702015-12-03 13:41:51 +0100353 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100354 break;
355 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100356 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100357 break;
358 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100359 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100360 break;
361 default:
362 /* cannot happen */
363 break;
364 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200365 }
366
367 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200368 if (session->opts.client.cpblts) {
369 for (i = 0; session->opts.client.cpblts[i]; i++) {
370 lydict_remove(session->ctx, session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200371 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200372 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200373 }
374 }
375
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100376 if (session->data && data_free) {
377 data_free(session->data);
378 }
379
Michal Vasko86d357c2016-03-11 13:46:38 +0100380 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200381 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100382 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200383
384 /* transport implementation cleanup */
385 switch (session->ti_type) {
386 case NC_TI_FD:
387 /* nothing needed - file descriptors were provided by caller,
388 * so it is up to the caller to close them correctly
389 * TODO use callbacks
390 */
Michal Vasko3512e402016-01-28 16:22:34 +0100391 /* just to avoid compiler warning */
392 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100393 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200394 break;
395
Radek Krejci53691be2016-02-22 13:58:37 +0100396#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200397 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100398 if (connected) {
399 ssh_channel_free(session->ti.libssh.channel);
400 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200401 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
402 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
403 * it.
404 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100405 multisession = 0;
406 if (session->ti.libssh.next) {
407 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
408 if (siter->status != NC_STATUS_STARTING) {
409 multisession = 1;
410 break;
411 }
412 }
413 }
414
415 if (!multisession) {
416 /* it's not multisession yet, but we still need to free the starting sessions */
417 if (session->ti.libssh.next) {
418 do {
419 siter = session->ti.libssh.next;
420 session->ti.libssh.next = siter->ti.libssh.next;
421
422 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100423 lydict_remove(session->ctx, session->username);
424 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100425 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100426 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100427 }
428
429 free(siter);
430 } while (session->ti.libssh.next != session);
431 }
Michal Vasko428087d2016-01-14 16:04:28 +0100432 if (connected) {
433 ssh_disconnect(session->ti.libssh.session);
434 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200435 ssh_free(session->ti.libssh.session);
436 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200437 /* remove the session from the list */
438 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100439 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200440 /* there will be only one session */
441 siter->ti.libssh.next = NULL;
442 } else {
443 /* there are still multiple sessions, keep the ring list */
444 siter->ti.libssh.next = session->ti.libssh.next;
445 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100446 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
447 if (session->flags & NC_SESSION_SSH_MSG_CB) {
448 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
449 if (siter->ti.libssh.next == session) {
450 ERRINT;
451 break;
452 }
453 }
454 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
455 siter->flags |= NC_SESSION_SSH_MSG_CB;
456 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200457 }
458 break;
459#endif
460
Radek Krejci53691be2016-02-22 13:58:37 +0100461#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200462 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100463 if (connected) {
464 SSL_shutdown(session->ti.tls);
465 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200466 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100467
Michal Vasko2e6defd2016-10-07 15:48:15 +0200468 if (session->side == NC_SERVER) {
469 X509_free(session->opts.server.client_cert);
470 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200471 break;
472#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100473 case NC_TI_NONE:
474 ERRINT;
475 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200476 }
Michal Vasko428087d2016-01-14 16:04:28 +0100477
Radek Krejciac6d3472015-10-22 15:47:18 +0200478 lydict_remove(session->ctx, session->username);
479 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200480
481 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100482 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100483 if (locked) {
484 pthread_mutex_unlock(session->ti_lock);
485 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100486 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100487 pthread_mutex_destroy(session->ti_lock);
488 free(session->ti_lock);
489 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200490 }
491
492 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100493 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200494 }
495
496 free(session);
497}
498
Michal Vasko086311b2016-01-08 09:53:11 +0100499static void
500add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
501{
502 if (*count == *size) {
503 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100504 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
505 if (!(*cpblts)) {
506 ERRMEM;
507 return;
508 }
Michal Vasko086311b2016-01-08 09:53:11 +0100509 }
510
511 if (capab) {
512 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
513 } else {
514 (*cpblts)[*count] = NULL;
515 }
516 ++(*count);
517}
518
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200519API const char **
520nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100521{
522 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200523 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 +0100524 const char **cpblts;
525 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200526 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200527#define NC_CPBLT_BUF_LEN 512
528 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100529
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200530 if (!ctx) {
531 ERRARG("ctx");
532 return NULL;
533 }
534
Michal Vasko086311b2016-01-08 09:53:11 +0100535 yanglib = ly_ctx_info(ctx);
536 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200537 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100538 return NULL;
539 }
540
541 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100542 if (!cpblts) {
543 ERRMEM;
544 return NULL;
545 }
Michal Vasko086311b2016-01-08 09:53:11 +0100546 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
547 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
548 count = 2;
549
550 /* capabilities */
551
552 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
553 if (mod) {
554 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100555 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100556 }
557 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100558 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100559 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100560 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100561 }
562 }
563 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100564 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100565 }
566 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100567 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100568 }
569 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100570 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100571 }
572 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100573 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100574 }
575 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100576 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100577 }
578 }
579
580 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
581 if (mod) {
582 if (!server_opts.wd_basic_mode) {
583 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
584 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100585 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100586 switch (server_opts.wd_basic_mode) {
587 case NC_WD_ALL:
588 strcat(str, "?basic-mode=report-all");
589 break;
590 case NC_WD_TRIM:
591 strcat(str, "?basic-mode=trim");
592 break;
593 case NC_WD_EXPLICIT:
594 strcat(str, "?basic-mode=explicit");
595 break;
596 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100597 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100598 break;
599 }
600
601 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200602 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100603 if (server_opts.wd_also_supported & NC_WD_ALL) {
604 strcat(str, "report-all,");
605 }
606 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
607 strcat(str, "report-all-tagged,");
608 }
609 if (server_opts.wd_also_supported & NC_WD_TRIM) {
610 strcat(str, "trim,");
611 }
612 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
613 strcat(str, "explicit,");
614 }
615 str[strlen(str) - 1] = '\0';
616
617 add_cpblt(ctx, str, &cpblts, &size, &count);
618 }
619 }
620 }
621
Michal Vasko1a38c862016-01-15 15:50:07 +0100622 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100623 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100624 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100625 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100626 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100627 }
628 }
629
630 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100631 LY_TREE_FOR(yanglib->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200632 if (!module_set_id) {
633 if (strcmp(child->prev->schema->name, "module-set-id")) {
634 ERRINT;
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200635 free(cpblts);
636 free(deviations);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200637 return NULL;
638 }
639 module_set_id = (struct lyd_node_leaf_list *)child->prev;
640 }
Michal Vasko086311b2016-01-08 09:53:11 +0100641 if (!strcmp(child->schema->name, "module")) {
642 LY_TREE_FOR(child->child, child2) {
643 if (!strcmp(child2->schema->name, "namespace")) {
644 ns = (struct lyd_node_leaf_list *)child2;
645 } else if (!strcmp(child2->schema->name, "name")) {
646 name = (struct lyd_node_leaf_list *)child2;
647 } else if (!strcmp(child2->schema->name, "revision")) {
648 rev = (struct lyd_node_leaf_list *)child2;
649 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100650 features = nc_realloc(features, ++feat_count * sizeof *features);
651 if (!features) {
652 ERRMEM;
653 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200654 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100655 return NULL;
656 }
Michal Vasko086311b2016-01-08 09:53:11 +0100657 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200658 } else if (!strcmp(child2->schema->name, "deviation")) {
659 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
660 if (!deviations) {
661 ERRMEM;
662 free(cpblts);
663 free(features);
664 return NULL;
665 }
Michal Vasko086311b2016-01-08 09:53:11 +0100666 }
667 }
668
669 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100670 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100671 continue;
672 }
673
Radek Krejcidf7ba522016-03-01 16:05:25 +0100674 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200675 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100676 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200677 strcat(str, "&features=");
678 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100679 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200680 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100681 ERRINT;
682 break;
683 }
Michal Vasko086311b2016-01-08 09:53:11 +0100684 if (i) {
685 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100686 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100687 }
688 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100689 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100690 }
691 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200692 if (dev_count) {
693 strcat(str, "&deviations=");
694 str_len += 12;
695 for (i = 0; i < dev_count; ++i) {
696 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
697 ERRINT;
698 break;
699 }
700 if (i) {
701 strcat(str, ",");
702 ++str_len;
703 }
704 strcat(str, deviations[i]->value_str);
705 str_len += strlen(deviations[i]->value_str);
706 }
707 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200708 if (!strcmp(name->value_str, "ietf-yang-library")) {
709 str_len += sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
710 }
Michal Vasko086311b2016-01-08 09:53:11 +0100711
712 add_cpblt(ctx, str, &cpblts, &size, &count);
713
714 ns = NULL;
715 name = NULL;
716 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200717 if (features || feat_count) {
718 free(features);
719 features = NULL;
720 feat_count = 0;
721 }
722 if (deviations || dev_count) {
723 free(deviations);
724 deviations = NULL;
725 dev_count = 0;
726 }
Michal Vasko086311b2016-01-08 09:53:11 +0100727 }
728 }
729
730 lyd_free(yanglib);
731
732 /* ending NULL capability */
733 add_cpblt(ctx, NULL, &cpblts, &size, &count);
734
735 return cpblts;
736}
737
Radek Krejci695d4fa2015-10-22 13:23:54 +0200738static int
739parse_cpblts(struct lyxml_elem *xml, const char ***list)
740{
741 struct lyxml_elem *cpblt;
742 int ver = -1;
743 int i = 0;
744
745 if (list) {
746 /* get the storage for server's capabilities */
747 LY_TREE_FOR(xml->child, cpblt) {
748 i++;
749 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100750 /* last item remains NULL */
751 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200752 if (!*list) {
753 ERRMEM;
754 return -1;
755 }
756 i = 0;
757 }
758
759 LY_TREE_FOR(xml->child, cpblt) {
760 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
761 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
762 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
763 return -1;
764 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
765 continue;
766 }
767
768 /* detect NETCONF version */
769 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
770 ver = 0;
771 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
772 ver = 1;
773 }
774
775 /* store capabilities */
776 if (list) {
777 (*list)[i] = cpblt->content;
778 cpblt->content = NULL;
779 i++;
780 }
781 }
782
783 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100784 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200785 }
786
787 return ver;
788}
789
790static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100791nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100792{
793 int r, i;
794 const char **cpblts;
795
Michal Vasko11d142a2016-01-19 15:58:24 +0100796 /* client side hello - send only NETCONF base capabilities */
797 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100798 if (!cpblts) {
799 ERRMEM;
800 return NC_MSG_ERROR;
801 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100802 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
803 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
804 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100805
Michal Vasko11d142a2016-01-19 15:58:24 +0100806 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100807
Michal Vasko086311b2016-01-08 09:53:11 +0100808 for (i = 0; cpblts[i]; ++i) {
809 lydict_remove(session->ctx, cpblts[i]);
810 }
811 free(cpblts);
812
813 if (r) {
814 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100815 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100816
817 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100818}
819
820static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100821nc_send_server_hello(struct nc_session *session)
822{
823 int r, i;
824 const char **cpblts;
825
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200826 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100827
828 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
829
Michal Vasko11d142a2016-01-19 15:58:24 +0100830 for (i = 0; cpblts[i]; ++i) {
831 lydict_remove(session->ctx, cpblts[i]);
832 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100833 free(cpblts);
834
835 if (r) {
836 return NC_MSG_ERROR;
837 }
838
839 return NC_MSG_HELLO;
840}
841
842static NC_MSG_TYPE
843nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200844{
845 struct lyxml_elem *xml = NULL, *node;
846 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
847 int ver = -1;
848 char *str;
849 long long int id;
850 int flag = 0;
851
Michal Vasko05ba9df2016-01-13 14:40:27 +0100852 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200853
854 switch(msgtype) {
855 case NC_MSG_HELLO:
856 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100857 LY_TREE_FOR(xml->child, node) {
858 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
859 continue;
860 } else if (!strcmp(node->name, "session-id")) {
861 if (!node->content || !strlen(node->content)) {
862 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200863 goto error;
864 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100865 str = NULL;
866 id = strtoll(node->content, &str, 10);
867 if (*str || id < 1 || id > UINT32_MAX) {
868 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200869 goto error;
870 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100871 session->id = (uint32_t)id;
872 continue;
873 } else if (strcmp(node->name, "capabilities")) {
874 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200875 goto error;
876 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100877
878 if (flag) {
879 /* multiple capabilities elements */
880 ERR("Invalid <hello> message (multiple <capabilities> elements).");
881 goto error;
882 }
883 flag = 1;
884
Michal Vasko2e6defd2016-10-07 15:48:15 +0200885 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100886 goto error;
887 }
888 session->version = ver;
889 }
890
891 if (!session->id) {
892 ERR("Missing <session-id> in server's <hello>.");
893 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200894 }
895 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200896 case NC_MSG_WOULDBLOCK:
897 ERR("Server's <hello> timeout elapsed.");
898 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200899 case NC_MSG_ERROR:
900 /* nothing special, just pass it out */
901 break;
902 default:
903 ERR("Unexpected message received instead of <hello>.");
904 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200905 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200906 }
907
908 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100909 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200910
911 return msgtype;
912
913error:
914 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100915 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200916
917 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200918}
919
Michal Vasko11d142a2016-01-19 15:58:24 +0100920static NC_MSG_TYPE
921nc_recv_server_hello(struct nc_session *session)
922{
923 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200924 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100925 int ver = -1;
926 int flag = 0;
927
Michal Vaskoadb850e2016-01-20 14:06:32 +0100928 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100929
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100930 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100931 case NC_MSG_HELLO:
932 /* get know NETCONF version */
933 LY_TREE_FOR(xml->child, node) {
934 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
935 continue;
936 } else if (strcmp(node->name, "capabilities")) {
937 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200938 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100939 goto cleanup;
940 }
941
942 if (flag) {
943 /* multiple capabilities elements */
944 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200945 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100946 goto cleanup;
947 }
948 flag = 1;
949
950 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200951 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100952 goto cleanup;
953 }
954 session->version = ver;
955 }
956 break;
957 case NC_MSG_ERROR:
958 /* nothing special, just pass it out */
959 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100960 case NC_MSG_WOULDBLOCK:
961 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100962 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100963 default:
964 ERR("Unexpected message received instead of <hello>.");
965 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200966 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100967 }
968
969cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100970 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100971
972 return msgtype;
973}
974
Michal Vasko71090fc2016-05-24 16:37:28 +0200975NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100976nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100977{
Michal Vasko086311b2016-01-08 09:53:11 +0100978 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100979
Michal Vasko11d142a2016-01-19 15:58:24 +0100980 if (session->side == NC_CLIENT) {
981 type = nc_send_client_hello(session);
982 } else {
983 type = nc_send_server_hello(session);
984 }
985
Michal Vasko086311b2016-01-08 09:53:11 +0100986 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200987 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100988 }
989
Michal Vasko11d142a2016-01-19 15:58:24 +0100990 if (session->side == NC_CLIENT) {
991 type = nc_recv_client_hello(session);
992 } else {
993 type = nc_recv_server_hello(session);
994 }
995
Michal Vasko71090fc2016-05-24 16:37:28 +0200996 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100997}
Michal Vasko086311b2016-01-08 09:53:11 +0100998
Radek Krejci53691be2016-02-22 13:58:37 +0100999#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001000
Michal Vasko8f0c0282016-02-29 10:17:14 +01001001static void
Michal Vasko086311b2016-01-08 09:53:11 +01001002nc_ssh_init(void)
1003{
1004 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1005 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001006}
1007
Michal Vasko8f0c0282016-02-29 10:17:14 +01001008static void
Michal Vasko086311b2016-01-08 09:53:11 +01001009nc_ssh_destroy(void)
1010{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001011 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001012 ENGINE_cleanup();
1013 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001014 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001015 ssh_finalize();
1016}
1017
Radek Krejci53691be2016-02-22 13:58:37 +01001018#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001019
Radek Krejci53691be2016-02-22 13:58:37 +01001020#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001021
Michal Vaskof0c92c02016-01-29 09:41:45 +01001022struct CRYPTO_dynlock_value {
1023 pthread_mutex_t lock;
1024};
1025
Michal Vaskof0c92c02016-01-29 09:41:45 +01001026static struct CRYPTO_dynlock_value *
1027tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1028{
1029 struct CRYPTO_dynlock_value *value;
1030
1031 value = malloc(sizeof *value);
1032 if (!value) {
1033 ERRMEM;
1034 return NULL;
1035 }
1036 pthread_mutex_init(&value->lock, NULL);
1037
1038 return value;
1039}
1040
1041static void
1042tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1043{
1044 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1045 * I found ignored this fact, what do I know... */
1046 if (mode & CRYPTO_LOCK) {
1047 pthread_mutex_lock(&l->lock);
1048 } else {
1049 pthread_mutex_unlock(&l->lock);
1050 }
1051}
1052
1053static void
1054tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1055{
1056 pthread_mutex_destroy(&l->lock);
1057 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001058}
1059
Michal Vasko8f0c0282016-02-29 10:17:14 +01001060#endif /* NC_ENABLED_TLS */
1061
1062#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1063
1064static pthread_mutex_t *tls_locks;
1065
1066static void
1067tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1068{
1069 if (mode & CRYPTO_LOCK) {
1070 pthread_mutex_lock(tls_locks + n);
1071 } else {
1072 pthread_mutex_unlock(tls_locks + n);
1073 }
1074}
1075
1076static void
1077tls_thread_id_func(CRYPTO_THREADID *tid)
1078{
1079 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1080}
1081
1082static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001083nc_tls_init(void)
1084{
1085 int i;
1086
1087 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001088 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001089 SSL_library_init();
1090
1091 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001092 if (!tls_locks) {
1093 ERRMEM;
1094 return;
1095 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001096 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1097 pthread_mutex_init(tls_locks + i, NULL);
1098 }
1099
Michal Vaskof0c92c02016-01-29 09:41:45 +01001100 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001101 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001102
1103 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1104 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1105 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001106}
1107
Michal Vasko8f0c0282016-02-29 10:17:14 +01001108static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001109nc_tls_destroy(void)
1110{
1111 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001112
Michal Vasko8f0c0282016-02-29 10:17:14 +01001113 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001114 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001115 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001116 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001117 ERR_free_strings();
1118 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001119
Michal Vaskob6e37262016-02-25 14:49:00 +01001120 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001121 CRYPTO_set_locking_callback(NULL);
1122 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1123 pthread_mutex_destroy(tls_locks + i);
1124 }
1125 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001126
1127 CRYPTO_set_dynlock_create_callback(NULL);
1128 CRYPTO_set_dynlock_lock_callback(NULL);
1129 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001130}
1131
Michal Vasko8f0c0282016-02-29 10:17:14 +01001132#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001133
Radek Krejci53691be2016-02-22 13:58:37 +01001134#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001135
Michal Vasko8f0c0282016-02-29 10:17:14 +01001136static void
Michal Vasko5e228792016-02-03 15:30:24 +01001137nc_ssh_tls_init(void)
1138{
1139 SSL_load_error_strings();
1140 ERR_load_BIO_strings();
1141 SSL_library_init();
1142
1143 nc_ssh_init();
1144
1145 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1146 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1147 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1148}
1149
Michal Vasko8f0c0282016-02-29 10:17:14 +01001150static void
Michal Vasko5e228792016-02-03 15:30:24 +01001151nc_ssh_tls_destroy(void)
1152{
1153 ERR_free_strings();
1154 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1155
1156 nc_ssh_destroy();
1157
1158 CRYPTO_set_dynlock_create_callback(NULL);
1159 CRYPTO_set_dynlock_lock_callback(NULL);
1160 CRYPTO_set_dynlock_destroy_callback(NULL);
1161}
1162
Radek Krejci53691be2016-02-22 13:58:37 +01001163#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001164
1165#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1166
1167API void
1168nc_thread_destroy(void)
1169{
1170 CRYPTO_THREADID crypto_tid;
1171
1172 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1173 //CRYPTO_cleanup_all_ex_data();
1174
1175 CRYPTO_THREADID_current(&crypto_tid);
1176 ERR_remove_thread_state(&crypto_tid);
1177}
1178
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001179#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1180
1181void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001182nc_init(void)
1183{
1184#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1185 nc_ssh_tls_init();
1186#elif defined(NC_ENABLED_SSH)
1187 nc_ssh_init();
1188#elif defined(NC_ENABLED_TLS)
1189 nc_tls_init();
1190#endif
1191}
1192
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001193void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001194nc_destroy(void)
1195{
1196#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1197 nc_ssh_tls_destroy();
1198#elif defined(NC_ENABLED_SSH)
1199 nc_ssh_destroy();
1200#elif defined(NC_ENABLED_TLS)
1201 nc_tls_destroy();
1202#endif
1203}