blob: b9dd8860758190c4f2bd2c918f946af40787b686 [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>
Michal Vasko58f31552016-01-19 12:39:15 +010019#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020020#include <libyang/libyang.h>
21
Michal Vasko5e228792016-02-03 15:30:24 +010022#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010023#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010024#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010025
Radek Krejci53691be2016-02-22 13:58:37 +010026#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020027
Michal Vasko086311b2016-01-08 09:53:11 +010028# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020029
Radek Krejci53691be2016-02-22 13:58:37 +010030#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020031
Radek Krejci53691be2016-02-22 13:58:37 +010032#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010033
Michal Vasko5e228792016-02-03 15:30:24 +010034# include <openssl/engine.h>
35# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010036# include <openssl/err.h>
37
Radek Krejci53691be2016-02-22 13:58:37 +010038#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010039
Michal Vasko086311b2016-01-08 09:53:11 +010040/* in seconds */
41#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020042
Michal Vasko05ba9df2016-01-13 14:40:27 +010043/* in milliseconds */
44#define NC_CLOSE_REPLY_TIMEOUT 200
45
Michal Vasko086311b2016-01-08 09:53:11 +010046extern struct nc_server_opts server_opts;
47
Radek Krejci7ac16052016-07-15 11:48:18 +020048int
49nc_gettimespec(struct timespec *ts)
50{
51#ifdef CLOCK_MONOTONIC
52 return clock_gettime(CLOCK_MONOTONIC, ts);
53#elif defined CLOCK_REALTIME
54 return clock_gettime(CLOCK_REALTIME, ts);
55#else
56 int rc;
57 struct timeval tv;
58
59 rc = gettimeofday(&tv, NULL);
60 if (!rc) {
61 ts->tv_sec = (time_t)tv.tv_sec;
62 ts->tv_nsec = 1000L * (long)tv.tv_usec;
63 }
64 return rc;
65#endif
66}
67
Radek Krejci28472922016-07-15 11:51:16 +020068#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
69int
70pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
71{
72 int rc;
73 struct timespec cur, dur;
74
75 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
76 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
77 nc_gettimespec(&cur);
78
79 if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
80 break;
81 }
82
83 dur.tv_sec = abstime->tv_sec - cur.tv_sec;
84 dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
85 if (dur.tv_nsec < 0) {
86 dur.tv_sec--;
87 dur.tv_nsec += 1000000000;
88 }
89
90 if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
91 dur.tv_sec = 0;
92 dur.tv_nsec = 5000000;
93 }
94
95 nanosleep(&dur, NULL);
96 }
97
98 return rc;
99}
100#endif
101
Michal Vasko96164bf2016-01-21 15:41:58 +0100102/*
103 * @return 1 - success
104 * 0 - timeout
105 * -1 - error
106 */
107int
Michal Vasko62be1ce2016-03-03 13:24:52 +0100108nc_timedlock(pthread_mutex_t *lock, int timeout)
Michal Vasko96164bf2016-01-21 15:41:58 +0100109{
110 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100111 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100112
113 if (timeout > 0) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200114 nc_gettimespec(&ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100115
Michal Vasko96164bf2016-01-21 15:41:58 +0100116 ts_timeout.tv_sec += timeout / 1000;
117 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
118
119 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100120 } else if (!timeout) {
121 ret = pthread_mutex_trylock(lock);
122 } else { /* timeout == -1 */
123 ret = pthread_mutex_lock(lock);
124 }
125
126 if (ret == ETIMEDOUT) {
127 /* timeout */
128 return 0;
129 } else if (ret) {
130 /* error */
Radek Krejcida8f58d2016-03-03 13:10:21 +0100131 ERR("Mutex lock failed (%s).", strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100132 return -1;
133 }
134
135 /* ok */
136 return 1;
137}
138
Michal Vasko8dadf782016-01-15 10:29:36 +0100139API NC_STATUS
140nc_session_get_status(const struct nc_session *session)
141{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100142 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200143 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100144 return 0;
145 }
146
Michal Vasko8dadf782016-01-15 10:29:36 +0100147 return session->status;
148}
149
150API uint32_t
151nc_session_get_id(const struct nc_session *session)
152{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100153 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200154 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100155 return 0;
156 }
157
Michal Vasko8dadf782016-01-15 10:29:36 +0100158 return session->id;
159}
160
Michal Vasko174fe8e2016-02-17 15:38:09 +0100161API int
162nc_session_get_version(const struct nc_session *session)
163{
164 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200165 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100166 return -1;
167 }
168
169 return (session->version == NC_VERSION_10 ? 0 : 1);
170}
171
Michal Vasko8dadf782016-01-15 10:29:36 +0100172API NC_TRANSPORT_IMPL
173nc_session_get_ti(const struct nc_session *session)
174{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100175 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200176 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100177 return 0;
178 }
179
Michal Vasko8dadf782016-01-15 10:29:36 +0100180 return session->ti_type;
181}
182
183API const char *
184nc_session_get_username(const struct nc_session *session)
185{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100186 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200187 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100188 return NULL;
189 }
190
Michal Vasko8dadf782016-01-15 10:29:36 +0100191 return session->username;
192}
193
194API const char *
195nc_session_get_host(const struct nc_session *session)
196{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100197 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200198 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100199 return NULL;
200 }
201
Michal Vasko8dadf782016-01-15 10:29:36 +0100202 return session->host;
203}
204
205API uint16_t
206nc_session_get_port(const struct nc_session *session)
207{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100208 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200209 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100210 return 0;
211 }
212
Michal Vasko8dadf782016-01-15 10:29:36 +0100213 return session->port;
214}
215
Michal Vasko9a25e932016-02-01 10:36:42 +0100216API struct ly_ctx *
217nc_session_get_ctx(const struct nc_session *session)
218{
219 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200220 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100221 return NULL;
222 }
223
224 return session->ctx;
225}
226
Michal Vasko2cc4c682016-03-01 09:16:48 +0100227API void
228nc_session_set_data(struct nc_session *session, void *data)
229{
230 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200231 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100232 return;
233 }
234
235 session->data = data;
236}
237
238API void *
239nc_session_get_data(const struct nc_session *session)
240{
241 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200242 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100243 return NULL;
244 }
245
246 return session->data;
247}
248
Michal Vasko086311b2016-01-08 09:53:11 +0100249NC_MSG_TYPE
250nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200251{
Michal Vasko086311b2016-01-08 09:53:11 +0100252 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200253
Michal Vasko086311b2016-01-08 09:53:11 +0100254 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100255 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
256 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100257 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100258 }
259
Michal Vasko086311b2016-01-08 09:53:11 +0100260 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
261
262 if (r) {
263 return NC_MSG_ERROR;
264 }
265
266 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100267}
268
Radek Krejci695d4fa2015-10-22 13:23:54 +0200269API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100270nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200271{
Michal Vasko9e99f012016-03-03 13:25:20 +0100272 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100273 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100274 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100275 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100276 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100277 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100278 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200279 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100280 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200281 void *p;
282
Michal Vasko428087d2016-01-14 16:04:28 +0100283 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200284 return;
285 }
286
Michal Vasko86d357c2016-03-11 13:46:38 +0100287 /* stop notifications loop if any */
288 if (session->ntf_tid) {
289 tid = *session->ntf_tid;
290 free((pthread_t *)session->ntf_tid);
291 session->ntf_tid = NULL;
292 /* the thread now knows it should quit */
293
294 pthread_join(tid, NULL);
295 }
296
Michal Vaskoadd4c792015-10-26 15:36:58 +0100297 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100298 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100299 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100300 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100301 } else if (!r) {
302 /* we failed to lock it, too bad */
303 locked = 0;
304 } else {
305 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100306 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200307 } else {
308 ERRINT;
309 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200310 }
311
Michal Vasko9e99f012016-03-03 13:25:20 +0100312 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200313 /* cleanup message queues */
314 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100315 for (contiter = session->notifs; contiter; ) {
316 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200317
Michal Vaskoad611702015-12-03 13:41:51 +0100318 p = contiter;
319 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200320 free(p);
321 }
322
323 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100324 for (contiter = session->replies; contiter; ) {
325 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200326
Michal Vaskoad611702015-12-03 13:41:51 +0100327 p = contiter;
328 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200329 free(p);
330 }
331
332 /* send closing info to the other side */
333 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
334 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100335 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200336 } else {
337 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100338 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200339 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100340 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100341 case NC_MSG_REPLY:
342 LY_TREE_FOR(rpl->child, child) {
343 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
344 break;
345 }
346 }
347 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100348 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100349 }
Michal Vaskoad611702015-12-03 13:41:51 +0100350 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100351 break;
352 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100353 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100354 break;
355 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100356 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100357 break;
358 default:
359 /* cannot happen */
360 break;
361 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200362 }
363
364 /* list of server's capabilities */
365 if (session->cpblts) {
366 for (i = 0; session->cpblts[i]; i++) {
367 lydict_remove(session->ctx, session->cpblts[i]);
368 }
369 free(session->cpblts);
370 }
371 }
372
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100373 if (session->data && data_free) {
374 data_free(session->data);
375 }
376
Michal Vasko86d357c2016-03-11 13:46:38 +0100377 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200378 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100379 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200380
381 /* transport implementation cleanup */
382 switch (session->ti_type) {
383 case NC_TI_FD:
384 /* nothing needed - file descriptors were provided by caller,
385 * so it is up to the caller to close them correctly
386 * TODO use callbacks
387 */
Michal Vasko3512e402016-01-28 16:22:34 +0100388 /* just to avoid compiler warning */
389 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100390 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200391 break;
392
Radek Krejci53691be2016-02-22 13:58:37 +0100393#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200394 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100395 if (connected) {
396 ssh_channel_free(session->ti.libssh.channel);
397 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200398 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
399 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
400 * it.
401 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100402 multisession = 0;
403 if (session->ti.libssh.next) {
404 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
405 if (siter->status != NC_STATUS_STARTING) {
406 multisession = 1;
407 break;
408 }
409 }
410 }
411
412 if (!multisession) {
413 /* it's not multisession yet, but we still need to free the starting sessions */
414 if (session->ti.libssh.next) {
415 do {
416 siter = session->ti.libssh.next;
417 session->ti.libssh.next = siter->ti.libssh.next;
418
419 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100420 lydict_remove(session->ctx, session->username);
421 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100422 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100423 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100424 }
425
426 free(siter);
427 } while (session->ti.libssh.next != session);
428 }
Michal Vasko428087d2016-01-14 16:04:28 +0100429 if (connected) {
430 ssh_disconnect(session->ti.libssh.session);
431 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200432 ssh_free(session->ti.libssh.session);
433 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200434 /* remove the session from the list */
435 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100436 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200437 /* there will be only one session */
438 siter->ti.libssh.next = NULL;
439 } else {
440 /* there are still multiple sessions, keep the ring list */
441 siter->ti.libssh.next = session->ti.libssh.next;
442 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100443 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
444 if (session->flags & NC_SESSION_SSH_MSG_CB) {
445 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
446 if (siter->ti.libssh.next == session) {
447 ERRINT;
448 break;
449 }
450 }
451 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
452 siter->flags |= NC_SESSION_SSH_MSG_CB;
453 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200454 }
455 break;
456#endif
457
Radek Krejci53691be2016-02-22 13:58:37 +0100458#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200459 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100460 if (connected) {
461 SSL_shutdown(session->ti.tls);
462 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200463 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100464
465 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200466 break;
467#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100468 case NC_TI_NONE:
469 ERRINT;
470 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200471 }
Michal Vasko428087d2016-01-14 16:04:28 +0100472
Radek Krejciac6d3472015-10-22 15:47:18 +0200473 lydict_remove(session->ctx, session->username);
474 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200475
476 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100477 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100478 if (locked) {
479 pthread_mutex_unlock(session->ti_lock);
480 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100481 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100482 pthread_mutex_destroy(session->ti_lock);
483 free(session->ti_lock);
484 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200485 }
486
487 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100488 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200489 }
490
491 free(session);
492}
493
Michal Vasko086311b2016-01-08 09:53:11 +0100494static void
495add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
496{
497 if (*count == *size) {
498 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100499 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
500 if (!(*cpblts)) {
501 ERRMEM;
502 return;
503 }
Michal Vasko086311b2016-01-08 09:53:11 +0100504 }
505
506 if (capab) {
507 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
508 } else {
509 (*cpblts)[*count] = NULL;
510 }
511 ++(*count);
512}
513
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200514API const char **
515nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100516{
517 struct lyd_node *child, *child2, *yanglib;
518 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
519 const char **cpblts;
520 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100521 int size = 10, count, feat_count = 0, i, str_len;
Michal Vaskoc1119d82016-06-20 10:03:24 +0200522#define NC_CPBLT_BUF_LEN 512
523 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100524
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200525 if (!ctx) {
526 ERRARG("ctx");
527 return NULL;
528 }
529
Michal Vasko086311b2016-01-08 09:53:11 +0100530 yanglib = ly_ctx_info(ctx);
531 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200532 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100533 return NULL;
534 }
535
536 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100537 if (!cpblts) {
538 ERRMEM;
539 return NULL;
540 }
Michal Vasko086311b2016-01-08 09:53:11 +0100541 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
542 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
543 count = 2;
544
545 /* capabilities */
546
547 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
548 if (mod) {
549 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100550 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100551 }
552 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100553 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100554 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100555 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100556 }
557 }
558 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100559 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100560 }
561 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100562 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100563 }
564 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100565 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100566 }
567 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100568 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100569 }
570 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100571 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100572 }
573 }
574
575 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
576 if (mod) {
577 if (!server_opts.wd_basic_mode) {
578 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
579 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100580 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100581 switch (server_opts.wd_basic_mode) {
582 case NC_WD_ALL:
583 strcat(str, "?basic-mode=report-all");
584 break;
585 case NC_WD_TRIM:
586 strcat(str, "?basic-mode=trim");
587 break;
588 case NC_WD_EXPLICIT:
589 strcat(str, "?basic-mode=explicit");
590 break;
591 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100592 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100593 break;
594 }
595
596 if (server_opts.wd_also_supported) {
Michal Vaskoc1119d82016-06-20 10:03:24 +0200597 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100598 if (server_opts.wd_also_supported & NC_WD_ALL) {
599 strcat(str, "report-all,");
600 }
601 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
602 strcat(str, "report-all-tagged,");
603 }
604 if (server_opts.wd_also_supported & NC_WD_TRIM) {
605 strcat(str, "trim,");
606 }
607 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
608 strcat(str, "explicit,");
609 }
610 str[strlen(str) - 1] = '\0';
611
612 add_cpblt(ctx, str, &cpblts, &size, &count);
613 }
614 }
615 }
616
Michal Vasko1a38c862016-01-15 15:50:07 +0100617 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100618 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100619 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100620 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100621 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100622 }
623 }
624
625 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100626 LY_TREE_FOR(yanglib->child, child) {
627 if (!strcmp(child->schema->name, "module")) {
628 LY_TREE_FOR(child->child, child2) {
629 if (!strcmp(child2->schema->name, "namespace")) {
630 ns = (struct lyd_node_leaf_list *)child2;
631 } else if (!strcmp(child2->schema->name, "name")) {
632 name = (struct lyd_node_leaf_list *)child2;
633 } else if (!strcmp(child2->schema->name, "revision")) {
634 rev = (struct lyd_node_leaf_list *)child2;
635 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100636 features = nc_realloc(features, ++feat_count * sizeof *features);
637 if (!features) {
638 ERRMEM;
639 free(cpblts);
640 return NULL;
641 }
Michal Vasko086311b2016-01-08 09:53:11 +0100642 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
643 }
644 }
645
646 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100647 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100648 continue;
649 }
650
Radek Krejcidf7ba522016-03-01 16:05:25 +0100651 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vaskoc1119d82016-06-20 10:03:24 +0200652 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100653 if (feat_count) {
Michal Vaskoc1119d82016-06-20 10:03:24 +0200654 strcat(str, "&features=");
655 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100656 for (i = 0; i < feat_count; ++i) {
Michal Vaskoc1119d82016-06-20 10:03:24 +0200657 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100658 ERRINT;
659 break;
660 }
Michal Vasko086311b2016-01-08 09:53:11 +0100661 if (i) {
662 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100663 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100664 }
665 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100666 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100667 }
668 }
669
670 add_cpblt(ctx, str, &cpblts, &size, &count);
671
672 ns = NULL;
673 name = NULL;
674 rev = NULL;
675 free(features);
676 features = NULL;
677 feat_count = 0;
678 }
679 }
680
681 lyd_free(yanglib);
682
683 /* ending NULL capability */
684 add_cpblt(ctx, NULL, &cpblts, &size, &count);
685
686 return cpblts;
687}
688
Radek Krejci695d4fa2015-10-22 13:23:54 +0200689static int
690parse_cpblts(struct lyxml_elem *xml, const char ***list)
691{
692 struct lyxml_elem *cpblt;
693 int ver = -1;
694 int i = 0;
695
696 if (list) {
697 /* get the storage for server's capabilities */
698 LY_TREE_FOR(xml->child, cpblt) {
699 i++;
700 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100701 /* last item remains NULL */
702 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200703 if (!*list) {
704 ERRMEM;
705 return -1;
706 }
707 i = 0;
708 }
709
710 LY_TREE_FOR(xml->child, cpblt) {
711 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
712 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
713 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
714 return -1;
715 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
716 continue;
717 }
718
719 /* detect NETCONF version */
720 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
721 ver = 0;
722 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
723 ver = 1;
724 }
725
726 /* store capabilities */
727 if (list) {
728 (*list)[i] = cpblt->content;
729 cpblt->content = NULL;
730 i++;
731 }
732 }
733
734 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100735 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200736 }
737
738 return ver;
739}
740
741static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100742nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100743{
744 int r, i;
745 const char **cpblts;
746
Michal Vasko11d142a2016-01-19 15:58:24 +0100747 /* client side hello - send only NETCONF base capabilities */
748 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100749 if (!cpblts) {
750 ERRMEM;
751 return NC_MSG_ERROR;
752 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100753 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
754 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
755 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100756
Michal Vasko11d142a2016-01-19 15:58:24 +0100757 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100758
Michal Vasko086311b2016-01-08 09:53:11 +0100759 for (i = 0; cpblts[i]; ++i) {
760 lydict_remove(session->ctx, cpblts[i]);
761 }
762 free(cpblts);
763
764 if (r) {
765 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100766 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100767
768 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100769}
770
771static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100772nc_send_server_hello(struct nc_session *session)
773{
774 int r, i;
775 const char **cpblts;
776
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200777 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100778
779 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
780
Michal Vasko11d142a2016-01-19 15:58:24 +0100781 for (i = 0; cpblts[i]; ++i) {
782 lydict_remove(session->ctx, cpblts[i]);
783 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100784 free(cpblts);
785
786 if (r) {
787 return NC_MSG_ERROR;
788 }
789
790 return NC_MSG_HELLO;
791}
792
793static NC_MSG_TYPE
794nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200795{
796 struct lyxml_elem *xml = NULL, *node;
797 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
798 int ver = -1;
799 char *str;
800 long long int id;
801 int flag = 0;
802
Michal Vasko05ba9df2016-01-13 14:40:27 +0100803 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200804
805 switch(msgtype) {
806 case NC_MSG_HELLO:
807 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100808 LY_TREE_FOR(xml->child, node) {
809 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
810 continue;
811 } else if (!strcmp(node->name, "session-id")) {
812 if (!node->content || !strlen(node->content)) {
813 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200814 goto error;
815 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100816 str = NULL;
817 id = strtoll(node->content, &str, 10);
818 if (*str || id < 1 || id > UINT32_MAX) {
819 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200820 goto error;
821 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100822 session->id = (uint32_t)id;
823 continue;
824 } else if (strcmp(node->name, "capabilities")) {
825 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200826 goto error;
827 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100828
829 if (flag) {
830 /* multiple capabilities elements */
831 ERR("Invalid <hello> message (multiple <capabilities> elements).");
832 goto error;
833 }
834 flag = 1;
835
836 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
837 goto error;
838 }
839 session->version = ver;
840 }
841
842 if (!session->id) {
843 ERR("Missing <session-id> in server's <hello>.");
844 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200845 }
846 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200847 case NC_MSG_WOULDBLOCK:
848 ERR("Server's <hello> timeout elapsed.");
849 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200850 case NC_MSG_ERROR:
851 /* nothing special, just pass it out */
852 break;
853 default:
854 ERR("Unexpected message received instead of <hello>.");
855 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200856 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200857 }
858
859 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100860 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200861
862 return msgtype;
863
864error:
865 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100866 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200867
868 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200869}
870
Michal Vasko11d142a2016-01-19 15:58:24 +0100871static NC_MSG_TYPE
872nc_recv_server_hello(struct nc_session *session)
873{
874 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200875 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100876 int ver = -1;
877 int flag = 0;
878
Michal Vaskoadb850e2016-01-20 14:06:32 +0100879 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100880
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100881 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100882 case NC_MSG_HELLO:
883 /* get know NETCONF version */
884 LY_TREE_FOR(xml->child, node) {
885 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
886 continue;
887 } else if (strcmp(node->name, "capabilities")) {
888 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200889 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100890 goto cleanup;
891 }
892
893 if (flag) {
894 /* multiple capabilities elements */
895 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200896 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100897 goto cleanup;
898 }
899 flag = 1;
900
901 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200902 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100903 goto cleanup;
904 }
905 session->version = ver;
906 }
907 break;
908 case NC_MSG_ERROR:
909 /* nothing special, just pass it out */
910 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100911 case NC_MSG_WOULDBLOCK:
912 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100913 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100914 default:
915 ERR("Unexpected message received instead of <hello>.");
916 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200917 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100918 }
919
920cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100921 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100922
923 return msgtype;
924}
925
Michal Vasko71090fc2016-05-24 16:37:28 +0200926NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100927nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100928{
Michal Vasko086311b2016-01-08 09:53:11 +0100929 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100930
Michal Vasko11d142a2016-01-19 15:58:24 +0100931 if (session->side == NC_CLIENT) {
932 type = nc_send_client_hello(session);
933 } else {
934 type = nc_send_server_hello(session);
935 }
936
Michal Vasko086311b2016-01-08 09:53:11 +0100937 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200938 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100939 }
940
Michal Vasko11d142a2016-01-19 15:58:24 +0100941 if (session->side == NC_CLIENT) {
942 type = nc_recv_client_hello(session);
943 } else {
944 type = nc_recv_server_hello(session);
945 }
946
Michal Vasko71090fc2016-05-24 16:37:28 +0200947 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100948}
Michal Vasko086311b2016-01-08 09:53:11 +0100949
Radek Krejci53691be2016-02-22 13:58:37 +0100950#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100951
Michal Vasko8f0c0282016-02-29 10:17:14 +0100952static void
Michal Vasko086311b2016-01-08 09:53:11 +0100953nc_ssh_init(void)
954{
955 ssh_threads_set_callbacks(ssh_threads_get_pthread());
956 ssh_init();
957 ssh_set_log_level(verbose_level);
958}
959
Michal Vasko8f0c0282016-02-29 10:17:14 +0100960static void
Michal Vasko086311b2016-01-08 09:53:11 +0100961nc_ssh_destroy(void)
962{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100963 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100964 ENGINE_cleanup();
965 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100966 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100967 ssh_finalize();
968}
969
Radek Krejci53691be2016-02-22 13:58:37 +0100970#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100971
Radek Krejci53691be2016-02-22 13:58:37 +0100972#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100973
Michal Vaskof0c92c02016-01-29 09:41:45 +0100974struct CRYPTO_dynlock_value {
975 pthread_mutex_t lock;
976};
977
Michal Vaskof0c92c02016-01-29 09:41:45 +0100978static struct CRYPTO_dynlock_value *
979tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
980{
981 struct CRYPTO_dynlock_value *value;
982
983 value = malloc(sizeof *value);
984 if (!value) {
985 ERRMEM;
986 return NULL;
987 }
988 pthread_mutex_init(&value->lock, NULL);
989
990 return value;
991}
992
993static void
994tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
995{
996 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
997 * I found ignored this fact, what do I know... */
998 if (mode & CRYPTO_LOCK) {
999 pthread_mutex_lock(&l->lock);
1000 } else {
1001 pthread_mutex_unlock(&l->lock);
1002 }
1003}
1004
1005static void
1006tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1007{
1008 pthread_mutex_destroy(&l->lock);
1009 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001010}
1011
Michal Vasko8f0c0282016-02-29 10:17:14 +01001012#endif /* NC_ENABLED_TLS */
1013
1014#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1015
1016static pthread_mutex_t *tls_locks;
1017
1018static void
1019tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1020{
1021 if (mode & CRYPTO_LOCK) {
1022 pthread_mutex_lock(tls_locks + n);
1023 } else {
1024 pthread_mutex_unlock(tls_locks + n);
1025 }
1026}
1027
1028static void
1029tls_thread_id_func(CRYPTO_THREADID *tid)
1030{
1031 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1032}
1033
1034static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001035nc_tls_init(void)
1036{
1037 int i;
1038
1039 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001040 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001041 SSL_library_init();
1042
1043 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001044 if (!tls_locks) {
1045 ERRMEM;
1046 return;
1047 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001048 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1049 pthread_mutex_init(tls_locks + i, NULL);
1050 }
1051
Michal Vaskof0c92c02016-01-29 09:41:45 +01001052 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001053 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001054
1055 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1056 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1057 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001058}
1059
Michal Vasko8f0c0282016-02-29 10:17:14 +01001060static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001061nc_tls_destroy(void)
1062{
1063 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001064
Michal Vasko8f0c0282016-02-29 10:17:14 +01001065 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001066 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001067 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001068 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001069 ERR_free_strings();
1070 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001071
Michal Vaskob6e37262016-02-25 14:49:00 +01001072 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001073 CRYPTO_set_locking_callback(NULL);
1074 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1075 pthread_mutex_destroy(tls_locks + i);
1076 }
1077 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001078
1079 CRYPTO_set_dynlock_create_callback(NULL);
1080 CRYPTO_set_dynlock_lock_callback(NULL);
1081 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001082}
1083
Michal Vasko8f0c0282016-02-29 10:17:14 +01001084#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001085
Radek Krejci53691be2016-02-22 13:58:37 +01001086#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001087
Michal Vasko8f0c0282016-02-29 10:17:14 +01001088static void
Michal Vasko5e228792016-02-03 15:30:24 +01001089nc_ssh_tls_init(void)
1090{
1091 SSL_load_error_strings();
1092 ERR_load_BIO_strings();
1093 SSL_library_init();
1094
1095 nc_ssh_init();
1096
1097 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1098 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1099 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1100}
1101
Michal Vasko8f0c0282016-02-29 10:17:14 +01001102static void
Michal Vasko5e228792016-02-03 15:30:24 +01001103nc_ssh_tls_destroy(void)
1104{
1105 ERR_free_strings();
1106 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1107
1108 nc_ssh_destroy();
1109
1110 CRYPTO_set_dynlock_create_callback(NULL);
1111 CRYPTO_set_dynlock_lock_callback(NULL);
1112 CRYPTO_set_dynlock_destroy_callback(NULL);
1113}
1114
Radek Krejci53691be2016-02-22 13:58:37 +01001115#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001116
1117#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1118
1119API void
1120nc_thread_destroy(void)
1121{
1122 CRYPTO_THREADID crypto_tid;
1123
1124 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1125 //CRYPTO_cleanup_all_ex_data();
1126
1127 CRYPTO_THREADID_current(&crypto_tid);
1128 ERR_remove_thread_state(&crypto_tid);
1129}
1130
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001131#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1132
1133void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001134nc_init(void)
1135{
1136#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1137 nc_ssh_tls_init();
1138#elif defined(NC_ENABLED_SSH)
1139 nc_ssh_init();
1140#elif defined(NC_ENABLED_TLS)
1141 nc_tls_init();
1142#endif
1143}
1144
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001145void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001146nc_destroy(void)
1147{
1148#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1149 nc_ssh_tls_destroy();
1150#elif defined(NC_ENABLED_SSH)
1151 nc_ssh_destroy();
1152#elif defined(NC_ENABLED_TLS)
1153 nc_tls_destroy();
1154#endif
1155}