blob: 453c6e8c2dd2a084900ee4363d7c7548149bd5e7 [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{
52#ifdef CLOCK_MONOTONIC
53 return clock_gettime(CLOCK_MONOTONIC, ts);
54#elif defined CLOCK_REALTIME
55 return clock_gettime(CLOCK_REALTIME, ts);
56#else
57 int rc;
58 struct timeval tv;
59
60 rc = gettimeofday(&tv, NULL);
61 if (!rc) {
62 ts->tv_sec = (time_t)tv.tv_sec;
63 ts->tv_nsec = 1000L * (long)tv.tv_usec;
64 }
65 return rc;
66#endif
67}
68
Radek Krejci28472922016-07-15 11:51:16 +020069#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
70int
71pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
72{
73 int rc;
74 struct timespec cur, dur;
75
76 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
77 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
78 nc_gettimespec(&cur);
79
80 if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
81 break;
82 }
83
84 dur.tv_sec = abstime->tv_sec - cur.tv_sec;
85 dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
86 if (dur.tv_nsec < 0) {
87 dur.tv_sec--;
88 dur.tv_nsec += 1000000000;
89 }
90
91 if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
92 dur.tv_sec = 0;
93 dur.tv_nsec = 5000000;
94 }
95
96 nanosleep(&dur, NULL);
97 }
98
99 return rc;
100}
101#endif
102
Michal Vasko96164bf2016-01-21 15:41:58 +0100103/*
104 * @return 1 - success
105 * 0 - timeout
106 * -1 - error
107 */
108int
Michal Vasko62be1ce2016-03-03 13:24:52 +0100109nc_timedlock(pthread_mutex_t *lock, int timeout)
Michal Vasko96164bf2016-01-21 15:41:58 +0100110{
111 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100112 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100113
114 if (timeout > 0) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200115 nc_gettimespec(&ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100116
Michal Vasko96164bf2016-01-21 15:41:58 +0100117 ts_timeout.tv_sec += timeout / 1000;
118 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
119
120 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100121 } else if (!timeout) {
122 ret = pthread_mutex_trylock(lock);
123 } else { /* timeout == -1 */
124 ret = pthread_mutex_lock(lock);
125 }
126
127 if (ret == ETIMEDOUT) {
128 /* timeout */
129 return 0;
130 } else if (ret) {
131 /* error */
Radek Krejcida8f58d2016-03-03 13:10:21 +0100132 ERR("Mutex lock failed (%s).", strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100133 return -1;
134 }
135
136 /* ok */
137 return 1;
138}
139
Michal Vasko8dadf782016-01-15 10:29:36 +0100140API NC_STATUS
141nc_session_get_status(const struct nc_session *session)
142{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100143 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200144 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100145 return 0;
146 }
147
Michal Vasko8dadf782016-01-15 10:29:36 +0100148 return session->status;
149}
150
151API uint32_t
152nc_session_get_id(const struct nc_session *session)
153{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100154 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200155 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100156 return 0;
157 }
158
Michal Vasko8dadf782016-01-15 10:29:36 +0100159 return session->id;
160}
161
Michal Vasko174fe8e2016-02-17 15:38:09 +0100162API int
163nc_session_get_version(const struct nc_session *session)
164{
165 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200166 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100167 return -1;
168 }
169
170 return (session->version == NC_VERSION_10 ? 0 : 1);
171}
172
Michal Vasko8dadf782016-01-15 10:29:36 +0100173API NC_TRANSPORT_IMPL
174nc_session_get_ti(const struct nc_session *session)
175{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100176 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200177 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100178 return 0;
179 }
180
Michal Vasko8dadf782016-01-15 10:29:36 +0100181 return session->ti_type;
182}
183
184API const char *
185nc_session_get_username(const struct nc_session *session)
186{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100187 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200188 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100189 return NULL;
190 }
191
Michal Vasko8dadf782016-01-15 10:29:36 +0100192 return session->username;
193}
194
195API const char *
196nc_session_get_host(const struct nc_session *session)
197{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100198 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200199 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100200 return NULL;
201 }
202
Michal Vasko8dadf782016-01-15 10:29:36 +0100203 return session->host;
204}
205
206API uint16_t
207nc_session_get_port(const struct nc_session *session)
208{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100209 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200210 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100211 return 0;
212 }
213
Michal Vasko8dadf782016-01-15 10:29:36 +0100214 return session->port;
215}
216
Michal Vasko9a25e932016-02-01 10:36:42 +0100217API struct ly_ctx *
218nc_session_get_ctx(const struct nc_session *session)
219{
220 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200221 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100222 return NULL;
223 }
224
225 return session->ctx;
226}
227
Michal Vasko2cc4c682016-03-01 09:16:48 +0100228API void
229nc_session_set_data(struct nc_session *session, void *data)
230{
231 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200232 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100233 return;
234 }
235
236 session->data = data;
237}
238
239API void *
240nc_session_get_data(const struct nc_session *session)
241{
242 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200243 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100244 return NULL;
245 }
246
247 return session->data;
248}
249
Michal Vasko086311b2016-01-08 09:53:11 +0100250NC_MSG_TYPE
251nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200252{
Michal Vasko086311b2016-01-08 09:53:11 +0100253 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200254
Michal Vasko086311b2016-01-08 09:53:11 +0100255 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100256 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
257 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100258 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100259 }
260
Michal Vasko086311b2016-01-08 09:53:11 +0100261 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
262
263 if (r) {
264 return NC_MSG_ERROR;
265 }
266
267 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100268}
269
Radek Krejci695d4fa2015-10-22 13:23:54 +0200270API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100271nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272{
Michal Vasko9e99f012016-03-03 13:25:20 +0100273 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100274 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100275 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100276 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100277 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100278 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100279 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200280 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100281 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200282 void *p;
283
Michal Vasko428087d2016-01-14 16:04:28 +0100284 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200285 return;
286 }
287
Michal Vasko86d357c2016-03-11 13:46:38 +0100288 /* stop notifications loop if any */
289 if (session->ntf_tid) {
290 tid = *session->ntf_tid;
291 free((pthread_t *)session->ntf_tid);
292 session->ntf_tid = NULL;
293 /* the thread now knows it should quit */
294
295 pthread_join(tid, NULL);
296 }
297
Michal Vaskoadd4c792015-10-26 15:36:58 +0100298 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100299 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100300 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100301 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100302 } else if (!r) {
303 /* we failed to lock it, too bad */
304 locked = 0;
305 } else {
306 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100307 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200308 } else {
309 ERRINT;
310 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200311 }
312
Michal Vasko9e99f012016-03-03 13:25:20 +0100313 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200314 /* cleanup message queues */
315 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100316 for (contiter = session->notifs; contiter; ) {
317 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200318
Michal Vaskoad611702015-12-03 13:41:51 +0100319 p = contiter;
320 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200321 free(p);
322 }
323
324 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100325 for (contiter = session->replies; contiter; ) {
326 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200327
Michal Vaskoad611702015-12-03 13:41:51 +0100328 p = contiter;
329 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200330 free(p);
331 }
332
333 /* send closing info to the other side */
334 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
335 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100336 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200337 } else {
338 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100339 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200340 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100341 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100342 case NC_MSG_REPLY:
343 LY_TREE_FOR(rpl->child, child) {
344 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
345 break;
346 }
347 }
348 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100349 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100350 }
Michal Vaskoad611702015-12-03 13:41:51 +0100351 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100352 break;
353 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100354 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100355 break;
356 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100357 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100358 break;
359 default:
360 /* cannot happen */
361 break;
362 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200363 }
364
365 /* list of server's capabilities */
366 if (session->cpblts) {
367 for (i = 0; session->cpblts[i]; i++) {
368 lydict_remove(session->ctx, session->cpblts[i]);
369 }
370 free(session->cpblts);
371 }
372 }
373
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100374 if (session->data && data_free) {
375 data_free(session->data);
376 }
377
Michal Vasko86d357c2016-03-11 13:46:38 +0100378 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200379 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100380 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200381
382 /* transport implementation cleanup */
383 switch (session->ti_type) {
384 case NC_TI_FD:
385 /* nothing needed - file descriptors were provided by caller,
386 * so it is up to the caller to close them correctly
387 * TODO use callbacks
388 */
Michal Vasko3512e402016-01-28 16:22:34 +0100389 /* just to avoid compiler warning */
390 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100391 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200392 break;
393
Radek Krejci53691be2016-02-22 13:58:37 +0100394#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200395 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100396 if (connected) {
397 ssh_channel_free(session->ti.libssh.channel);
398 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200399 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
400 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
401 * it.
402 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100403 multisession = 0;
404 if (session->ti.libssh.next) {
405 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
406 if (siter->status != NC_STATUS_STARTING) {
407 multisession = 1;
408 break;
409 }
410 }
411 }
412
413 if (!multisession) {
414 /* it's not multisession yet, but we still need to free the starting sessions */
415 if (session->ti.libssh.next) {
416 do {
417 siter = session->ti.libssh.next;
418 session->ti.libssh.next = siter->ti.libssh.next;
419
420 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100421 lydict_remove(session->ctx, session->username);
422 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100423 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100424 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100425 }
426
427 free(siter);
428 } while (session->ti.libssh.next != session);
429 }
Michal Vasko428087d2016-01-14 16:04:28 +0100430 if (connected) {
431 ssh_disconnect(session->ti.libssh.session);
432 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200433 ssh_free(session->ti.libssh.session);
434 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200435 /* remove the session from the list */
436 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100437 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200438 /* there will be only one session */
439 siter->ti.libssh.next = NULL;
440 } else {
441 /* there are still multiple sessions, keep the ring list */
442 siter->ti.libssh.next = session->ti.libssh.next;
443 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100444 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
445 if (session->flags & NC_SESSION_SSH_MSG_CB) {
446 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
447 if (siter->ti.libssh.next == session) {
448 ERRINT;
449 break;
450 }
451 }
452 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
453 siter->flags |= NC_SESSION_SSH_MSG_CB;
454 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200455 }
456 break;
457#endif
458
Radek Krejci53691be2016-02-22 13:58:37 +0100459#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200460 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100461 if (connected) {
462 SSL_shutdown(session->ti.tls);
463 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200464 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100465
466 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200467 break;
468#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100469 case NC_TI_NONE:
470 ERRINT;
471 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200472 }
Michal Vasko428087d2016-01-14 16:04:28 +0100473
Radek Krejciac6d3472015-10-22 15:47:18 +0200474 lydict_remove(session->ctx, session->username);
475 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200476
477 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100478 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100479 if (locked) {
480 pthread_mutex_unlock(session->ti_lock);
481 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100482 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100483 pthread_mutex_destroy(session->ti_lock);
484 free(session->ti_lock);
485 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200486 }
487
488 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100489 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200490 }
491
492 free(session);
493}
494
Michal Vasko086311b2016-01-08 09:53:11 +0100495static void
496add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
497{
498 if (*count == *size) {
499 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100500 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
501 if (!(*cpblts)) {
502 ERRMEM;
503 return;
504 }
Michal Vasko086311b2016-01-08 09:53:11 +0100505 }
506
507 if (capab) {
508 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
509 } else {
510 (*cpblts)[*count] = NULL;
511 }
512 ++(*count);
513}
514
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200515API const char **
516nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100517{
518 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200519 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 +0100520 const char **cpblts;
521 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200522 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200523#define NC_CPBLT_BUF_LEN 512
524 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100525
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200526 if (!ctx) {
527 ERRARG("ctx");
528 return NULL;
529 }
530
Michal Vasko086311b2016-01-08 09:53:11 +0100531 yanglib = ly_ctx_info(ctx);
532 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200533 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100534 return NULL;
535 }
536
537 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100538 if (!cpblts) {
539 ERRMEM;
540 return NULL;
541 }
Michal Vasko086311b2016-01-08 09:53:11 +0100542 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
543 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
544 count = 2;
545
546 /* capabilities */
547
548 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
549 if (mod) {
550 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100551 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100552 }
553 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100554 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100555 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100556 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100557 }
558 }
559 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100560 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100561 }
562 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100563 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100564 }
565 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100566 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100567 }
568 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100569 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100570 }
571 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100572 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100573 }
574 }
575
576 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
577 if (mod) {
578 if (!server_opts.wd_basic_mode) {
579 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
580 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100581 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100582 switch (server_opts.wd_basic_mode) {
583 case NC_WD_ALL:
584 strcat(str, "?basic-mode=report-all");
585 break;
586 case NC_WD_TRIM:
587 strcat(str, "?basic-mode=trim");
588 break;
589 case NC_WD_EXPLICIT:
590 strcat(str, "?basic-mode=explicit");
591 break;
592 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100593 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100594 break;
595 }
596
597 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200598 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100599 if (server_opts.wd_also_supported & NC_WD_ALL) {
600 strcat(str, "report-all,");
601 }
602 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
603 strcat(str, "report-all-tagged,");
604 }
605 if (server_opts.wd_also_supported & NC_WD_TRIM) {
606 strcat(str, "trim,");
607 }
608 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
609 strcat(str, "explicit,");
610 }
611 str[strlen(str) - 1] = '\0';
612
613 add_cpblt(ctx, str, &cpblts, &size, &count);
614 }
615 }
616 }
617
Michal Vasko1a38c862016-01-15 15:50:07 +0100618 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100619 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100620 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100621 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100622 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100623 }
624 }
625
626 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100627 LY_TREE_FOR(yanglib->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200628 if (!module_set_id) {
629 if (strcmp(child->prev->schema->name, "module-set-id")) {
630 ERRINT;
631 return NULL;
632 }
633 module_set_id = (struct lyd_node_leaf_list *)child->prev;
634 }
Michal Vasko086311b2016-01-08 09:53:11 +0100635 if (!strcmp(child->schema->name, "module")) {
636 LY_TREE_FOR(child->child, child2) {
637 if (!strcmp(child2->schema->name, "namespace")) {
638 ns = (struct lyd_node_leaf_list *)child2;
639 } else if (!strcmp(child2->schema->name, "name")) {
640 name = (struct lyd_node_leaf_list *)child2;
641 } else if (!strcmp(child2->schema->name, "revision")) {
642 rev = (struct lyd_node_leaf_list *)child2;
643 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100644 features = nc_realloc(features, ++feat_count * sizeof *features);
645 if (!features) {
646 ERRMEM;
647 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200648 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100649 return NULL;
650 }
Michal Vasko086311b2016-01-08 09:53:11 +0100651 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200652 } else if (!strcmp(child2->schema->name, "deviation")) {
653 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
654 if (!deviations) {
655 ERRMEM;
656 free(cpblts);
657 free(features);
658 return NULL;
659 }
Michal Vasko086311b2016-01-08 09:53:11 +0100660 }
661 }
662
663 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100664 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100665 continue;
666 }
667
Radek Krejcidf7ba522016-03-01 16:05:25 +0100668 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200669 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100670 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200671 strcat(str, "&features=");
672 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100673 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200674 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100675 ERRINT;
676 break;
677 }
Michal Vasko086311b2016-01-08 09:53:11 +0100678 if (i) {
679 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100680 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100681 }
682 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100683 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100684 }
685 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200686 if (dev_count) {
687 strcat(str, "&deviations=");
688 str_len += 12;
689 for (i = 0; i < dev_count; ++i) {
690 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
691 ERRINT;
692 break;
693 }
694 if (i) {
695 strcat(str, ",");
696 ++str_len;
697 }
698 strcat(str, deviations[i]->value_str);
699 str_len += strlen(deviations[i]->value_str);
700 }
701 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200702 if (!strcmp(name->value_str, "ietf-yang-library")) {
703 str_len += sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
704 }
Michal Vasko086311b2016-01-08 09:53:11 +0100705
706 add_cpblt(ctx, str, &cpblts, &size, &count);
707
708 ns = NULL;
709 name = NULL;
710 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200711 if (features || feat_count) {
712 free(features);
713 features = NULL;
714 feat_count = 0;
715 }
716 if (deviations || dev_count) {
717 free(deviations);
718 deviations = NULL;
719 dev_count = 0;
720 }
Michal Vasko086311b2016-01-08 09:53:11 +0100721 }
722 }
723
724 lyd_free(yanglib);
725
726 /* ending NULL capability */
727 add_cpblt(ctx, NULL, &cpblts, &size, &count);
728
729 return cpblts;
730}
731
Radek Krejci695d4fa2015-10-22 13:23:54 +0200732static int
733parse_cpblts(struct lyxml_elem *xml, const char ***list)
734{
735 struct lyxml_elem *cpblt;
736 int ver = -1;
737 int i = 0;
738
739 if (list) {
740 /* get the storage for server's capabilities */
741 LY_TREE_FOR(xml->child, cpblt) {
742 i++;
743 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100744 /* last item remains NULL */
745 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200746 if (!*list) {
747 ERRMEM;
748 return -1;
749 }
750 i = 0;
751 }
752
753 LY_TREE_FOR(xml->child, cpblt) {
754 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
755 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
756 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
757 return -1;
758 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
759 continue;
760 }
761
762 /* detect NETCONF version */
763 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
764 ver = 0;
765 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
766 ver = 1;
767 }
768
769 /* store capabilities */
770 if (list) {
771 (*list)[i] = cpblt->content;
772 cpblt->content = NULL;
773 i++;
774 }
775 }
776
777 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100778 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200779 }
780
781 return ver;
782}
783
784static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100785nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100786{
787 int r, i;
788 const char **cpblts;
789
Michal Vasko11d142a2016-01-19 15:58:24 +0100790 /* client side hello - send only NETCONF base capabilities */
791 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100792 if (!cpblts) {
793 ERRMEM;
794 return NC_MSG_ERROR;
795 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100796 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
797 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
798 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100799
Michal Vasko11d142a2016-01-19 15:58:24 +0100800 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100801
Michal Vasko086311b2016-01-08 09:53:11 +0100802 for (i = 0; cpblts[i]; ++i) {
803 lydict_remove(session->ctx, cpblts[i]);
804 }
805 free(cpblts);
806
807 if (r) {
808 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100809 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100810
811 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100812}
813
814static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100815nc_send_server_hello(struct nc_session *session)
816{
817 int r, i;
818 const char **cpblts;
819
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200820 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100821
822 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
823
Michal Vasko11d142a2016-01-19 15:58:24 +0100824 for (i = 0; cpblts[i]; ++i) {
825 lydict_remove(session->ctx, cpblts[i]);
826 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100827 free(cpblts);
828
829 if (r) {
830 return NC_MSG_ERROR;
831 }
832
833 return NC_MSG_HELLO;
834}
835
836static NC_MSG_TYPE
837nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200838{
839 struct lyxml_elem *xml = NULL, *node;
840 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
841 int ver = -1;
842 char *str;
843 long long int id;
844 int flag = 0;
845
Michal Vasko05ba9df2016-01-13 14:40:27 +0100846 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200847
848 switch(msgtype) {
849 case NC_MSG_HELLO:
850 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100851 LY_TREE_FOR(xml->child, node) {
852 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
853 continue;
854 } else if (!strcmp(node->name, "session-id")) {
855 if (!node->content || !strlen(node->content)) {
856 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200857 goto error;
858 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100859 str = NULL;
860 id = strtoll(node->content, &str, 10);
861 if (*str || id < 1 || id > UINT32_MAX) {
862 ERR("Invalid 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 session->id = (uint32_t)id;
866 continue;
867 } else if (strcmp(node->name, "capabilities")) {
868 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200869 goto error;
870 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100871
872 if (flag) {
873 /* multiple capabilities elements */
874 ERR("Invalid <hello> message (multiple <capabilities> elements).");
875 goto error;
876 }
877 flag = 1;
878
879 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
880 goto error;
881 }
882 session->version = ver;
883 }
884
885 if (!session->id) {
886 ERR("Missing <session-id> in server's <hello>.");
887 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200888 }
889 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200890 case NC_MSG_WOULDBLOCK:
891 ERR("Server's <hello> timeout elapsed.");
892 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200893 case NC_MSG_ERROR:
894 /* nothing special, just pass it out */
895 break;
896 default:
897 ERR("Unexpected message received instead of <hello>.");
898 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200899 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200900 }
901
902 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100903 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200904
905 return msgtype;
906
907error:
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 NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200912}
913
Michal Vasko11d142a2016-01-19 15:58:24 +0100914static NC_MSG_TYPE
915nc_recv_server_hello(struct nc_session *session)
916{
917 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200918 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100919 int ver = -1;
920 int flag = 0;
921
Michal Vaskoadb850e2016-01-20 14:06:32 +0100922 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100923
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100924 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100925 case NC_MSG_HELLO:
926 /* get know NETCONF version */
927 LY_TREE_FOR(xml->child, node) {
928 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
929 continue;
930 } else if (strcmp(node->name, "capabilities")) {
931 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200932 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100933 goto cleanup;
934 }
935
936 if (flag) {
937 /* multiple capabilities elements */
938 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200939 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100940 goto cleanup;
941 }
942 flag = 1;
943
944 if ((ver = parse_cpblts(node, NULL)) < 0) {
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 session->version = ver;
949 }
950 break;
951 case NC_MSG_ERROR:
952 /* nothing special, just pass it out */
953 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100954 case NC_MSG_WOULDBLOCK:
955 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100956 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100957 default:
958 ERR("Unexpected message received instead of <hello>.");
959 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200960 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100961 }
962
963cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100964 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100965
966 return msgtype;
967}
968
Michal Vasko71090fc2016-05-24 16:37:28 +0200969NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100970nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100971{
Michal Vasko086311b2016-01-08 09:53:11 +0100972 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100973
Michal Vasko11d142a2016-01-19 15:58:24 +0100974 if (session->side == NC_CLIENT) {
975 type = nc_send_client_hello(session);
976 } else {
977 type = nc_send_server_hello(session);
978 }
979
Michal Vasko086311b2016-01-08 09:53:11 +0100980 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200981 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100982 }
983
Michal Vasko11d142a2016-01-19 15:58:24 +0100984 if (session->side == NC_CLIENT) {
985 type = nc_recv_client_hello(session);
986 } else {
987 type = nc_recv_server_hello(session);
988 }
989
Michal Vasko71090fc2016-05-24 16:37:28 +0200990 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100991}
Michal Vasko086311b2016-01-08 09:53:11 +0100992
Radek Krejci53691be2016-02-22 13:58:37 +0100993#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100994
Michal Vasko8f0c0282016-02-29 10:17:14 +0100995static void
Michal Vasko086311b2016-01-08 09:53:11 +0100996nc_ssh_init(void)
997{
998 ssh_threads_set_callbacks(ssh_threads_get_pthread());
999 ssh_init();
1000 ssh_set_log_level(verbose_level);
1001}
1002
Michal Vasko8f0c0282016-02-29 10:17:14 +01001003static void
Michal Vasko086311b2016-01-08 09:53:11 +01001004nc_ssh_destroy(void)
1005{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001006 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001007 ENGINE_cleanup();
1008 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001009 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001010 ssh_finalize();
1011}
1012
Radek Krejci53691be2016-02-22 13:58:37 +01001013#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001014
Radek Krejci53691be2016-02-22 13:58:37 +01001015#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001016
Michal Vaskof0c92c02016-01-29 09:41:45 +01001017struct CRYPTO_dynlock_value {
1018 pthread_mutex_t lock;
1019};
1020
Michal Vaskof0c92c02016-01-29 09:41:45 +01001021static struct CRYPTO_dynlock_value *
1022tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1023{
1024 struct CRYPTO_dynlock_value *value;
1025
1026 value = malloc(sizeof *value);
1027 if (!value) {
1028 ERRMEM;
1029 return NULL;
1030 }
1031 pthread_mutex_init(&value->lock, NULL);
1032
1033 return value;
1034}
1035
1036static void
1037tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1038{
1039 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1040 * I found ignored this fact, what do I know... */
1041 if (mode & CRYPTO_LOCK) {
1042 pthread_mutex_lock(&l->lock);
1043 } else {
1044 pthread_mutex_unlock(&l->lock);
1045 }
1046}
1047
1048static void
1049tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1050{
1051 pthread_mutex_destroy(&l->lock);
1052 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001053}
1054
Michal Vasko8f0c0282016-02-29 10:17:14 +01001055#endif /* NC_ENABLED_TLS */
1056
1057#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1058
1059static pthread_mutex_t *tls_locks;
1060
1061static void
1062tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1063{
1064 if (mode & CRYPTO_LOCK) {
1065 pthread_mutex_lock(tls_locks + n);
1066 } else {
1067 pthread_mutex_unlock(tls_locks + n);
1068 }
1069}
1070
1071static void
1072tls_thread_id_func(CRYPTO_THREADID *tid)
1073{
1074 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1075}
1076
1077static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001078nc_tls_init(void)
1079{
1080 int i;
1081
1082 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001083 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001084 SSL_library_init();
1085
1086 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001087 if (!tls_locks) {
1088 ERRMEM;
1089 return;
1090 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001091 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1092 pthread_mutex_init(tls_locks + i, NULL);
1093 }
1094
Michal Vaskof0c92c02016-01-29 09:41:45 +01001095 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001096 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001097
1098 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1099 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1100 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001101}
1102
Michal Vasko8f0c0282016-02-29 10:17:14 +01001103static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001104nc_tls_destroy(void)
1105{
1106 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001107
Michal Vasko8f0c0282016-02-29 10:17:14 +01001108 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001109 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001110 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001111 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001112 ERR_free_strings();
1113 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001114
Michal Vaskob6e37262016-02-25 14:49:00 +01001115 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001116 CRYPTO_set_locking_callback(NULL);
1117 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1118 pthread_mutex_destroy(tls_locks + i);
1119 }
1120 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001121
1122 CRYPTO_set_dynlock_create_callback(NULL);
1123 CRYPTO_set_dynlock_lock_callback(NULL);
1124 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001125}
1126
Michal Vasko8f0c0282016-02-29 10:17:14 +01001127#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001128
Radek Krejci53691be2016-02-22 13:58:37 +01001129#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001130
Michal Vasko8f0c0282016-02-29 10:17:14 +01001131static void
Michal Vasko5e228792016-02-03 15:30:24 +01001132nc_ssh_tls_init(void)
1133{
1134 SSL_load_error_strings();
1135 ERR_load_BIO_strings();
1136 SSL_library_init();
1137
1138 nc_ssh_init();
1139
1140 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1141 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1142 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1143}
1144
Michal Vasko8f0c0282016-02-29 10:17:14 +01001145static void
Michal Vasko5e228792016-02-03 15:30:24 +01001146nc_ssh_tls_destroy(void)
1147{
1148 ERR_free_strings();
1149 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1150
1151 nc_ssh_destroy();
1152
1153 CRYPTO_set_dynlock_create_callback(NULL);
1154 CRYPTO_set_dynlock_lock_callback(NULL);
1155 CRYPTO_set_dynlock_destroy_callback(NULL);
1156}
1157
Radek Krejci53691be2016-02-22 13:58:37 +01001158#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001159
1160#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1161
1162API void
1163nc_thread_destroy(void)
1164{
1165 CRYPTO_THREADID crypto_tid;
1166
1167 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1168 //CRYPTO_cleanup_all_ex_data();
1169
1170 CRYPTO_THREADID_current(&crypto_tid);
1171 ERR_remove_thread_state(&crypto_tid);
1172}
1173
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001174#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1175
1176void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001177nc_init(void)
1178{
1179#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1180 nc_ssh_tls_init();
1181#elif defined(NC_ENABLED_SSH)
1182 nc_ssh_init();
1183#elif defined(NC_ENABLED_TLS)
1184 nc_tls_init();
1185#endif
1186}
1187
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001188void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001189nc_destroy(void)
1190{
1191#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1192 nc_ssh_tls_destroy();
1193#elif defined(NC_ENABLED_SSH)
1194 nc_ssh_destroy();
1195#elif defined(NC_ENABLED_TLS)
1196 nc_tls_destroy();
1197#endif
1198}