blob: 83a453834807d6e8c7cd47dc5c4b74b97ff59d9a [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
14
Radek Krejci206fcd62015-10-07 15:42:48 +020015#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010016#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010017#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020018#include <pthread.h>
Michal Vasko58f31552016-01-19 12:39:15 +010019#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020020#include <libyang/libyang.h>
21
Michal Vasko5e228792016-02-03 15:30:24 +010022#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010023#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010024#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010025
Radek Krejci53691be2016-02-22 13:58:37 +010026#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020027
Michal Vasko086311b2016-01-08 09:53:11 +010028# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020029
Radek Krejci53691be2016-02-22 13:58:37 +010030#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020031
Radek Krejci53691be2016-02-22 13:58:37 +010032#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010033
Michal Vasko5e228792016-02-03 15:30:24 +010034# include <openssl/engine.h>
35# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010036# include <openssl/err.h>
37
Radek Krejci53691be2016-02-22 13:58:37 +010038#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010039
Michal Vasko086311b2016-01-08 09:53:11 +010040/* in seconds */
41#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020042
Michal Vasko05ba9df2016-01-13 14:40:27 +010043/* in milliseconds */
44#define NC_CLOSE_REPLY_TIMEOUT 200
45
Michal Vasko086311b2016-01-08 09:53:11 +010046extern struct nc_server_opts server_opts;
47
Michal Vasko96164bf2016-01-21 15:41:58 +010048/*
49 * @return 1 - success
50 * 0 - timeout
51 * -1 - error
52 */
53int
Michal Vasko62be1ce2016-03-03 13:24:52 +010054nc_timedlock(pthread_mutex_t *lock, int timeout)
Michal Vasko96164bf2016-01-21 15:41:58 +010055{
56 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +010057 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +010058
59 if (timeout > 0) {
60 clock_gettime(CLOCK_REALTIME, &ts_timeout);
61
Michal Vasko96164bf2016-01-21 15:41:58 +010062 ts_timeout.tv_sec += timeout / 1000;
63 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
64
65 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +010066 } else if (!timeout) {
67 ret = pthread_mutex_trylock(lock);
68 } else { /* timeout == -1 */
69 ret = pthread_mutex_lock(lock);
70 }
71
72 if (ret == ETIMEDOUT) {
73 /* timeout */
74 return 0;
75 } else if (ret) {
76 /* error */
Radek Krejcida8f58d2016-03-03 13:10:21 +010077 ERR("Mutex lock failed (%s).", strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +010078 return -1;
79 }
80
81 /* ok */
82 return 1;
83}
84
Michal Vasko8dadf782016-01-15 10:29:36 +010085API NC_STATUS
86nc_session_get_status(const struct nc_session *session)
87{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010088 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +020089 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +010090 return 0;
91 }
92
Michal Vasko8dadf782016-01-15 10:29:36 +010093 return session->status;
94}
95
96API uint32_t
97nc_session_get_id(const struct nc_session *session)
98{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010099 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200100 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100101 return 0;
102 }
103
Michal Vasko8dadf782016-01-15 10:29:36 +0100104 return session->id;
105}
106
Michal Vasko174fe8e2016-02-17 15:38:09 +0100107API int
108nc_session_get_version(const struct nc_session *session)
109{
110 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200111 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100112 return -1;
113 }
114
115 return (session->version == NC_VERSION_10 ? 0 : 1);
116}
117
Michal Vasko8dadf782016-01-15 10:29:36 +0100118API NC_TRANSPORT_IMPL
119nc_session_get_ti(const struct nc_session *session)
120{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100121 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200122 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100123 return 0;
124 }
125
Michal Vasko8dadf782016-01-15 10:29:36 +0100126 return session->ti_type;
127}
128
129API const char *
130nc_session_get_username(const struct nc_session *session)
131{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100132 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200133 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100134 return NULL;
135 }
136
Michal Vasko8dadf782016-01-15 10:29:36 +0100137 return session->username;
138}
139
140API const char *
141nc_session_get_host(const struct nc_session *session)
142{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100143 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200144 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100145 return NULL;
146 }
147
Michal Vasko8dadf782016-01-15 10:29:36 +0100148 return session->host;
149}
150
151API uint16_t
152nc_session_get_port(const struct nc_session *session)
153{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100154 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200155 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100156 return 0;
157 }
158
Michal Vasko8dadf782016-01-15 10:29:36 +0100159 return session->port;
160}
161
Michal Vasko9a25e932016-02-01 10:36:42 +0100162API struct ly_ctx *
163nc_session_get_ctx(const struct nc_session *session)
164{
165 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200166 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100167 return NULL;
168 }
169
170 return session->ctx;
171}
172
Michal Vasko2cc4c682016-03-01 09:16:48 +0100173API void
174nc_session_set_data(struct nc_session *session, void *data)
175{
176 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200177 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100178 return;
179 }
180
181 session->data = data;
182}
183
184API void *
185nc_session_get_data(const struct nc_session *session)
186{
187 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200188 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100189 return NULL;
190 }
191
192 return session->data;
193}
194
Michal Vasko086311b2016-01-08 09:53:11 +0100195NC_MSG_TYPE
196nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200197{
Michal Vasko086311b2016-01-08 09:53:11 +0100198 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200199
Michal Vasko086311b2016-01-08 09:53:11 +0100200 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100201 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
202 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100203 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100204 }
205
Michal Vasko086311b2016-01-08 09:53:11 +0100206 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
207
208 if (r) {
209 return NC_MSG_ERROR;
210 }
211
212 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100213}
214
Radek Krejci695d4fa2015-10-22 13:23:54 +0200215API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100216nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200217{
Michal Vasko9e99f012016-03-03 13:25:20 +0100218 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100219 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100220 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100221 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100222 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100223 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100224 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200225 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100226 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200227 void *p;
228
Michal Vasko428087d2016-01-14 16:04:28 +0100229 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200230 return;
231 }
232
Michal Vasko86d357c2016-03-11 13:46:38 +0100233 /* stop notifications loop if any */
234 if (session->ntf_tid) {
235 tid = *session->ntf_tid;
236 free((pthread_t *)session->ntf_tid);
237 session->ntf_tid = NULL;
238 /* the thread now knows it should quit */
239
240 pthread_join(tid, NULL);
241 }
242
Michal Vaskoadd4c792015-10-26 15:36:58 +0100243 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100244 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100245 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100246 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100247 } else if (!r) {
248 /* we failed to lock it, too bad */
249 locked = 0;
250 } else {
251 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100252 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200253 } else {
254 ERRINT;
255 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200256 }
257
Michal Vasko9e99f012016-03-03 13:25:20 +0100258 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200259 /* cleanup message queues */
260 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100261 for (contiter = session->notifs; contiter; ) {
262 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200263
Michal Vaskoad611702015-12-03 13:41:51 +0100264 p = contiter;
265 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200266 free(p);
267 }
268
269 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100270 for (contiter = session->replies; contiter; ) {
271 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272
Michal Vaskoad611702015-12-03 13:41:51 +0100273 p = contiter;
274 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200275 free(p);
276 }
277
278 /* send closing info to the other side */
279 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
280 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100281 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200282 } else {
283 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100284 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200285 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100286 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100287 case NC_MSG_REPLY:
288 LY_TREE_FOR(rpl->child, child) {
289 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
290 break;
291 }
292 }
293 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100294 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100295 }
Michal Vaskoad611702015-12-03 13:41:51 +0100296 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100297 break;
298 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100299 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100300 break;
301 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100302 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100303 break;
304 default:
305 /* cannot happen */
306 break;
307 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200308 }
309
310 /* list of server's capabilities */
311 if (session->cpblts) {
312 for (i = 0; session->cpblts[i]; i++) {
313 lydict_remove(session->ctx, session->cpblts[i]);
314 }
315 free(session->cpblts);
316 }
317 }
318
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100319 if (session->data && data_free) {
320 data_free(session->data);
321 }
322
Michal Vasko86d357c2016-03-11 13:46:38 +0100323 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200324 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100325 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200326
327 /* transport implementation cleanup */
328 switch (session->ti_type) {
329 case NC_TI_FD:
330 /* nothing needed - file descriptors were provided by caller,
331 * so it is up to the caller to close them correctly
332 * TODO use callbacks
333 */
Michal Vasko3512e402016-01-28 16:22:34 +0100334 /* just to avoid compiler warning */
335 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100336 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200337 break;
338
Radek Krejci53691be2016-02-22 13:58:37 +0100339#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200340 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100341 if (connected) {
342 ssh_channel_free(session->ti.libssh.channel);
343 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200344 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
345 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
346 * it.
347 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100348 multisession = 0;
349 if (session->ti.libssh.next) {
350 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
351 if (siter->status != NC_STATUS_STARTING) {
352 multisession = 1;
353 break;
354 }
355 }
356 }
357
358 if (!multisession) {
359 /* it's not multisession yet, but we still need to free the starting sessions */
360 if (session->ti.libssh.next) {
361 do {
362 siter = session->ti.libssh.next;
363 session->ti.libssh.next = siter->ti.libssh.next;
364
365 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100366 lydict_remove(session->ctx, session->username);
367 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100368 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100369 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100370 }
371
372 free(siter);
373 } while (session->ti.libssh.next != session);
374 }
Michal Vasko428087d2016-01-14 16:04:28 +0100375 if (connected) {
376 ssh_disconnect(session->ti.libssh.session);
377 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200378 ssh_free(session->ti.libssh.session);
379 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200380 /* remove the session from the list */
381 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100382 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200383 /* there will be only one session */
384 siter->ti.libssh.next = NULL;
385 } else {
386 /* there are still multiple sessions, keep the ring list */
387 siter->ti.libssh.next = session->ti.libssh.next;
388 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100389 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
390 if (session->flags & NC_SESSION_SSH_MSG_CB) {
391 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
392 if (siter->ti.libssh.next == session) {
393 ERRINT;
394 break;
395 }
396 }
397 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
398 siter->flags |= NC_SESSION_SSH_MSG_CB;
399 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200400 }
401 break;
402#endif
403
Radek Krejci53691be2016-02-22 13:58:37 +0100404#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200405 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100406 if (connected) {
407 SSL_shutdown(session->ti.tls);
408 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200409 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100410
411 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200412 break;
413#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100414 case NC_TI_NONE:
415 ERRINT;
416 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200417 }
Michal Vasko428087d2016-01-14 16:04:28 +0100418
Radek Krejciac6d3472015-10-22 15:47:18 +0200419 lydict_remove(session->ctx, session->username);
420 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200421
422 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100423 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100424 if (locked) {
425 pthread_mutex_unlock(session->ti_lock);
426 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100427 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100428 pthread_mutex_destroy(session->ti_lock);
429 free(session->ti_lock);
430 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200431 }
432
433 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100434 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200435 }
436
437 free(session);
438}
439
Michal Vasko086311b2016-01-08 09:53:11 +0100440static void
441add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
442{
443 if (*count == *size) {
444 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100445 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
446 if (!(*cpblts)) {
447 ERRMEM;
448 return;
449 }
Michal Vasko086311b2016-01-08 09:53:11 +0100450 }
451
452 if (capab) {
453 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
454 } else {
455 (*cpblts)[*count] = NULL;
456 }
457 ++(*count);
458}
459
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200460API const char **
461nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100462{
463 struct lyd_node *child, *child2, *yanglib;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200464 struct lyd_node_leaf_list **features = NULL, **deviations = NULL, *ns = NULL, *rev = NULL, *name = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100465 const char **cpblts;
466 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200467 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200468#define NC_CPBLT_BUF_LEN 512
469 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100470
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200471 if (!ctx) {
472 ERRARG("ctx");
473 return NULL;
474 }
475
Michal Vasko086311b2016-01-08 09:53:11 +0100476 yanglib = ly_ctx_info(ctx);
477 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200478 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100479 return NULL;
480 }
481
482 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100483 if (!cpblts) {
484 ERRMEM;
485 return NULL;
486 }
Michal Vasko086311b2016-01-08 09:53:11 +0100487 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
488 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
489 count = 2;
490
491 /* capabilities */
492
493 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
494 if (mod) {
495 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100496 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100497 }
498 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100499 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100500 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100501 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100502 }
503 }
504 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100505 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100506 }
507 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100508 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100509 }
510 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100511 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100512 }
513 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100514 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100515 }
516 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100517 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100518 }
519 }
520
521 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
522 if (mod) {
523 if (!server_opts.wd_basic_mode) {
524 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
525 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100526 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100527 switch (server_opts.wd_basic_mode) {
528 case NC_WD_ALL:
529 strcat(str, "?basic-mode=report-all");
530 break;
531 case NC_WD_TRIM:
532 strcat(str, "?basic-mode=trim");
533 break;
534 case NC_WD_EXPLICIT:
535 strcat(str, "?basic-mode=explicit");
536 break;
537 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100538 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100539 break;
540 }
541
542 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200543 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100544 if (server_opts.wd_also_supported & NC_WD_ALL) {
545 strcat(str, "report-all,");
546 }
547 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
548 strcat(str, "report-all-tagged,");
549 }
550 if (server_opts.wd_also_supported & NC_WD_TRIM) {
551 strcat(str, "trim,");
552 }
553 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
554 strcat(str, "explicit,");
555 }
556 str[strlen(str) - 1] = '\0';
557
558 add_cpblt(ctx, str, &cpblts, &size, &count);
559 }
560 }
561 }
562
Michal Vasko1a38c862016-01-15 15:50:07 +0100563 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100564 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100565 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100566 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100567 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100568 }
569 }
570
571 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100572 LY_TREE_FOR(yanglib->child, child) {
573 if (!strcmp(child->schema->name, "module")) {
574 LY_TREE_FOR(child->child, child2) {
575 if (!strcmp(child2->schema->name, "namespace")) {
576 ns = (struct lyd_node_leaf_list *)child2;
577 } else if (!strcmp(child2->schema->name, "name")) {
578 name = (struct lyd_node_leaf_list *)child2;
579 } else if (!strcmp(child2->schema->name, "revision")) {
580 rev = (struct lyd_node_leaf_list *)child2;
581 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100582 features = nc_realloc(features, ++feat_count * sizeof *features);
583 if (!features) {
584 ERRMEM;
585 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200586 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100587 return NULL;
588 }
Michal Vasko086311b2016-01-08 09:53:11 +0100589 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200590 } else if (!strcmp(child2->schema->name, "deviation")) {
591 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
592 if (!deviations) {
593 ERRMEM;
594 free(cpblts);
595 free(features);
596 return NULL;
597 }
Michal Vasko086311b2016-01-08 09:53:11 +0100598 }
599 }
600
601 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100602 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100603 continue;
604 }
605
Radek Krejcidf7ba522016-03-01 16:05:25 +0100606 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200607 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100608 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200609 strcat(str, "&features=");
610 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100611 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200612 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100613 ERRINT;
614 break;
615 }
Michal Vasko086311b2016-01-08 09:53:11 +0100616 if (i) {
617 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100618 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100619 }
620 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100621 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100622 }
623 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200624 if (dev_count) {
625 strcat(str, "&deviations=");
626 str_len += 12;
627 for (i = 0; i < dev_count; ++i) {
628 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
629 ERRINT;
630 break;
631 }
632 if (i) {
633 strcat(str, ",");
634 ++str_len;
635 }
636 strcat(str, deviations[i]->value_str);
637 str_len += strlen(deviations[i]->value_str);
638 }
639 }
Michal Vasko086311b2016-01-08 09:53:11 +0100640
641 add_cpblt(ctx, str, &cpblts, &size, &count);
642
643 ns = NULL;
644 name = NULL;
645 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200646 if (features || feat_count) {
647 free(features);
648 features = NULL;
649 feat_count = 0;
650 }
651 if (deviations || dev_count) {
652 free(deviations);
653 deviations = NULL;
654 dev_count = 0;
655 }
Michal Vasko086311b2016-01-08 09:53:11 +0100656 }
657 }
658
659 lyd_free(yanglib);
660
661 /* ending NULL capability */
662 add_cpblt(ctx, NULL, &cpblts, &size, &count);
663
664 return cpblts;
665}
666
Radek Krejci695d4fa2015-10-22 13:23:54 +0200667static int
668parse_cpblts(struct lyxml_elem *xml, const char ***list)
669{
670 struct lyxml_elem *cpblt;
671 int ver = -1;
672 int i = 0;
673
674 if (list) {
675 /* get the storage for server's capabilities */
676 LY_TREE_FOR(xml->child, cpblt) {
677 i++;
678 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100679 /* last item remains NULL */
680 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200681 if (!*list) {
682 ERRMEM;
683 return -1;
684 }
685 i = 0;
686 }
687
688 LY_TREE_FOR(xml->child, cpblt) {
689 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
690 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
691 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
692 return -1;
693 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
694 continue;
695 }
696
697 /* detect NETCONF version */
698 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
699 ver = 0;
700 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
701 ver = 1;
702 }
703
704 /* store capabilities */
705 if (list) {
706 (*list)[i] = cpblt->content;
707 cpblt->content = NULL;
708 i++;
709 }
710 }
711
712 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100713 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200714 }
715
716 return ver;
717}
718
719static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100720nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100721{
722 int r, i;
723 const char **cpblts;
724
Michal Vasko11d142a2016-01-19 15:58:24 +0100725 /* client side hello - send only NETCONF base capabilities */
726 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100727 if (!cpblts) {
728 ERRMEM;
729 return NC_MSG_ERROR;
730 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100731 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
732 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
733 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100734
Michal Vasko11d142a2016-01-19 15:58:24 +0100735 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100736
Michal Vasko086311b2016-01-08 09:53:11 +0100737 for (i = 0; cpblts[i]; ++i) {
738 lydict_remove(session->ctx, cpblts[i]);
739 }
740 free(cpblts);
741
742 if (r) {
743 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100744 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100745
746 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100747}
748
749static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100750nc_send_server_hello(struct nc_session *session)
751{
752 int r, i;
753 const char **cpblts;
754
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200755 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100756
757 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
758
Michal Vasko11d142a2016-01-19 15:58:24 +0100759 for (i = 0; cpblts[i]; ++i) {
760 lydict_remove(session->ctx, cpblts[i]);
761 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100762 free(cpblts);
763
764 if (r) {
765 return NC_MSG_ERROR;
766 }
767
768 return NC_MSG_HELLO;
769}
770
771static NC_MSG_TYPE
772nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200773{
774 struct lyxml_elem *xml = NULL, *node;
775 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
776 int ver = -1;
777 char *str;
778 long long int id;
779 int flag = 0;
780
Michal Vasko05ba9df2016-01-13 14:40:27 +0100781 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200782
783 switch(msgtype) {
784 case NC_MSG_HELLO:
785 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100786 LY_TREE_FOR(xml->child, node) {
787 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
788 continue;
789 } else if (!strcmp(node->name, "session-id")) {
790 if (!node->content || !strlen(node->content)) {
791 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200792 goto error;
793 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100794 str = NULL;
795 id = strtoll(node->content, &str, 10);
796 if (*str || id < 1 || id > UINT32_MAX) {
797 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200798 goto error;
799 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100800 session->id = (uint32_t)id;
801 continue;
802 } else if (strcmp(node->name, "capabilities")) {
803 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200804 goto error;
805 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100806
807 if (flag) {
808 /* multiple capabilities elements */
809 ERR("Invalid <hello> message (multiple <capabilities> elements).");
810 goto error;
811 }
812 flag = 1;
813
814 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
815 goto error;
816 }
817 session->version = ver;
818 }
819
820 if (!session->id) {
821 ERR("Missing <session-id> in server's <hello>.");
822 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200823 }
824 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200825 case NC_MSG_WOULDBLOCK:
826 ERR("Server's <hello> timeout elapsed.");
827 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200828 case NC_MSG_ERROR:
829 /* nothing special, just pass it out */
830 break;
831 default:
832 ERR("Unexpected message received instead of <hello>.");
833 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200834 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200835 }
836
837 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100838 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200839
840 return msgtype;
841
842error:
843 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100844 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200845
846 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200847}
848
Michal Vasko11d142a2016-01-19 15:58:24 +0100849static NC_MSG_TYPE
850nc_recv_server_hello(struct nc_session *session)
851{
852 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200853 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100854 int ver = -1;
855 int flag = 0;
856
Michal Vaskoadb850e2016-01-20 14:06:32 +0100857 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100858
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100859 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100860 case NC_MSG_HELLO:
861 /* get know NETCONF version */
862 LY_TREE_FOR(xml->child, node) {
863 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
864 continue;
865 } else if (strcmp(node->name, "capabilities")) {
866 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200867 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100868 goto cleanup;
869 }
870
871 if (flag) {
872 /* multiple capabilities elements */
873 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200874 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100875 goto cleanup;
876 }
877 flag = 1;
878
879 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200880 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100881 goto cleanup;
882 }
883 session->version = ver;
884 }
885 break;
886 case NC_MSG_ERROR:
887 /* nothing special, just pass it out */
888 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100889 case NC_MSG_WOULDBLOCK:
890 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100891 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100892 default:
893 ERR("Unexpected message received instead of <hello>.");
894 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200895 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100896 }
897
898cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100899 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100900
901 return msgtype;
902}
903
Michal Vasko71090fc2016-05-24 16:37:28 +0200904NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100905nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100906{
Michal Vasko086311b2016-01-08 09:53:11 +0100907 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100908
Michal Vasko11d142a2016-01-19 15:58:24 +0100909 if (session->side == NC_CLIENT) {
910 type = nc_send_client_hello(session);
911 } else {
912 type = nc_send_server_hello(session);
913 }
914
Michal Vasko086311b2016-01-08 09:53:11 +0100915 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200916 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100917 }
918
Michal Vasko11d142a2016-01-19 15:58:24 +0100919 if (session->side == NC_CLIENT) {
920 type = nc_recv_client_hello(session);
921 } else {
922 type = nc_recv_server_hello(session);
923 }
924
Michal Vasko71090fc2016-05-24 16:37:28 +0200925 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100926}
Michal Vasko086311b2016-01-08 09:53:11 +0100927
Radek Krejci53691be2016-02-22 13:58:37 +0100928#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100929
Michal Vasko8f0c0282016-02-29 10:17:14 +0100930static void
Michal Vasko086311b2016-01-08 09:53:11 +0100931nc_ssh_init(void)
932{
933 ssh_threads_set_callbacks(ssh_threads_get_pthread());
934 ssh_init();
935 ssh_set_log_level(verbose_level);
936}
937
Michal Vasko8f0c0282016-02-29 10:17:14 +0100938static void
Michal Vasko086311b2016-01-08 09:53:11 +0100939nc_ssh_destroy(void)
940{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100941 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100942 ENGINE_cleanup();
943 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100944 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100945 ssh_finalize();
946}
947
Radek Krejci53691be2016-02-22 13:58:37 +0100948#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100949
Radek Krejci53691be2016-02-22 13:58:37 +0100950#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100951
Michal Vaskof0c92c02016-01-29 09:41:45 +0100952struct CRYPTO_dynlock_value {
953 pthread_mutex_t lock;
954};
955
Michal Vaskof0c92c02016-01-29 09:41:45 +0100956static struct CRYPTO_dynlock_value *
957tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
958{
959 struct CRYPTO_dynlock_value *value;
960
961 value = malloc(sizeof *value);
962 if (!value) {
963 ERRMEM;
964 return NULL;
965 }
966 pthread_mutex_init(&value->lock, NULL);
967
968 return value;
969}
970
971static void
972tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
973{
974 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
975 * I found ignored this fact, what do I know... */
976 if (mode & CRYPTO_LOCK) {
977 pthread_mutex_lock(&l->lock);
978 } else {
979 pthread_mutex_unlock(&l->lock);
980 }
981}
982
983static void
984tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
985{
986 pthread_mutex_destroy(&l->lock);
987 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100988}
989
Michal Vasko8f0c0282016-02-29 10:17:14 +0100990#endif /* NC_ENABLED_TLS */
991
992#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
993
994static pthread_mutex_t *tls_locks;
995
996static void
997tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
998{
999 if (mode & CRYPTO_LOCK) {
1000 pthread_mutex_lock(tls_locks + n);
1001 } else {
1002 pthread_mutex_unlock(tls_locks + n);
1003 }
1004}
1005
1006static void
1007tls_thread_id_func(CRYPTO_THREADID *tid)
1008{
1009 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1010}
1011
1012static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001013nc_tls_init(void)
1014{
1015 int i;
1016
1017 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001018 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001019 SSL_library_init();
1020
1021 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001022 if (!tls_locks) {
1023 ERRMEM;
1024 return;
1025 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001026 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1027 pthread_mutex_init(tls_locks + i, NULL);
1028 }
1029
Michal Vaskof0c92c02016-01-29 09:41:45 +01001030 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001031 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001032
1033 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1034 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1035 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001036}
1037
Michal Vasko8f0c0282016-02-29 10:17:14 +01001038static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001039nc_tls_destroy(void)
1040{
1041 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001042
Michal Vasko8f0c0282016-02-29 10:17:14 +01001043 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001044 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001045 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001046 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001047 ERR_free_strings();
1048 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001049
Michal Vaskob6e37262016-02-25 14:49:00 +01001050 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001051 CRYPTO_set_locking_callback(NULL);
1052 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1053 pthread_mutex_destroy(tls_locks + i);
1054 }
1055 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001056
1057 CRYPTO_set_dynlock_create_callback(NULL);
1058 CRYPTO_set_dynlock_lock_callback(NULL);
1059 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001060}
1061
Michal Vasko8f0c0282016-02-29 10:17:14 +01001062#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001063
Radek Krejci53691be2016-02-22 13:58:37 +01001064#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001065
Michal Vasko8f0c0282016-02-29 10:17:14 +01001066static void
Michal Vasko5e228792016-02-03 15:30:24 +01001067nc_ssh_tls_init(void)
1068{
1069 SSL_load_error_strings();
1070 ERR_load_BIO_strings();
1071 SSL_library_init();
1072
1073 nc_ssh_init();
1074
1075 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1076 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1077 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1078}
1079
Michal Vasko8f0c0282016-02-29 10:17:14 +01001080static void
Michal Vasko5e228792016-02-03 15:30:24 +01001081nc_ssh_tls_destroy(void)
1082{
1083 ERR_free_strings();
1084 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1085
1086 nc_ssh_destroy();
1087
1088 CRYPTO_set_dynlock_create_callback(NULL);
1089 CRYPTO_set_dynlock_lock_callback(NULL);
1090 CRYPTO_set_dynlock_destroy_callback(NULL);
1091}
1092
Radek Krejci53691be2016-02-22 13:58:37 +01001093#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001094
1095#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1096
1097API void
1098nc_thread_destroy(void)
1099{
1100 CRYPTO_THREADID crypto_tid;
1101
1102 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1103 //CRYPTO_cleanup_all_ex_data();
1104
1105 CRYPTO_THREADID_current(&crypto_tid);
1106 ERR_remove_thread_state(&crypto_tid);
1107}
1108
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001109#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1110
1111void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001112nc_init(void)
1113{
1114#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1115 nc_ssh_tls_init();
1116#elif defined(NC_ENABLED_SSH)
1117 nc_ssh_init();
1118#elif defined(NC_ENABLED_TLS)
1119 nc_tls_init();
1120#endif
1121}
1122
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001123void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001124nc_destroy(void)
1125{
1126#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1127 nc_ssh_tls_destroy();
1128#elif defined(NC_ENABLED_SSH)
1129 nc_ssh_destroy();
1130#elif defined(NC_ENABLED_TLS)
1131 nc_tls_destroy();
1132#endif
1133}