blob: 484e6b7ce5dec3a9ac5c299adb07c0a19cb596ea [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
14
Radek Krejci206fcd62015-10-07 15:42:48 +020015#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010016#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010017#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020018#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020019#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010020#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020021#include <libyang/libyang.h>
22
Michal Vasko5e228792016-02-03 15:30:24 +010023#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010024#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010025#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010026
Radek Krejci53691be2016-02-22 13:58:37 +010027#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020028
Michal Vasko086311b2016-01-08 09:53:11 +010029# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Radek Krejci53691be2016-02-22 13:58:37 +010031#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020032
Radek Krejci53691be2016-02-22 13:58:37 +010033#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010034
Michal Vasko5e228792016-02-03 15:30:24 +010035# include <openssl/engine.h>
36# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010037# include <openssl/err.h>
38
Radek Krejci53691be2016-02-22 13:58:37 +010039#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010040
Michal Vasko086311b2016-01-08 09:53:11 +010041/* in seconds */
42#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020043
Michal Vasko05ba9df2016-01-13 14:40:27 +010044/* in milliseconds */
45#define NC_CLOSE_REPLY_TIMEOUT 200
46
Michal Vasko086311b2016-01-08 09:53:11 +010047extern struct nc_server_opts server_opts;
48
Radek Krejci7ac16052016-07-15 11:48:18 +020049int
50nc_gettimespec(struct timespec *ts)
51{
Michal Vasko4f9c1412016-09-21 14:03:18 +020052#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020053 return clock_gettime(CLOCK_REALTIME, ts);
54#else
55 int rc;
56 struct timeval tv;
57
58 rc = gettimeofday(&tv, NULL);
59 if (!rc) {
60 ts->tv_sec = (time_t)tv.tv_sec;
61 ts->tv_nsec = 1000L * (long)tv.tv_usec;
62 }
63 return rc;
64#endif
65}
66
Radek Krejci28472922016-07-15 11:51:16 +020067#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
68int
69pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
70{
71 int rc;
72 struct timespec cur, dur;
73
74 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
75 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
76 nc_gettimespec(&cur);
77
78 if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
79 break;
80 }
81
82 dur.tv_sec = abstime->tv_sec - cur.tv_sec;
83 dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
84 if (dur.tv_nsec < 0) {
85 dur.tv_sec--;
86 dur.tv_nsec += 1000000000;
87 }
88
89 if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
90 dur.tv_sec = 0;
91 dur.tv_nsec = 5000000;
92 }
93
94 nanosleep(&dur, NULL);
95 }
96
97 return rc;
98}
99#endif
100
Michal Vasko96164bf2016-01-21 15:41:58 +0100101/*
102 * @return 1 - success
103 * 0 - timeout
104 * -1 - error
105 */
106int
Michal vasko953939c2016-10-04 13:46:20 +0200107nc_timedlock(pthread_mutex_t *lock, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100108{
109 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100110 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100111
112 if (timeout > 0) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200113 nc_gettimespec(&ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100114
Michal Vasko96164bf2016-01-21 15:41:58 +0100115 ts_timeout.tv_sec += timeout / 1000;
116 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
117
118 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100119 } else if (!timeout) {
120 ret = pthread_mutex_trylock(lock);
121 } else { /* timeout == -1 */
122 ret = pthread_mutex_lock(lock);
123 }
124
125 if (ret == ETIMEDOUT) {
126 /* timeout */
127 return 0;
128 } else if (ret) {
129 /* error */
Michal vasko953939c2016-10-04 13:46:20 +0200130 ERR("Mutex lock failed (%s, %s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100131 return -1;
132 }
133
134 /* ok */
135 return 1;
136}
137
Michal Vasko8dadf782016-01-15 10:29:36 +0100138API NC_STATUS
139nc_session_get_status(const struct nc_session *session)
140{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100141 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200142 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100143 return 0;
144 }
145
Michal Vasko8dadf782016-01-15 10:29:36 +0100146 return session->status;
147}
148
149API uint32_t
150nc_session_get_id(const struct nc_session *session)
151{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100152 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200153 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100154 return 0;
155 }
156
Michal Vasko8dadf782016-01-15 10:29:36 +0100157 return session->id;
158}
159
Michal Vasko174fe8e2016-02-17 15:38:09 +0100160API int
161nc_session_get_version(const struct nc_session *session)
162{
163 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200164 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100165 return -1;
166 }
167
168 return (session->version == NC_VERSION_10 ? 0 : 1);
169}
170
Michal Vasko8dadf782016-01-15 10:29:36 +0100171API NC_TRANSPORT_IMPL
172nc_session_get_ti(const struct nc_session *session)
173{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100174 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200175 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100176 return 0;
177 }
178
Michal Vasko8dadf782016-01-15 10:29:36 +0100179 return session->ti_type;
180}
181
182API const char *
183nc_session_get_username(const struct nc_session *session)
184{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100185 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200186 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100187 return NULL;
188 }
189
Michal Vasko8dadf782016-01-15 10:29:36 +0100190 return session->username;
191}
192
193API const char *
194nc_session_get_host(const struct nc_session *session)
195{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100196 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200197 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100198 return NULL;
199 }
200
Michal Vasko8dadf782016-01-15 10:29:36 +0100201 return session->host;
202}
203
204API uint16_t
205nc_session_get_port(const struct nc_session *session)
206{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100207 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200208 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100209 return 0;
210 }
211
Michal Vasko8dadf782016-01-15 10:29:36 +0100212 return session->port;
213}
214
Michal Vasko9a25e932016-02-01 10:36:42 +0100215API struct ly_ctx *
216nc_session_get_ctx(const struct nc_session *session)
217{
218 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200219 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100220 return NULL;
221 }
222
223 return session->ctx;
224}
225
Michal Vasko2cc4c682016-03-01 09:16:48 +0100226API void
227nc_session_set_data(struct nc_session *session, void *data)
228{
229 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200230 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100231 return;
232 }
233
234 session->data = data;
235}
236
237API void *
238nc_session_get_data(const struct nc_session *session)
239{
240 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200241 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100242 return NULL;
243 }
244
245 return session->data;
246}
247
Michal Vasko086311b2016-01-08 09:53:11 +0100248NC_MSG_TYPE
249nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200250{
Michal Vasko086311b2016-01-08 09:53:11 +0100251 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200252
Michal Vasko086311b2016-01-08 09:53:11 +0100253 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100254 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
255 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100256 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100257 }
258
Michal Vasko086311b2016-01-08 09:53:11 +0100259 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
260
261 if (r) {
262 return NC_MSG_ERROR;
263 }
264
265 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100266}
267
Radek Krejci695d4fa2015-10-22 13:23:54 +0200268API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100269nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200270{
Michal Vasko9e99f012016-03-03 13:25:20 +0100271 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100272 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100273 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100274 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100275 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100276 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100277 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200278 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100279 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200280 void *p;
281
Michal Vasko428087d2016-01-14 16:04:28 +0100282 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200283 return;
284 }
285
Michal Vasko86d357c2016-03-11 13:46:38 +0100286 /* stop notifications loop if any */
287 if (session->ntf_tid) {
288 tid = *session->ntf_tid;
289 free((pthread_t *)session->ntf_tid);
290 session->ntf_tid = NULL;
291 /* the thread now knows it should quit */
292
293 pthread_join(tid, NULL);
294 }
295
Michal Vaskoadd4c792015-10-26 15:36:58 +0100296 if (session->ti_lock) {
Michal vasko953939c2016-10-04 13:46:20 +0200297 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100298 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100299 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100300 } else if (!r) {
301 /* we failed to lock it, too bad */
302 locked = 0;
303 } else {
304 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100305 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200306 } else {
307 ERRINT;
308 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200309 }
310
Michal Vasko9e99f012016-03-03 13:25:20 +0100311 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200312 /* cleanup message queues */
313 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100314 for (contiter = session->notifs; contiter; ) {
315 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200316
Michal Vaskoad611702015-12-03 13:41:51 +0100317 p = contiter;
318 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200319 free(p);
320 }
321
322 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100323 for (contiter = session->replies; contiter; ) {
324 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200325
Michal Vaskoad611702015-12-03 13:41:51 +0100326 p = contiter;
327 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200328 free(p);
329 }
330
331 /* send closing info to the other side */
332 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
333 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100334 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200335 } else {
336 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100337 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200338 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100339 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100340 case NC_MSG_REPLY:
341 LY_TREE_FOR(rpl->child, child) {
342 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
343 break;
344 }
345 }
346 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100347 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100348 }
Michal Vaskoad611702015-12-03 13:41:51 +0100349 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100350 break;
351 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100352 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100353 break;
354 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100355 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100356 break;
357 default:
358 /* cannot happen */
359 break;
360 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200361 }
362
363 /* list of server's capabilities */
364 if (session->cpblts) {
365 for (i = 0; session->cpblts[i]; i++) {
366 lydict_remove(session->ctx, session->cpblts[i]);
367 }
368 free(session->cpblts);
369 }
370 }
371
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100372 if (session->data && data_free) {
373 data_free(session->data);
374 }
375
Michal Vasko86d357c2016-03-11 13:46:38 +0100376 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200377 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100378 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200379
380 /* transport implementation cleanup */
381 switch (session->ti_type) {
382 case NC_TI_FD:
383 /* nothing needed - file descriptors were provided by caller,
384 * so it is up to the caller to close them correctly
385 * TODO use callbacks
386 */
Michal Vasko3512e402016-01-28 16:22:34 +0100387 /* just to avoid compiler warning */
388 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100389 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200390 break;
391
Radek Krejci53691be2016-02-22 13:58:37 +0100392#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200393 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100394 if (connected) {
395 ssh_channel_free(session->ti.libssh.channel);
396 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200397 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
398 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
399 * it.
400 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100401 multisession = 0;
402 if (session->ti.libssh.next) {
403 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
404 if (siter->status != NC_STATUS_STARTING) {
405 multisession = 1;
406 break;
407 }
408 }
409 }
410
411 if (!multisession) {
412 /* it's not multisession yet, but we still need to free the starting sessions */
413 if (session->ti.libssh.next) {
414 do {
415 siter = session->ti.libssh.next;
416 session->ti.libssh.next = siter->ti.libssh.next;
417
418 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100419 lydict_remove(session->ctx, session->username);
420 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100421 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100422 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100423 }
424
425 free(siter);
426 } while (session->ti.libssh.next != session);
427 }
Michal Vasko428087d2016-01-14 16:04:28 +0100428 if (connected) {
429 ssh_disconnect(session->ti.libssh.session);
430 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200431 ssh_free(session->ti.libssh.session);
432 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200433 /* remove the session from the list */
434 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100435 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200436 /* there will be only one session */
437 siter->ti.libssh.next = NULL;
438 } else {
439 /* there are still multiple sessions, keep the ring list */
440 siter->ti.libssh.next = session->ti.libssh.next;
441 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100442 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
443 if (session->flags & NC_SESSION_SSH_MSG_CB) {
444 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
445 if (siter->ti.libssh.next == session) {
446 ERRINT;
447 break;
448 }
449 }
450 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
451 siter->flags |= NC_SESSION_SSH_MSG_CB;
452 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200453 }
454 break;
455#endif
456
Radek Krejci53691be2016-02-22 13:58:37 +0100457#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200458 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100459 if (connected) {
460 SSL_shutdown(session->ti.tls);
461 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200462 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100463
464 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200465 break;
466#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100467 case NC_TI_NONE:
468 ERRINT;
469 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200470 }
Michal Vasko428087d2016-01-14 16:04:28 +0100471
Radek Krejciac6d3472015-10-22 15:47:18 +0200472 lydict_remove(session->ctx, session->username);
473 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200474
475 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100476 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100477 if (locked) {
478 pthread_mutex_unlock(session->ti_lock);
479 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100480 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100481 pthread_mutex_destroy(session->ti_lock);
482 free(session->ti_lock);
483 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200484 }
485
486 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100487 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200488 }
489
490 free(session);
491}
492
Michal Vasko086311b2016-01-08 09:53:11 +0100493static void
494add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
495{
496 if (*count == *size) {
497 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100498 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
499 if (!(*cpblts)) {
500 ERRMEM;
501 return;
502 }
Michal Vasko086311b2016-01-08 09:53:11 +0100503 }
504
505 if (capab) {
506 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
507 } else {
508 (*cpblts)[*count] = NULL;
509 }
510 ++(*count);
511}
512
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200513API const char **
514nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100515{
516 struct lyd_node *child, *child2, *yanglib;
517 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
518 const char **cpblts;
519 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100520 int size = 10, count, feat_count = 0, i, str_len;
Michal Vaskoc1119d82016-06-20 10:03:24 +0200521#define NC_CPBLT_BUF_LEN 512
522 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100523
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200524 if (!ctx) {
525 ERRARG("ctx");
526 return NULL;
527 }
528
Michal Vasko086311b2016-01-08 09:53:11 +0100529 yanglib = ly_ctx_info(ctx);
530 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200531 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100532 return NULL;
533 }
534
535 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100536 if (!cpblts) {
537 ERRMEM;
538 return NULL;
539 }
Michal Vasko086311b2016-01-08 09:53:11 +0100540 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
541 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
542 count = 2;
543
544 /* capabilities */
545
546 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
547 if (mod) {
548 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100549 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100550 }
551 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100552 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100553 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100554 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100555 }
556 }
557 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100558 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100559 }
560 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100561 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100562 }
563 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100564 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100565 }
566 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100567 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100568 }
569 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100570 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100571 }
572 }
573
574 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
575 if (mod) {
576 if (!server_opts.wd_basic_mode) {
577 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
578 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100579 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100580 switch (server_opts.wd_basic_mode) {
581 case NC_WD_ALL:
582 strcat(str, "?basic-mode=report-all");
583 break;
584 case NC_WD_TRIM:
585 strcat(str, "?basic-mode=trim");
586 break;
587 case NC_WD_EXPLICIT:
588 strcat(str, "?basic-mode=explicit");
589 break;
590 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100591 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100592 break;
593 }
594
595 if (server_opts.wd_also_supported) {
Michal Vaskoc1119d82016-06-20 10:03:24 +0200596 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100597 if (server_opts.wd_also_supported & NC_WD_ALL) {
598 strcat(str, "report-all,");
599 }
600 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
601 strcat(str, "report-all-tagged,");
602 }
603 if (server_opts.wd_also_supported & NC_WD_TRIM) {
604 strcat(str, "trim,");
605 }
606 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
607 strcat(str, "explicit,");
608 }
609 str[strlen(str) - 1] = '\0';
610
611 add_cpblt(ctx, str, &cpblts, &size, &count);
612 }
613 }
614 }
615
Michal Vasko1a38c862016-01-15 15:50:07 +0100616 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100617 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100618 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100619 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100620 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100621 }
622 }
623
624 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100625 LY_TREE_FOR(yanglib->child, child) {
626 if (!strcmp(child->schema->name, "module")) {
627 LY_TREE_FOR(child->child, child2) {
628 if (!strcmp(child2->schema->name, "namespace")) {
629 ns = (struct lyd_node_leaf_list *)child2;
630 } else if (!strcmp(child2->schema->name, "name")) {
631 name = (struct lyd_node_leaf_list *)child2;
632 } else if (!strcmp(child2->schema->name, "revision")) {
633 rev = (struct lyd_node_leaf_list *)child2;
634 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100635 features = nc_realloc(features, ++feat_count * sizeof *features);
636 if (!features) {
637 ERRMEM;
638 free(cpblts);
639 return NULL;
640 }
Michal Vasko086311b2016-01-08 09:53:11 +0100641 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
642 }
643 }
644
645 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100646 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100647 continue;
648 }
649
Radek Krejcidf7ba522016-03-01 16:05:25 +0100650 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vaskoc1119d82016-06-20 10:03:24 +0200651 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100652 if (feat_count) {
Michal Vaskoc1119d82016-06-20 10:03:24 +0200653 strcat(str, "&features=");
654 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100655 for (i = 0; i < feat_count; ++i) {
Michal Vaskoc1119d82016-06-20 10:03:24 +0200656 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100657 ERRINT;
658 break;
659 }
Michal Vasko086311b2016-01-08 09:53:11 +0100660 if (i) {
661 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100662 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100663 }
664 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100665 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100666 }
667 }
668
669 add_cpblt(ctx, str, &cpblts, &size, &count);
670
671 ns = NULL;
672 name = NULL;
673 rev = NULL;
674 free(features);
675 features = NULL;
676 feat_count = 0;
677 }
678 }
679
680 lyd_free(yanglib);
681
682 /* ending NULL capability */
683 add_cpblt(ctx, NULL, &cpblts, &size, &count);
684
685 return cpblts;
686}
687
Radek Krejci695d4fa2015-10-22 13:23:54 +0200688static int
689parse_cpblts(struct lyxml_elem *xml, const char ***list)
690{
691 struct lyxml_elem *cpblt;
692 int ver = -1;
693 int i = 0;
694
695 if (list) {
696 /* get the storage for server's capabilities */
697 LY_TREE_FOR(xml->child, cpblt) {
698 i++;
699 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100700 /* last item remains NULL */
701 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200702 if (!*list) {
703 ERRMEM;
704 return -1;
705 }
706 i = 0;
707 }
708
709 LY_TREE_FOR(xml->child, cpblt) {
710 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
711 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
712 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
713 return -1;
714 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
715 continue;
716 }
717
718 /* detect NETCONF version */
719 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
720 ver = 0;
721 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
722 ver = 1;
723 }
724
725 /* store capabilities */
726 if (list) {
727 (*list)[i] = cpblt->content;
728 cpblt->content = NULL;
729 i++;
730 }
731 }
732
733 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100734 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200735 }
736
737 return ver;
738}
739
740static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100741nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100742{
743 int r, i;
744 const char **cpblts;
745
Michal Vasko11d142a2016-01-19 15:58:24 +0100746 /* client side hello - send only NETCONF base capabilities */
747 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100748 if (!cpblts) {
749 ERRMEM;
750 return NC_MSG_ERROR;
751 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100752 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
753 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
754 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100755
Michal Vasko11d142a2016-01-19 15:58:24 +0100756 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100757
Michal Vasko086311b2016-01-08 09:53:11 +0100758 for (i = 0; cpblts[i]; ++i) {
759 lydict_remove(session->ctx, cpblts[i]);
760 }
761 free(cpblts);
762
763 if (r) {
764 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100765 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100766
767 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100768}
769
770static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100771nc_send_server_hello(struct nc_session *session)
772{
773 int r, i;
774 const char **cpblts;
775
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200776 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100777
778 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
779
Michal Vasko11d142a2016-01-19 15:58:24 +0100780 for (i = 0; cpblts[i]; ++i) {
781 lydict_remove(session->ctx, cpblts[i]);
782 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100783 free(cpblts);
784
785 if (r) {
786 return NC_MSG_ERROR;
787 }
788
789 return NC_MSG_HELLO;
790}
791
792static NC_MSG_TYPE
793nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200794{
795 struct lyxml_elem *xml = NULL, *node;
796 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
797 int ver = -1;
798 char *str;
799 long long int id;
800 int flag = 0;
801
Michal Vasko05ba9df2016-01-13 14:40:27 +0100802 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200803
804 switch(msgtype) {
805 case NC_MSG_HELLO:
806 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100807 LY_TREE_FOR(xml->child, node) {
808 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
809 continue;
810 } else if (!strcmp(node->name, "session-id")) {
811 if (!node->content || !strlen(node->content)) {
812 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200813 goto error;
814 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100815 str = NULL;
816 id = strtoll(node->content, &str, 10);
817 if (*str || id < 1 || id > UINT32_MAX) {
818 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200819 goto error;
820 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100821 session->id = (uint32_t)id;
822 continue;
823 } else if (strcmp(node->name, "capabilities")) {
824 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200825 goto error;
826 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100827
828 if (flag) {
829 /* multiple capabilities elements */
830 ERR("Invalid <hello> message (multiple <capabilities> elements).");
831 goto error;
832 }
833 flag = 1;
834
835 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
836 goto error;
837 }
838 session->version = ver;
839 }
840
841 if (!session->id) {
842 ERR("Missing <session-id> in server's <hello>.");
843 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200844 }
845 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200846 case NC_MSG_WOULDBLOCK:
847 ERR("Server's <hello> timeout elapsed.");
848 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200849 case NC_MSG_ERROR:
850 /* nothing special, just pass it out */
851 break;
852 default:
853 ERR("Unexpected message received instead of <hello>.");
854 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200855 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200856 }
857
858 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100859 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200860
861 return msgtype;
862
863error:
864 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100865 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200866
867 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200868}
869
Michal Vasko11d142a2016-01-19 15:58:24 +0100870static NC_MSG_TYPE
871nc_recv_server_hello(struct nc_session *session)
872{
873 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200874 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100875 int ver = -1;
876 int flag = 0;
877
Michal Vaskoadb850e2016-01-20 14:06:32 +0100878 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100879
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100880 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100881 case NC_MSG_HELLO:
882 /* get know NETCONF version */
883 LY_TREE_FOR(xml->child, node) {
884 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
885 continue;
886 } else if (strcmp(node->name, "capabilities")) {
887 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200888 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100889 goto cleanup;
890 }
891
892 if (flag) {
893 /* multiple capabilities elements */
894 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200895 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100896 goto cleanup;
897 }
898 flag = 1;
899
900 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200901 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100902 goto cleanup;
903 }
904 session->version = ver;
905 }
906 break;
907 case NC_MSG_ERROR:
908 /* nothing special, just pass it out */
909 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100910 case NC_MSG_WOULDBLOCK:
911 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100912 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100913 default:
914 ERR("Unexpected message received instead of <hello>.");
915 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200916 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100917 }
918
919cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100920 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100921
922 return msgtype;
923}
924
Michal Vasko71090fc2016-05-24 16:37:28 +0200925NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100926nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100927{
Michal Vasko086311b2016-01-08 09:53:11 +0100928 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100929
Michal Vasko11d142a2016-01-19 15:58:24 +0100930 if (session->side == NC_CLIENT) {
931 type = nc_send_client_hello(session);
932 } else {
933 type = nc_send_server_hello(session);
934 }
935
Michal Vasko086311b2016-01-08 09:53:11 +0100936 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200937 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100938 }
939
Michal Vasko11d142a2016-01-19 15:58:24 +0100940 if (session->side == NC_CLIENT) {
941 type = nc_recv_client_hello(session);
942 } else {
943 type = nc_recv_server_hello(session);
944 }
945
Michal Vasko71090fc2016-05-24 16:37:28 +0200946 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100947}
Michal Vasko086311b2016-01-08 09:53:11 +0100948
Radek Krejci53691be2016-02-22 13:58:37 +0100949#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100950
Michal Vasko8f0c0282016-02-29 10:17:14 +0100951static void
Michal Vasko086311b2016-01-08 09:53:11 +0100952nc_ssh_init(void)
953{
954 ssh_threads_set_callbacks(ssh_threads_get_pthread());
955 ssh_init();
956 ssh_set_log_level(verbose_level);
957}
958
Michal Vasko8f0c0282016-02-29 10:17:14 +0100959static void
Michal Vasko086311b2016-01-08 09:53:11 +0100960nc_ssh_destroy(void)
961{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100962 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100963 ENGINE_cleanup();
964 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100965 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100966 ssh_finalize();
967}
968
Radek Krejci53691be2016-02-22 13:58:37 +0100969#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100970
Radek Krejci53691be2016-02-22 13:58:37 +0100971#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100972
Michal Vaskof0c92c02016-01-29 09:41:45 +0100973struct CRYPTO_dynlock_value {
974 pthread_mutex_t lock;
975};
976
Michal Vaskof0c92c02016-01-29 09:41:45 +0100977static struct CRYPTO_dynlock_value *
978tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
979{
980 struct CRYPTO_dynlock_value *value;
981
982 value = malloc(sizeof *value);
983 if (!value) {
984 ERRMEM;
985 return NULL;
986 }
987 pthread_mutex_init(&value->lock, NULL);
988
989 return value;
990}
991
992static void
993tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
994{
995 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
996 * I found ignored this fact, what do I know... */
997 if (mode & CRYPTO_LOCK) {
998 pthread_mutex_lock(&l->lock);
999 } else {
1000 pthread_mutex_unlock(&l->lock);
1001 }
1002}
1003
1004static void
1005tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1006{
1007 pthread_mutex_destroy(&l->lock);
1008 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001009}
1010
Michal Vasko8f0c0282016-02-29 10:17:14 +01001011#endif /* NC_ENABLED_TLS */
1012
1013#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1014
1015static pthread_mutex_t *tls_locks;
1016
1017static void
1018tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1019{
1020 if (mode & CRYPTO_LOCK) {
1021 pthread_mutex_lock(tls_locks + n);
1022 } else {
1023 pthread_mutex_unlock(tls_locks + n);
1024 }
1025}
1026
1027static void
1028tls_thread_id_func(CRYPTO_THREADID *tid)
1029{
1030 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1031}
1032
1033static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001034nc_tls_init(void)
1035{
1036 int i;
1037
1038 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001039 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001040 SSL_library_init();
1041
1042 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001043 if (!tls_locks) {
1044 ERRMEM;
1045 return;
1046 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001047 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1048 pthread_mutex_init(tls_locks + i, NULL);
1049 }
1050
Michal Vaskof0c92c02016-01-29 09:41:45 +01001051 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001052 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001053
1054 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1055 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1056 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001057}
1058
Michal Vasko8f0c0282016-02-29 10:17:14 +01001059static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001060nc_tls_destroy(void)
1061{
1062 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001063
Michal Vasko8f0c0282016-02-29 10:17:14 +01001064 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001065 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001066 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001067 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001068 ERR_free_strings();
1069 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001070
Michal Vaskob6e37262016-02-25 14:49:00 +01001071 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001072 CRYPTO_set_locking_callback(NULL);
1073 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1074 pthread_mutex_destroy(tls_locks + i);
1075 }
1076 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001077
1078 CRYPTO_set_dynlock_create_callback(NULL);
1079 CRYPTO_set_dynlock_lock_callback(NULL);
1080 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001081}
1082
Michal Vasko8f0c0282016-02-29 10:17:14 +01001083#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001084
Radek Krejci53691be2016-02-22 13:58:37 +01001085#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001086
Michal Vasko8f0c0282016-02-29 10:17:14 +01001087static void
Michal Vasko5e228792016-02-03 15:30:24 +01001088nc_ssh_tls_init(void)
1089{
1090 SSL_load_error_strings();
1091 ERR_load_BIO_strings();
1092 SSL_library_init();
1093
1094 nc_ssh_init();
1095
1096 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1097 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1098 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1099}
1100
Michal Vasko8f0c0282016-02-29 10:17:14 +01001101static void
Michal Vasko5e228792016-02-03 15:30:24 +01001102nc_ssh_tls_destroy(void)
1103{
1104 ERR_free_strings();
1105 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1106
1107 nc_ssh_destroy();
1108
1109 CRYPTO_set_dynlock_create_callback(NULL);
1110 CRYPTO_set_dynlock_lock_callback(NULL);
1111 CRYPTO_set_dynlock_destroy_callback(NULL);
1112}
1113
Radek Krejci53691be2016-02-22 13:58:37 +01001114#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001115
1116#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1117
1118API void
1119nc_thread_destroy(void)
1120{
1121 CRYPTO_THREADID crypto_tid;
1122
1123 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1124 //CRYPTO_cleanup_all_ex_data();
1125
1126 CRYPTO_THREADID_current(&crypto_tid);
1127 ERR_remove_thread_state(&crypto_tid);
1128}
1129
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001130#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1131
1132void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001133nc_init(void)
1134{
1135#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1136 nc_ssh_tls_init();
1137#elif defined(NC_ENABLED_SSH)
1138 nc_ssh_init();
1139#elif defined(NC_ENABLED_TLS)
1140 nc_tls_init();
1141#endif
1142}
1143
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001144void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001145nc_destroy(void)
1146{
1147#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1148 nc_ssh_tls_destroy();
1149#elif defined(NC_ENABLED_SSH)
1150 nc_ssh_destroy();
1151#elif defined(NC_ENABLED_TLS)
1152 nc_tls_destroy();
1153#endif
1154}