blob: d02906b4bae286052627cbbf0b732ec22bbac6f8 [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>
Michal Vasko3512e402016-01-28 16:22:34 +010025#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020026#include <pthread.h>
Michal Vasko58f31552016-01-19 12:39:15 +010027#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020028#include <libyang/libyang.h>
29
Michal Vaskob48aa812016-01-18 14:13:09 +010030#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010031#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010032
Michal Vasko086311b2016-01-08 09:53:11 +010033#ifdef ENABLE_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020034
Michal Vasko086311b2016-01-08 09:53:11 +010035# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020036
Michal Vasko086311b2016-01-08 09:53:11 +010037#endif /* ENABLE_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020038
Michal Vaskoc14e3c82016-01-11 16:14:30 +010039#ifdef ENABLE_TLS
40
41# include <openssl/err.h>
42
43#endif /* ENABLE_TLS */
44
Michal Vasko086311b2016-01-08 09:53:11 +010045/* in seconds */
46#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020047
Michal Vasko05ba9df2016-01-13 14:40:27 +010048/* in milliseconds */
49#define NC_CLOSE_REPLY_TIMEOUT 200
50
Michal Vasko086311b2016-01-08 09:53:11 +010051extern struct nc_server_opts server_opts;
52
Michal Vasko96164bf2016-01-21 15:41:58 +010053/*
54 * @return 1 - success
55 * 0 - timeout
56 * -1 - error
57 */
58int
59nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed)
60{
61 int ret;
62 struct timespec ts_timeout, ts_old, ts_new;
63
64 if (timeout > 0) {
65 clock_gettime(CLOCK_REALTIME, &ts_timeout);
66
67 if (elapsed) {
68 ts_old = ts_timeout;
69 }
70
71 ts_timeout.tv_sec += timeout / 1000;
72 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
73
74 ret = pthread_mutex_timedlock(lock, &ts_timeout);
75
76 if (elapsed) {
77 clock_gettime(CLOCK_REALTIME, &ts_new);
78
79 *elapsed += (ts_new.tv_sec - ts_old.tv_sec) * 1000;
80 *elapsed += (ts_new.tv_nsec - ts_old.tv_nsec) / 1000000;
81 }
82 } else if (!timeout) {
83 ret = pthread_mutex_trylock(lock);
84 } else { /* timeout == -1 */
85 ret = pthread_mutex_lock(lock);
86 }
87
88 if (ret == ETIMEDOUT) {
89 /* timeout */
90 return 0;
91 } else if (ret) {
92 /* error */
93 ERR("Mutex lock failed (%s).", strerror(errno));
94 return -1;
95 }
96
97 /* ok */
98 return 1;
99}
100
101void
102nc_subtract_elapsed(int *timeout, struct timespec *old_ts)
103{
104 struct timespec new_ts;
105
106 clock_gettime(CLOCK_MONOTONIC_RAW, &new_ts);
107
108 *timeout -= (new_ts.tv_sec - old_ts->tv_sec) * 1000;
109 *timeout -= (new_ts.tv_nsec - old_ts->tv_nsec) / 1000000;
110
111 *old_ts = new_ts;
112}
113
Michal Vasko8dadf782016-01-15 10:29:36 +0100114API NC_STATUS
115nc_session_get_status(const struct nc_session *session)
116{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100117 if (!session) {
118 ERRARG;
119 return 0;
120 }
121
Michal Vasko8dadf782016-01-15 10:29:36 +0100122 return session->status;
123}
124
125API uint32_t
126nc_session_get_id(const struct nc_session *session)
127{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100128 if (!session) {
129 ERRARG;
130 return 0;
131 }
132
Michal Vasko8dadf782016-01-15 10:29:36 +0100133 return session->id;
134}
135
136API NC_TRANSPORT_IMPL
137nc_session_get_ti(const struct nc_session *session)
138{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100139 if (!session) {
140 ERRARG;
141 return 0;
142 }
143
Michal Vasko8dadf782016-01-15 10:29:36 +0100144 return session->ti_type;
145}
146
147API const char *
148nc_session_get_username(const struct nc_session *session)
149{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100150 if (!session) {
151 ERRARG;
152 return NULL;
153 }
154
Michal Vasko8dadf782016-01-15 10:29:36 +0100155 return session->username;
156}
157
158API const char *
159nc_session_get_host(const struct nc_session *session)
160{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100161 if (!session) {
162 ERRARG;
163 return NULL;
164 }
165
Michal Vasko8dadf782016-01-15 10:29:36 +0100166 return session->host;
167}
168
169API uint16_t
170nc_session_get_port(const struct nc_session *session)
171{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100172 if (!session) {
173 ERRARG;
174 return 0;
175 }
176
Michal Vasko8dadf782016-01-15 10:29:36 +0100177 return session->port;
178}
179
180API const char **
181nc_session_get_cpblts(const struct nc_session *session)
182{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100183 if (!session) {
184 ERRARG;
185 return NULL;
186 }
187
Michal Vasko8dadf782016-01-15 10:29:36 +0100188 return session->cpblts;
189}
190
191API const char *
192nc_session_cpblt(const struct nc_session *session, const char *capab)
193{
194 int i, len;
195
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100196 if (!session || !capab) {
197 ERRARG;
198 return NULL;
199 }
200
Michal Vasko8dadf782016-01-15 10:29:36 +0100201 len = strlen(capab);
202 for (i = 0; session->cpblts[i]; ++i) {
203 if (!strncmp(session->cpblts[i], capab, len)) {
204 return session->cpblts[i];
205 }
206 }
207
208 return NULL;
209}
210
Michal Vasko086311b2016-01-08 09:53:11 +0100211NC_MSG_TYPE
212nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200213{
Michal Vasko086311b2016-01-08 09:53:11 +0100214 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200215
Michal Vasko086311b2016-01-08 09:53:11 +0100216 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100217 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
218 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100219 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100220 }
221
Michal Vasko086311b2016-01-08 09:53:11 +0100222 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
223
224 if (r) {
225 return NC_MSG_ERROR;
226 }
227
228 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100229}
230
Radek Krejci695d4fa2015-10-22 13:23:54 +0200231API void
232nc_session_free(struct nc_session *session)
233{
234 int r, i;
Michal Vasko428087d2016-01-14 16:04:28 +0100235 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100236 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100237 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100238 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100239 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100240 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200241 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100242 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200243 void *p;
244
Michal Vasko428087d2016-01-14 16:04:28 +0100245 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200246 return;
247 }
248
249 /* mark session for closing */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100250 if (session->ti_lock) {
Michal Vasko4589bbe2016-01-29 09:41:30 +0100251 r = nc_timedlock(session->ti_lock, -1, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100252 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 */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100258 if (session->ntf_tid) {
259 tid = *session->ntf_tid;
260 free((pthread_t *)session->ntf_tid);
261 session->ntf_tid = NULL;
262 /* the thread now knows it should quit */
263
264 pthread_join(tid, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200265 }
266
Michal Vasko428087d2016-01-14 16:04:28 +0100267 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200268 /* cleanup message queues */
269 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100270 for (contiter = session->notifs; 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 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100279 for (contiter = session->replies; contiter; ) {
280 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200281
Michal Vaskoad611702015-12-03 13:41:51 +0100282 p = contiter;
283 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200284 free(p);
285 }
286
287 /* send closing info to the other side */
288 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
289 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100290 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200291 } else {
292 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100293 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200294 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100295 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100296 case NC_MSG_REPLY:
297 LY_TREE_FOR(rpl->child, child) {
298 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
299 break;
300 }
301 }
302 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100303 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100304 }
Michal Vaskoad611702015-12-03 13:41:51 +0100305 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100306 break;
307 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100308 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100309 break;
310 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100311 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100312 break;
313 default:
314 /* cannot happen */
315 break;
316 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200317 }
318
319 /* list of server's capabilities */
320 if (session->cpblts) {
321 for (i = 0; session->cpblts[i]; i++) {
322 lydict_remove(session->ctx, session->cpblts[i]);
323 }
324 free(session->cpblts);
325 }
326 }
327
328 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100329 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200330
331 /* transport implementation cleanup */
332 switch (session->ti_type) {
333 case NC_TI_FD:
334 /* nothing needed - file descriptors were provided by caller,
335 * so it is up to the caller to close them correctly
336 * TODO use callbacks
337 */
Michal Vasko3512e402016-01-28 16:22:34 +0100338 /* just to avoid compiler warning */
339 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100340 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200341 break;
342
Michal Vaskofb2fb762015-10-27 11:44:32 +0100343#ifdef ENABLE_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200344 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100345 if (connected) {
346 ssh_channel_free(session->ti.libssh.channel);
347 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200348 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
349 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
350 * it.
351 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100352 multisession = 0;
353 if (session->ti.libssh.next) {
354 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
355 if (siter->status != NC_STATUS_STARTING) {
356 multisession = 1;
357 break;
358 }
359 }
360 }
361
362 if (!multisession) {
363 /* it's not multisession yet, but we still need to free the starting sessions */
364 if (session->ti.libssh.next) {
365 do {
366 siter = session->ti.libssh.next;
367 session->ti.libssh.next = siter->ti.libssh.next;
368
369 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
370 if (session->side == NC_SERVER) {
371 nc_ctx_lock(-1, NULL);
372 }
373 lydict_remove(session->ctx, session->username);
374 lydict_remove(session->ctx, session->host);
375 if (session->side == NC_SERVER) {
376 nc_ctx_unlock();
377 }
378 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
379 ly_ctx_destroy(session->ctx);
380 }
381
382 free(siter);
383 } while (session->ti.libssh.next != session);
384 }
Michal Vasko428087d2016-01-14 16:04:28 +0100385 if (connected) {
386 ssh_disconnect(session->ti.libssh.session);
387 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200388 ssh_free(session->ti.libssh.session);
389 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200390 /* remove the session from the list */
391 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100392 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200393 /* there will be only one session */
394 siter->ti.libssh.next = NULL;
395 } else {
396 /* there are still multiple sessions, keep the ring list */
397 siter->ti.libssh.next = session->ti.libssh.next;
398 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100399 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
400 if (session->flags & NC_SESSION_SSH_MSG_CB) {
401 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
402 if (siter->ti.libssh.next == session) {
403 ERRINT;
404 break;
405 }
406 }
407 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
408 siter->flags |= NC_SESSION_SSH_MSG_CB;
409 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200410 }
411 break;
412#endif
413
414#ifdef ENABLE_TLS
415 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100416 if (connected) {
417 SSL_shutdown(session->ti.tls);
418 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200419 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100420
421 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200422 break;
423#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100424 case NC_TI_NONE:
425 ERRINT;
426 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200427 }
Michal Vasko428087d2016-01-14 16:04:28 +0100428
Michal Vasko11d142a2016-01-19 15:58:24 +0100429 if (session->side == NC_SERVER) {
430 nc_ctx_lock(-1, NULL);
431 }
Radek Krejciac6d3472015-10-22 15:47:18 +0200432 lydict_remove(session->ctx, session->username);
433 lydict_remove(session->ctx, session->host);
Michal Vasko11d142a2016-01-19 15:58:24 +0100434 if (session->side == NC_SERVER) {
435 nc_ctx_unlock();
436 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200437
438 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100439 if (session->ti_lock) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100440 pthread_mutex_unlock(session->ti_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100441 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100442 pthread_mutex_destroy(session->ti_lock);
443 free(session->ti_lock);
444 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200445 }
446
447 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
448 ly_ctx_destroy(session->ctx);
449 }
450
451 free(session);
452}
453
Michal Vasko086311b2016-01-08 09:53:11 +0100454static void
455add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
456{
457 if (*count == *size) {
458 *size += 5;
459 *cpblts = realloc(*cpblts, *size * sizeof **cpblts);
460 }
461
462 if (capab) {
463 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
464 } else {
465 (*cpblts)[*count] = NULL;
466 }
467 ++(*count);
468}
469
470static const char **
471create_cpblts(struct ly_ctx *ctx)
472{
473 struct lyd_node *child, *child2, *yanglib;
474 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
475 const char **cpblts;
476 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100477 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100478 char str[512];
479
Michal Vasko11d142a2016-01-19 15:58:24 +0100480 nc_ctx_lock(-1, NULL);
481
Michal Vasko086311b2016-01-08 09:53:11 +0100482 yanglib = ly_ctx_info(ctx);
483 if (!yanglib) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100484 nc_ctx_unlock();
Michal Vasko086311b2016-01-08 09:53:11 +0100485 return NULL;
486 }
487
488 cpblts = malloc(size * sizeof *cpblts);
489 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
490 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
491 count = 2;
492
493 /* capabilities */
494
495 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
496 if (mod) {
497 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100498 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100499 }
500 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100501 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100502 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100503 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100504 }
505 }
506 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100507 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100508 }
509 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100510 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100511 }
512 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100513 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100514 }
515 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100516 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100517 }
518 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100519 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100520 }
521 }
522
523 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
524 if (mod) {
525 if (!server_opts.wd_basic_mode) {
526 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
527 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100528 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100529 switch (server_opts.wd_basic_mode) {
530 case NC_WD_ALL:
531 strcat(str, "?basic-mode=report-all");
532 break;
533 case NC_WD_TRIM:
534 strcat(str, "?basic-mode=trim");
535 break;
536 case NC_WD_EXPLICIT:
537 strcat(str, "?basic-mode=explicit");
538 break;
539 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100540 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100541 break;
542 }
543
544 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100545 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100546 if (server_opts.wd_also_supported & NC_WD_ALL) {
547 strcat(str, "report-all,");
548 }
549 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
550 strcat(str, "report-all-tagged,");
551 }
552 if (server_opts.wd_also_supported & NC_WD_TRIM) {
553 strcat(str, "trim,");
554 }
555 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
556 strcat(str, "explicit,");
557 }
558 str[strlen(str) - 1] = '\0';
559
560 add_cpblt(ctx, str, &cpblts, &size, &count);
561 }
562 }
563 }
564
Michal Vasko1a38c862016-01-15 15:50:07 +0100565 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100566 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100567 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100568 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100569 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100570 }
571 }
572
573 /* models */
574
575 LY_TREE_FOR(yanglib->child, child) {
576 if (!strcmp(child->schema->name, "module")) {
577 LY_TREE_FOR(child->child, child2) {
578 if (!strcmp(child2->schema->name, "namespace")) {
579 ns = (struct lyd_node_leaf_list *)child2;
580 } else if (!strcmp(child2->schema->name, "name")) {
581 name = (struct lyd_node_leaf_list *)child2;
582 } else if (!strcmp(child2->schema->name, "revision")) {
583 rev = (struct lyd_node_leaf_list *)child2;
584 } else if (!strcmp(child2->schema->name, "feature")) {
585 features = realloc(features, feat_count++ * sizeof *features);
586 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
587 }
588 }
589
590 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100591 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100592 continue;
593 }
594
Michal Vasko11d142a2016-01-19 15:58:24 +0100595 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 +0100596 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100597 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100598 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100599 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100600 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
601 ERRINT;
602 break;
603 }
Michal Vasko086311b2016-01-08 09:53:11 +0100604 if (i) {
605 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100606 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100607 }
608 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100609 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100610 }
611 }
612
613 add_cpblt(ctx, str, &cpblts, &size, &count);
614
615 ns = NULL;
616 name = NULL;
617 rev = NULL;
618 free(features);
619 features = NULL;
620 feat_count = 0;
621 }
622 }
623
624 lyd_free(yanglib);
625
Michal Vasko11d142a2016-01-19 15:58:24 +0100626 nc_ctx_unlock();
627
Michal Vasko086311b2016-01-08 09:53:11 +0100628 /* ending NULL capability */
629 add_cpblt(ctx, NULL, &cpblts, &size, &count);
630
631 return cpblts;
632}
633
Radek Krejci695d4fa2015-10-22 13:23:54 +0200634static int
635parse_cpblts(struct lyxml_elem *xml, const char ***list)
636{
637 struct lyxml_elem *cpblt;
638 int ver = -1;
639 int i = 0;
640
641 if (list) {
642 /* get the storage for server's capabilities */
643 LY_TREE_FOR(xml->child, cpblt) {
644 i++;
645 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100646 /* last item remains NULL */
647 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200648 if (!*list) {
649 ERRMEM;
650 return -1;
651 }
652 i = 0;
653 }
654
655 LY_TREE_FOR(xml->child, cpblt) {
656 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
657 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
658 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
659 return -1;
660 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
661 continue;
662 }
663
664 /* detect NETCONF version */
665 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
666 ver = 0;
667 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
668 ver = 1;
669 }
670
671 /* store capabilities */
672 if (list) {
673 (*list)[i] = cpblt->content;
674 cpblt->content = NULL;
675 i++;
676 }
677 }
678
679 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100680 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200681 }
682
683 return ver;
684}
685
686static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100687nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100688{
689 int r, i;
690 const char **cpblts;
691
Michal Vasko11d142a2016-01-19 15:58:24 +0100692 /* client side hello - send only NETCONF base capabilities */
693 cpblts = malloc(3 * sizeof *cpblts);
694 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
695 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
696 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100697
Michal Vasko11d142a2016-01-19 15:58:24 +0100698 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100699
Michal Vasko086311b2016-01-08 09:53:11 +0100700 for (i = 0; cpblts[i]; ++i) {
701 lydict_remove(session->ctx, cpblts[i]);
702 }
703 free(cpblts);
704
705 if (r) {
706 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100707 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100708
709 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100710}
711
712static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100713nc_send_server_hello(struct nc_session *session)
714{
715 int r, i;
716 const char **cpblts;
717
718 cpblts = create_cpblts(session->ctx);
719
720 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
721
722 nc_ctx_lock(-1, NULL);
723 for (i = 0; cpblts[i]; ++i) {
724 lydict_remove(session->ctx, cpblts[i]);
725 }
726 nc_ctx_unlock();
727 free(cpblts);
728
729 if (r) {
730 return NC_MSG_ERROR;
731 }
732
733 return NC_MSG_HELLO;
734}
735
736static NC_MSG_TYPE
737nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200738{
739 struct lyxml_elem *xml = NULL, *node;
740 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
741 int ver = -1;
742 char *str;
743 long long int id;
744 int flag = 0;
745
Michal Vasko05ba9df2016-01-13 14:40:27 +0100746 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200747
748 switch(msgtype) {
749 case NC_MSG_HELLO:
750 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100751 LY_TREE_FOR(xml->child, node) {
752 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
753 continue;
754 } else if (!strcmp(node->name, "session-id")) {
755 if (!node->content || !strlen(node->content)) {
756 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200757 goto error;
758 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100759 str = NULL;
760 id = strtoll(node->content, &str, 10);
761 if (*str || id < 1 || id > UINT32_MAX) {
762 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200763 goto error;
764 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100765 session->id = (uint32_t)id;
766 continue;
767 } else if (strcmp(node->name, "capabilities")) {
768 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200769 goto error;
770 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100771
772 if (flag) {
773 /* multiple capabilities elements */
774 ERR("Invalid <hello> message (multiple <capabilities> elements).");
775 goto error;
776 }
777 flag = 1;
778
779 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
780 goto error;
781 }
782 session->version = ver;
783 }
784
785 if (!session->id) {
786 ERR("Missing <session-id> in server's <hello>.");
787 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200788 }
789 break;
790 case NC_MSG_ERROR:
791 /* nothing special, just pass it out */
792 break;
793 default:
794 ERR("Unexpected message received instead of <hello>.");
795 msgtype = NC_MSG_ERROR;
796 }
797
798 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100799 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200800
801 return msgtype;
802
803error:
804 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100805 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200806
807 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200808}
809
Michal Vasko11d142a2016-01-19 15:58:24 +0100810static NC_MSG_TYPE
811nc_recv_server_hello(struct nc_session *session)
812{
813 struct lyxml_elem *xml = NULL, *node;
814 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
815 int ver = -1;
816 int flag = 0;
817
Michal Vaskoadb850e2016-01-20 14:06:32 +0100818 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100819
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100820 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100821 case NC_MSG_HELLO:
822 /* get know NETCONF version */
823 LY_TREE_FOR(xml->child, node) {
824 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
825 continue;
826 } else if (strcmp(node->name, "capabilities")) {
827 ERR("Unexpected <%s> element in client's <hello>.", node->name);
828 msgtype = NC_MSG_ERROR;
829 goto cleanup;
830 }
831
832 if (flag) {
833 /* multiple capabilities elements */
834 ERR("Invalid <hello> message (multiple <capabilities> elements).");
835 msgtype = NC_MSG_ERROR;
836 goto cleanup;
837 }
838 flag = 1;
839
840 if ((ver = parse_cpblts(node, NULL)) < 0) {
841 msgtype = NC_MSG_ERROR;
842 goto cleanup;
843 }
844 session->version = ver;
845 }
846 break;
847 case NC_MSG_ERROR:
848 /* nothing special, just pass it out */
849 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100850 case NC_MSG_WOULDBLOCK:
851 ERR("Client's <hello> timeout elapsed.");
852 msgtype = NC_MSG_ERROR;
853 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100854 default:
855 ERR("Unexpected message received instead of <hello>.");
856 msgtype = NC_MSG_ERROR;
857 }
858
859cleanup:
860 nc_ctx_lock(-1, NULL);
861 lyxml_free(session->ctx, xml);
862 nc_ctx_unlock();
863
864 return msgtype;
865}
866
Michal Vasko80cad7f2015-12-08 14:42:27 +0100867int
Michal Vasko086311b2016-01-08 09:53:11 +0100868nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100869{
Michal Vasko086311b2016-01-08 09:53:11 +0100870 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100871
Michal Vasko11d142a2016-01-19 15:58:24 +0100872 if (session->side == NC_CLIENT) {
873 type = nc_send_client_hello(session);
874 } else {
875 type = nc_send_server_hello(session);
876 }
877
Michal Vasko086311b2016-01-08 09:53:11 +0100878 if (type != NC_MSG_HELLO) {
879 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100880 }
881
Michal Vasko11d142a2016-01-19 15:58:24 +0100882 if (session->side == NC_CLIENT) {
883 type = nc_recv_client_hello(session);
884 } else {
885 type = nc_recv_server_hello(session);
886 }
887
Michal Vasko086311b2016-01-08 09:53:11 +0100888 if (type != NC_MSG_HELLO) {
889 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100890 }
891
Michal Vasko086311b2016-01-08 09:53:11 +0100892 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100893}
Michal Vasko086311b2016-01-08 09:53:11 +0100894
895#ifdef ENABLE_SSH
896
897API void
898nc_ssh_init(void)
899{
900 ssh_threads_set_callbacks(ssh_threads_get_pthread());
901 ssh_init();
902 ssh_set_log_level(verbose_level);
903}
904
905API void
906nc_ssh_destroy(void)
907{
908 ssh_finalize();
909}
910
911#endif /* ENABLE_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100912
913#ifdef ENABLE_TLS
914
915static pthread_mutex_t *tls_locks;
916
917static void
Michal Vaskob48aa812016-01-18 14:13:09 +0100918tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100919{
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100920 if (mode & CRYPTO_LOCK) {
921 pthread_mutex_lock(tls_locks + n);
922 } else {
923 pthread_mutex_unlock(tls_locks + n);
924 }
925}
926
927static unsigned long
928tls_thread_id_func(void)
929{
930 return (unsigned long)pthread_self();
931}
932
933API void
934nc_tls_init(void)
935{
936 int i;
937
938 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100939 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100940 SSL_library_init();
941
942 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
943 for (i = 0; i < CRYPTO_num_locks(); ++i) {
944 pthread_mutex_init(tls_locks + i, NULL);
945 }
946
947 CRYPTO_set_id_callback(tls_thread_id_func);
948 CRYPTO_set_locking_callback(tls_thread_locking_func);
949}
950
951API void
952nc_tls_destroy(void)
953{
954 int i;
955
956 CRYPTO_THREADID crypto_tid;
957
958 EVP_cleanup();
959 CRYPTO_cleanup_all_ex_data();
960 ERR_free_strings();
961 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
962 CRYPTO_THREADID_current(&crypto_tid);
963 ERR_remove_thread_state(&crypto_tid);
964
965 CRYPTO_set_id_callback(NULL);
966 CRYPTO_set_locking_callback(NULL);
967 for (i = 0; i < CRYPTO_num_locks(); ++i) {
968 pthread_mutex_destroy(tls_locks + i);
969 }
970 free(tls_locks);
971}
972
973#endif /* ENABLE_TLS */