blob: 8923550445767cf6158ffa5ea983d573f83ebe33 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
14
Radek Krejci206fcd62015-10-07 15:42:48 +020015#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010016#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010017#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020018#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020019#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010020#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020021#include <libyang/libyang.h>
22
Michal Vasko5e228792016-02-03 15:30:24 +010023#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010024#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010025#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010026
Radek Krejci53691be2016-02-22 13:58:37 +010027#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020028
Michal Vasko086311b2016-01-08 09:53:11 +010029# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Radek Krejci53691be2016-02-22 13:58:37 +010031#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020032
Radek Krejci53691be2016-02-22 13:58:37 +010033#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010034
Michal Vasko5e228792016-02-03 15:30:24 +010035# include <openssl/engine.h>
36# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010037# include <openssl/err.h>
38
Radek Krejci53691be2016-02-22 13:58:37 +010039#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010040
Michal Vasko086311b2016-01-08 09:53:11 +010041/* in seconds */
42#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020043
Michal Vasko05ba9df2016-01-13 14:40:27 +010044/* in milliseconds */
45#define NC_CLOSE_REPLY_TIMEOUT 200
46
Michal Vasko086311b2016-01-08 09:53:11 +010047extern struct nc_server_opts server_opts;
48
Radek Krejci7ac16052016-07-15 11:48:18 +020049int
50nc_gettimespec(struct timespec *ts)
51{
Michal Vasko0ef46df2016-09-21 14:03:18 +020052#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020053 return clock_gettime(CLOCK_REALTIME, ts);
54#else
55 int rc;
56 struct timeval tv;
57
58 rc = gettimeofday(&tv, NULL);
59 if (!rc) {
60 ts->tv_sec = (time_t)tv.tv_sec;
61 ts->tv_nsec = 1000L * (long)tv.tv_usec;
62 }
63 return rc;
64#endif
65}
66
Radek Krejci28472922016-07-15 11:51:16 +020067#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
68int
69pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
70{
71 int rc;
72 struct timespec cur, dur;
73
74 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
75 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
76 nc_gettimespec(&cur);
77
78 if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
79 break;
80 }
81
82 dur.tv_sec = abstime->tv_sec - cur.tv_sec;
83 dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
84 if (dur.tv_nsec < 0) {
85 dur.tv_sec--;
86 dur.tv_nsec += 1000000000;
87 }
88
89 if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
90 dur.tv_sec = 0;
91 dur.tv_nsec = 5000000;
92 }
93
94 nanosleep(&dur, NULL);
95 }
96
97 return rc;
98}
99#endif
100
Michal Vasko96164bf2016-01-21 15:41:58 +0100101/*
102 * @return 1 - success
103 * 0 - timeout
104 * -1 - error
105 */
106int
Michal vasko50cc94f2016-10-04 13:46:20 +0200107nc_timedlock(pthread_mutex_t *lock, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100108{
109 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100110 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100111
112 if (timeout > 0) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200113 nc_gettimespec(&ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100114
Michal Vasko96164bf2016-01-21 15:41:58 +0100115 ts_timeout.tv_sec += timeout / 1000;
116 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
117
118 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100119 } else if (!timeout) {
120 ret = pthread_mutex_trylock(lock);
Michal vasko2f8e4b52016-10-05 13:04:11 +0200121 if (ret == EBUSY) {
122 /* equivalent in this case */
123 ret = ETIMEDOUT;
124 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100125 } else { /* timeout == -1 */
126 ret = pthread_mutex_lock(lock);
127 }
128
129 if (ret == ETIMEDOUT) {
130 /* timeout */
131 return 0;
132 } else if (ret) {
133 /* error */
Michal vasko50cc94f2016-10-04 13:46:20 +0200134 ERR("Mutex lock failed (%s, %s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100135 return -1;
136 }
137
138 /* ok */
139 return 1;
140}
141
Michal Vasko8dadf782016-01-15 10:29:36 +0100142API NC_STATUS
143nc_session_get_status(const struct nc_session *session)
144{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100145 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200146 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100147 return 0;
148 }
149
Michal Vasko8dadf782016-01-15 10:29:36 +0100150 return session->status;
151}
152
153API uint32_t
154nc_session_get_id(const struct nc_session *session)
155{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100156 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200157 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100158 return 0;
159 }
160
Michal Vasko8dadf782016-01-15 10:29:36 +0100161 return session->id;
162}
163
Michal Vasko174fe8e2016-02-17 15:38:09 +0100164API int
165nc_session_get_version(const struct nc_session *session)
166{
167 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200168 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100169 return -1;
170 }
171
172 return (session->version == NC_VERSION_10 ? 0 : 1);
173}
174
Michal Vasko8dadf782016-01-15 10:29:36 +0100175API NC_TRANSPORT_IMPL
176nc_session_get_ti(const struct nc_session *session)
177{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100178 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200179 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100180 return 0;
181 }
182
Michal Vasko8dadf782016-01-15 10:29:36 +0100183 return session->ti_type;
184}
185
186API const char *
187nc_session_get_username(const struct nc_session *session)
188{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100189 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200190 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100191 return NULL;
192 }
193
Michal Vasko8dadf782016-01-15 10:29:36 +0100194 return session->username;
195}
196
197API const char *
198nc_session_get_host(const struct nc_session *session)
199{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100200 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200201 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100202 return NULL;
203 }
204
Michal Vasko8dadf782016-01-15 10:29:36 +0100205 return session->host;
206}
207
208API uint16_t
209nc_session_get_port(const struct nc_session *session)
210{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100211 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200212 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100213 return 0;
214 }
215
Michal Vasko8dadf782016-01-15 10:29:36 +0100216 return session->port;
217}
218
Michal Vasko9a25e932016-02-01 10:36:42 +0100219API struct ly_ctx *
220nc_session_get_ctx(const struct nc_session *session)
221{
222 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200223 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100224 return NULL;
225 }
226
227 return session->ctx;
228}
229
Michal Vasko2cc4c682016-03-01 09:16:48 +0100230API void
231nc_session_set_data(struct nc_session *session, void *data)
232{
233 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200234 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100235 return;
236 }
237
238 session->data = data;
239}
240
241API void *
242nc_session_get_data(const struct nc_session *session)
243{
244 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200245 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100246 return NULL;
247 }
248
249 return session->data;
250}
251
Michal Vasko086311b2016-01-08 09:53:11 +0100252NC_MSG_TYPE
253nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200254{
Michal Vasko086311b2016-01-08 09:53:11 +0100255 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200256
Michal Vasko086311b2016-01-08 09:53:11 +0100257 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100258 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
259 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100260 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100261 }
262
Michal Vasko086311b2016-01-08 09:53:11 +0100263 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
264
265 if (r) {
266 return NC_MSG_ERROR;
267 }
268
269 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100270}
271
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100273nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200274{
Michal Vasko9e99f012016-03-03 13:25:20 +0100275 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100276 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100277 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100278 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100279 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100280 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100281 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200282 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100283 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200284 void *p;
285
Michal Vasko428087d2016-01-14 16:04:28 +0100286 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200287 return;
288 }
289
Michal Vasko86d357c2016-03-11 13:46:38 +0100290 /* stop notifications loop if any */
291 if (session->ntf_tid) {
292 tid = *session->ntf_tid;
293 free((pthread_t *)session->ntf_tid);
294 session->ntf_tid = NULL;
295 /* the thread now knows it should quit */
296
297 pthread_join(tid, NULL);
298 }
299
Michal Vaskoadd4c792015-10-26 15:36:58 +0100300 if (session->ti_lock) {
Michal vasko50cc94f2016-10-04 13:46:20 +0200301 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100302 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100303 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100304 } else if (!r) {
305 /* we failed to lock it, too bad */
306 locked = 0;
307 } else {
308 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100309 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200310 } else {
311 ERRINT;
312 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200313 }
314
Michal Vasko9e99f012016-03-03 13:25:20 +0100315 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200316 /* cleanup message queues */
317 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100318 for (contiter = session->notifs; contiter; ) {
319 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200320
Michal Vaskoad611702015-12-03 13:41:51 +0100321 p = contiter;
322 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200323 free(p);
324 }
325
326 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100327 for (contiter = session->replies; contiter; ) {
328 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200329
Michal Vaskoad611702015-12-03 13:41:51 +0100330 p = contiter;
331 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200332 free(p);
333 }
334
335 /* send closing info to the other side */
336 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
337 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100338 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200339 } else {
340 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100341 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200342 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100343 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100344 case NC_MSG_REPLY:
345 LY_TREE_FOR(rpl->child, child) {
346 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
347 break;
348 }
349 }
350 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100351 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100352 }
Michal Vaskoad611702015-12-03 13:41:51 +0100353 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100354 break;
355 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100356 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100357 break;
358 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100359 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100360 break;
361 default:
362 /* cannot happen */
363 break;
364 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200365 }
366
367 /* list of server's capabilities */
368 if (session->cpblts) {
369 for (i = 0; session->cpblts[i]; i++) {
370 lydict_remove(session->ctx, session->cpblts[i]);
371 }
372 free(session->cpblts);
373 }
374 }
375
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100376 if (session->data && data_free) {
377 data_free(session->data);
378 }
379
Michal Vasko86d357c2016-03-11 13:46:38 +0100380 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200381 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100382 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200383
384 /* transport implementation cleanup */
385 switch (session->ti_type) {
386 case NC_TI_FD:
387 /* nothing needed - file descriptors were provided by caller,
388 * so it is up to the caller to close them correctly
389 * TODO use callbacks
390 */
Michal Vasko3512e402016-01-28 16:22:34 +0100391 /* just to avoid compiler warning */
392 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100393 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200394 break;
395
Radek Krejci53691be2016-02-22 13:58:37 +0100396#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200397 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100398 if (connected) {
399 ssh_channel_free(session->ti.libssh.channel);
400 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200401 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
402 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
403 * it.
404 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100405 multisession = 0;
406 if (session->ti.libssh.next) {
407 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
408 if (siter->status != NC_STATUS_STARTING) {
409 multisession = 1;
410 break;
411 }
412 }
413 }
414
415 if (!multisession) {
416 /* it's not multisession yet, but we still need to free the starting sessions */
417 if (session->ti.libssh.next) {
418 do {
419 siter = session->ti.libssh.next;
420 session->ti.libssh.next = siter->ti.libssh.next;
421
422 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100423 lydict_remove(session->ctx, session->username);
424 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100425 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100426 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100427 }
428
429 free(siter);
430 } while (session->ti.libssh.next != session);
431 }
Michal Vasko428087d2016-01-14 16:04:28 +0100432 if (connected) {
433 ssh_disconnect(session->ti.libssh.session);
434 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200435 ssh_free(session->ti.libssh.session);
436 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200437 /* remove the session from the list */
438 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100439 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200440 /* there will be only one session */
441 siter->ti.libssh.next = NULL;
442 } else {
443 /* there are still multiple sessions, keep the ring list */
444 siter->ti.libssh.next = session->ti.libssh.next;
445 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100446 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
447 if (session->flags & NC_SESSION_SSH_MSG_CB) {
448 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
449 if (siter->ti.libssh.next == session) {
450 ERRINT;
451 break;
452 }
453 }
454 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
455 siter->flags |= NC_SESSION_SSH_MSG_CB;
456 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200457 }
458 break;
459#endif
460
Radek Krejci53691be2016-02-22 13:58:37 +0100461#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200462 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100463 if (connected) {
464 SSL_shutdown(session->ti.tls);
465 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200466 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100467
468 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200469 break;
470#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100471 case NC_TI_NONE:
472 ERRINT;
473 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200474 }
Michal Vasko428087d2016-01-14 16:04:28 +0100475
Radek Krejciac6d3472015-10-22 15:47:18 +0200476 lydict_remove(session->ctx, session->username);
477 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200478
479 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100480 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100481 if (locked) {
482 pthread_mutex_unlock(session->ti_lock);
483 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100484 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100485 pthread_mutex_destroy(session->ti_lock);
486 free(session->ti_lock);
487 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200488 }
489
490 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100491 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200492 }
493
494 free(session);
495}
496
Michal Vasko086311b2016-01-08 09:53:11 +0100497static void
498add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
499{
500 if (*count == *size) {
501 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100502 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
503 if (!(*cpblts)) {
504 ERRMEM;
505 return;
506 }
Michal Vasko086311b2016-01-08 09:53:11 +0100507 }
508
509 if (capab) {
510 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
511 } else {
512 (*cpblts)[*count] = NULL;
513 }
514 ++(*count);
515}
516
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200517API const char **
518nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100519{
520 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200521 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 +0100522 const char **cpblts;
523 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200524 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200525#define NC_CPBLT_BUF_LEN 512
526 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100527
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200528 if (!ctx) {
529 ERRARG("ctx");
530 return NULL;
531 }
532
Michal Vasko086311b2016-01-08 09:53:11 +0100533 yanglib = ly_ctx_info(ctx);
534 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200535 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100536 return NULL;
537 }
538
539 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100540 if (!cpblts) {
541 ERRMEM;
542 return NULL;
543 }
Michal Vasko086311b2016-01-08 09:53:11 +0100544 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
545 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
546 count = 2;
547
548 /* capabilities */
549
550 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
551 if (mod) {
552 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100553 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100554 }
555 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100556 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100557 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100558 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100559 }
560 }
561 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100562 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100563 }
564 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100565 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100566 }
567 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100568 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100569 }
570 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100571 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100572 }
573 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100574 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100575 }
576 }
577
578 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
579 if (mod) {
580 if (!server_opts.wd_basic_mode) {
581 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
582 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100583 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100584 switch (server_opts.wd_basic_mode) {
585 case NC_WD_ALL:
586 strcat(str, "?basic-mode=report-all");
587 break;
588 case NC_WD_TRIM:
589 strcat(str, "?basic-mode=trim");
590 break;
591 case NC_WD_EXPLICIT:
592 strcat(str, "?basic-mode=explicit");
593 break;
594 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100595 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100596 break;
597 }
598
599 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200600 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100601 if (server_opts.wd_also_supported & NC_WD_ALL) {
602 strcat(str, "report-all,");
603 }
604 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
605 strcat(str, "report-all-tagged,");
606 }
607 if (server_opts.wd_also_supported & NC_WD_TRIM) {
608 strcat(str, "trim,");
609 }
610 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
611 strcat(str, "explicit,");
612 }
613 str[strlen(str) - 1] = '\0';
614
615 add_cpblt(ctx, str, &cpblts, &size, &count);
616 }
617 }
618 }
619
Michal Vasko1a38c862016-01-15 15:50:07 +0100620 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100621 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100622 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100623 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100624 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100625 }
626 }
627
628 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100629 LY_TREE_FOR(yanglib->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200630 if (!module_set_id) {
631 if (strcmp(child->prev->schema->name, "module-set-id")) {
632 ERRINT;
633 return NULL;
634 }
635 module_set_id = (struct lyd_node_leaf_list *)child->prev;
636 }
Michal Vasko086311b2016-01-08 09:53:11 +0100637 if (!strcmp(child->schema->name, "module")) {
638 LY_TREE_FOR(child->child, child2) {
639 if (!strcmp(child2->schema->name, "namespace")) {
640 ns = (struct lyd_node_leaf_list *)child2;
641 } else if (!strcmp(child2->schema->name, "name")) {
642 name = (struct lyd_node_leaf_list *)child2;
643 } else if (!strcmp(child2->schema->name, "revision")) {
644 rev = (struct lyd_node_leaf_list *)child2;
645 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100646 features = nc_realloc(features, ++feat_count * sizeof *features);
647 if (!features) {
648 ERRMEM;
649 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200650 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100651 return NULL;
652 }
Michal Vasko086311b2016-01-08 09:53:11 +0100653 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200654 } else if (!strcmp(child2->schema->name, "deviation")) {
655 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
656 if (!deviations) {
657 ERRMEM;
658 free(cpblts);
659 free(features);
660 return NULL;
661 }
Michal Vasko086311b2016-01-08 09:53:11 +0100662 }
663 }
664
665 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100666 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100667 continue;
668 }
669
Radek Krejcidf7ba522016-03-01 16:05:25 +0100670 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200671 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100672 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200673 strcat(str, "&features=");
674 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100675 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200676 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100677 ERRINT;
678 break;
679 }
Michal Vasko086311b2016-01-08 09:53:11 +0100680 if (i) {
681 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100682 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100683 }
684 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100685 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100686 }
687 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200688 if (dev_count) {
689 strcat(str, "&deviations=");
690 str_len += 12;
691 for (i = 0; i < dev_count; ++i) {
692 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
693 ERRINT;
694 break;
695 }
696 if (i) {
697 strcat(str, ",");
698 ++str_len;
699 }
700 strcat(str, deviations[i]->value_str);
701 str_len += strlen(deviations[i]->value_str);
702 }
703 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200704 if (!strcmp(name->value_str, "ietf-yang-library")) {
705 str_len += sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
706 }
Michal Vasko086311b2016-01-08 09:53:11 +0100707
708 add_cpblt(ctx, str, &cpblts, &size, &count);
709
710 ns = NULL;
711 name = NULL;
712 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200713 if (features || feat_count) {
714 free(features);
715 features = NULL;
716 feat_count = 0;
717 }
718 if (deviations || dev_count) {
719 free(deviations);
720 deviations = NULL;
721 dev_count = 0;
722 }
Michal Vasko086311b2016-01-08 09:53:11 +0100723 }
724 }
725
726 lyd_free(yanglib);
727
728 /* ending NULL capability */
729 add_cpblt(ctx, NULL, &cpblts, &size, &count);
730
731 return cpblts;
732}
733
Radek Krejci695d4fa2015-10-22 13:23:54 +0200734static int
735parse_cpblts(struct lyxml_elem *xml, const char ***list)
736{
737 struct lyxml_elem *cpblt;
738 int ver = -1;
739 int i = 0;
740
741 if (list) {
742 /* get the storage for server's capabilities */
743 LY_TREE_FOR(xml->child, cpblt) {
744 i++;
745 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100746 /* last item remains NULL */
747 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200748 if (!*list) {
749 ERRMEM;
750 return -1;
751 }
752 i = 0;
753 }
754
755 LY_TREE_FOR(xml->child, cpblt) {
756 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
757 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
758 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
759 return -1;
760 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
761 continue;
762 }
763
764 /* detect NETCONF version */
765 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
766 ver = 0;
767 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
768 ver = 1;
769 }
770
771 /* store capabilities */
772 if (list) {
773 (*list)[i] = cpblt->content;
774 cpblt->content = NULL;
775 i++;
776 }
777 }
778
779 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100780 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200781 }
782
783 return ver;
784}
785
786static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100787nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100788{
789 int r, i;
790 const char **cpblts;
791
Michal Vasko11d142a2016-01-19 15:58:24 +0100792 /* client side hello - send only NETCONF base capabilities */
793 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100794 if (!cpblts) {
795 ERRMEM;
796 return NC_MSG_ERROR;
797 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100798 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
799 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
800 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100801
Michal Vasko11d142a2016-01-19 15:58:24 +0100802 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100803
Michal Vasko086311b2016-01-08 09:53:11 +0100804 for (i = 0; cpblts[i]; ++i) {
805 lydict_remove(session->ctx, cpblts[i]);
806 }
807 free(cpblts);
808
809 if (r) {
810 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100811 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100812
813 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100814}
815
816static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100817nc_send_server_hello(struct nc_session *session)
818{
819 int r, i;
820 const char **cpblts;
821
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200822 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100823
824 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
825
Michal Vasko11d142a2016-01-19 15:58:24 +0100826 for (i = 0; cpblts[i]; ++i) {
827 lydict_remove(session->ctx, cpblts[i]);
828 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100829 free(cpblts);
830
831 if (r) {
832 return NC_MSG_ERROR;
833 }
834
835 return NC_MSG_HELLO;
836}
837
838static NC_MSG_TYPE
839nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200840{
841 struct lyxml_elem *xml = NULL, *node;
842 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
843 int ver = -1;
844 char *str;
845 long long int id;
846 int flag = 0;
847
Michal Vasko05ba9df2016-01-13 14:40:27 +0100848 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200849
850 switch(msgtype) {
851 case NC_MSG_HELLO:
852 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100853 LY_TREE_FOR(xml->child, node) {
854 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
855 continue;
856 } else if (!strcmp(node->name, "session-id")) {
857 if (!node->content || !strlen(node->content)) {
858 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200859 goto error;
860 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100861 str = NULL;
862 id = strtoll(node->content, &str, 10);
863 if (*str || id < 1 || id > UINT32_MAX) {
864 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200865 goto error;
866 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100867 session->id = (uint32_t)id;
868 continue;
869 } else if (strcmp(node->name, "capabilities")) {
870 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200871 goto error;
872 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100873
874 if (flag) {
875 /* multiple capabilities elements */
876 ERR("Invalid <hello> message (multiple <capabilities> elements).");
877 goto error;
878 }
879 flag = 1;
880
881 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
882 goto error;
883 }
884 session->version = ver;
885 }
886
887 if (!session->id) {
888 ERR("Missing <session-id> in server's <hello>.");
889 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200890 }
891 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200892 case NC_MSG_WOULDBLOCK:
893 ERR("Server's <hello> timeout elapsed.");
894 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200895 case NC_MSG_ERROR:
896 /* nothing special, just pass it out */
897 break;
898 default:
899 ERR("Unexpected message received instead of <hello>.");
900 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200901 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200902 }
903
904 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100905 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200906
907 return msgtype;
908
909error:
910 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100911 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200912
913 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200914}
915
Michal Vasko11d142a2016-01-19 15:58:24 +0100916static NC_MSG_TYPE
917nc_recv_server_hello(struct nc_session *session)
918{
919 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200920 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100921 int ver = -1;
922 int flag = 0;
923
Michal Vaskoadb850e2016-01-20 14:06:32 +0100924 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100925
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100926 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100927 case NC_MSG_HELLO:
928 /* get know NETCONF version */
929 LY_TREE_FOR(xml->child, node) {
930 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
931 continue;
932 } else if (strcmp(node->name, "capabilities")) {
933 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200934 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100935 goto cleanup;
936 }
937
938 if (flag) {
939 /* multiple capabilities elements */
940 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200941 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100942 goto cleanup;
943 }
944 flag = 1;
945
946 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200947 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100948 goto cleanup;
949 }
950 session->version = ver;
951 }
952 break;
953 case NC_MSG_ERROR:
954 /* nothing special, just pass it out */
955 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100956 case NC_MSG_WOULDBLOCK:
957 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100958 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100959 default:
960 ERR("Unexpected message received instead of <hello>.");
961 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200962 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100963 }
964
965cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100966 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100967
968 return msgtype;
969}
970
Michal Vasko71090fc2016-05-24 16:37:28 +0200971NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100972nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100973{
Michal Vasko086311b2016-01-08 09:53:11 +0100974 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100975
Michal Vasko11d142a2016-01-19 15:58:24 +0100976 if (session->side == NC_CLIENT) {
977 type = nc_send_client_hello(session);
978 } else {
979 type = nc_send_server_hello(session);
980 }
981
Michal Vasko086311b2016-01-08 09:53:11 +0100982 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200983 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100984 }
985
Michal Vasko11d142a2016-01-19 15:58:24 +0100986 if (session->side == NC_CLIENT) {
987 type = nc_recv_client_hello(session);
988 } else {
989 type = nc_recv_server_hello(session);
990 }
991
Michal Vasko71090fc2016-05-24 16:37:28 +0200992 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100993}
Michal Vasko086311b2016-01-08 09:53:11 +0100994
Radek Krejci53691be2016-02-22 13:58:37 +0100995#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100996
Michal Vasko8f0c0282016-02-29 10:17:14 +0100997static void
Michal Vasko086311b2016-01-08 09:53:11 +0100998nc_ssh_init(void)
999{
1000 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1001 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001002}
1003
Michal Vasko8f0c0282016-02-29 10:17:14 +01001004static void
Michal Vasko086311b2016-01-08 09:53:11 +01001005nc_ssh_destroy(void)
1006{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001007 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001008 ENGINE_cleanup();
1009 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001010 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001011 ssh_finalize();
1012}
1013
Radek Krejci53691be2016-02-22 13:58:37 +01001014#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001015
Radek Krejci53691be2016-02-22 13:58:37 +01001016#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001017
Michal Vaskof0c92c02016-01-29 09:41:45 +01001018struct CRYPTO_dynlock_value {
1019 pthread_mutex_t lock;
1020};
1021
Michal Vaskof0c92c02016-01-29 09:41:45 +01001022static struct CRYPTO_dynlock_value *
1023tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1024{
1025 struct CRYPTO_dynlock_value *value;
1026
1027 value = malloc(sizeof *value);
1028 if (!value) {
1029 ERRMEM;
1030 return NULL;
1031 }
1032 pthread_mutex_init(&value->lock, NULL);
1033
1034 return value;
1035}
1036
1037static void
1038tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1039{
1040 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1041 * I found ignored this fact, what do I know... */
1042 if (mode & CRYPTO_LOCK) {
1043 pthread_mutex_lock(&l->lock);
1044 } else {
1045 pthread_mutex_unlock(&l->lock);
1046 }
1047}
1048
1049static void
1050tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1051{
1052 pthread_mutex_destroy(&l->lock);
1053 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001054}
1055
Michal Vasko8f0c0282016-02-29 10:17:14 +01001056#endif /* NC_ENABLED_TLS */
1057
1058#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1059
1060static pthread_mutex_t *tls_locks;
1061
1062static void
1063tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1064{
1065 if (mode & CRYPTO_LOCK) {
1066 pthread_mutex_lock(tls_locks + n);
1067 } else {
1068 pthread_mutex_unlock(tls_locks + n);
1069 }
1070}
1071
1072static void
1073tls_thread_id_func(CRYPTO_THREADID *tid)
1074{
1075 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1076}
1077
1078static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001079nc_tls_init(void)
1080{
1081 int i;
1082
1083 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001084 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001085 SSL_library_init();
1086
1087 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001088 if (!tls_locks) {
1089 ERRMEM;
1090 return;
1091 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001092 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1093 pthread_mutex_init(tls_locks + i, NULL);
1094 }
1095
Michal Vaskof0c92c02016-01-29 09:41:45 +01001096 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001097 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001098
1099 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1100 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1101 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001102}
1103
Michal Vasko8f0c0282016-02-29 10:17:14 +01001104static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001105nc_tls_destroy(void)
1106{
1107 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001108
Michal Vasko8f0c0282016-02-29 10:17:14 +01001109 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001110 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001111 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001112 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001113 ERR_free_strings();
1114 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001115
Michal Vaskob6e37262016-02-25 14:49:00 +01001116 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001117 CRYPTO_set_locking_callback(NULL);
1118 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1119 pthread_mutex_destroy(tls_locks + i);
1120 }
1121 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001122
1123 CRYPTO_set_dynlock_create_callback(NULL);
1124 CRYPTO_set_dynlock_lock_callback(NULL);
1125 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001126}
1127
Michal Vasko8f0c0282016-02-29 10:17:14 +01001128#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001129
Radek Krejci53691be2016-02-22 13:58:37 +01001130#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001131
Michal Vasko8f0c0282016-02-29 10:17:14 +01001132static void
Michal Vasko5e228792016-02-03 15:30:24 +01001133nc_ssh_tls_init(void)
1134{
1135 SSL_load_error_strings();
1136 ERR_load_BIO_strings();
1137 SSL_library_init();
1138
1139 nc_ssh_init();
1140
1141 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1142 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1143 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1144}
1145
Michal Vasko8f0c0282016-02-29 10:17:14 +01001146static void
Michal Vasko5e228792016-02-03 15:30:24 +01001147nc_ssh_tls_destroy(void)
1148{
1149 ERR_free_strings();
1150 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1151
1152 nc_ssh_destroy();
1153
1154 CRYPTO_set_dynlock_create_callback(NULL);
1155 CRYPTO_set_dynlock_lock_callback(NULL);
1156 CRYPTO_set_dynlock_destroy_callback(NULL);
1157}
1158
Radek Krejci53691be2016-02-22 13:58:37 +01001159#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001160
1161#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1162
1163API void
1164nc_thread_destroy(void)
1165{
1166 CRYPTO_THREADID crypto_tid;
1167
1168 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1169 //CRYPTO_cleanup_all_ex_data();
1170
1171 CRYPTO_THREADID_current(&crypto_tid);
1172 ERR_remove_thread_state(&crypto_tid);
1173}
1174
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001175#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1176
1177void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001178nc_init(void)
1179{
1180#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1181 nc_ssh_tls_init();
1182#elif defined(NC_ENABLED_SSH)
1183 nc_ssh_init();
1184#elif defined(NC_ENABLED_TLS)
1185 nc_tls_init();
1186#endif
1187}
1188
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001189void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001190nc_destroy(void)
1191{
1192#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1193 nc_ssh_tls_destroy();
1194#elif defined(NC_ENABLED_SSH)
1195 nc_ssh_destroy();
1196#elif defined(NC_ENABLED_TLS)
1197 nc_tls_destroy();
1198#endif
1199}