blob: 23cf3cf6ea5bdee04c3412f58ce2084e7661441b [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
Radek Krejci206fcd62015-10-07 15:42:48 +020023#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010024#include <stdlib.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020025#include <pthread.h>
Michal Vasko58f31552016-01-19 12:39:15 +010026#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020027#include <libyang/libyang.h>
28
Michal Vaskob48aa812016-01-18 14:13:09 +010029#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010030#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010031
Michal Vasko086311b2016-01-08 09:53:11 +010032#ifdef ENABLE_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020033
Michal Vasko086311b2016-01-08 09:53:11 +010034# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020035
Michal Vasko086311b2016-01-08 09:53:11 +010036#endif /* ENABLE_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020037
Michal Vaskoc14e3c82016-01-11 16:14:30 +010038#ifdef ENABLE_TLS
39
40# include <openssl/err.h>
41
42#endif /* ENABLE_TLS */
43
Michal Vasko086311b2016-01-08 09:53:11 +010044/* in seconds */
45#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020046
Michal Vasko05ba9df2016-01-13 14:40:27 +010047/* in milliseconds */
48#define NC_CLOSE_REPLY_TIMEOUT 200
49
Michal Vasko086311b2016-01-08 09:53:11 +010050extern struct nc_server_opts server_opts;
51
Michal Vasko96164bf2016-01-21 15:41:58 +010052/*
53 * @return 1 - success
54 * 0 - timeout
55 * -1 - error
56 */
57int
58nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed)
59{
60 int ret;
61 struct timespec ts_timeout, ts_old, ts_new;
62
63 if (timeout > 0) {
64 clock_gettime(CLOCK_REALTIME, &ts_timeout);
65
66 if (elapsed) {
67 ts_old = ts_timeout;
68 }
69
70 ts_timeout.tv_sec += timeout / 1000;
71 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
72
73 ret = pthread_mutex_timedlock(lock, &ts_timeout);
74
75 if (elapsed) {
76 clock_gettime(CLOCK_REALTIME, &ts_new);
77
78 *elapsed += (ts_new.tv_sec - ts_old.tv_sec) * 1000;
79 *elapsed += (ts_new.tv_nsec - ts_old.tv_nsec) / 1000000;
80 }
81 } else if (!timeout) {
82 ret = pthread_mutex_trylock(lock);
83 } else { /* timeout == -1 */
84 ret = pthread_mutex_lock(lock);
85 }
86
87 if (ret == ETIMEDOUT) {
88 /* timeout */
89 return 0;
90 } else if (ret) {
91 /* error */
92 ERR("Mutex lock failed (%s).", strerror(errno));
93 return -1;
94 }
95
96 /* ok */
97 return 1;
98}
99
100void
101nc_subtract_elapsed(int *timeout, struct timespec *old_ts)
102{
103 struct timespec new_ts;
104
105 clock_gettime(CLOCK_MONOTONIC_RAW, &new_ts);
106
107 *timeout -= (new_ts.tv_sec - old_ts->tv_sec) * 1000;
108 *timeout -= (new_ts.tv_nsec - old_ts->tv_nsec) / 1000000;
109
110 *old_ts = new_ts;
111}
112
Michal Vasko8dadf782016-01-15 10:29:36 +0100113API NC_STATUS
114nc_session_get_status(const struct nc_session *session)
115{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100116 if (!session) {
117 ERRARG;
118 return 0;
119 }
120
Michal Vasko8dadf782016-01-15 10:29:36 +0100121 return session->status;
122}
123
124API uint32_t
125nc_session_get_id(const struct nc_session *session)
126{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100127 if (!session) {
128 ERRARG;
129 return 0;
130 }
131
Michal Vasko8dadf782016-01-15 10:29:36 +0100132 return session->id;
133}
134
135API NC_TRANSPORT_IMPL
136nc_session_get_ti(const struct nc_session *session)
137{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100138 if (!session) {
139 ERRARG;
140 return 0;
141 }
142
Michal Vasko8dadf782016-01-15 10:29:36 +0100143 return session->ti_type;
144}
145
146API const char *
147nc_session_get_username(const struct nc_session *session)
148{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100149 if (!session) {
150 ERRARG;
151 return NULL;
152 }
153
Michal Vasko8dadf782016-01-15 10:29:36 +0100154 return session->username;
155}
156
157API const char *
158nc_session_get_host(const struct nc_session *session)
159{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100160 if (!session) {
161 ERRARG;
162 return NULL;
163 }
164
Michal Vasko8dadf782016-01-15 10:29:36 +0100165 return session->host;
166}
167
168API uint16_t
169nc_session_get_port(const struct nc_session *session)
170{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100171 if (!session) {
172 ERRARG;
173 return 0;
174 }
175
Michal Vasko8dadf782016-01-15 10:29:36 +0100176 return session->port;
177}
178
179API const char **
180nc_session_get_cpblts(const struct nc_session *session)
181{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100182 if (!session) {
183 ERRARG;
184 return NULL;
185 }
186
Michal Vasko8dadf782016-01-15 10:29:36 +0100187 return session->cpblts;
188}
189
190API const char *
191nc_session_cpblt(const struct nc_session *session, const char *capab)
192{
193 int i, len;
194
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100195 if (!session || !capab) {
196 ERRARG;
197 return NULL;
198 }
199
Michal Vasko8dadf782016-01-15 10:29:36 +0100200 len = strlen(capab);
201 for (i = 0; session->cpblts[i]; ++i) {
202 if (!strncmp(session->cpblts[i], capab, len)) {
203 return session->cpblts[i];
204 }
205 }
206
207 return NULL;
208}
209
Michal Vasko086311b2016-01-08 09:53:11 +0100210NC_MSG_TYPE
211nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200212{
Michal Vasko086311b2016-01-08 09:53:11 +0100213 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200214
Michal Vasko086311b2016-01-08 09:53:11 +0100215 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100216 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
217 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100218 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100219 }
220
Michal Vasko086311b2016-01-08 09:53:11 +0100221 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
222
223 if (r) {
224 return NC_MSG_ERROR;
225 }
226
227 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100228}
229
Radek Krejci695d4fa2015-10-22 13:23:54 +0200230API void
231nc_session_free(struct nc_session *session)
232{
233 int r, i;
Michal Vasko428087d2016-01-14 16:04:28 +0100234 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100235 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200236 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100237 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100238 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200239 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100240 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200241 void *p;
242
Michal Vasko428087d2016-01-14 16:04:28 +0100243 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200244 return;
245 }
246
247 /* mark session for closing */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100248 if (session->ti_lock) {
249 do {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100250 r = nc_timedlock(session->ti_lock, 0, NULL);
251 } while (!r);
252 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100253 return;
254 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200255 }
256
257 /* stop notifications loop if any */
258 if (session->notif) {
259 pthread_cancel(*session->notif);
260 pthread_join(*session->notif, NULL);
261 }
262
Michal Vasko428087d2016-01-14 16:04:28 +0100263 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200264 /* cleanup message queues */
265 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100266 for (contiter = session->notifs; contiter; ) {
267 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200268
Michal Vaskoad611702015-12-03 13:41:51 +0100269 p = contiter;
270 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200271 free(p);
272 }
273
274 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100275 for (contiter = session->replies; contiter; ) {
276 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200277
Michal Vaskoad611702015-12-03 13:41:51 +0100278 p = contiter;
279 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200280 free(p);
281 }
282
283 /* send closing info to the other side */
284 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
285 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100286 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200287 } else {
288 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100289 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200290 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100291 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100292 case NC_MSG_REPLY:
293 LY_TREE_FOR(rpl->child, child) {
294 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
295 break;
296 }
297 }
298 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100299 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100300 }
Michal Vaskoad611702015-12-03 13:41:51 +0100301 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100302 break;
303 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100304 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100305 break;
306 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100307 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100308 break;
309 default:
310 /* cannot happen */
311 break;
312 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200313 }
314
315 /* list of server's capabilities */
316 if (session->cpblts) {
317 for (i = 0; session->cpblts[i]; i++) {
318 lydict_remove(session->ctx, session->cpblts[i]);
319 }
320 free(session->cpblts);
321 }
322 }
323
324 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 */
334 break;
335
Michal Vaskofb2fb762015-10-27 11:44:32 +0100336#ifdef ENABLE_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200337 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100338 if (connected) {
339 ssh_channel_free(session->ti.libssh.channel);
340 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200341 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
342 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
343 * it.
344 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100345 multisession = 0;
346 if (session->ti.libssh.next) {
347 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
348 if (siter->status != NC_STATUS_STARTING) {
349 multisession = 1;
350 break;
351 }
352 }
353 }
354
355 if (!multisession) {
356 /* it's not multisession yet, but we still need to free the starting sessions */
357 if (session->ti.libssh.next) {
358 do {
359 siter = session->ti.libssh.next;
360 session->ti.libssh.next = siter->ti.libssh.next;
361
362 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
363 if (session->side == NC_SERVER) {
364 nc_ctx_lock(-1, NULL);
365 }
366 lydict_remove(session->ctx, session->username);
367 lydict_remove(session->ctx, session->host);
368 if (session->side == NC_SERVER) {
369 nc_ctx_unlock();
370 }
371 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
372 ly_ctx_destroy(session->ctx);
373 }
374
375 free(siter);
376 } while (session->ti.libssh.next != session);
377 }
Michal Vasko428087d2016-01-14 16:04:28 +0100378 if (connected) {
379 ssh_disconnect(session->ti.libssh.session);
380 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200381 ssh_free(session->ti.libssh.session);
382 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200383 /* remove the session from the list */
384 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100385 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200386 /* there will be only one session */
387 siter->ti.libssh.next = NULL;
388 } else {
389 /* there are still multiple sessions, keep the ring list */
390 siter->ti.libssh.next = session->ti.libssh.next;
391 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100392 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
393 if (session->flags & NC_SESSION_SSH_MSG_CB) {
394 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
395 if (siter->ti.libssh.next == session) {
396 ERRINT;
397 break;
398 }
399 }
400 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
401 siter->flags |= NC_SESSION_SSH_MSG_CB;
402 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200403 }
404 break;
405#endif
406
407#ifdef ENABLE_TLS
408 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100409 if (connected) {
410 SSL_shutdown(session->ti.tls);
411 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200412 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100413
414 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200415 break;
416#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100417 case NC_TI_NONE:
418 ERRINT;
419 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200420 }
Michal Vasko428087d2016-01-14 16:04:28 +0100421
Michal Vasko11d142a2016-01-19 15:58:24 +0100422 if (session->side == NC_SERVER) {
423 nc_ctx_lock(-1, NULL);
424 }
Radek Krejciac6d3472015-10-22 15:47:18 +0200425 lydict_remove(session->ctx, session->username);
426 lydict_remove(session->ctx, session->host);
Michal Vasko11d142a2016-01-19 15:58:24 +0100427 if (session->side == NC_SERVER) {
428 nc_ctx_unlock();
429 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200430
431 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100432 if (session->ti_lock) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100433 pthread_mutex_unlock(session->ti_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100434 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100435 pthread_mutex_destroy(session->ti_lock);
436 free(session->ti_lock);
437 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200438 }
439
440 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
441 ly_ctx_destroy(session->ctx);
442 }
443
444 free(session);
445}
446
Michal Vasko086311b2016-01-08 09:53:11 +0100447static void
448add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
449{
450 if (*count == *size) {
451 *size += 5;
452 *cpblts = realloc(*cpblts, *size * sizeof **cpblts);
453 }
454
455 if (capab) {
456 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
457 } else {
458 (*cpblts)[*count] = NULL;
459 }
460 ++(*count);
461}
462
463static const char **
464create_cpblts(struct ly_ctx *ctx)
465{
466 struct lyd_node *child, *child2, *yanglib;
467 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
468 const char **cpblts;
469 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100470 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100471 char str[512];
472
Michal Vasko11d142a2016-01-19 15:58:24 +0100473 nc_ctx_lock(-1, NULL);
474
Michal Vasko086311b2016-01-08 09:53:11 +0100475 yanglib = ly_ctx_info(ctx);
476 if (!yanglib) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100477 nc_ctx_unlock();
Michal Vasko086311b2016-01-08 09:53:11 +0100478 return NULL;
479 }
480
481 cpblts = malloc(size * sizeof *cpblts);
482 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
483 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
484 count = 2;
485
486 /* capabilities */
487
488 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
489 if (mod) {
490 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100491 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100492 }
493 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100494 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100495 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100496 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100497 }
498 }
499 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100500 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100501 }
502 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100503 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100504 }
505 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100506 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100507 }
508 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100509 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100510 }
511 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100512 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100513 }
514 }
515
516 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
517 if (mod) {
518 if (!server_opts.wd_basic_mode) {
519 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
520 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100521 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100522 switch (server_opts.wd_basic_mode) {
523 case NC_WD_ALL:
524 strcat(str, "?basic-mode=report-all");
525 break;
526 case NC_WD_TRIM:
527 strcat(str, "?basic-mode=trim");
528 break;
529 case NC_WD_EXPLICIT:
530 strcat(str, "?basic-mode=explicit");
531 break;
532 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100533 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100534 break;
535 }
536
537 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100538 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100539 if (server_opts.wd_also_supported & NC_WD_ALL) {
540 strcat(str, "report-all,");
541 }
542 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
543 strcat(str, "report-all-tagged,");
544 }
545 if (server_opts.wd_also_supported & NC_WD_TRIM) {
546 strcat(str, "trim,");
547 }
548 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
549 strcat(str, "explicit,");
550 }
551 str[strlen(str) - 1] = '\0';
552
553 add_cpblt(ctx, str, &cpblts, &size, &count);
554 }
555 }
556 }
557
Michal Vasko1a38c862016-01-15 15:50:07 +0100558 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100559 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100560 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100561 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100562 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100563 }
564 }
565
566 /* models */
567
568 LY_TREE_FOR(yanglib->child, child) {
569 if (!strcmp(child->schema->name, "module")) {
570 LY_TREE_FOR(child->child, child2) {
571 if (!strcmp(child2->schema->name, "namespace")) {
572 ns = (struct lyd_node_leaf_list *)child2;
573 } else if (!strcmp(child2->schema->name, "name")) {
574 name = (struct lyd_node_leaf_list *)child2;
575 } else if (!strcmp(child2->schema->name, "revision")) {
576 rev = (struct lyd_node_leaf_list *)child2;
577 } else if (!strcmp(child2->schema->name, "feature")) {
578 features = realloc(features, feat_count++ * sizeof *features);
579 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
580 }
581 }
582
583 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100584 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100585 continue;
586 }
587
Michal Vasko11d142a2016-01-19 15:58:24 +0100588 str_len = sprintf(str, "%s?module=%s&amp;revision=%s", ns->value_str, name->value_str, rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100589 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100590 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100591 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100592 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100593 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
594 ERRINT;
595 break;
596 }
Michal Vasko086311b2016-01-08 09:53:11 +0100597 if (i) {
598 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100599 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100600 }
601 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100602 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100603 }
604 }
605
606 add_cpblt(ctx, str, &cpblts, &size, &count);
607
608 ns = NULL;
609 name = NULL;
610 rev = NULL;
611 free(features);
612 features = NULL;
613 feat_count = 0;
614 }
615 }
616
617 lyd_free(yanglib);
618
Michal Vasko11d142a2016-01-19 15:58:24 +0100619 nc_ctx_unlock();
620
Michal Vasko086311b2016-01-08 09:53:11 +0100621 /* ending NULL capability */
622 add_cpblt(ctx, NULL, &cpblts, &size, &count);
623
624 return cpblts;
625}
626
Radek Krejci695d4fa2015-10-22 13:23:54 +0200627static int
628parse_cpblts(struct lyxml_elem *xml, const char ***list)
629{
630 struct lyxml_elem *cpblt;
631 int ver = -1;
632 int i = 0;
633
634 if (list) {
635 /* get the storage for server's capabilities */
636 LY_TREE_FOR(xml->child, cpblt) {
637 i++;
638 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100639 /* last item remains NULL */
640 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200641 if (!*list) {
642 ERRMEM;
643 return -1;
644 }
645 i = 0;
646 }
647
648 LY_TREE_FOR(xml->child, cpblt) {
649 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
650 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
651 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
652 return -1;
653 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
654 continue;
655 }
656
657 /* detect NETCONF version */
658 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
659 ver = 0;
660 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
661 ver = 1;
662 }
663
664 /* store capabilities */
665 if (list) {
666 (*list)[i] = cpblt->content;
667 cpblt->content = NULL;
668 i++;
669 }
670 }
671
672 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100673 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200674 }
675
676 return ver;
677}
678
679static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100680nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100681{
682 int r, i;
683 const char **cpblts;
684
Michal Vasko11d142a2016-01-19 15:58:24 +0100685 /* client side hello - send only NETCONF base capabilities */
686 cpblts = malloc(3 * sizeof *cpblts);
687 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
688 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
689 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100690
Michal Vasko11d142a2016-01-19 15:58:24 +0100691 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100692
Michal Vasko086311b2016-01-08 09:53:11 +0100693 for (i = 0; cpblts[i]; ++i) {
694 lydict_remove(session->ctx, cpblts[i]);
695 }
696 free(cpblts);
697
698 if (r) {
699 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100700 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100701
702 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100703}
704
705static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100706nc_send_server_hello(struct nc_session *session)
707{
708 int r, i;
709 const char **cpblts;
710
711 cpblts = create_cpblts(session->ctx);
712
713 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
714
715 nc_ctx_lock(-1, NULL);
716 for (i = 0; cpblts[i]; ++i) {
717 lydict_remove(session->ctx, cpblts[i]);
718 }
719 nc_ctx_unlock();
720 free(cpblts);
721
722 if (r) {
723 return NC_MSG_ERROR;
724 }
725
726 return NC_MSG_HELLO;
727}
728
729static NC_MSG_TYPE
730nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200731{
732 struct lyxml_elem *xml = NULL, *node;
733 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
734 int ver = -1;
735 char *str;
736 long long int id;
737 int flag = 0;
738
Michal Vasko05ba9df2016-01-13 14:40:27 +0100739 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200740
741 switch(msgtype) {
742 case NC_MSG_HELLO:
743 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100744 LY_TREE_FOR(xml->child, node) {
745 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
746 continue;
747 } else if (!strcmp(node->name, "session-id")) {
748 if (!node->content || !strlen(node->content)) {
749 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200750 goto error;
751 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100752 str = NULL;
753 id = strtoll(node->content, &str, 10);
754 if (*str || id < 1 || id > UINT32_MAX) {
755 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200756 goto error;
757 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100758 session->id = (uint32_t)id;
759 continue;
760 } else if (strcmp(node->name, "capabilities")) {
761 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200762 goto error;
763 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100764
765 if (flag) {
766 /* multiple capabilities elements */
767 ERR("Invalid <hello> message (multiple <capabilities> elements).");
768 goto error;
769 }
770 flag = 1;
771
772 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
773 goto error;
774 }
775 session->version = ver;
776 }
777
778 if (!session->id) {
779 ERR("Missing <session-id> in server's <hello>.");
780 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200781 }
782 break;
783 case NC_MSG_ERROR:
784 /* nothing special, just pass it out */
785 break;
786 default:
787 ERR("Unexpected message received instead of <hello>.");
788 msgtype = NC_MSG_ERROR;
789 }
790
791 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100792 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200793
794 return msgtype;
795
796error:
797 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100798 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200799
800 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200801}
802
Michal Vasko11d142a2016-01-19 15:58:24 +0100803static NC_MSG_TYPE
804nc_recv_server_hello(struct nc_session *session)
805{
806 struct lyxml_elem *xml = NULL, *node;
807 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
808 int ver = -1;
809 int flag = 0;
810
Michal Vaskoadb850e2016-01-20 14:06:32 +0100811 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100812
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100813 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100814 case NC_MSG_HELLO:
815 /* get know NETCONF version */
816 LY_TREE_FOR(xml->child, node) {
817 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
818 continue;
819 } else if (strcmp(node->name, "capabilities")) {
820 ERR("Unexpected <%s> element in client's <hello>.", node->name);
821 msgtype = NC_MSG_ERROR;
822 goto cleanup;
823 }
824
825 if (flag) {
826 /* multiple capabilities elements */
827 ERR("Invalid <hello> message (multiple <capabilities> elements).");
828 msgtype = NC_MSG_ERROR;
829 goto cleanup;
830 }
831 flag = 1;
832
833 if ((ver = parse_cpblts(node, NULL)) < 0) {
834 msgtype = NC_MSG_ERROR;
835 goto cleanup;
836 }
837 session->version = ver;
838 }
839 break;
840 case NC_MSG_ERROR:
841 /* nothing special, just pass it out */
842 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100843 case NC_MSG_WOULDBLOCK:
844 ERR("Client's <hello> timeout elapsed.");
845 msgtype = NC_MSG_ERROR;
846 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100847 default:
848 ERR("Unexpected message received instead of <hello>.");
849 msgtype = NC_MSG_ERROR;
850 }
851
852cleanup:
853 nc_ctx_lock(-1, NULL);
854 lyxml_free(session->ctx, xml);
855 nc_ctx_unlock();
856
857 return msgtype;
858}
859
Michal Vasko80cad7f2015-12-08 14:42:27 +0100860int
Michal Vasko086311b2016-01-08 09:53:11 +0100861nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100862{
Michal Vasko086311b2016-01-08 09:53:11 +0100863 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100864
Michal Vasko11d142a2016-01-19 15:58:24 +0100865 if (session->side == NC_CLIENT) {
866 type = nc_send_client_hello(session);
867 } else {
868 type = nc_send_server_hello(session);
869 }
870
Michal Vasko086311b2016-01-08 09:53:11 +0100871 if (type != NC_MSG_HELLO) {
872 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100873 }
874
Michal Vasko11d142a2016-01-19 15:58:24 +0100875 if (session->side == NC_CLIENT) {
876 type = nc_recv_client_hello(session);
877 } else {
878 type = nc_recv_server_hello(session);
879 }
880
Michal Vasko086311b2016-01-08 09:53:11 +0100881 if (type != NC_MSG_HELLO) {
882 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100883 }
884
Michal Vasko086311b2016-01-08 09:53:11 +0100885 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100886}
Michal Vasko086311b2016-01-08 09:53:11 +0100887
888#ifdef ENABLE_SSH
889
890API void
891nc_ssh_init(void)
892{
893 ssh_threads_set_callbacks(ssh_threads_get_pthread());
894 ssh_init();
895 ssh_set_log_level(verbose_level);
896}
897
898API void
899nc_ssh_destroy(void)
900{
901 ssh_finalize();
902}
903
904#endif /* ENABLE_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100905
906#ifdef ENABLE_TLS
907
908static pthread_mutex_t *tls_locks;
909
910static void
Michal Vaskob48aa812016-01-18 14:13:09 +0100911tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100912{
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100913 if (mode & CRYPTO_LOCK) {
914 pthread_mutex_lock(tls_locks + n);
915 } else {
916 pthread_mutex_unlock(tls_locks + n);
917 }
918}
919
920static unsigned long
921tls_thread_id_func(void)
922{
923 return (unsigned long)pthread_self();
924}
925
926API void
927nc_tls_init(void)
928{
929 int i;
930
931 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100932 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100933 SSL_library_init();
934
935 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
936 for (i = 0; i < CRYPTO_num_locks(); ++i) {
937 pthread_mutex_init(tls_locks + i, NULL);
938 }
939
940 CRYPTO_set_id_callback(tls_thread_id_func);
941 CRYPTO_set_locking_callback(tls_thread_locking_func);
942}
943
944API void
945nc_tls_destroy(void)
946{
947 int i;
948
949 CRYPTO_THREADID crypto_tid;
950
951 EVP_cleanup();
952 CRYPTO_cleanup_all_ex_data();
953 ERR_free_strings();
954 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
955 CRYPTO_THREADID_current(&crypto_tid);
956 ERR_remove_thread_state(&crypto_tid);
957
958 CRYPTO_set_id_callback(NULL);
959 CRYPTO_set_locking_callback(NULL);
960 for (i = 0; i < CRYPTO_num_locks(); ++i) {
961 pthread_mutex_destroy(tls_locks + i);
962 }
963 free(tls_locks);
964}
965
966#endif /* ENABLE_TLS */