blob: 9a5fa83252d5aac2bd3825044d74254107c6a36a [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
Radek Krejcifd5b6682017-06-13 15:52:53 +020015#define _GNU_SOURCE
Michal Vasko086311b2016-01-08 09:53:11 +010016#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010020#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010021#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020026#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <sys/types.h>
28#include <unistd.h>
29#include <arpa/inet.h>
30#include <poll.h>
31
32#include <libyang/libyang.h>
33
Michal Vasko086311b2016-01-08 09:53:11 +010034#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010035#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010036#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010037
Michal Vasko80ef5d22016-01-18 09:21:02 +010038static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
39
Radek Krejci62aa0642017-05-25 16:33:49 +020040#ifdef NC_ENABLED_SSH
41int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
42char *sshauth_password(const char *username, const char *hostname, void *priv);
43char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
44char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
45#endif /* NC_ENABLED_SSH */
46
47static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
48static pthread_key_t nc_client_context_key;
49#ifdef __linux__
50static struct nc_client_context context_main = {
51 /* .opts zeroed */
52#ifdef NC_ENABLED_SSH
53 .ssh_opts = {
54 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
55 .auth_hostkey_check = sshauth_hostkey_check,
56 .auth_password = sshauth_password,
57 .auth_interactive = sshauth_interactive,
58 .auth_privkey_passphrase = sshauth_privkey_passphrase
59 },
60 .ssh_ch_opts = {
61 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
62 .auth_hostkey_check = sshauth_hostkey_check,
63 .auth_password = sshauth_password,
64 .auth_interactive = sshauth_interactive,
65 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020066 },
Radek Krejci62aa0642017-05-25 16:33:49 +020067#endif /* NC_ENABLED_SSH */
68 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020069 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020070};
71#endif
72
73static void
74nc_client_context_free(void *ptr)
75{
76 struct nc_client_context *c = (struct nc_client_context *)ptr;
77
Radek Krejci5cebc6b2017-05-26 13:24:38 +020078 if (--(c->refcount)) {
79 /* still used */
80 return;
81 }
82
Radek Krejci62aa0642017-05-25 16:33:49 +020083#ifdef __linux__
84 /* in __linux__ we use static memory in the main thread,
85 * so this check is for programs terminating the main()
86 * function by pthread_exit() :)
87 */
88 if (c != &context_main)
89#endif
90 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +020091 /* for the main thread the same is done in nc_client_destroy() */
Radek Krejci62aa0642017-05-25 16:33:49 +020092 nc_client_set_schema_searchpath(NULL);
93#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
94 nc_client_ch_del_bind(NULL, 0, 0);
95#endif
96#ifdef NC_ENABLED_SSH
97 nc_client_ssh_destroy_opts();
98#endif
99#ifdef NC_ENABLED_TLS
100 nc_client_tls_destroy_opts();
101#endif
102 free(c);
103 }
104}
105
106static void
107nc_client_context_createkey(void)
108{
109 int r;
110
111 /* initiate */
112 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
113 pthread_setspecific(nc_client_context_key, NULL);
114}
115
116struct nc_client_context *
117nc_client_context_location(void)
118{
119 struct nc_client_context *e;
120
121 pthread_once(&nc_client_context_once, nc_client_context_createkey);
122 e = pthread_getspecific(nc_client_context_key);
123 if (!e) {
124 /* prepare ly_err storage */
125#ifdef __linux__
126 if (getpid() == syscall(SYS_gettid)) {
127 /* main thread - use global variable instead of thread-specific variable. */
128 e = &context_main;
129 } else
130#endif /* __linux__ */
131 {
132 e = calloc(1, sizeof *e);
133 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200134 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200135#ifdef NC_ENABLED_SSH
136 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
137 e->ssh_opts.auth_pref[0].value = 3;
138 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
139 e->ssh_opts.auth_pref[1].value = 2;
140 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
141 e->ssh_opts.auth_pref[2].value = 1;
142 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
143 e->ssh_opts.auth_password = sshauth_password;
144 e->ssh_opts.auth_interactive = sshauth_interactive;
145 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
146
147 /* callhome settings are the same except the inverted auth methods preferences */
148 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
149 e->ssh_ch_opts.auth_pref[0].value = 1;
150 e->ssh_ch_opts.auth_pref[1].value = 2;
151 e->ssh_ch_opts.auth_pref[2].value = 3;
152#endif /* NC_ENABLED_SSH */
153 }
154 pthread_setspecific(nc_client_context_key, e);
155 }
156
157 return e;
158}
159
160#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100161
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162API void *
163nc_client_get_thread_context(void)
164{
165 return nc_client_context_location();
166}
167
168API void
169nc_client_set_thread_context(void *context)
170{
171 struct nc_client_context *old, *new;
172
173 if (!context) {
174 ERRARG(context);
175 return;
176 }
177
178 new = (struct nc_client_context *)context;
179 old = nc_client_context_location();
180 if (old == new) {
181 /* nothing to change */
182 return;
183 }
184
185 /* replace old by new, increase reference counter in the newly set context */
186 nc_client_context_free(old);
187 new->refcount++;
188 pthread_setspecific(nc_client_context_key, new);
189}
190
Radek Krejcifd5b6682017-06-13 15:52:53 +0200191int
192nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
193{
194 /* assign context (dicionary needed for handshake) */
195 if (!ctx) {
Radek Krejci3222b7d2017-09-21 16:04:30 +0200196 ctx = ly_ctx_new(NULL, 0);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200197 if (!ctx) {
198 return EXIT_FAILURE;
199 }
200
201 /* user path must be first, the first path is used to store schemas retreived via get-schema */
202 if (client_opts.schema_searchpath) {
203 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
204 }
205 ly_ctx_set_searchdir(ctx, SCHEMAS_DIR);
206
207 /* set callback for getting schemas, if provided */
208 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
209 } else {
210 session->flags |= NC_SESSION_SHAREDCTX;
211 }
212
213 session->ctx = ctx;
214
215 return EXIT_SUCCESS;
216}
217
Michal Vasko086311b2016-01-08 09:53:11 +0100218API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100219nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100220{
Michal Vasko3031aae2016-01-27 16:07:18 +0100221 if (client_opts.schema_searchpath) {
222 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 }
Michal Vasko086311b2016-01-08 09:53:11 +0100224
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100225 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100226 client_opts.schema_searchpath = strdup(path);
227 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100228 ERRMEM;
229 return 1;
230 }
231 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100232 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100233 }
234
235 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100236}
237
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100238API const char *
239nc_client_get_schema_searchpath(void)
240{
241 return client_opts.schema_searchpath;
242}
243
Radek Krejcifd5b6682017-06-13 15:52:53 +0200244API int
245nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100246{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200247 client_opts.schema_clb = clb;
248 if (clb) {
249 client_opts.schema_clb_data = user_data;
250 } else {
251 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100252 }
253
254 return 0;
255}
256
Radek Krejcifd5b6682017-06-13 15:52:53 +0200257API ly_module_imp_clb
258nc_client_get_schema_callback(void **user_data)
259{
260 if (user_data) {
261 (*user_data) = client_opts.schema_clb_data;
262 }
263 return client_opts.schema_clb;
264}
265
Michal Vasko1aaa6602016-02-09 11:04:33 +0100266/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100267static int
Michal Vasko1b2ddc92017-05-24 08:59:49 +0200268ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100269{
270 int i;
271 const struct lys_module *ietfnc;
272
Radek Krejci3222b7d2017-09-21 16:04:30 +0200273 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100274 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100275 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
276 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100277 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100278 }
279 }
280 if (!ietfnc) {
281 ERR("Loading base NETCONF schema failed.");
282 return 1;
283 }
284
285 /* set supported capabilities from ietf-netconf */
286 for (i = 0; cpblts[i]; ++i) {
287 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
288 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
289 lys_features_enable(ietfnc, "writable-running");
290 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
291 lys_features_enable(ietfnc, "candidate");
292 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
293 lys_features_enable(ietfnc, "confirmed-commit");
294 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
295 lys_features_enable(ietfnc, "rollback-on-error");
296 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
297 lys_features_enable(ietfnc, "validate");
298 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
299 lys_features_enable(ietfnc, "startup");
300 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
301 lys_features_enable(ietfnc, "url");
302 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
303 lys_features_enable(ietfnc, "xpath");
304 }
305 }
306 }
307
308 return 0;
309}
310
311static char *
Radek Krejcifd5b6682017-06-13 15:52:53 +0200312getschema_module_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev,
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200313 void *user_data, LYS_INFORMAT *format, void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100314{
315 struct nc_session *session = (struct nc_session *)user_data;
316 struct nc_rpc *rpc;
317 struct nc_reply *reply;
318 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200319 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200320 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100321 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200322 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100323 uint64_t msgid;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200324 char *filename = NULL;
325 const char * const *searchdirs;
326 FILE *output;
Michal Vasko086311b2016-01-08 09:53:11 +0100327
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200328 if (submod_name) {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200329 rpc = nc_rpc_getschema(submod_name, submod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200330 } else {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200331 rpc = nc_rpc_getschema(mod_name, mod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200332 }
Michal Vasko086311b2016-01-08 09:53:11 +0100333
334 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
335 usleep(1000);
336 }
337 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100338 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100339 nc_rpc_free(rpc);
340 return NULL;
341 }
342
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200343 do {
Michal Vaskof471fa02017-02-15 10:48:12 +0100344 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200345 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100346 nc_rpc_free(rpc);
347 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100348 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100349 return NULL;
350 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100351 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100352 return NULL;
353 }
354
Michal Vasko998ba412016-09-16 12:00:07 +0200355 switch (reply->type) {
356 case NC_RPL_OK:
357 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", session->id);
358 nc_reply_free(reply);
359 return NULL;
360 case NC_RPL_DATA:
361 /* fine */
362 break;
363 case NC_RPL_ERROR:
364 error_rpl = (struct nc_reply_error *)reply;
365 if (error_rpl->count) {
366 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
367 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
368 } else {
369 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", session->id);
370 }
371 nc_reply_free(reply);
372 return NULL;
373 case NC_RPL_NOTIF:
374 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100375 nc_reply_free(reply);
376 return NULL;
377 }
378
Michal Vasko086311b2016-01-08 09:53:11 +0100379 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200380 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
381 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
382 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
383 nc_reply_free(reply);
384 return NULL;
385 }
Radek Krejci539efb62016-08-24 15:05:16 +0200386 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
387 switch (get_schema_data->value_type) {
388 case LYD_ANYDATA_CONSTSTRING:
389 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200390 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200391 break;
392 case LYD_ANYDATA_DATATREE:
393 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
394 break;
395 case LYD_ANYDATA_XML:
396 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
397 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200398 case LYD_ANYDATA_JSON:
399 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200400 case LYD_ANYDATA_SXML:
401 case LYD_ANYDATA_SXMLD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200402 ERRINT;
403 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200404 }
Michal Vasko086311b2016-01-08 09:53:11 +0100405 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100406 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200407 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100408
Radek Krejcifd5b6682017-06-13 15:52:53 +0200409 /* try to store the model_data into local schema repository */
410 if (model_data) {
411 searchdirs = ly_ctx_get_searchdirs(session->ctx);
412 asprintf(&filename, "%s/%s%s%s.yang", searchdirs ? searchdirs[0] : ".", mod_name,
413 mod_rev ? "@" : "", mod_rev ? mod_rev : "");
414 output = fopen(filename, "w");
415 if (!output) {
416 WRN("Unable to store \"%s\" as a local copy of schema retreived via <get-schema> (%s).",
417 filename, strerror(errno));
418 } else {
419 fputs(model_data, output);
420 fclose(output);
421 }
422 free(filename);
423 }
424
Michal Vasko086311b2016-01-08 09:53:11 +0100425 return model_data;
426}
427
Radek Krejcifd5b6682017-06-13 15:52:53 +0200428/* SCHEMAS_DIR not used (implicitly) */
429static int
430nc_ctx_fill_cpblts(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
431{
432 int ret = 1;
433 LY_LOG_LEVEL verb;
434 const struct lys_module *mod;
435 char *ptr, *ptr2;
436 const char *module_cpblt;
437 char *name = NULL, *revision = NULL, *features = NULL;
438 unsigned int u;
439
440 for (u = 0; session->opts.client.cpblts[u]; ++u) {
441 module_cpblt = strstr(session->opts.client.cpblts[u], "module=");
442 /* this capability requires a module */
443 if (!module_cpblt) {
444 continue;
445 }
446
447 /* get module name */
448 ptr = (char *)module_cpblt + 7;
449 ptr2 = strchr(ptr, '&');
450 if (!ptr2) {
451 ptr2 = ptr + strlen(ptr);
452 }
453 free(name);
454 name = strndup(ptr, ptr2 - ptr);
455
456 /* get module revision */
457 free(revision); revision = NULL;
458 ptr = strstr(module_cpblt, "revision=");
459 if (ptr) {
460 ptr += 9;
461 ptr2 = strchr(ptr, '&');
462 if (!ptr2) {
463 ptr2 = ptr + strlen(ptr);
464 }
465 revision = strndup(ptr, ptr2 - ptr);
466 }
467
Radek Krejci3222b7d2017-09-21 16:04:30 +0200468 mod = ly_ctx_get_module(session->ctx, name, revision, 0);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200469 if (mod) {
470 if (!mod->implemented) {
471 /* make the present module implemented */
472 if (lys_set_implemented(mod)) {
473 ERR("Failed to implement model \"%s\".", mod->name);
474 goto cleanup;
475 }
476 }
477 } else {
478 /* missing implemented module, load it ... */
479
480 /* expecting errors here */
481 verb = ly_verb(LY_LLSILENT);
482
483 /* 1) using only searchpaths */
484 mod = ly_ctx_load_module(session->ctx, name, revision);
485
486 /* 2) using user callback */
487 if (!mod && user_clb) {
488 ly_ctx_set_module_imp_clb(session->ctx, user_clb, user_data);
489 mod = ly_ctx_load_module(session->ctx, name, revision);
490 }
491
492 /* 3) using get-schema callback */
493 if (!mod) {
494 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
495 mod = ly_ctx_load_module(session->ctx, name, revision);
496 }
497
498 /* revert the changed verbosity level */
499 ly_verb(verb);
500
501 /* unset callback back to use searchpath */
502 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
503 }
504
505 if (!mod) {
Michal Vasko60f66602017-10-17 13:52:18 +0200506 if (session->status != NC_STATUS_RUNNING) {
507 /* something bad heppened, discard the session */
508 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
509 ret = 1;
510 goto cleanup;
511 }
512
Radek Krejcifd5b6682017-06-13 15:52:53 +0200513 /* all loading ways failed, the schema will be ignored in the received data */
514 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
515 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
516
517 /* TODO: maybe re-print hidden error messages */
518 } else {
519 /* set features - first disable all to enable specified then */
520 lys_features_disable(mod, "*");
521
522 ptr = strstr(module_cpblt, "features=");
523 if (ptr) {
524 ptr += 9;
525 ptr2 = strchr(ptr, '&');
526 if (!ptr2) {
527 ptr2 = ptr + strlen(ptr);
528 }
529 free(features);
530 features = strndup(ptr, ptr2 - ptr);
531
532 /* basically manual strtok_r (to avoid macro) */
533 ptr2 = features;
534 for (ptr = features; *ptr; ++ptr) {
535 if (*ptr == ',') {
536 *ptr = '\0';
537 /* remember last feature */
538 ptr2 = ptr + 1;
539 }
540 }
541
542 ptr = features;
543 while (1) {
544 lys_features_enable(mod, ptr);
545 if (ptr != ptr2) {
546 ptr += strlen(ptr) + 1;
547 } else {
548 break;
549 }
550 }
551 }
552 }
553 }
554
555 ret = 0;
556
557cleanup:
558 free(name);
559 free(revision);
560 free(features);
561
562 return ret;
563}
564
565static int
566nc_ctx_fill_yl(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
567{
568 int ret = 1;
569 LY_LOG_LEVEL verb;
570 struct nc_rpc *rpc = NULL;
571 struct nc_reply *reply = NULL;
572 struct nc_reply_error *error_rpl;
573 struct nc_reply_data *data_rpl;
574 NC_MSG_TYPE msg;
575 uint64_t msgid;
576 struct lyd_node *data = NULL, *iter;
577 struct ly_set *modules = NULL, *imports = NULL, *features = NULL;
578 unsigned int u, v;
579 const char *name, *revision;
580 int implemented, imports_flag = 0;
581 const struct lys_module *mod;
582
583 /* get yang-library data from the server */
584 rpc = nc_rpc_get("/ietf-yang-library:*//.", 0, NC_PARAMTYPE_CONST);
585 if (!rpc) {
586 goto cleanup;
587 }
588
589 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
590 usleep(1000);
591 }
592 if (msg == NC_MSG_ERROR) {
Michal Vasko60f66602017-10-17 13:52:18 +0200593 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200594 session->id);
595 goto cleanup;
596 }
597
598 do {
599 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
600 } while (msg == NC_MSG_NOTIF);
601 if (msg == NC_MSG_WOULDBLOCK) {
602 ERR("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
603 goto cleanup;
604 } else if (msg == NC_MSG_ERROR) {
605 ERR("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
606 goto cleanup;
607 }
608
609 switch (reply->type) {
610 case NC_RPL_OK:
611 ERR("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
612 goto cleanup;
613 case NC_RPL_DATA:
614 /* fine */
615 break;
616 case NC_RPL_ERROR:
617 error_rpl = (struct nc_reply_error *)reply;
618 if (error_rpl->count) {
619 ERR("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
620 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
621 } else {
622 ERR("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
623 }
624 goto cleanup;
625 case NC_RPL_NOTIF:
626 ERR("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
627 goto cleanup;
628 }
629
630 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko09319322017-10-02 09:26:54 +0200631 if (!data_rpl->data || strcmp(data_rpl->data->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200632 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
633 goto cleanup;
634 }
635
Michal Vaskof4c8b792017-07-28 14:57:22 +0200636 modules = lyd_find_path(data_rpl->data, "/ietf-yang-library:modules-state/module");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200637 if (!modules || !modules->number) {
638 ERR("No yang-library modules information for session %u.", session->id);
639 goto cleanup;
640 }
641
642 features = ly_set_new();
643 imports = ly_set_new();
644
645parse:
646 for (u = modules->number - 1; u < modules->number; u--) {
647 name = revision = NULL;
648 ly_set_clean(features);
649 implemented = 0;
650
651 /* store the data */
652 LY_TREE_FOR(modules->set.d[u]->child, iter) {
653 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
654 /* ignore empty nodes */
655 continue;
656 }
657 if (!strcmp(iter->schema->name, "name")) {
658 name = ((struct lyd_node_leaf_list *)iter)->value_str;
659 } else if (!strcmp(iter->schema->name, "revision")) {
660 revision = ((struct lyd_node_leaf_list *)iter)->value_str;
661 } else if (!strcmp(iter->schema->name, "conformance-type")) {
662 implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
663 } else if (!strcmp(iter->schema->name, "feature")) {
664 ly_set_add(features, (void*)((struct lyd_node_leaf_list *)iter)->value_str, LY_SET_OPT_USEASLIST);
665 }
666 }
667
Radek Krejci3222b7d2017-09-21 16:04:30 +0200668 mod = ly_ctx_get_module(session->ctx, name, revision, 0);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200669 if (mod) {
670 if (implemented && !mod->implemented) {
671 /* make the present module implemented */
672 if (lys_set_implemented(mod)) {
673 ERR("Failed to implement model \"%s\".", mod->name);
674 ret = -1; /* fatal error, not caused just by missing schema but by something in the context */
675 goto cleanup;
676 }
677 }
678 } else if (!mod && implemented) {
679 /* missing implemented module, load it ... */
680
681 /* expecting errors here */
682 verb = ly_verb(LY_LLSILENT);
683
684 /* 1) using only searchpaths */
685 mod = ly_ctx_load_module(session->ctx, name, revision);
686
687 /* 2) using user callback */
688 if (!mod && user_clb) {
689 ly_ctx_set_module_imp_clb(session->ctx, user_clb, user_data);
690 mod = ly_ctx_load_module(session->ctx, name, revision);
691 }
692
693 /* 3) using get-schema callback */
694 if (!mod) {
695 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
696 mod = ly_ctx_load_module(session->ctx, name, revision);
697 }
698
699 /* revert the changed verbosity level */
700 ly_verb(verb);
701
702 /* unset callback back to use searchpath */
703 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
704 } else { /* !mod && !implemented - will be loaded automatically, but remember to set features in the end */
Michal Vasko3c556372017-08-09 10:17:34 +0200705 if (imports_flag) {
706 ERR("Module \"%s@%s\" is supposed to be imported, but no other module imports it.",
707 name, revision ? revision : "<latest>");
708 ret = -1;
709 goto cleanup;
710 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200711 ly_set_add(imports, modules->set.d[u], LY_SET_OPT_USEASLIST);
712 continue;
713 }
714
715 if (!mod) {
716 /* all loading ways failed, the schema will be ignored in the received data */
717 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
718 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
719
720 /* TODO: maybe re-print hidden error messages */
721 } else {
722 /* set features - first disable all to enable specified then */
723 lys_features_disable(mod, "*");
724 for (v = 0; v < features->number; v++) {
725 lys_features_enable(mod, (const char*)features->set.g[v]);
726 }
727 }
728 }
729
730 if (!imports_flag && imports->number) {
731 /* even imported modules should be now loaded as dependency, so just go through
732 * the parsing again and just set the features */
733 ly_set_free(modules);
734 modules = imports;
735 imports = NULL;
736 imports_flag = 1;
737 goto parse;
738 }
739
740 /* done */
741 ret = 0;
742
743cleanup:
744 nc_rpc_free(rpc);
745 nc_reply_free(reply);
746 lyd_free_withsiblings(data);
747
748 ly_set_free(modules);
749 ly_set_free(imports);
750 ly_set_free(features);
751
Michal Vasko60f66602017-10-17 13:52:18 +0200752 if (session->status != NC_STATUS_RUNNING) {
753 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
754 ret = -1;
755 }
756
Radek Krejcifd5b6682017-06-13 15:52:53 +0200757 return ret;
758}
759
Michal Vasko086311b2016-01-08 09:53:11 +0100760int
761nc_ctx_check_and_fill(struct nc_session *session)
762{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200763 int i, get_schema_support = 0, yanglib_support = 0, ret = -1, r;
Michal Vaskocdeee432017-01-13 13:51:01 +0100764 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100765 void *old_data = NULL;
766
Michal Vasko2e6defd2016-10-07 15:48:15 +0200767 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100768
Radek Krejcifd5b6682017-06-13 15:52:53 +0200769 /* store the original user's callback, here we will be switching between searchpath, user callback
770 * and get-schema callback */
771 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
772 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); /* unset callback, so we prefer local searchpath */
773
Michal Vasko086311b2016-01-08 09:53:11 +0100774 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200775 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200776 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100777 get_schema_support = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200778 if (yanglib_support) {
779 break;
780 }
781 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-yang-library?", 46)) {
782 yanglib_support = 1;
783 if (get_schema_support) {
784 break;
785 }
Michal Vasko086311b2016-01-08 09:53:11 +0100786 }
787 }
788
789 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200790 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200791 if (!lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100792 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200793 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100794 }
795 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200796 /* yang-library present does not need to be checked, it is one of the libyang's internal modules,
797 * so it is always present */
Michal Vasko086311b2016-01-08 09:53:11 +0100798
799 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200800 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200801 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100802 }
803
Radek Krejcifd5b6682017-06-13 15:52:53 +0200804 if (yanglib_support && get_schema_support) {
805 /* load schemas according to the ietf-yang-library data, which are more precise than capabilities list */
806 r = nc_ctx_fill_yl(session, old_clb, old_data);
807 if (r == -1) {
808 goto cleanup;
809 } else if (r == 1) {
Michal Vasko60f66602017-10-17 13:52:18 +0200810 VRB("Session %d: trying to use capabilities instead of ietf-yang-library data.", nc_session_get_id(session));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200811 /* try to use standard capabilities */
812 goto capabilities;
813 }
814 } else {
815capabilities:
Michal Vaskoef578332016-01-25 13:20:09 +0100816
Michal Vasko60f66602017-10-17 13:52:18 +0200817 if (nc_ctx_fill_cpblts(session, old_clb, old_data)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200818 goto cleanup;
Michal Vaskoef578332016-01-25 13:20:09 +0100819 }
Michal Vasko086311b2016-01-08 09:53:11 +0100820 }
821
Radek Krejcifd5b6682017-06-13 15:52:53 +0200822 /* succsess */
823 ret = 0;
824
Michal Vaskoeee99412016-11-21 10:19:43 +0100825 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
826 WRN("Some models failed to be loaded, any data from these models (and any other unknown) will be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +0100827 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200828
829cleanup:
830 /* set user callback back */
831 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
832
Michal Vaskoef578332016-01-25 13:20:09 +0100833 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100834}
835
836API struct nc_session *
837nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
838{
Michal Vaskod083db62016-01-19 10:31:29 +0100839 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100840
Michal Vasko45e53ae2016-04-07 11:46:03 +0200841 if (fdin < 0) {
842 ERRARG("fdin");
843 return NULL;
844 } else if (fdout < 0) {
845 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100846 return NULL;
847 }
848
849 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +0100850 session = nc_new_session(0);
Michal Vasko086311b2016-01-08 09:53:11 +0100851 if (!session) {
852 ERRMEM;
853 return NULL;
854 }
855 session->status = NC_STATUS_STARTING;
856 session->side = NC_CLIENT;
857
858 /* transport specific data */
859 session->ti_type = NC_TI_FD;
Michal Vaskoade892d2017-02-22 13:40:35 +0100860 pthread_mutex_init(session->ti_lock, NULL);
861 pthread_cond_init(session->ti_cond, NULL);
862 *session->ti_inuse = 0;
863
Michal Vasko086311b2016-01-08 09:53:11 +0100864 session->ti.fd.in = fdin;
865 session->ti.fd.out = fdout;
866
867 /* assign context (dicionary needed for handshake) */
868 if (!ctx) {
Radek Krejci3222b7d2017-09-21 16:04:30 +0200869 ctx = ly_ctx_new(SCHEMAS_DIR, 0);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100870 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200871 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100872 /* that's just it */
873 goto fail;
874 }
Michal Vasko086311b2016-01-08 09:53:11 +0100875 } else {
876 session->flags |= NC_SESSION_SHAREDCTX;
877 }
878 session->ctx = ctx;
879
880 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200881 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100882 goto fail;
883 }
884 session->status = NC_STATUS_RUNNING;
885
Michal Vaskoef578332016-01-25 13:20:09 +0100886 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100887 goto fail;
888 }
889
890 return session;
891
892fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100893 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100894 return NULL;
895}
896
897int
Michal Vaskof05562c2016-01-20 12:06:43 +0100898nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100899{
Michal Vasko0190bc32016-03-02 15:47:49 +0100900 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100901 struct addrinfo hints, *res_list, *res;
902 char port_s[6]; /* length of string representation of short int */
903
904 snprintf(port_s, 6, "%u", port);
905
906 /* Connect to a server */
907 memset(&hints, 0, sizeof hints);
908 hints.ai_family = AF_UNSPEC;
909 hints.ai_socktype = SOCK_STREAM;
910 hints.ai_protocol = IPPROTO_TCP;
911 i = getaddrinfo(host, port_s, &hints, &res_list);
912 if (i != 0) {
913 ERR("Unable to translate the host address (%s).", gai_strerror(i));
914 return -1;
915 }
916
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200917 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100918 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
919 if (sock == -1) {
920 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200921 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100922 }
923
924 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
925 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100926 close(sock);
927 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200928 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100929 }
930
Michal Vasko0190bc32016-03-02 15:47:49 +0100931 /* make the socket non-blocking */
932 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
933 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100934 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200935 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100936 return -1;
937 }
938
Michal Vasko086311b2016-01-08 09:53:11 +0100939 /* we're done, network connection established */
940 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100941 }
942
Michal Vasko29af44b2016-10-13 10:59:55 +0200943 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100944 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
Michal Vasko086311b2016-01-08 09:53:11 +0100945 }
946 freeaddrinfo(res_list);
947
948 return sock;
949}
950
Michal Vasko086311b2016-01-08 09:53:11 +0100951static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100952get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100953{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100954 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100955 char *ptr;
956 const char *str_msgid;
957 uint64_t cur_msgid;
958 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100959 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100960 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
961
Michal Vaskoade892d2017-02-22 13:40:35 +0100962 r = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100963 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100964 /* error */
965 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100966 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100967 /* timeout */
968 return NC_MSG_WOULDBLOCK;
969 }
970
971 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200972 if (!msgid && session->opts.client.notifs) {
973 cont = session->opts.client.notifs;
974 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100975
Michal Vasko71ba2da2016-05-04 10:53:16 +0200976 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100977 free(cont);
978
Michal Vasko71ba2da2016-05-04 10:53:16 +0200979 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100980 }
981
982 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200983 if (msgid && session->opts.client.replies) {
984 cont = session->opts.client.replies;
985 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100986
Michal Vasko71ba2da2016-05-04 10:53:16 +0200987 xml = cont->msg;
988 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100989
Michal Vasko71ba2da2016-05-04 10:53:16 +0200990 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100991 }
992
Michal Vasko71ba2da2016-05-04 10:53:16 +0200993 if (!msgtype) {
994 /* read message from wire */
995 msgtype = nc_read_msg_poll(session, timeout, &xml);
996 }
Michal Vasko086311b2016-01-08 09:53:11 +0100997
998 /* we read rpc-reply, want a notif */
999 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001000 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001001 while (*cont_ptr) {
1002 cont_ptr = &((*cont_ptr)->next);
1003 }
1004 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001005 if (!*cont_ptr) {
1006 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001007 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001008 lyxml_free(session->ctx, xml);
1009 return NC_MSG_ERROR;
1010 }
Michal Vasko086311b2016-01-08 09:53:11 +01001011 (*cont_ptr)->msg = xml;
1012 (*cont_ptr)->next = NULL;
1013 }
1014
1015 /* we read notif, want a rpc-reply */
1016 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001017 if (!session->opts.client.ntf_tid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001018 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +01001019 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001020 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001021 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001022 }
Michal Vasko086311b2016-01-08 09:53:11 +01001023
Michal Vasko2e6defd2016-10-07 15:48:15 +02001024 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001025 while (*cont_ptr) {
1026 cont_ptr = &((*cont_ptr)->next);
1027 }
1028 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001029 if (!cont_ptr) {
1030 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001031 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001032 lyxml_free(session->ctx, xml);
1033 return NC_MSG_ERROR;
1034 }
Michal Vasko086311b2016-01-08 09:53:11 +01001035 (*cont_ptr)->msg = xml;
1036 (*cont_ptr)->next = NULL;
1037 }
1038
Michal Vaskoade892d2017-02-22 13:40:35 +01001039 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01001040
1041 switch (msgtype) {
1042 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001043 if (!msgid) {
1044 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001045 }
Michal Vasko086311b2016-01-08 09:53:11 +01001046 break;
1047
1048 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001049 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001050 /* check message-id */
1051 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1052 if (!str_msgid) {
1053 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
1054 msgtype = NC_MSG_REPLY_ERR_MSGID;
1055 } else {
1056 cur_msgid = strtoul(str_msgid, &ptr, 10);
1057 if (cur_msgid != msgid) {
1058 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1059 session->id, str_msgid);
1060 msgtype = NC_MSG_REPLY_ERR_MSGID;
1061 }
1062 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001063 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001064 }
Michal Vasko086311b2016-01-08 09:53:11 +01001065 break;
1066
1067 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001068 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001069 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001070 msgtype = NC_MSG_ERROR;
1071 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001072
1073 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001074 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001075 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001076 msgtype = NC_MSG_ERROR;
1077 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001078
1079 default:
1080 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1081 * NC_MSG_NONE is not returned by nc_read_msg()
1082 */
1083 break;
1084 }
1085
1086 return msgtype;
1087}
1088
1089/* cannot strictly fail, but does not need to fill any error parameter at all */
1090static void
1091parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1092{
1093 struct lyxml_elem *iter, *next, *info;
1094
1095 LY_TREE_FOR(xml->child, iter) {
1096 if (!iter->ns) {
1097 if (iter->content) {
1098 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1099 } else {
1100 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1101 }
1102 continue;
1103 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1104 if (iter->content) {
1105 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1106 iter->name, iter->content, iter->ns->value);
1107 } else {
1108 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1109 }
1110 continue;
1111 }
1112
1113 if (!strcmp(iter->name, "error-type")) {
1114 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1115 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1116 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1117 } else if (err->type) {
1118 WRN("<rpc-error> <error-type> duplicated.");
1119 } else {
1120 err->type = lydict_insert(ctx, iter->content, 0);
1121 }
1122 } else if (!strcmp(iter->name, "error-tag")) {
1123 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1124 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1125 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1126 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1127 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1128 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1129 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1130 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1131 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1132 && strcmp(iter->content, "malformed-message"))) {
1133 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1134 } else if (err->tag) {
1135 WRN("<rpc-error> <error-tag> duplicated.");
1136 } else {
1137 err->tag = lydict_insert(ctx, iter->content, 0);
1138 }
1139 } else if (!strcmp(iter->name, "error-severity")) {
1140 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1141 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1142 } else if (err->severity) {
1143 WRN("<rpc-error> <error-severity> duplicated.");
1144 } else {
1145 err->severity = lydict_insert(ctx, iter->content, 0);
1146 }
1147 } else if (!strcmp(iter->name, "error-app-tag")) {
1148 if (err->apptag) {
1149 WRN("<rpc-error> <error-app-tag> duplicated.");
1150 } else {
1151 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1152 }
1153 } else if (!strcmp(iter->name, "error-path")) {
1154 if (err->path) {
1155 WRN("<rpc-error> <error-path> duplicated.");
1156 } else {
1157 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1158 }
1159 } else if (!strcmp(iter->name, "error-message")) {
1160 if (err->message) {
1161 WRN("<rpc-error> <error-message> duplicated.");
1162 } else {
1163 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001164 if (err->message_lang) {
1165 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1166 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001167 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1168 }
1169 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1170 }
1171 } else if (!strcmp(iter->name, "error-info")) {
1172 LY_TREE_FOR_SAFE(iter->child, next, info) {
1173 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1174 if (!strcmp(info->name, "session-id")) {
1175 if (err->sid) {
1176 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1177 } else {
1178 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1179 }
Michal Vasko630485f2018-01-03 14:28:32 +01001180 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001181 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001182 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1183 if (!err->attr) {
1184 ERRMEM;
1185 return;
1186 }
Michal Vasko086311b2016-01-08 09:53:11 +01001187 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1188 } else if (!strcmp(info->name, "bad-element")) {
1189 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001190 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1191 if (!err->elem) {
1192 ERRMEM;
1193 return;
1194 }
Michal Vasko086311b2016-01-08 09:53:11 +01001195 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1196 } else if (!strcmp(info->name, "bad-namespace")) {
1197 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001198 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1199 if (!err->ns) {
1200 ERRMEM;
1201 return;
1202 }
Michal Vasko086311b2016-01-08 09:53:11 +01001203 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1204 } else {
1205 if (info->content) {
1206 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1207 info->name, info->content);
1208 } else {
1209 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1210 }
1211 }
1212 } else {
1213 lyxml_unlink(ctx, info);
1214 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001215 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1216 if (!err->other) {
1217 ERRMEM;
1218 return;
1219 }
Michal Vasko086311b2016-01-08 09:53:11 +01001220 err->other[err->other_count - 1] = info;
1221 }
1222 }
1223 } else {
1224 if (iter->content) {
1225 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1226 } else {
1227 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1228 }
1229 }
1230 }
1231}
1232
1233static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001234parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001235{
1236 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001237 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001238 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001239 struct nc_reply_data *data_rpl;
1240 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001241 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001242 int i;
1243
1244 if (!xml->child) {
1245 ERR("An empty <rpc-reply>.");
1246 return NULL;
1247 }
1248
1249 /* rpc-error */
1250 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1251 /* count and check elements */
1252 i = 0;
1253 LY_TREE_FOR(xml->child, iter) {
1254 if (strcmp(iter->name, "rpc-error")) {
1255 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1256 return NULL;
1257 } else if (!iter->ns) {
1258 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1259 return NULL;
1260 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1261 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1262 return NULL;
1263 }
1264 ++i;
1265 }
1266
1267 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001268 if (!error_rpl) {
1269 ERRMEM;
1270 return NULL;
1271 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001272 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001273 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001274 if (!error_rpl->err) {
1275 ERRMEM;
1276 free(error_rpl);
1277 return NULL;
1278 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001279 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001280 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001281 reply = (struct nc_reply *)error_rpl;
1282
1283 i = 0;
1284 LY_TREE_FOR(xml->child, iter) {
1285 parse_rpc_error(ctx, iter, error_rpl->err + i);
1286 ++i;
1287 }
1288
1289 /* ok */
1290 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1291 if (xml->child->next) {
1292 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1293 return NULL;
1294 }
1295 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001296 if (!reply) {
1297 ERRMEM;
1298 return NULL;
1299 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001300 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001301
1302 /* some RPC output */
1303 } else {
1304 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001305 case NC_RPC_ACT_GENERIC:
1306 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001307
1308 if (rpc_gen->has_data) {
Michal Vaskoe1708602016-10-18 12:17:22 +02001309 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +01001310 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001311 rpc_act = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
Michal Vaskoe1708602016-10-18 12:17:22 +02001312 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001313 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001314 return NULL;
1315 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001316 }
Michal Vasko086311b2016-01-08 09:53:11 +01001317 break;
1318
1319 case NC_RPC_GETCONFIG:
1320 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001321 if (!xml->child->child) {
1322 /* we did not receive any data */
1323 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001324 if (!data_rpl) {
1325 ERRMEM;
1326 return NULL;
1327 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001328 data_rpl->type = NC_RPL_DATA;
1329 data_rpl->data = NULL;
1330 return (struct nc_reply *)data_rpl;
1331 }
1332
Michal Vasko086311b2016-01-08 09:53:11 +01001333 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001334 data = lyd_parse_xml(ctx, &xml->child->child,
1335 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001336 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001337 if (!data) {
1338 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1339 return NULL;
1340 }
1341 break;
1342
1343 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001344 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001345 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001346 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001347 return NULL;
1348 }
1349 break;
1350
1351 case NC_RPC_EDIT:
1352 case NC_RPC_COPY:
1353 case NC_RPC_DELETE:
1354 case NC_RPC_LOCK:
1355 case NC_RPC_UNLOCK:
1356 case NC_RPC_KILL:
1357 case NC_RPC_COMMIT:
1358 case NC_RPC_DISCARD:
1359 case NC_RPC_CANCEL:
1360 case NC_RPC_VALIDATE:
1361 case NC_RPC_SUBSCRIBE:
1362 /* there is no output defined */
1363 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1364 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001365 default:
1366 ERRINT;
1367 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001368 }
1369
1370 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001371 if (!data_rpl) {
1372 ERRMEM;
1373 return NULL;
1374 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001375 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001376 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001377 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001378 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001379 } else {
1380 /* <get>, <get-config> */
1381 data_rpl->data = data;
1382 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001383 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001384 if (!data_rpl->data) {
1385 ERR("Failed to parse <rpc-reply>.");
1386 free(data_rpl);
1387 return NULL;
1388 }
1389 reply = (struct nc_reply *)data_rpl;
1390 }
1391
1392 return reply;
1393}
1394
Radek Krejci53691be2016-02-22 13:58:37 +01001395#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001396
Michal Vasko3031aae2016-01-27 16:07:18 +01001397int
1398nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1399{
1400 int sock;
1401
Michal Vasko45e53ae2016-04-07 11:46:03 +02001402 if (!address) {
1403 ERRARG("address");
1404 return -1;
1405 } else if (!port) {
1406 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001407 return -1;
1408 }
1409
1410 sock = nc_sock_listen(address, port);
1411 if (sock == -1) {
1412 return -1;
1413 }
1414
1415 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001416 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1417 if (!client_opts.ch_binds) {
1418 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001419 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001420 return -1;
1421 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001422
Michal Vasko2e6defd2016-10-07 15:48:15 +02001423 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1424 if (!client_opts.ch_bind_ti) {
1425 ERRMEM;
1426 close(sock);
1427 return -1;
1428 }
1429 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1430
Michal Vasko3031aae2016-01-27 16:07:18 +01001431 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001432 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1433 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001434 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001435 return -1;
1436 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001437 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1438 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001439 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001440
1441 return 0;
1442}
1443
1444int
1445nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1446{
1447 uint32_t i;
1448 int ret = -1;
1449
1450 if (!address && !port && !ti) {
1451 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1452 close(client_opts.ch_binds[i].sock);
1453 free((char *)client_opts.ch_binds[i].address);
1454
1455 ret = 0;
1456 }
1457 free(client_opts.ch_binds);
1458 client_opts.ch_binds = NULL;
1459 client_opts.ch_bind_count = 0;
1460 } else {
1461 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1462 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1463 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001464 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001465 close(client_opts.ch_binds[i].sock);
1466 free((char *)client_opts.ch_binds[i].address);
1467
1468 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001469 if (!client_opts.ch_bind_count) {
1470 free(client_opts.ch_binds);
1471 client_opts.ch_binds = NULL;
1472 } else if (i < client_opts.ch_bind_count) {
1473 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1474 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1475 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001476
1477 ret = 0;
1478 }
1479 }
1480 }
1481
1482 return ret;
1483}
1484
1485API int
1486nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1487{
1488 int sock;
1489 char *host = NULL;
1490 uint16_t port, idx;
1491
Michal Vasko45e53ae2016-04-07 11:46:03 +02001492 if (!client_opts.ch_binds) {
1493 ERRINIT;
1494 return -1;
1495 } else if (!session) {
1496 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001497 return -1;
1498 }
1499
1500 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1501
Michal Vasko50456e82016-02-02 12:16:08 +01001502 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001503 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001504 return sock;
1505 }
1506
Radek Krejci53691be2016-02-22 13:58:37 +01001507#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001508 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001509 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001510 } else
1511#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001512#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001513 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001514 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001515 } else
1516#endif
1517 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001518 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001519 *session = NULL;
1520 }
1521
1522 free(host);
1523
1524 if (!(*session)) {
1525 return -1;
1526 }
1527
1528 return 1;
1529}
1530
Radek Krejci53691be2016-02-22 13:58:37 +01001531#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001532
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001533API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001534nc_session_get_cpblts(const struct nc_session *session)
1535{
1536 if (!session) {
1537 ERRARG("session");
1538 return NULL;
1539 }
1540
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001541 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001542}
1543
1544API const char *
1545nc_session_cpblt(const struct nc_session *session, const char *capab)
1546{
1547 int i, len;
1548
1549 if (!session) {
1550 ERRARG("session");
1551 return NULL;
1552 } else if (!capab) {
1553 ERRARG("capab");
1554 return NULL;
1555 }
1556
1557 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001558 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1559 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1560 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001561 }
1562 }
1563
1564 return NULL;
1565}
1566
Michal Vasko9cd26a82016-05-31 08:58:48 +02001567API int
1568nc_session_ntf_thread_running(const struct nc_session *session)
1569{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001570 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001571 ERRARG("session");
1572 return 0;
1573 }
1574
Michal Vasko2e6defd2016-10-07 15:48:15 +02001575 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001576}
1577
Michal Vaskob7558c52016-02-26 15:04:19 +01001578API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001579nc_client_init(void)
1580{
1581 nc_init();
1582}
1583
1584API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001585nc_client_destroy(void)
1586{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001587 nc_client_set_schema_searchpath(NULL);
1588#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1589 nc_client_ch_del_bind(NULL, 0, 0);
1590#endif
1591#ifdef NC_ENABLED_SSH
1592 nc_client_ssh_destroy_opts();
1593#endif
1594#ifdef NC_ENABLED_TLS
1595 nc_client_tls_destroy_opts();
1596#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001597 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001598}
1599
Michal Vasko086311b2016-01-08 09:53:11 +01001600API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001601nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, int parseroptions, struct nc_reply **reply)
Michal Vasko086311b2016-01-08 09:53:11 +01001602{
1603 struct lyxml_elem *xml;
1604 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1605
Michal Vasko45e53ae2016-04-07 11:46:03 +02001606 if (!session) {
1607 ERRARG("session");
1608 return NC_MSG_ERROR;
1609 } else if (!rpc) {
1610 ERRARG("rpc");
1611 return NC_MSG_ERROR;
1612 } else if (!reply) {
1613 ERRARG("reply");
1614 return NC_MSG_ERROR;
1615 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1616 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001617 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001618 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001619 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001620 return NC_MSG_ERROR;
1621 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001622 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001623 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1624 parseroptions &= LYD_OPT_STRICT;
1625 }
Michal Vasko41adf392017-01-17 10:39:04 +01001626 /* no mechanism to check external dependencies is provided */
1627 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001628 *reply = NULL;
1629
1630 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001631
Michal Vasko71ba2da2016-05-04 10:53:16 +02001632 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001633 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001634 lyxml_free(session->ctx, xml);
1635 if (!(*reply)) {
1636 return NC_MSG_ERROR;
1637 }
1638 }
1639
1640 return msgtype;
1641}
1642
1643API NC_MSG_TYPE
1644nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1645{
1646 struct lyxml_elem *xml, *ev_time;
1647 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1648
Michal Vasko45e53ae2016-04-07 11:46:03 +02001649 if (!session) {
1650 ERRARG("session");
1651 return NC_MSG_ERROR;
1652 } else if (!notif) {
1653 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001654 return NC_MSG_ERROR;
1655 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001656 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001657 return NC_MSG_ERROR;
1658 }
1659
1660 msgtype = get_msg(session, timeout, 0, &xml);
1661
1662 if (msgtype == NC_MSG_NOTIF) {
1663 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001664 if (!*notif) {
1665 ERRMEM;
1666 lyxml_free(session->ctx, xml);
1667 return NC_MSG_ERROR;
1668 }
Michal Vasko086311b2016-01-08 09:53:11 +01001669
1670 /* eventTime */
1671 LY_TREE_FOR(xml->child, ev_time) {
1672 if (!strcmp(ev_time->name, "eventTime")) {
1673 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1674 /* lyd_parse does not know this element */
1675 lyxml_free(session->ctx, ev_time);
1676 break;
1677 }
1678 }
1679 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001680 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001681 goto fail;
1682 }
1683
1684 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001685 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01001686 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001687 lyxml_free(session->ctx, xml);
1688 xml = NULL;
1689 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001690 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001691 goto fail;
1692 }
1693 }
1694
1695 return msgtype;
1696
1697fail:
1698 lydict_remove(session->ctx, (*notif)->datetime);
1699 lyd_free((*notif)->tree);
1700 free(*notif);
1701 *notif = NULL;
1702 lyxml_free(session->ctx, xml);
1703
1704 return NC_MSG_ERROR;
1705}
1706
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001707static void *
1708nc_recv_notif_thread(void *arg)
1709{
1710 struct nc_ntf_thread_arg *ntarg;
1711 struct nc_session *session;
1712 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1713 struct nc_notif *notif;
1714 NC_MSG_TYPE msgtype;
1715
1716 ntarg = (struct nc_ntf_thread_arg *)arg;
1717 session = ntarg->session;
1718 notif_clb = ntarg->notif_clb;
1719 free(ntarg);
1720
Michal Vasko2e6defd2016-10-07 15:48:15 +02001721 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001722 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001723 if (msgtype == NC_MSG_NOTIF) {
1724 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001725 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1726 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1727 nc_notif_free(notif);
1728 break;
1729 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001730 nc_notif_free(notif);
Michal Vasko132f7f42017-09-14 13:40:18 +02001731 } else if ((msgtype == NC_MSG_NOTIF) && (session->status != NC_STATUS_RUNNING)) {
1732 /* quit this thread once the session is broken */
1733 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001734 }
1735
1736 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1737 }
1738
Michal Vasko0651c902016-05-19 15:55:42 +02001739 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001740 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001741 return NULL;
1742}
1743
1744API int
1745nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1746{
1747 struct nc_ntf_thread_arg *ntarg;
1748 int ret;
1749
Michal Vasko45e53ae2016-04-07 11:46:03 +02001750 if (!session) {
1751 ERRARG("session");
1752 return -1;
1753 } else if (!notif_clb) {
1754 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001755 return -1;
1756 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1757 ERR("Session %u: invalid session to receive Notifications.", session->id);
1758 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001759 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001760 ERR("Session %u: separate notification thread is already running.", session->id);
1761 return -1;
1762 }
1763
1764 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001765 if (!ntarg) {
1766 ERRMEM;
1767 return -1;
1768 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001769 ntarg->session = session;
1770 ntarg->notif_clb = notif_clb;
1771
1772 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001773 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1774 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001775 ERRMEM;
1776 free(ntarg);
1777 return -1;
1778 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001779
Michal Vasko2e6defd2016-10-07 15:48:15 +02001780 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001781 if (ret) {
1782 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1783 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001784 free((pthread_t *)session->opts.client.ntf_tid);
1785 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001786 return -1;
1787 }
1788
1789 return 0;
1790}
1791
Michal Vasko086311b2016-01-08 09:53:11 +01001792API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001793nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001794{
1795 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001796 int ret;
Michal Vasko90e8e692016-07-13 12:27:57 +02001797 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001798 struct nc_rpc_getconfig *rpc_gc;
1799 struct nc_rpc_edit *rpc_e;
1800 struct nc_rpc_copy *rpc_cp;
1801 struct nc_rpc_delete *rpc_del;
1802 struct nc_rpc_lock *rpc_lock;
1803 struct nc_rpc_get *rpc_g;
1804 struct nc_rpc_kill *rpc_k;
1805 struct nc_rpc_commit *rpc_com;
1806 struct nc_rpc_cancel *rpc_can;
1807 struct nc_rpc_validate *rpc_val;
1808 struct nc_rpc_getschema *rpc_gs;
1809 struct nc_rpc_subscribe *rpc_sub;
1810 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001811 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001812 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001813 uint64_t cur_msgid;
1814
Michal Vasko45e53ae2016-04-07 11:46:03 +02001815 if (!session) {
1816 ERRARG("session");
1817 return NC_MSG_ERROR;
1818 } else if (!rpc) {
1819 ERRARG("rpc");
1820 return NC_MSG_ERROR;
1821 } else if (!msgid) {
1822 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001823 return NC_MSG_ERROR;
1824 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001825 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001826 return NC_MSG_ERROR;
1827 }
1828
Michal Vasko90e8e692016-07-13 12:27:57 +02001829 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001830 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001831 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01001832 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001833 return NC_MSG_ERROR;
1834 }
1835 }
1836
1837 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001838 case NC_RPC_ACT_GENERIC:
1839 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001840
1841 if (rpc_gen->has_data) {
1842 data = rpc_gen->content.data;
1843 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01001844 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01001845 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01001846 if (!data) {
1847 return NC_MSG_ERROR;
1848 }
Michal Vasko086311b2016-01-08 09:53:11 +01001849 }
1850 break;
1851
1852 case NC_RPC_GETCONFIG:
1853 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1854
1855 data = lyd_new(NULL, ietfnc, "get-config");
1856 node = lyd_new(data, ietfnc, "source");
1857 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1858 if (!node) {
1859 lyd_free(data);
1860 return NC_MSG_ERROR;
1861 }
1862 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001863 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001864 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001865 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001866 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001867 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001868 lyd_insert_attr(node, NULL, "type", "xpath");
1869 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001870 }
1871 if (!node) {
1872 lyd_free(data);
1873 return NC_MSG_ERROR;
1874 }
1875 }
1876
1877 if (rpc_gc->wd_mode) {
1878 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001879 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001880 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001881 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001882 return NC_MSG_ERROR;
1883 }
1884 }
1885 switch (rpc_gc->wd_mode) {
1886 case NC_WD_UNKNOWN:
1887 /* cannot get here */
1888 break;
1889 case NC_WD_ALL:
1890 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1891 break;
1892 case NC_WD_ALL_TAG:
1893 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1894 break;
1895 case NC_WD_TRIM:
1896 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1897 break;
1898 case NC_WD_EXPLICIT:
1899 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1900 break;
1901 }
1902 if (!node) {
1903 lyd_free(data);
1904 return NC_MSG_ERROR;
1905 }
1906 }
1907 break;
1908
1909 case NC_RPC_EDIT:
1910 rpc_e = (struct nc_rpc_edit *)rpc;
1911
1912 data = lyd_new(NULL, ietfnc, "edit-config");
1913 node = lyd_new(data, ietfnc, "target");
1914 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1915 if (!node) {
1916 lyd_free(data);
1917 return NC_MSG_ERROR;
1918 }
1919
1920 if (rpc_e->default_op) {
1921 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1922 if (!node) {
1923 lyd_free(data);
1924 return NC_MSG_ERROR;
1925 }
1926 }
1927
1928 if (rpc_e->test_opt) {
1929 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1930 if (!node) {
1931 lyd_free(data);
1932 return NC_MSG_ERROR;
1933 }
1934 }
1935
1936 if (rpc_e->error_opt) {
1937 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1938 if (!node) {
1939 lyd_free(data);
1940 return NC_MSG_ERROR;
1941 }
1942 }
1943
Michal Vasko7793bc62016-09-16 11:58:41 +02001944 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001945 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001946 } else {
1947 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1948 }
1949 if (!node) {
1950 lyd_free(data);
1951 return NC_MSG_ERROR;
1952 }
1953 break;
1954
1955 case NC_RPC_COPY:
1956 rpc_cp = (struct nc_rpc_copy *)rpc;
1957
1958 data = lyd_new(NULL, ietfnc, "copy-config");
1959 node = lyd_new(data, ietfnc, "target");
1960 if (rpc_cp->url_trg) {
1961 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1962 } else {
1963 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1964 }
1965 if (!node) {
1966 lyd_free(data);
1967 return NC_MSG_ERROR;
1968 }
1969
1970 node = lyd_new(data, ietfnc, "source");
1971 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001972 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001973 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001974 } else {
1975 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1976 }
1977 } else {
1978 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1979 }
1980 if (!node) {
1981 lyd_free(data);
1982 return NC_MSG_ERROR;
1983 }
1984
1985 if (rpc_cp->wd_mode) {
1986 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001987 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001988 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001989 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001990 return NC_MSG_ERROR;
1991 }
1992 }
1993 switch (rpc_cp->wd_mode) {
1994 case NC_WD_UNKNOWN:
1995 /* cannot get here */
1996 break;
1997 case NC_WD_ALL:
1998 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1999 break;
2000 case NC_WD_ALL_TAG:
2001 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2002 break;
2003 case NC_WD_TRIM:
2004 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2005 break;
2006 case NC_WD_EXPLICIT:
2007 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2008 break;
2009 }
2010 if (!node) {
2011 lyd_free(data);
2012 return NC_MSG_ERROR;
2013 }
2014 }
2015 break;
2016
2017 case NC_RPC_DELETE:
2018 rpc_del = (struct nc_rpc_delete *)rpc;
2019
2020 data = lyd_new(NULL, ietfnc, "delete-config");
2021 node = lyd_new(data, ietfnc, "target");
2022 if (rpc_del->url) {
2023 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2024 } else {
2025 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2026 }
2027 if (!node) {
2028 lyd_free(data);
2029 return NC_MSG_ERROR;
2030 }
2031 break;
2032
2033 case NC_RPC_LOCK:
2034 rpc_lock = (struct nc_rpc_lock *)rpc;
2035
2036 data = lyd_new(NULL, ietfnc, "lock");
2037 node = lyd_new(data, ietfnc, "target");
2038 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2039 if (!node) {
2040 lyd_free(data);
2041 return NC_MSG_ERROR;
2042 }
2043 break;
2044
2045 case NC_RPC_UNLOCK:
2046 rpc_lock = (struct nc_rpc_lock *)rpc;
2047
2048 data = lyd_new(NULL, ietfnc, "unlock");
2049 node = lyd_new(data, ietfnc, "target");
2050 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2051 if (!node) {
2052 lyd_free(data);
2053 return NC_MSG_ERROR;
2054 }
2055 break;
2056
2057 case NC_RPC_GET:
2058 rpc_g = (struct nc_rpc_get *)rpc;
2059
2060 data = lyd_new(NULL, ietfnc, "get");
2061 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002062 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002063 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002064 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002065 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002066 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002067 lyd_insert_attr(node, NULL, "type", "xpath");
2068 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002069 }
2070 if (!node) {
2071 lyd_free(data);
2072 return NC_MSG_ERROR;
2073 }
2074 }
2075
2076 if (rpc_g->wd_mode) {
2077 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002078 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002079 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002080 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002081 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002082 return NC_MSG_ERROR;
2083 }
2084 }
2085 switch (rpc_g->wd_mode) {
2086 case NC_WD_UNKNOWN:
2087 /* cannot get here */
2088 break;
2089 case NC_WD_ALL:
2090 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2091 break;
2092 case NC_WD_ALL_TAG:
2093 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2094 break;
2095 case NC_WD_TRIM:
2096 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2097 break;
2098 case NC_WD_EXPLICIT:
2099 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2100 break;
2101 }
2102 if (!node) {
2103 lyd_free(data);
2104 return NC_MSG_ERROR;
2105 }
2106 }
2107 break;
2108
2109 case NC_RPC_KILL:
2110 rpc_k = (struct nc_rpc_kill *)rpc;
2111
2112 data = lyd_new(NULL, ietfnc, "kill-session");
2113 sprintf(str, "%u", rpc_k->sid);
2114 lyd_new_leaf(data, ietfnc, "session-id", str);
2115 break;
2116
2117 case NC_RPC_COMMIT:
2118 rpc_com = (struct nc_rpc_commit *)rpc;
2119
2120 data = lyd_new(NULL, ietfnc, "commit");
2121 if (rpc_com->confirmed) {
2122 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2123 }
2124
2125 if (rpc_com->confirm_timeout) {
2126 sprintf(str, "%u", rpc_com->confirm_timeout);
2127 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2128 }
2129
2130 if (rpc_com->persist) {
2131 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2132 if (!node) {
2133 lyd_free(data);
2134 return NC_MSG_ERROR;
2135 }
2136 }
2137
2138 if (rpc_com->persist_id) {
2139 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2140 if (!node) {
2141 lyd_free(data);
2142 return NC_MSG_ERROR;
2143 }
2144 }
2145 break;
2146
2147 case NC_RPC_DISCARD:
2148 data = lyd_new(NULL, ietfnc, "discard-changes");
2149 break;
2150
2151 case NC_RPC_CANCEL:
2152 rpc_can = (struct nc_rpc_cancel *)rpc;
2153
2154 data = lyd_new(NULL, ietfnc, "cancel-commit");
2155 if (rpc_can->persist_id) {
2156 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2157 if (!node) {
2158 lyd_free(data);
2159 return NC_MSG_ERROR;
2160 }
2161 }
2162 break;
2163
2164 case NC_RPC_VALIDATE:
2165 rpc_val = (struct nc_rpc_validate *)rpc;
2166
2167 data = lyd_new(NULL, ietfnc, "validate");
2168 node = lyd_new(data, ietfnc, "source");
2169 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002170 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002171 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002172 } else {
2173 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2174 }
2175 } else {
2176 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2177 }
2178 if (!node) {
2179 lyd_free(data);
2180 return NC_MSG_ERROR;
2181 }
2182 break;
2183
2184 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002185 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002186 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002187 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002188 return NC_MSG_ERROR;
2189 }
2190
2191 rpc_gs = (struct nc_rpc_getschema *)rpc;
2192
2193 data = lyd_new(NULL, ietfncmon, "get-schema");
2194 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2195 if (!node) {
2196 lyd_free(data);
2197 return NC_MSG_ERROR;
2198 }
2199 if (rpc_gs->version) {
2200 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2201 if (!node) {
2202 lyd_free(data);
2203 return NC_MSG_ERROR;
2204 }
2205 }
2206 if (rpc_gs->format) {
2207 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2208 if (!node) {
2209 lyd_free(data);
2210 return NC_MSG_ERROR;
2211 }
2212 }
2213 break;
2214
2215 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002216 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002217 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002218 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002219 return NC_MSG_ERROR;
2220 }
2221
2222 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2223
2224 data = lyd_new(NULL, notifs, "create-subscription");
2225 if (rpc_sub->stream) {
2226 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2227 if (!node) {
2228 lyd_free(data);
2229 return NC_MSG_ERROR;
2230 }
2231 }
2232
2233 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002234 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002235 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002236 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002237 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002238 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002239 lyd_insert_attr(node, NULL, "type", "xpath");
2240 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002241 }
2242 if (!node) {
2243 lyd_free(data);
2244 return NC_MSG_ERROR;
2245 }
2246 }
2247
2248 if (rpc_sub->start) {
2249 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2250 if (!node) {
2251 lyd_free(data);
2252 return NC_MSG_ERROR;
2253 }
2254 }
2255
2256 if (rpc_sub->stop) {
2257 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2258 if (!node) {
2259 lyd_free(data);
2260 return NC_MSG_ERROR;
2261 }
2262 }
2263 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002264 default:
2265 ERRINT;
2266 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002267 }
2268
Michal Vasko41adf392017-01-17 10:39:04 +01002269 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2270 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Michal Vasko086311b2016-01-08 09:53:11 +01002271 lyd_free(data);
2272 return NC_MSG_ERROR;
2273 }
2274
Michal Vaskoade892d2017-02-22 13:40:35 +01002275 ret = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002276 if (ret == -1) {
2277 /* error */
2278 r = NC_MSG_ERROR;
2279 } else if (!ret) {
2280 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01002281 r = NC_MSG_WOULDBLOCK;
2282 } else {
2283 /* send RPC, store its message ID */
2284 r = nc_send_msg(session, data);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002285 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002286 }
Michal Vaskoade892d2017-02-22 13:40:35 +01002287 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01002288
2289 lyd_free(data);
2290
2291 if (r != NC_MSG_RPC) {
2292 return r;
2293 }
2294
2295 *msgid = cur_msgid;
2296 return NC_MSG_RPC;
2297}
Michal Vaskode2946c2017-01-12 12:19:26 +01002298
2299API void
2300nc_client_session_set_not_strict(struct nc_session *session)
2301{
2302 if (session->side != NC_CLIENT) {
2303 ERRARG("session");
2304 return;
2305 }
2306
2307 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2308}