blob: 17997823cbb731777e64ce9461b0d026b8542f8a [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
14
Radek Krejci206fcd62015-10-07 15:42:48 +020015#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010016#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010017#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020018#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020019#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010020#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020021#include <libyang/libyang.h>
22
Michal Vasko5e228792016-02-03 15:30:24 +010023#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010024#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010025#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010026
Radek Krejci53691be2016-02-22 13:58:37 +010027#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020028
Michal Vasko086311b2016-01-08 09:53:11 +010029# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Radek Krejci53691be2016-02-22 13:58:37 +010031#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020032
Radek Krejci53691be2016-02-22 13:58:37 +010033#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010034
Michal Vasko5e228792016-02-03 15:30:24 +010035# include <openssl/engine.h>
36# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010037# include <openssl/err.h>
38
Radek Krejci53691be2016-02-22 13:58:37 +010039#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010040
Michal Vasko086311b2016-01-08 09:53:11 +010041/* in seconds */
42#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020043
Michal Vasko05ba9df2016-01-13 14:40:27 +010044/* in milliseconds */
45#define NC_CLOSE_REPLY_TIMEOUT 200
46
Michal Vasko086311b2016-01-08 09:53:11 +010047extern struct nc_server_opts server_opts;
48
Radek Krejci7ac16052016-07-15 11:48:18 +020049int
50nc_gettimespec(struct timespec *ts)
51{
Michal Vasko0ef46df2016-09-21 14:03:18 +020052#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020053 return clock_gettime(CLOCK_REALTIME, ts);
54#else
55 int rc;
56 struct timeval tv;
57
58 rc = gettimeofday(&tv, NULL);
59 if (!rc) {
60 ts->tv_sec = (time_t)tv.tv_sec;
61 ts->tv_nsec = 1000L * (long)tv.tv_usec;
62 }
63 return rc;
64#endif
65}
66
Radek Krejci28472922016-07-15 11:51:16 +020067#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
68int
69pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
70{
71 int rc;
72 struct timespec cur, dur;
73
74 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
75 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
76 nc_gettimespec(&cur);
77
78 if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
79 break;
80 }
81
82 dur.tv_sec = abstime->tv_sec - cur.tv_sec;
83 dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
84 if (dur.tv_nsec < 0) {
85 dur.tv_sec--;
86 dur.tv_nsec += 1000000000;
87 }
88
89 if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
90 dur.tv_sec = 0;
91 dur.tv_nsec = 5000000;
92 }
93
94 nanosleep(&dur, NULL);
95 }
96
97 return rc;
98}
99#endif
100
Michal Vasko96164bf2016-01-21 15:41:58 +0100101/*
102 * @return 1 - success
103 * 0 - timeout
104 * -1 - error
105 */
106int
Michal Vasko62be1ce2016-03-03 13:24:52 +0100107nc_timedlock(pthread_mutex_t *lock, int timeout)
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 */
Radek Krejcida8f58d2016-03-03 13:10:21 +0100130 ERR("Mutex lock failed (%s).", 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 Vasko9e99f012016-03-03 13:25:20 +0100297 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000);
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;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200517 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 +0100518 const char **cpblts;
519 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200520 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Michal Vasko2e47ef92016-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 Vasko2e47ef92016-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) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200626 if (!module_set_id) {
627 if (strcmp(child->prev->schema->name, "module-set-id")) {
628 ERRINT;
629 return NULL;
630 }
631 module_set_id = (struct lyd_node_leaf_list *)child->prev;
632 }
Michal Vasko086311b2016-01-08 09:53:11 +0100633 if (!strcmp(child->schema->name, "module")) {
634 LY_TREE_FOR(child->child, child2) {
635 if (!strcmp(child2->schema->name, "namespace")) {
636 ns = (struct lyd_node_leaf_list *)child2;
637 } else if (!strcmp(child2->schema->name, "name")) {
638 name = (struct lyd_node_leaf_list *)child2;
639 } else if (!strcmp(child2->schema->name, "revision")) {
640 rev = (struct lyd_node_leaf_list *)child2;
641 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100642 features = nc_realloc(features, ++feat_count * sizeof *features);
643 if (!features) {
644 ERRMEM;
645 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200646 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100647 return NULL;
648 }
Michal Vasko086311b2016-01-08 09:53:11 +0100649 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200650 } else if (!strcmp(child2->schema->name, "deviation")) {
651 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
652 if (!deviations) {
653 ERRMEM;
654 free(cpblts);
655 free(features);
656 return NULL;
657 }
Michal Vasko086311b2016-01-08 09:53:11 +0100658 }
659 }
660
661 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100662 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100663 continue;
664 }
665
Radek Krejcidf7ba522016-03-01 16:05:25 +0100666 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200667 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100668 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200669 strcat(str, "&features=");
670 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100671 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200672 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100673 ERRINT;
674 break;
675 }
Michal Vasko086311b2016-01-08 09:53:11 +0100676 if (i) {
677 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100678 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100679 }
680 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100681 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100682 }
683 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200684 if (dev_count) {
685 strcat(str, "&deviations=");
686 str_len += 12;
687 for (i = 0; i < dev_count; ++i) {
688 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
689 ERRINT;
690 break;
691 }
692 if (i) {
693 strcat(str, ",");
694 ++str_len;
695 }
696 strcat(str, deviations[i]->value_str);
697 str_len += strlen(deviations[i]->value_str);
698 }
699 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200700 if (!strcmp(name->value_str, "ietf-yang-library")) {
701 str_len += sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
702 }
Michal Vasko086311b2016-01-08 09:53:11 +0100703
704 add_cpblt(ctx, str, &cpblts, &size, &count);
705
706 ns = NULL;
707 name = NULL;
708 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200709 if (features || feat_count) {
710 free(features);
711 features = NULL;
712 feat_count = 0;
713 }
714 if (deviations || dev_count) {
715 free(deviations);
716 deviations = NULL;
717 dev_count = 0;
718 }
Michal Vasko086311b2016-01-08 09:53:11 +0100719 }
720 }
721
722 lyd_free(yanglib);
723
724 /* ending NULL capability */
725 add_cpblt(ctx, NULL, &cpblts, &size, &count);
726
727 return cpblts;
728}
729
Radek Krejci695d4fa2015-10-22 13:23:54 +0200730static int
731parse_cpblts(struct lyxml_elem *xml, const char ***list)
732{
733 struct lyxml_elem *cpblt;
734 int ver = -1;
735 int i = 0;
736
737 if (list) {
738 /* get the storage for server's capabilities */
739 LY_TREE_FOR(xml->child, cpblt) {
740 i++;
741 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100742 /* last item remains NULL */
743 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200744 if (!*list) {
745 ERRMEM;
746 return -1;
747 }
748 i = 0;
749 }
750
751 LY_TREE_FOR(xml->child, cpblt) {
752 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
753 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
754 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
755 return -1;
756 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
757 continue;
758 }
759
760 /* detect NETCONF version */
761 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
762 ver = 0;
763 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
764 ver = 1;
765 }
766
767 /* store capabilities */
768 if (list) {
769 (*list)[i] = cpblt->content;
770 cpblt->content = NULL;
771 i++;
772 }
773 }
774
775 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100776 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200777 }
778
779 return ver;
780}
781
782static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100783nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100784{
785 int r, i;
786 const char **cpblts;
787
Michal Vasko11d142a2016-01-19 15:58:24 +0100788 /* client side hello - send only NETCONF base capabilities */
789 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100790 if (!cpblts) {
791 ERRMEM;
792 return NC_MSG_ERROR;
793 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100794 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
795 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
796 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100797
Michal Vasko11d142a2016-01-19 15:58:24 +0100798 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100799
Michal Vasko086311b2016-01-08 09:53:11 +0100800 for (i = 0; cpblts[i]; ++i) {
801 lydict_remove(session->ctx, cpblts[i]);
802 }
803 free(cpblts);
804
805 if (r) {
806 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100807 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100808
809 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100810}
811
812static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100813nc_send_server_hello(struct nc_session *session)
814{
815 int r, i;
816 const char **cpblts;
817
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200818 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100819
820 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
821
Michal Vasko11d142a2016-01-19 15:58:24 +0100822 for (i = 0; cpblts[i]; ++i) {
823 lydict_remove(session->ctx, cpblts[i]);
824 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100825 free(cpblts);
826
827 if (r) {
828 return NC_MSG_ERROR;
829 }
830
831 return NC_MSG_HELLO;
832}
833
834static NC_MSG_TYPE
835nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200836{
837 struct lyxml_elem *xml = NULL, *node;
838 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
839 int ver = -1;
840 char *str;
841 long long int id;
842 int flag = 0;
843
Michal Vasko05ba9df2016-01-13 14:40:27 +0100844 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200845
846 switch(msgtype) {
847 case NC_MSG_HELLO:
848 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100849 LY_TREE_FOR(xml->child, node) {
850 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
851 continue;
852 } else if (!strcmp(node->name, "session-id")) {
853 if (!node->content || !strlen(node->content)) {
854 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200855 goto error;
856 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100857 str = NULL;
858 id = strtoll(node->content, &str, 10);
859 if (*str || id < 1 || id > UINT32_MAX) {
860 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200861 goto error;
862 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100863 session->id = (uint32_t)id;
864 continue;
865 } else if (strcmp(node->name, "capabilities")) {
866 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200867 goto error;
868 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100869
870 if (flag) {
871 /* multiple capabilities elements */
872 ERR("Invalid <hello> message (multiple <capabilities> elements).");
873 goto error;
874 }
875 flag = 1;
876
877 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
878 goto error;
879 }
880 session->version = ver;
881 }
882
883 if (!session->id) {
884 ERR("Missing <session-id> in server's <hello>.");
885 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200886 }
887 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200888 case NC_MSG_WOULDBLOCK:
889 ERR("Server's <hello> timeout elapsed.");
890 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200891 case NC_MSG_ERROR:
892 /* nothing special, just pass it out */
893 break;
894 default:
895 ERR("Unexpected message received instead of <hello>.");
896 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200897 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200898 }
899
900 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100901 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200902
903 return msgtype;
904
905error:
906 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100907 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200908
909 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200910}
911
Michal Vasko11d142a2016-01-19 15:58:24 +0100912static NC_MSG_TYPE
913nc_recv_server_hello(struct nc_session *session)
914{
915 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200916 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100917 int ver = -1;
918 int flag = 0;
919
Michal Vaskoadb850e2016-01-20 14:06:32 +0100920 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100921
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100922 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100923 case NC_MSG_HELLO:
924 /* get know NETCONF version */
925 LY_TREE_FOR(xml->child, node) {
926 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
927 continue;
928 } else if (strcmp(node->name, "capabilities")) {
929 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200930 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100931 goto cleanup;
932 }
933
934 if (flag) {
935 /* multiple capabilities elements */
936 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200937 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100938 goto cleanup;
939 }
940 flag = 1;
941
942 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200943 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100944 goto cleanup;
945 }
946 session->version = ver;
947 }
948 break;
949 case NC_MSG_ERROR:
950 /* nothing special, just pass it out */
951 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100952 case NC_MSG_WOULDBLOCK:
953 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100954 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100955 default:
956 ERR("Unexpected message received instead of <hello>.");
957 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200958 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100959 }
960
961cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100962 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100963
964 return msgtype;
965}
966
Michal Vasko71090fc2016-05-24 16:37:28 +0200967NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100968nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100969{
Michal Vasko086311b2016-01-08 09:53:11 +0100970 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100971
Michal Vasko11d142a2016-01-19 15:58:24 +0100972 if (session->side == NC_CLIENT) {
973 type = nc_send_client_hello(session);
974 } else {
975 type = nc_send_server_hello(session);
976 }
977
Michal Vasko086311b2016-01-08 09:53:11 +0100978 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200979 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100980 }
981
Michal Vasko11d142a2016-01-19 15:58:24 +0100982 if (session->side == NC_CLIENT) {
983 type = nc_recv_client_hello(session);
984 } else {
985 type = nc_recv_server_hello(session);
986 }
987
Michal Vasko71090fc2016-05-24 16:37:28 +0200988 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100989}
Michal Vasko086311b2016-01-08 09:53:11 +0100990
Radek Krejci53691be2016-02-22 13:58:37 +0100991#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100992
Michal Vasko8f0c0282016-02-29 10:17:14 +0100993static void
Michal Vasko086311b2016-01-08 09:53:11 +0100994nc_ssh_init(void)
995{
996 ssh_threads_set_callbacks(ssh_threads_get_pthread());
997 ssh_init();
998 ssh_set_log_level(verbose_level);
999}
1000
Michal Vasko8f0c0282016-02-29 10:17:14 +01001001static void
Michal Vasko086311b2016-01-08 09:53:11 +01001002nc_ssh_destroy(void)
1003{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001004 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001005 ENGINE_cleanup();
1006 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001007 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001008 ssh_finalize();
1009}
1010
Radek Krejci53691be2016-02-22 13:58:37 +01001011#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001012
Radek Krejci53691be2016-02-22 13:58:37 +01001013#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001014
Michal Vaskof0c92c02016-01-29 09:41:45 +01001015struct CRYPTO_dynlock_value {
1016 pthread_mutex_t lock;
1017};
1018
Michal Vaskof0c92c02016-01-29 09:41:45 +01001019static struct CRYPTO_dynlock_value *
1020tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1021{
1022 struct CRYPTO_dynlock_value *value;
1023
1024 value = malloc(sizeof *value);
1025 if (!value) {
1026 ERRMEM;
1027 return NULL;
1028 }
1029 pthread_mutex_init(&value->lock, NULL);
1030
1031 return value;
1032}
1033
1034static void
1035tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1036{
1037 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1038 * I found ignored this fact, what do I know... */
1039 if (mode & CRYPTO_LOCK) {
1040 pthread_mutex_lock(&l->lock);
1041 } else {
1042 pthread_mutex_unlock(&l->lock);
1043 }
1044}
1045
1046static void
1047tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1048{
1049 pthread_mutex_destroy(&l->lock);
1050 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001051}
1052
Michal Vasko8f0c0282016-02-29 10:17:14 +01001053#endif /* NC_ENABLED_TLS */
1054
1055#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1056
1057static pthread_mutex_t *tls_locks;
1058
1059static void
1060tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1061{
1062 if (mode & CRYPTO_LOCK) {
1063 pthread_mutex_lock(tls_locks + n);
1064 } else {
1065 pthread_mutex_unlock(tls_locks + n);
1066 }
1067}
1068
1069static void
1070tls_thread_id_func(CRYPTO_THREADID *tid)
1071{
1072 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1073}
1074
1075static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001076nc_tls_init(void)
1077{
1078 int i;
1079
1080 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001081 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001082 SSL_library_init();
1083
1084 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001085 if (!tls_locks) {
1086 ERRMEM;
1087 return;
1088 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001089 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1090 pthread_mutex_init(tls_locks + i, NULL);
1091 }
1092
Michal Vaskof0c92c02016-01-29 09:41:45 +01001093 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001094 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001095
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);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001099}
1100
Michal Vasko8f0c0282016-02-29 10:17:14 +01001101static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001102nc_tls_destroy(void)
1103{
1104 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001105
Michal Vasko8f0c0282016-02-29 10:17:14 +01001106 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001107 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001108 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001109 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001110 ERR_free_strings();
1111 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001112
Michal Vaskob6e37262016-02-25 14:49:00 +01001113 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001114 CRYPTO_set_locking_callback(NULL);
1115 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1116 pthread_mutex_destroy(tls_locks + i);
1117 }
1118 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001119
1120 CRYPTO_set_dynlock_create_callback(NULL);
1121 CRYPTO_set_dynlock_lock_callback(NULL);
1122 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001123}
1124
Michal Vasko8f0c0282016-02-29 10:17:14 +01001125#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001126
Radek Krejci53691be2016-02-22 13:58:37 +01001127#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001128
Michal Vasko8f0c0282016-02-29 10:17:14 +01001129static void
Michal Vasko5e228792016-02-03 15:30:24 +01001130nc_ssh_tls_init(void)
1131{
1132 SSL_load_error_strings();
1133 ERR_load_BIO_strings();
1134 SSL_library_init();
1135
1136 nc_ssh_init();
1137
1138 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1139 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1140 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1141}
1142
Michal Vasko8f0c0282016-02-29 10:17:14 +01001143static void
Michal Vasko5e228792016-02-03 15:30:24 +01001144nc_ssh_tls_destroy(void)
1145{
1146 ERR_free_strings();
1147 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1148
1149 nc_ssh_destroy();
1150
1151 CRYPTO_set_dynlock_create_callback(NULL);
1152 CRYPTO_set_dynlock_lock_callback(NULL);
1153 CRYPTO_set_dynlock_destroy_callback(NULL);
1154}
1155
Radek Krejci53691be2016-02-22 13:58:37 +01001156#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001157
1158#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1159
1160API void
1161nc_thread_destroy(void)
1162{
1163 CRYPTO_THREADID crypto_tid;
1164
1165 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1166 //CRYPTO_cleanup_all_ex_data();
1167
1168 CRYPTO_THREADID_current(&crypto_tid);
1169 ERR_remove_thread_state(&crypto_tid);
1170}
1171
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001172#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1173
1174void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001175nc_init(void)
1176{
1177#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1178 nc_ssh_tls_init();
1179#elif defined(NC_ENABLED_SSH)
1180 nc_ssh_init();
1181#elif defined(NC_ENABLED_TLS)
1182 nc_tls_init();
1183#endif
1184}
1185
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001186void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001187nc_destroy(void)
1188{
1189#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1190 nc_ssh_tls_destroy();
1191#elif defined(NC_ENABLED_SSH)
1192 nc_ssh_destroy();
1193#elif defined(NC_ENABLED_TLS)
1194 nc_tls_destroy();
1195#endif
1196}