blob: 4b44699ef2b2313e2533d39bd0c0171520fae6d6 [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"
30
Michal Vasko086311b2016-01-08 09:53:11 +010031#ifdef ENABLE_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020032
Michal Vasko086311b2016-01-08 09:53:11 +010033# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020034
Michal Vasko086311b2016-01-08 09:53:11 +010035#endif /* ENABLE_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020036
Michal Vaskoc14e3c82016-01-11 16:14:30 +010037#ifdef ENABLE_TLS
38
39# include <openssl/err.h>
40
41#endif /* ENABLE_TLS */
42
Michal Vasko086311b2016-01-08 09:53:11 +010043/* in seconds */
44#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020045
Michal Vasko05ba9df2016-01-13 14:40:27 +010046/* in milliseconds */
47#define NC_CLOSE_REPLY_TIMEOUT 200
48
Michal Vasko086311b2016-01-08 09:53:11 +010049extern struct nc_server_opts server_opts;
50
Michal Vasko8dadf782016-01-15 10:29:36 +010051API NC_STATUS
52nc_session_get_status(const struct nc_session *session)
53{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010054 if (!session) {
55 ERRARG;
56 return 0;
57 }
58
Michal Vasko8dadf782016-01-15 10:29:36 +010059 return session->status;
60}
61
62API uint32_t
63nc_session_get_id(const struct nc_session *session)
64{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010065 if (!session) {
66 ERRARG;
67 return 0;
68 }
69
Michal Vasko8dadf782016-01-15 10:29:36 +010070 return session->id;
71}
72
73API NC_TRANSPORT_IMPL
74nc_session_get_ti(const struct nc_session *session)
75{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010076 if (!session) {
77 ERRARG;
78 return 0;
79 }
80
Michal Vasko8dadf782016-01-15 10:29:36 +010081 return session->ti_type;
82}
83
84API const char *
85nc_session_get_username(const struct nc_session *session)
86{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010087 if (!session) {
88 ERRARG;
89 return NULL;
90 }
91
Michal Vasko8dadf782016-01-15 10:29:36 +010092 return session->username;
93}
94
95API const char *
96nc_session_get_host(const struct nc_session *session)
97{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010098 if (!session) {
99 ERRARG;
100 return NULL;
101 }
102
Michal Vasko8dadf782016-01-15 10:29:36 +0100103 return session->host;
104}
105
106API uint16_t
107nc_session_get_port(const struct nc_session *session)
108{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100109 if (!session) {
110 ERRARG;
111 return 0;
112 }
113
Michal Vasko8dadf782016-01-15 10:29:36 +0100114 return session->port;
115}
116
117API const char **
118nc_session_get_cpblts(const struct nc_session *session)
119{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100120 if (!session) {
121 ERRARG;
122 return NULL;
123 }
124
Michal Vasko8dadf782016-01-15 10:29:36 +0100125 return session->cpblts;
126}
127
128API const char *
129nc_session_cpblt(const struct nc_session *session, const char *capab)
130{
131 int i, len;
132
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100133 if (!session || !capab) {
134 ERRARG;
135 return NULL;
136 }
137
Michal Vasko8dadf782016-01-15 10:29:36 +0100138 len = strlen(capab);
139 for (i = 0; session->cpblts[i]; ++i) {
140 if (!strncmp(session->cpblts[i], capab, len)) {
141 return session->cpblts[i];
142 }
143 }
144
145 return NULL;
146}
147
Michal Vasko086311b2016-01-08 09:53:11 +0100148NC_MSG_TYPE
149nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200150{
Michal Vasko086311b2016-01-08 09:53:11 +0100151 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200152
Michal Vasko086311b2016-01-08 09:53:11 +0100153 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100154 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
155 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100156 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100157 }
158
Michal Vasko086311b2016-01-08 09:53:11 +0100159 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
160
161 if (r) {
162 return NC_MSG_ERROR;
163 }
164
165 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100166}
167
Radek Krejci5686ff72015-10-09 13:33:56 +0200168/*
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100169 * @return 1 - success
170 * 0 - timeout
171 * -1 - error
Radek Krejci5686ff72015-10-09 13:33:56 +0200172 */
Michal Vasko086311b2016-01-08 09:53:11 +0100173int
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100174nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed)
Radek Krejci206fcd62015-10-07 15:42:48 +0200175{
Michal Vasko58f31552016-01-19 12:39:15 +0100176 int ret;
177 struct timespec ts_timeout, ts_old, ts_new;
Radek Krejci206fcd62015-10-07 15:42:48 +0200178
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100179 if (timeout > 0) {
Michal Vasko58f31552016-01-19 12:39:15 +0100180 ts_timeout.tv_sec = timeout / 1000;
181 ts_timeout.tv_nsec = (timeout % 1000) * 1000000;
182
183 clock_gettime(CLOCK_REALTIME, &ts_old);
184
185 ret = pthread_mutex_timedlock(lock, &ts_timeout);
186
187 clock_gettime(CLOCK_REALTIME, &ts_new);
Radek Krejci206fcd62015-10-07 15:42:48 +0200188
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100189 if (elapsed) {
Michal Vasko58f31552016-01-19 12:39:15 +0100190 *elapsed += (ts_new.tv_sec - ts_old.tv_sec) * 1000;
191 *elapsed += (ts_new.tv_nsec - ts_old.tv_nsec) / 1000000;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100192 }
193 } else if (!timeout) {
194 ret = pthread_mutex_trylock(lock);
195 } else { /* timeout == -1 */
196 ret = pthread_mutex_lock(lock);
Radek Krejci5686ff72015-10-09 13:33:56 +0200197 }
Radek Krejci5686ff72015-10-09 13:33:56 +0200198
Michal Vasko58f31552016-01-19 12:39:15 +0100199 if (ret == ETIMEDOUT) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100200 /* timeout */
201 return 0;
202 } else if (ret) {
203 /* error */
Michal Vaskod083db62016-01-19 10:31:29 +0100204 ERR("Mutex lock failed (%s).", strerror(errno));
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100205 return -1;
206 }
207
208 /* ok */
209 return 1;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200210}
211
Radek Krejci695d4fa2015-10-22 13:23:54 +0200212API void
213nc_session_free(struct nc_session *session)
214{
215 int r, i;
Michal Vasko428087d2016-01-14 16:04:28 +0100216 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100217 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200218 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100219 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100220 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200221 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100222 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200223 void *p;
224
Michal Vasko428087d2016-01-14 16:04:28 +0100225 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200226 return;
227 }
228
229 /* mark session for closing */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100230 if (session->ti_lock) {
231 do {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100232 r = nc_timedlock(session->ti_lock, 0, NULL);
233 } while (!r);
234 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100235 return;
236 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200237 }
238
239 /* stop notifications loop if any */
240 if (session->notif) {
241 pthread_cancel(*session->notif);
242 pthread_join(*session->notif, NULL);
243 }
244
Michal Vasko428087d2016-01-14 16:04:28 +0100245 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200246 /* cleanup message queues */
247 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100248 for (contiter = session->notifs; contiter; ) {
249 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200250
Michal Vaskoad611702015-12-03 13:41:51 +0100251 p = contiter;
252 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200253 free(p);
254 }
255
256 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100257 for (contiter = session->replies; contiter; ) {
258 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200259
Michal Vaskoad611702015-12-03 13:41:51 +0100260 p = contiter;
261 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200262 free(p);
263 }
264
265 /* send closing info to the other side */
266 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
267 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100268 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200269 } else {
270 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100271 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100273 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100274 case NC_MSG_REPLY:
275 LY_TREE_FOR(rpl->child, child) {
276 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
277 break;
278 }
279 }
280 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100281 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100282 }
Michal Vaskoad611702015-12-03 13:41:51 +0100283 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100284 break;
285 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100286 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100287 break;
288 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100289 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100290 break;
291 default:
292 /* cannot happen */
293 break;
294 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200295 }
296
297 /* list of server's capabilities */
298 if (session->cpblts) {
299 for (i = 0; session->cpblts[i]; i++) {
300 lydict_remove(session->ctx, session->cpblts[i]);
301 }
302 free(session->cpblts);
303 }
304 }
305
306 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100307 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200308
309 /* transport implementation cleanup */
310 switch (session->ti_type) {
311 case NC_TI_FD:
312 /* nothing needed - file descriptors were provided by caller,
313 * so it is up to the caller to close them correctly
314 * TODO use callbacks
315 */
316 break;
317
Michal Vaskofb2fb762015-10-27 11:44:32 +0100318#ifdef ENABLE_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200319 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100320 if (connected) {
321 ssh_channel_free(session->ti.libssh.channel);
322 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200323 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
324 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
325 * it.
326 */
327 if (!session->ti.libssh.next) {
Michal Vasko428087d2016-01-14 16:04:28 +0100328 if (connected) {
329 ssh_disconnect(session->ti.libssh.session);
330 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200331 ssh_free(session->ti.libssh.session);
332 } else {
333 /* multiple NETCONF sessions on a single SSH session */
334 multisession = 1;
335 /* remove the session from the list */
336 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100337 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200338 /* there will be only one session */
339 siter->ti.libssh.next = NULL;
340 } else {
341 /* there are still multiple sessions, keep the ring list */
342 siter->ti.libssh.next = session->ti.libssh.next;
343 }
344 }
345 break;
346#endif
347
348#ifdef ENABLE_TLS
349 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100350 if (connected) {
351 SSL_shutdown(session->ti.tls);
352 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200353 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100354
355 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200356 break;
357#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100358 case NC_TI_NONE:
359 ERRINT;
360 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200361 }
Michal Vasko428087d2016-01-14 16:04:28 +0100362
Radek Krejciac6d3472015-10-22 15:47:18 +0200363 lydict_remove(session->ctx, session->username);
364 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200365
366 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100367 if (session->ti_lock) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100368 pthread_mutex_unlock(session->ti_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100369 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100370 pthread_mutex_destroy(session->ti_lock);
371 free(session->ti_lock);
372 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200373 }
374
375 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
376 ly_ctx_destroy(session->ctx);
377 }
378
379 free(session);
380}
381
Michal Vasko086311b2016-01-08 09:53:11 +0100382static void
383add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
384{
385 if (*count == *size) {
386 *size += 5;
387 *cpblts = realloc(*cpblts, *size * sizeof **cpblts);
388 }
389
390 if (capab) {
391 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
392 } else {
393 (*cpblts)[*count] = NULL;
394 }
395 ++(*count);
396}
397
398static const char **
399create_cpblts(struct ly_ctx *ctx)
400{
401 struct lyd_node *child, *child2, *yanglib;
402 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
403 const char **cpblts;
404 const struct lys_module *mod;
405 int size = 10, count, feat_count = 0, i;
406 char str[512];
407
408 yanglib = ly_ctx_info(ctx);
409 if (!yanglib) {
410 return NULL;
411 }
412
413 cpblts = malloc(size * sizeof *cpblts);
414 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
415 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
416 count = 2;
417
418 /* capabilities */
419
420 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
421 if (mod) {
422 if (lys_features_state(mod, "writable-running") == 1) {
423 add_cpblt(ctx, "urn:ietf:params:netconf:writable-running:1.0", &cpblts, &size, &count);
424 }
425 if (lys_features_state(mod, "candidate") == 1) {
426 add_cpblt(ctx, "urn:ietf:params:netconf:candidate:1.0", &cpblts, &size, &count);
427 if (lys_features_state(mod, "confirmed-commit") == 1) {
428 add_cpblt(ctx, "urn:ietf:params:netconf:confirmed-commit:1.1", &cpblts, &size, &count);
429 }
430 }
431 if (lys_features_state(mod, "rollback-on-error") == 1) {
432 add_cpblt(ctx, "urn:ietf:params:netconf:rollback-on-error:1.0", &cpblts, &size, &count);
433 }
434 if (lys_features_state(mod, "validate") == 1) {
435 add_cpblt(ctx, "urn:ietf:params:netconf:validate:1.1", &cpblts, &size, &count);
436 }
437 if (lys_features_state(mod, "startup") == 1) {
438 add_cpblt(ctx, "urn:ietf:params:netconf:startup:1.0", &cpblts, &size, &count);
439 }
440 if (lys_features_state(mod, "url") == 1) {
441 add_cpblt(ctx, "urn:ietf:params:netconf:url:1.0", &cpblts, &size, &count);
442 }
443 if (lys_features_state(mod, "xpath") == 1) {
444 add_cpblt(ctx, "urn:ietf:params:netconf:xpath:1.0", &cpblts, &size, &count);
445 }
446 }
447
448 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
449 if (mod) {
450 if (!server_opts.wd_basic_mode) {
451 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
452 } else {
453 strcpy(str, "urn:ietf:params:netconf:with-defaults:1.0");
454 switch (server_opts.wd_basic_mode) {
455 case NC_WD_ALL:
456 strcat(str, "?basic-mode=report-all");
457 break;
458 case NC_WD_TRIM:
459 strcat(str, "?basic-mode=trim");
460 break;
461 case NC_WD_EXPLICIT:
462 strcat(str, "?basic-mode=explicit");
463 break;
464 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100465 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100466 break;
467 }
468
469 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100470 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100471 if (server_opts.wd_also_supported & NC_WD_ALL) {
472 strcat(str, "report-all,");
473 }
474 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
475 strcat(str, "report-all-tagged,");
476 }
477 if (server_opts.wd_also_supported & NC_WD_TRIM) {
478 strcat(str, "trim,");
479 }
480 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
481 strcat(str, "explicit,");
482 }
483 str[strlen(str) - 1] = '\0';
484
485 add_cpblt(ctx, str, &cpblts, &size, &count);
486 }
487 }
488 }
489
Michal Vasko1a38c862016-01-15 15:50:07 +0100490 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100491 if (mod) {
492 add_cpblt(ctx, "urn:ietf:params:netconf:notification:1.0", &cpblts, &size, &count);
493 if (server_opts.interleave_capab) {
494 add_cpblt(ctx, "urn:ietf:params:netconf:interleave:1.0", &cpblts, &size, &count);
495 }
496 }
497
498 /* models */
499
500 LY_TREE_FOR(yanglib->child, child) {
501 if (!strcmp(child->schema->name, "module")) {
502 LY_TREE_FOR(child->child, child2) {
503 if (!strcmp(child2->schema->name, "namespace")) {
504 ns = (struct lyd_node_leaf_list *)child2;
505 } else if (!strcmp(child2->schema->name, "name")) {
506 name = (struct lyd_node_leaf_list *)child2;
507 } else if (!strcmp(child2->schema->name, "revision")) {
508 rev = (struct lyd_node_leaf_list *)child2;
509 } else if (!strcmp(child2->schema->name, "feature")) {
510 features = realloc(features, feat_count++ * sizeof *features);
511 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
512 }
513 }
514
515 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100516 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100517 continue;
518 }
519
Radek Krejcif9b28322016-01-08 14:56:43 +0100520 sprintf(str, "%s?module=%s&amp;revision=%s", ns->value_str, name->value_str, rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100521 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100522 strcat(str, "&amp;features=");
Michal Vasko086311b2016-01-08 09:53:11 +0100523 for (i = 0; i < feat_count; ++i) {
524 if (i) {
525 strcat(str, ",");
526 }
527 strcat(str, features[i]->value_str);
528 }
529 }
530
531 add_cpblt(ctx, str, &cpblts, &size, &count);
532
533 ns = NULL;
534 name = NULL;
535 rev = NULL;
536 free(features);
537 features = NULL;
538 feat_count = 0;
539 }
540 }
541
542 lyd_free(yanglib);
543
544 /* ending NULL capability */
545 add_cpblt(ctx, NULL, &cpblts, &size, &count);
546
547 return cpblts;
548}
549
Radek Krejci695d4fa2015-10-22 13:23:54 +0200550static int
551parse_cpblts(struct lyxml_elem *xml, const char ***list)
552{
553 struct lyxml_elem *cpblt;
554 int ver = -1;
555 int i = 0;
556
557 if (list) {
558 /* get the storage for server's capabilities */
559 LY_TREE_FOR(xml->child, cpblt) {
560 i++;
561 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100562 /* last item remains NULL */
563 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200564 if (!*list) {
565 ERRMEM;
566 return -1;
567 }
568 i = 0;
569 }
570
571 LY_TREE_FOR(xml->child, cpblt) {
572 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
573 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
574 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
575 return -1;
576 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
577 continue;
578 }
579
580 /* detect NETCONF version */
581 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
582 ver = 0;
583 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
584 ver = 1;
585 }
586
587 /* store capabilities */
588 if (list) {
589 (*list)[i] = cpblt->content;
590 cpblt->content = NULL;
591 i++;
592 }
593 }
594
595 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100596 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200597 }
598
599 return ver;
600}
601
602static NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100603nc_send_hello(struct nc_session *session)
604{
605 int r, i;
606 const char **cpblts;
607
608 if (session->side == NC_CLIENT) {
609 /* client side hello - send only NETCONF base capabilities */
610 cpblts = malloc(3 * sizeof *cpblts);
611 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
612 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
613 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100614
615 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100616 } else {
617 cpblts = create_cpblts(session->ctx);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100618
619 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100620 }
621
Michal Vasko086311b2016-01-08 09:53:11 +0100622 for (i = 0; cpblts[i]; ++i) {
623 lydict_remove(session->ctx, cpblts[i]);
624 }
625 free(cpblts);
626
627 if (r) {
628 return NC_MSG_ERROR;
629 } else {
630 return NC_MSG_HELLO;
631 }
632}
633
634static NC_MSG_TYPE
Radek Krejci695d4fa2015-10-22 13:23:54 +0200635nc_recv_hello(struct nc_session *session)
636{
637 struct lyxml_elem *xml = NULL, *node;
638 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
639 int ver = -1;
640 char *str;
641 long long int id;
642 int flag = 0;
643
Michal Vasko05ba9df2016-01-13 14:40:27 +0100644 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200645
646 switch(msgtype) {
647 case NC_MSG_HELLO:
648 /* parse <hello> data */
649 if (session->side == NC_SERVER) {
650 /* get know NETCONF version */
651 LY_TREE_FOR(xml->child, node) {
652 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
653 continue;
654 } else if (strcmp(node->name, "capabilities")) {
655 ERR("Unexpected <%s> element in client's <hello>.", node->name);
656 goto error;
657 }
658
659 if (flag) {
660 /* multiple capabilities elements */
Michal Vaskod083db62016-01-19 10:31:29 +0100661 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200662 goto error;
663 }
664 flag = 1;
665
Radek Krejci153f5ca2016-01-08 15:38:17 +0100666 if ((ver = parse_cpblts(node, NULL)) < 0) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200667 goto error;
668 }
669 session->version = ver;
670 }
671 } else { /* NC_CLIENT */
672 LY_TREE_FOR(xml->child, node) {
673 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
674 continue;
675 } else if (!strcmp(node->name, "session-id")) {
676 if (!node->content || !strlen(node->content)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100677 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200678 goto error;
679 }
680 str = NULL;
681 id = strtoll(node->content, &str, 10);
682 if (*str || id < 1 || id > UINT32_MAX) {
Michal Vaskod083db62016-01-19 10:31:29 +0100683 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200684 goto error;
685 }
686 session->id = (uint32_t)id;
687 continue;
688 } else if (strcmp(node->name, "capabilities")) {
689 ERR("Unexpected <%s> element in client's <hello>.", node->name);
690 goto error;
691 }
692
693 if (flag) {
694 /* multiple capabilities elements */
Michal Vaskod083db62016-01-19 10:31:29 +0100695 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200696 goto error;
697 }
698 flag = 1;
699
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100700 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200701 goto error;
702 }
703 session->version = ver;
704 }
705
706 if (!session->id) {
Michal Vaskod083db62016-01-19 10:31:29 +0100707 ERR("Missing <session-id> in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200708 goto error;
709 }
710 }
711 break;
712 case NC_MSG_ERROR:
713 /* nothing special, just pass it out */
714 break;
715 default:
716 ERR("Unexpected message received instead of <hello>.");
717 msgtype = NC_MSG_ERROR;
718 }
719
720 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100721 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200722
723 return msgtype;
724
725error:
726 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100727 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200728
729 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200730}
731
Michal Vasko80cad7f2015-12-08 14:42:27 +0100732int
Michal Vasko086311b2016-01-08 09:53:11 +0100733nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100734{
Michal Vasko086311b2016-01-08 09:53:11 +0100735 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100736
Michal Vasko086311b2016-01-08 09:53:11 +0100737 type = nc_send_hello(session);
738 if (type != NC_MSG_HELLO) {
739 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100740 }
741
Michal Vasko086311b2016-01-08 09:53:11 +0100742 type = nc_recv_hello(session);
743 if (type != NC_MSG_HELLO) {
744 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100745 }
746
Michal Vasko086311b2016-01-08 09:53:11 +0100747 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100748}
Michal Vasko086311b2016-01-08 09:53:11 +0100749
750#ifdef ENABLE_SSH
751
752API void
753nc_ssh_init(void)
754{
755 ssh_threads_set_callbacks(ssh_threads_get_pthread());
756 ssh_init();
757 ssh_set_log_level(verbose_level);
758}
759
760API void
761nc_ssh_destroy(void)
762{
763 ssh_finalize();
764}
765
766#endif /* ENABLE_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100767
768#ifdef ENABLE_TLS
769
770static pthread_mutex_t *tls_locks;
771
772static void
Michal Vaskob48aa812016-01-18 14:13:09 +0100773tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100774{
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100775 if (mode & CRYPTO_LOCK) {
776 pthread_mutex_lock(tls_locks + n);
777 } else {
778 pthread_mutex_unlock(tls_locks + n);
779 }
780}
781
782static unsigned long
783tls_thread_id_func(void)
784{
785 return (unsigned long)pthread_self();
786}
787
788API void
789nc_tls_init(void)
790{
791 int i;
792
793 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100794 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100795 SSL_library_init();
796
797 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
798 for (i = 0; i < CRYPTO_num_locks(); ++i) {
799 pthread_mutex_init(tls_locks + i, NULL);
800 }
801
802 CRYPTO_set_id_callback(tls_thread_id_func);
803 CRYPTO_set_locking_callback(tls_thread_locking_func);
804}
805
806API void
807nc_tls_destroy(void)
808{
809 int i;
810
811 CRYPTO_THREADID crypto_tid;
812
813 EVP_cleanup();
814 CRYPTO_cleanup_all_ex_data();
815 ERR_free_strings();
816 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
817 CRYPTO_THREADID_current(&crypto_tid);
818 ERR_remove_thread_state(&crypto_tid);
819
820 CRYPTO_set_id_callback(NULL);
821 CRYPTO_set_locking_callback(NULL);
822 for (i = 0; i < CRYPTO_num_locks(); ++i) {
823 pthread_mutex_destroy(tls_locks + i);
824 }
825 free(tls_locks);
826}
827
828#endif /* ENABLE_TLS */