blob: 44380a6441f99ae86bd8d9b0bfe4c80c0f8f8abd [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 Krejcib1d250e2018-04-12 13:54:18 +0200196 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
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 }
Michal Vaskoff7e3562018-02-15 13:41:22 +0100205 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200206
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
Radek Krejci65ef6d52018-08-16 16:35:02 +0200266
267struct schema_info {
268 char *name;
269 char *revision;
270 struct {
271 char *name;
272 char *revision;
273 } *submodules;
274 struct ly_set features;
275 int implemented;
276};
277
278struct clb_data_s {
279 void *user_data;
280 ly_module_imp_clb user_clb;
281 struct schema_info *schemas;
282 struct nc_session *session;
283 int has_get_schema;
284};
285
286static char *
287retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
288 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100289{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200290 char *localfile = NULL;
291 FILE *f;
292 long length, l;
293 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100294
Radek Krejci65ef6d52018-08-16 16:35:02 +0200295 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx), name, rev, &localfile, format)) {
296 return NULL;
297 }
298 if (localfile) {
299 VRB("Session %u: reading schema from localfile \"%s\".", clb_data->session->id, localfile);
300 f = fopen(localfile, "r");
301 if (!f) {
302 ERR("Session %u: unable to open \"%s\" file to get schema (%s).",
303 clb_data->session->id, localfile, strerror(errno));
304 free(localfile);
305 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100306 }
Michal Vasko086311b2016-01-08 09:53:11 +0100307
Radek Krejci65ef6d52018-08-16 16:35:02 +0200308 fseek(f, 0, SEEK_END);
309 length = ftell(f);
310 fseek(f, 0, SEEK_SET);
311
312 model_data = malloc(length + 1);
313 if (!model_data) {
314 ERRMEM;
315 } else if ((l = fread(model_data, 1, length, f)) != length) {
316 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
317 clb_data->session->id, localfile, l, length);
318 free(model_data);
319 model_data = NULL;
320 } else {
321 /* terminating NULL byte */
322 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100323 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200324 fclose(f);
325 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100326 }
327
Radek Krejci65ef6d52018-08-16 16:35:02 +0200328 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100329}
330
331static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200332retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
333 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100334{
Michal Vasko086311b2016-01-08 09:53:11 +0100335 struct nc_rpc *rpc;
336 struct nc_reply *reply;
337 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200338 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200339 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100340 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100341 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200342 char *localfile = NULL;
343 FILE *f;
344 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100345
Radek Krejci65ef6d52018-08-16 16:35:02 +0200346 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
347 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100348
Radek Krejci65ef6d52018-08-16 16:35:02 +0200349 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100350 usleep(1000);
351 }
352 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200353 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100354 nc_rpc_free(rpc);
355 return NULL;
356 }
357
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200358 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200359 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200360 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100361 nc_rpc_free(rpc);
362 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200363 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100364 return NULL;
365 } else if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200366 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100367 return NULL;
368 }
369
Michal Vasko998ba412016-09-16 12:00:07 +0200370 switch (reply->type) {
371 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200372 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200373 nc_reply_free(reply);
374 return NULL;
375 case NC_RPL_DATA:
376 /* fine */
377 break;
378 case NC_RPL_ERROR:
379 error_rpl = (struct nc_reply_error *)reply;
380 if (error_rpl->count) {
381 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200382 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200383 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200384 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200385 }
386 nc_reply_free(reply);
387 return NULL;
388 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200389 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100390 nc_reply_free(reply);
391 return NULL;
392 }
393
Michal Vasko086311b2016-01-08 09:53:11 +0100394 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200395 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
396 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200397 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200398 nc_reply_free(reply);
399 return NULL;
400 }
Radek Krejci539efb62016-08-24 15:05:16 +0200401 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
402 switch (get_schema_data->value_type) {
403 case LYD_ANYDATA_CONSTSTRING:
404 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200405 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200406 break;
407 case LYD_ANYDATA_DATATREE:
408 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
409 break;
410 case LYD_ANYDATA_XML:
411 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
412 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200413 case LYD_ANYDATA_JSON:
414 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200415 case LYD_ANYDATA_SXML:
416 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200417 case LYD_ANYDATA_LYB:
418 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200419 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200420 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200421 }
Michal Vasko086311b2016-01-08 09:53:11 +0100422 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100423
Radek Krejcifd5b6682017-06-13 15:52:53 +0200424 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200425 if (model_data) {
426 *format = LYS_IN_YANG;
427 if (client_opts.schema_searchpath) {
428 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
429 rev ? "@" : "", rev ? rev : "") == -1) {
430 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100431 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200432 f = fopen(localfile, "w");
433 if (!f) {
434 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
435 localfile, strerror(errno));
436 } else {
437 fputs(model_data, f);
438 fclose(f);
439 }
440 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100441 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200442 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200443 }
444
Michal Vasko086311b2016-01-08 09:53:11 +0100445 return model_data;
446}
447
Radek Krejci65ef6d52018-08-16 16:35:02 +0200448static char *
449retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
450 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data))
451{
452 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
453 unsigned int u, v, match = 1;
454 const char *name = NULL, *rev = NULL;
455 char *model_data = NULL;
456
457 /* get and check the final name and revision of the schema to be retrieved */
458 if (!mod_rev || !mod_rev[0]) {
459 /* newest revision requested - get the newest revision from the list of available modules on server */
460 match = 0;
461 for (u = 0; clb_data->schemas[u].name; ++u) {
462 if (strcmp(mod_name, clb_data->schemas[u].name)) {
463 continue;
464 }
465 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
466 mod_rev = clb_data->schemas[u].revision;
467 }
468 match = u + 1;
469 }
470 if (!match) {
471 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
472 clb_data->session->id, mod_name);
473 }
474 }
475 if (submod_name) {
476 name = submod_name;
477 if (sub_rev) {
478 rev = sub_rev;
479 } else {
480 if (!clb_data->schemas[u].submodules) {
481 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
482 clb_data->session->id, submod_name, mod_name);
483 return NULL;
484 }
485 for (v = 0; clb_data->schemas[u].submodules[v].name; ++v) {
486 if (!strcmp(submod_name, clb_data->schemas[u].submodules[v].name)) {
487 rev = sub_rev = clb_data->schemas[u].submodules[v].revision;
488 }
489 }
490 if (!rev) {
491 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
492 clb_data->session->id, submod_name, mod_name);
493 return NULL;
494 }
495 }
496 } else {
497 name = mod_name;
498 rev = mod_rev;
499 }
500
501 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
502
503 if (match) {
504 /* we have enough information to avoid communication with server and try to get
505 * the schema locally */
506
507 /* 1. try to get data locally */
508 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
509
510 /* 2. try to use <get-schema> */
511 if (!model_data && clb_data->has_get_schema) {
512 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
513 }
514 } else {
515 /* we are unsure which revision of the schema we should load, so first try to get
516 * the newest revision from the server via get-schema and only if the server does not
517 * implement get-schema, try to load the newest revision locally. This is imperfect
518 * solution, but there are situation when a client does not know what revision is
519 * actually implemented by the server. */
520
521 /* 1. try to use <get-schema> */
522 if (clb_data->has_get_schema) {
523 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
524 }
525
526 /* 2. try to get data locally */
527 if (!model_data) {
528 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
529 }
530 }
531
532 /* 3. try to use user callback */
533 if (!model_data && clb_data->user_clb) {
534 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
535 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
536 }
537
538 *free_module_data = free;
539 return model_data;
540}
541
Michal Vaskoceae0152018-02-14 16:03:59 +0100542static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200543nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200544 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, const struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100545{
546 int ret = 0;
547 struct ly_err_item *eitem;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200548 char *module_data = NULL;
549 LYS_INFORMAT format;
550 void (*free_module_data)(void*) = NULL;
551 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100552
Radek Krejci65ef6d52018-08-16 16:35:02 +0200553 *mod = NULL;
554 if (revision) {
555 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
556 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100557 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200558 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100559 /* make the present module implemented */
560 if (lys_set_implemented(*mod)) {
561 ERR("Failed to implement model \"%s\".", (*mod)->name);
562 ret = -1;
563 }
564 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200565 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100566 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200567 clb_data.has_get_schema = has_get_schema;
568 clb_data.schemas = schemas;
569 clb_data.session = session;
570 clb_data.user_clb = user_clb;
571 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100572
573 /* clear all the errors and just collect them for now */
574 ly_err_clean(session->ctx, NULL);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200575 //ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100576
Radek Krejci65ef6d52018-08-16 16:35:02 +0200577 /* get module data */
578 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100579
Radek Krejci65ef6d52018-08-16 16:35:02 +0200580 if (module_data) {
581 /* parse the schema */
582 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100583
Radek Krejci65ef6d52018-08-16 16:35:02 +0200584 *mod = lys_parse_mem(session->ctx, module_data, format);
585 if (*free_module_data) {
586 (*free_module_data)(module_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100587 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100588
Radek Krejci65ef6d52018-08-16 16:35:02 +0200589 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
590 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100591
Michal Vaskodbf7c272018-02-19 09:07:59 +0100592 /* restore logging options, then print errors on definite failure */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200593 //ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100594 if (!(*mod)) {
595 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
596 ly_err_print(eitem);
597 }
598 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200599 } else {
600 /* print only warnings */
601 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
602 if (eitem->level == LY_LLWRN) {
603 ly_err_print(eitem);
604 }
605 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100606 }
607
Michal Vaskodbf7c272018-02-19 09:07:59 +0100608 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100609 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100610 }
611
612 return ret;
613}
614
Radek Krejci65ef6d52018-08-16 16:35:02 +0200615static void
616free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200617{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200618 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200619
Radek Krejci65ef6d52018-08-16 16:35:02 +0200620 if (!list) {
621 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200622 }
623
Radek Krejci65ef6d52018-08-16 16:35:02 +0200624 for (u = 0; list[u].name; ++u) {
625 free(list[u].name);
626 free(list[u].revision);
627 for (v = 0; v < list[u].features.number; ++v) {
628 free(list[u].features.set.g[v]);
629 }
630 free(list[u].features.set.g);
631 }
632 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200633}
634
Radek Krejci65ef6d52018-08-16 16:35:02 +0200635static struct schema_info *
636build_schema_info_yl(struct nc_session *session)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200637{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200638 struct nc_rpc *rpc = NULL;
639 struct nc_reply *reply = NULL;
640 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200641 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200642 NC_MSG_TYPE msg;
643 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200644 struct ly_set *modules;
645 struct schema_info *result;
646 unsigned int u, c;
647 struct lyd_node *iter;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200648
649 /* get yang-library data from the server */
Michal Vaskoac0356e2018-06-22 13:24:29 +0200650 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200651 if (!rpc) {
652 goto cleanup;
653 }
654
655 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
656 usleep(1000);
657 }
658 if (msg == NC_MSG_ERROR) {
Michal Vasko60f66602017-10-17 13:52:18 +0200659 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200660 session->id);
661 goto cleanup;
662 }
663
664 do {
665 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
666 } while (msg == NC_MSG_NOTIF);
667 if (msg == NC_MSG_WOULDBLOCK) {
668 ERR("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
669 goto cleanup;
670 } else if (msg == NC_MSG_ERROR) {
671 ERR("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
672 goto cleanup;
673 }
674
675 switch (reply->type) {
676 case NC_RPL_OK:
677 ERR("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
678 goto cleanup;
679 case NC_RPL_DATA:
680 /* fine */
681 break;
682 case NC_RPL_ERROR:
683 error_rpl = (struct nc_reply_error *)reply;
684 if (error_rpl->count) {
685 ERR("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
686 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
687 } else {
688 ERR("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
689 }
690 goto cleanup;
691 case NC_RPL_NOTIF:
692 ERR("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
693 goto cleanup;
694 }
695
Radek Krejci65ef6d52018-08-16 16:35:02 +0200696 yldata = ((struct nc_reply_data *)reply)->data;
697 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200698 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
699 goto cleanup;
700 }
701
Radek Krejci65ef6d52018-08-16 16:35:02 +0200702 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
703
704 c = modules ? modules->number : 0;
705 result = calloc(c + 1, sizeof *result);
706 if (!result) {
707 return NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200708 }
709
Radek Krejci65ef6d52018-08-16 16:35:02 +0200710 for (u = 0; u < c; ++u) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200711 LY_TREE_FOR(modules->set.d[u]->child, iter) {
712 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
713 /* ignore empty nodes */
714 continue;
715 }
716 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200717 result[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200718 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200719 result[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200720 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200721 result[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200722 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200723 ly_set_add(&result[u].features, (void *)strdup(((struct lyd_node_leaf_list *)iter)->value_str), LY_SET_OPT_USEASLIST);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200724 }
725 }
726 }
727
Radek Krejcifd5b6682017-06-13 15:52:53 +0200728
729cleanup:
730 nc_rpc_free(rpc);
731 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200732 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200733
Radek Krejci65ef6d52018-08-16 16:35:02 +0200734 return result;
735}
736
737static struct schema_info *
738build_schema_info_cpblts(char **cpblts)
739{
740 unsigned int u, v;
741 struct schema_info *result;
742 char *module_cpblt, *ptr, *ptr2;
743
744 for (u = 0; cpblts[u]; ++u);
745 result = calloc(u + 1, sizeof *result);
746
747 for (u = v = 0; cpblts[u]; ++u) {
748 module_cpblt = strstr(cpblts[u], "module=");
749 /* this capability requires a module */
750 if (!module_cpblt) {
751 continue;
752 }
753
754 /* get module's name */
755 ptr = (char *)module_cpblt + 7;
756 ptr2 = strchr(ptr, '&');
757 if (!ptr2) {
758 ptr2 = ptr + strlen(ptr);
759 }
760 result[v].name = strndup(ptr, ptr2 - ptr);
761
762 /* get module's revision */
763 ptr = strstr(module_cpblt, "revision=");
764 if (ptr) {
765 ptr += 9;
766 ptr2 = strchr(ptr, '&');
767 if (!ptr2) {
768 ptr2 = ptr + strlen(ptr);
769 }
770 result[v].revision = strndup(ptr, ptr2 - ptr);
771 }
772
773 /* all are implemented since there is no better information in capabilities list */
774 result[v].implemented = 1;
775
776 /* get module's features */
777 ptr = strstr(module_cpblt, "features=");
778 if (ptr) {
779 ptr += 9;
780 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
781 if (*ptr == ',') {
782 ly_set_add(&result[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
783 ptr2 = ptr + 1;
784 }
785 }
786 /* the last one */
787 ly_set_add(&result[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
788 }
789 ++v;
790 }
791 return result;
792}
793
794static int
795nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
796{
797 int ret = EXIT_FAILURE;
798 const struct lys_module *mod;
799 unsigned int u, v;
800
801 for (u = 0; modules[u].name; ++u) {
802 /* we can continue even if it fails */
803 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
804
805 if (!mod) {
806 if (session->status != NC_STATUS_RUNNING) {
807 /* something bad heppened, discard the session */
808 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
809 goto cleanup;
810 }
811
812 /* all loading ways failed, the schema will be ignored in the received data */
813 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
814 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
815 } else {
816 /* set features - first disable all to enable specified then */
817 lys_features_disable(mod, "*");
818 for (v = 0; v < modules[u].features.number; v++) {
819 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
820 }
821 }
Michal Vasko60f66602017-10-17 13:52:18 +0200822 }
823
Radek Krejci65ef6d52018-08-16 16:35:02 +0200824 /* done */
825 ret = EXIT_SUCCESS;
826
827cleanup:
828
Radek Krejcifd5b6682017-06-13 15:52:53 +0200829 return ret;
830}
831
Radek Krejci65ef6d52018-08-16 16:35:02 +0200832static int
833nc_ctx_fill_ietf_netconf(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
834{
835 unsigned int u, v;
836 const struct lys_module *ietfnc;
837
838 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
839 if (!ietfnc) {
840 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
841 if (!ietfnc) {
842 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
843 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
844 }
845 }
846 if (!ietfnc) {
847 ERR("Loading base NETCONF schema failed.");
848 return 1;
849 }
850
851 /* set supported capabilities from ietf-netconf */
852 for (u = 0; modules[u].name; ++u) {
853 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
854 continue;
855 }
856
857 lys_features_disable(ietfnc, "*");
858 for (v = 0; v < modules[u].features.number; v++) {
859 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
860 }
861 }
862
863 return 0;
864}
865
Michal Vasko086311b2016-01-08 09:53:11 +0100866int
867nc_ctx_check_and_fill(struct nc_session *session)
868{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200869 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100870 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100871 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200872 const struct lys_module *mod = NULL;
873 char *revision;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200874 struct schema_info *server_modules = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100875
Michal Vasko2e6defd2016-10-07 15:48:15 +0200876 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100877
Radek Krejci65ef6d52018-08-16 16:35:02 +0200878 /* store the original user's callback, we will be switching between local search, get-schema and user callback */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200879 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200880
Radek Krejci65ef6d52018-08-16 16:35:02 +0200881 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
882 * schemas only to those present on the server side */
883 ly_ctx_set_disable_searchdirs(session->ctx);
884
885 /* our callback is set later with appropriate data */
886 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
887
888 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200889 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200890 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200891 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200892 if (yanglib_support) {
893 break;
894 }
895 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-yang-library?", 46)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200896 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200897 if (get_schema_support) {
898 break;
899 }
Michal Vasko086311b2016-01-08 09:53:11 +0100900 }
901 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200902
903 /* get information about server's schemas from capabilities list until we will have yang-library */
904 if (!(server_modules = build_schema_info_cpblts(session->opts.client.cpblts))) {
905 goto cleanup;
906 }
907
Michal Vasko086311b2016-01-08 09:53:11 +0100908 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200909 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200910 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
911 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200912 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100913 }
914 }
915
916 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200917 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200918 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100919 }
920
Radek Krejci65ef6d52018-08-16 16:35:02 +0200921 /* get correct version of ietf-yang-library into context */
922 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200923 /* use get schema to get server's ietf-yang-library */
924 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
925 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200926 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
927 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +0200928 yanglib_support = 0;
929 } else {
930 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200931 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
932 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +0200933 yanglib_support = 0;
934 }
935 free(revision);
936 }
937 }
938
Radek Krejci65ef6d52018-08-16 16:35:02 +0200939 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200940 if (yanglib_support) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200941 if (server_modules) {
942 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
943 free_schema_info(server_modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200944 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200945 server_modules = build_schema_info_yl(session);
946 if (!server_modules) {
947 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
948 }
949 }
950 if (!server_modules) { /* also in case of error of getting yang-library data */
951 server_modules = build_schema_info_cpblts(session->opts.client.cpblts);
952 }
953 if (!server_modules) {
954 goto cleanup;
955 }
Michal Vaskoef578332016-01-25 13:20:09 +0100956
Radek Krejci65ef6d52018-08-16 16:35:02 +0200957 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
958 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100959 }
960
Radek Krejcifd5b6682017-06-13 15:52:53 +0200961 /* succsess */
962 ret = 0;
963
Michal Vaskoeee99412016-11-21 10:19:43 +0100964 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200965 WRN("Session %u: some models failed to be loaded, any data from these models (and any other unknown) will be ignored.", session->id);
Michal Vaskoef578332016-01-25 13:20:09 +0100966 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200967
968cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200969 free_schema_info(server_modules);
970
Radek Krejcifd5b6682017-06-13 15:52:53 +0200971 /* set user callback back */
972 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200973 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200974
Michal Vaskoef578332016-01-25 13:20:09 +0100975 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100976}
977
978API struct nc_session *
979nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
980{
Michal Vaskod083db62016-01-19 10:31:29 +0100981 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100982
Michal Vasko45e53ae2016-04-07 11:46:03 +0200983 if (fdin < 0) {
984 ERRARG("fdin");
985 return NULL;
986 } else if (fdout < 0) {
987 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100988 return NULL;
989 }
990
991 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200992 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100993 if (!session) {
994 ERRMEM;
995 return NULL;
996 }
997 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +0100998
999 /* transport specific data */
1000 session->ti_type = NC_TI_FD;
1001 session->ti.fd.in = fdin;
1002 session->ti.fd.out = fdout;
1003
1004 /* assign context (dicionary needed for handshake) */
1005 if (!ctx) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001006 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001007 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001008 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001009 /* that's just it */
1010 goto fail;
1011 }
Michal Vasko086311b2016-01-08 09:53:11 +01001012 } else {
1013 session->flags |= NC_SESSION_SHAREDCTX;
1014 }
1015 session->ctx = ctx;
1016
1017 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001018 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001019 goto fail;
1020 }
1021 session->status = NC_STATUS_RUNNING;
1022
Michal Vaskoef578332016-01-25 13:20:09 +01001023 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001024 goto fail;
1025 }
1026
1027 return session;
1028
1029fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001030 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001031 return NULL;
1032}
1033
Frank Rimpler9f838b02018-07-25 06:44:03 +00001034/*
1035 Helper for a non-blocking connect (which is required because of the locking
1036 concept for e.g. call home settings). For more details see nc_sock_connect().
1037 */
1038static int
1039_non_blocking_connect(int timeout, int* sock_pending, struct addrinfo *res)
Michal Vasko086311b2016-01-08 09:53:11 +01001040{
Frank Rimplerc7a2acb2018-08-06 07:31:08 +00001041 int flags, ret=0;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001042 int sock = -1;
1043 fd_set wset;
1044 struct timeval ts;
1045 int error = 0;
1046 socklen_t len = sizeof(int);
Michal Vasko086311b2016-01-08 09:53:11 +01001047
Frank Rimpler9f838b02018-07-25 06:44:03 +00001048 if (sock_pending && *sock_pending != -1) {
1049 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1050 sock = *sock_pending;
1051 } else {
1052 assert(res);
1053 VRB("Trying to connect via %s.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1054 /* Connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001055 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1056 if (sock == -1) {
Frank Rimpler9f838b02018-07-25 06:44:03 +00001057 ERR("socket couldn't be created.", strerror(errno));
1058 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001059 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001060 /* make the socket non-blocking */
1061 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
1062 ERR("Fcntl failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001063 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001064 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001065 /* non-blocking connect! */
1066 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1067 if (errno != EINPROGRESS) {
1068 /* network connection failed, try another resource */
1069 ERR("connect failed: (%s).", strerror(errno));
1070 goto cleanup;
1071 }
1072 }
1073 /* check the usability of the socket */
1074 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1075 ERR("getsockopt failed: (%s).", strerror(errno));
1076 goto cleanup;
1077 }
1078 if (error == ECONNREFUSED) {
1079 /* network connection failed, try another resource */
1080 VRB("getsockopt error: (%s).", strerror(error));
1081 goto cleanup;
1082 }
1083 }
1084 ts.tv_sec = timeout;
1085 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001086
Frank Rimpler9f838b02018-07-25 06:44:03 +00001087 FD_ZERO(&wset);
1088 FD_SET(sock, &wset);
1089
1090 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
1091 ERR("select failed: (%s).", strerror(errno));
1092 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001093 }
1094
Frank Rimpler9f838b02018-07-25 06:44:03 +00001095 if (ret == 0) { //we had a timeout
1096 VRB("timed out after %ds (%s).", timeout, strerror(errno));
1097 if (sock_pending) {
1098 /* no sock-close, we'll try it again */
1099 *sock_pending = sock;
1100 } else {
1101 close(sock);
1102 }
1103 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001104 }
Michal Vasko086311b2016-01-08 09:53:11 +01001105 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001106
Frank Rimpler9f838b02018-07-25 06:44:03 +00001107cleanup:
1108 if (sock_pending) {
1109 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001110 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001111 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001112 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001113}
1114
Frank Rimpler9f838b02018-07-25 06:44:03 +00001115/* A given timeout value limits the time how long the function blocks. If it has to block
1116 only for some seconds, a socket connection might not yet have been fully established.
1117 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1118 value will be -1. In such a case a subsequent invokation is required, by providing the
1119 stored sock_pending, again.
1120 In general, if this function returns -1, when a timeout has been given, this function
1121 has to be invoked, until it returns a valid socket.
1122 */
1123int
1124nc_sock_connect(const char* host, uint16_t port, int timeout, int* sock_pending)
1125{
1126 int i;
1127 int sock = sock_pending?*sock_pending:-1;
1128 struct addrinfo hints, *res_list, *res;
1129 char port_s[6]; /* length of string representation of short int */
1130
1131 VRB("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
1132
1133 /* no pending socket */
1134 if (sock == -1) {
1135 /* Connect to a server */
1136 snprintf(port_s, 6, "%u", port);
1137 memset(&hints, 0, sizeof hints);
1138 hints.ai_family = AF_UNSPEC;
1139 hints.ai_socktype = SOCK_STREAM;
1140 hints.ai_protocol = IPPROTO_TCP;
1141 i = getaddrinfo(host, port_s, &hints, &res_list);
1142 if (i != 0) {
1143 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1144 return -1;
1145 }
1146
1147 for (res = res_list; res != NULL; res = res->ai_next) {
1148 sock = _non_blocking_connect(timeout, sock_pending, res);
1149 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1150 /* try the next resource */
1151 continue;
1152 }
1153 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1154 break;
1155 }
1156 freeaddrinfo(res_list);
1157
1158 } else {
1159 /* try to get a connection with the pending socket */
1160 assert(sock_pending);
1161 sock = _non_blocking_connect(timeout, sock_pending, NULL);
1162 }
1163
1164 return sock;
1165}
1166
Michal Vasko086311b2016-01-08 09:53:11 +01001167static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001168get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001169{
Michal Vasko086311b2016-01-08 09:53:11 +01001170 char *ptr;
1171 const char *str_msgid;
1172 uint64_t cur_msgid;
1173 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001174 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001175 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1176
Michal Vasko086311b2016-01-08 09:53:11 +01001177 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001178 if (!msgid && session->opts.client.notifs) {
1179 cont = session->opts.client.notifs;
1180 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001181
Michal Vasko71ba2da2016-05-04 10:53:16 +02001182 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001183 free(cont);
1184
Michal Vasko71ba2da2016-05-04 10:53:16 +02001185 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001186 }
1187
1188 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001189 if (msgid && session->opts.client.replies) {
1190 cont = session->opts.client.replies;
1191 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001192
Michal Vasko71ba2da2016-05-04 10:53:16 +02001193 xml = cont->msg;
1194 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001195
Michal Vasko71ba2da2016-05-04 10:53:16 +02001196 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001197 }
1198
Michal Vasko71ba2da2016-05-04 10:53:16 +02001199 if (!msgtype) {
1200 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001201 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001202 }
Michal Vasko086311b2016-01-08 09:53:11 +01001203
1204 /* we read rpc-reply, want a notif */
1205 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001206 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001207 while (*cont_ptr) {
1208 cont_ptr = &((*cont_ptr)->next);
1209 }
1210 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001211 if (!*cont_ptr) {
1212 ERRMEM;
1213 lyxml_free(session->ctx, xml);
1214 return NC_MSG_ERROR;
1215 }
Michal Vasko086311b2016-01-08 09:53:11 +01001216 (*cont_ptr)->msg = xml;
1217 (*cont_ptr)->next = NULL;
1218 }
1219
1220 /* we read notif, want a rpc-reply */
1221 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001222 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001223 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001224 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001225 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001226 }
Michal Vasko086311b2016-01-08 09:53:11 +01001227
Michal Vasko2e6defd2016-10-07 15:48:15 +02001228 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001229 while (*cont_ptr) {
1230 cont_ptr = &((*cont_ptr)->next);
1231 }
1232 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001233 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001234 ERRMEM;
1235 lyxml_free(session->ctx, xml);
1236 return NC_MSG_ERROR;
1237 }
Michal Vasko086311b2016-01-08 09:53:11 +01001238 (*cont_ptr)->msg = xml;
1239 (*cont_ptr)->next = NULL;
1240 }
1241
Michal Vasko086311b2016-01-08 09:53:11 +01001242 switch (msgtype) {
1243 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001244 if (!msgid) {
1245 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001246 }
Michal Vasko086311b2016-01-08 09:53:11 +01001247 break;
1248
1249 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001250 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001251 /* check message-id */
1252 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1253 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001254 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001255 } else {
1256 cur_msgid = strtoul(str_msgid, &ptr, 10);
1257 if (cur_msgid != msgid) {
1258 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1259 session->id, str_msgid);
1260 msgtype = NC_MSG_REPLY_ERR_MSGID;
1261 }
1262 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001263 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001264 }
Michal Vasko086311b2016-01-08 09:53:11 +01001265 break;
1266
1267 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001268 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001269 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001270 msgtype = NC_MSG_ERROR;
1271 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001272
1273 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001274 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001275 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001276 msgtype = NC_MSG_ERROR;
1277 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001278
1279 default:
1280 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1281 * NC_MSG_NONE is not returned by nc_read_msg()
1282 */
1283 break;
1284 }
1285
1286 return msgtype;
1287}
1288
1289/* cannot strictly fail, but does not need to fill any error parameter at all */
1290static void
1291parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1292{
1293 struct lyxml_elem *iter, *next, *info;
1294
1295 LY_TREE_FOR(xml->child, iter) {
1296 if (!iter->ns) {
1297 if (iter->content) {
1298 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1299 } else {
1300 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1301 }
1302 continue;
1303 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1304 if (iter->content) {
1305 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1306 iter->name, iter->content, iter->ns->value);
1307 } else {
1308 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1309 }
1310 continue;
1311 }
1312
1313 if (!strcmp(iter->name, "error-type")) {
1314 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1315 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1316 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1317 } else if (err->type) {
1318 WRN("<rpc-error> <error-type> duplicated.");
1319 } else {
1320 err->type = lydict_insert(ctx, iter->content, 0);
1321 }
1322 } else if (!strcmp(iter->name, "error-tag")) {
1323 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1324 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1325 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1326 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1327 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1328 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1329 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1330 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1331 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1332 && strcmp(iter->content, "malformed-message"))) {
1333 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1334 } else if (err->tag) {
1335 WRN("<rpc-error> <error-tag> duplicated.");
1336 } else {
1337 err->tag = lydict_insert(ctx, iter->content, 0);
1338 }
1339 } else if (!strcmp(iter->name, "error-severity")) {
1340 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1341 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1342 } else if (err->severity) {
1343 WRN("<rpc-error> <error-severity> duplicated.");
1344 } else {
1345 err->severity = lydict_insert(ctx, iter->content, 0);
1346 }
1347 } else if (!strcmp(iter->name, "error-app-tag")) {
1348 if (err->apptag) {
1349 WRN("<rpc-error> <error-app-tag> duplicated.");
1350 } else {
1351 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1352 }
1353 } else if (!strcmp(iter->name, "error-path")) {
1354 if (err->path) {
1355 WRN("<rpc-error> <error-path> duplicated.");
1356 } else {
1357 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1358 }
1359 } else if (!strcmp(iter->name, "error-message")) {
1360 if (err->message) {
1361 WRN("<rpc-error> <error-message> duplicated.");
1362 } else {
1363 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001364 if (err->message_lang) {
1365 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1366 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001367 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1368 }
1369 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1370 }
1371 } else if (!strcmp(iter->name, "error-info")) {
1372 LY_TREE_FOR_SAFE(iter->child, next, info) {
1373 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1374 if (!strcmp(info->name, "session-id")) {
1375 if (err->sid) {
1376 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1377 } else {
1378 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1379 }
Michal Vasko630485f2018-01-03 14:28:32 +01001380 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001381 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001382 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1383 if (!err->attr) {
1384 ERRMEM;
1385 return;
1386 }
Michal Vasko086311b2016-01-08 09:53:11 +01001387 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1388 } else if (!strcmp(info->name, "bad-element")) {
1389 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001390 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1391 if (!err->elem) {
1392 ERRMEM;
1393 return;
1394 }
Michal Vasko086311b2016-01-08 09:53:11 +01001395 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1396 } else if (!strcmp(info->name, "bad-namespace")) {
1397 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001398 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1399 if (!err->ns) {
1400 ERRMEM;
1401 return;
1402 }
Michal Vasko086311b2016-01-08 09:53:11 +01001403 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1404 } else {
1405 if (info->content) {
1406 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1407 info->name, info->content);
1408 } else {
1409 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1410 }
1411 }
1412 } else {
1413 lyxml_unlink(ctx, info);
1414 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001415 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1416 if (!err->other) {
1417 ERRMEM;
1418 return;
1419 }
Michal Vasko086311b2016-01-08 09:53:11 +01001420 err->other[err->other_count - 1] = info;
1421 }
1422 }
1423 } else {
1424 if (iter->content) {
1425 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1426 } else {
1427 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1428 }
1429 }
1430 }
1431}
1432
1433static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001434parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001435{
1436 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001437 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001438 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001439 struct nc_reply_data *data_rpl;
1440 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001441 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001442 int i;
1443
1444 if (!xml->child) {
1445 ERR("An empty <rpc-reply>.");
1446 return NULL;
1447 }
1448
1449 /* rpc-error */
1450 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1451 /* count and check elements */
1452 i = 0;
1453 LY_TREE_FOR(xml->child, iter) {
1454 if (strcmp(iter->name, "rpc-error")) {
1455 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1456 return NULL;
1457 } else if (!iter->ns) {
1458 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1459 return NULL;
1460 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1461 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1462 return NULL;
1463 }
1464 ++i;
1465 }
1466
1467 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001468 if (!error_rpl) {
1469 ERRMEM;
1470 return NULL;
1471 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001472 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001473 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001474 if (!error_rpl->err) {
1475 ERRMEM;
1476 free(error_rpl);
1477 return NULL;
1478 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001479 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001480 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001481 reply = (struct nc_reply *)error_rpl;
1482
1483 i = 0;
1484 LY_TREE_FOR(xml->child, iter) {
1485 parse_rpc_error(ctx, iter, error_rpl->err + i);
1486 ++i;
1487 }
1488
1489 /* ok */
1490 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1491 if (xml->child->next) {
1492 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1493 return NULL;
1494 }
1495 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001496 if (!reply) {
1497 ERRMEM;
1498 return NULL;
1499 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001500 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001501
1502 /* some RPC output */
1503 } else {
1504 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001505 case NC_RPC_ACT_GENERIC:
1506 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001507
1508 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001509 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1510 if (!rpc_act) {
1511 ERR("Failed to duplicate a generic RPC/action.");
1512 return NULL;
1513 }
Michal Vasko086311b2016-01-08 09:53:11 +01001514 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001515 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 +02001516 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001517 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001518 return NULL;
1519 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001520 }
Michal Vasko086311b2016-01-08 09:53:11 +01001521 break;
1522
1523 case NC_RPC_GETCONFIG:
1524 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001525 if (!xml->child->child) {
1526 /* we did not receive any data */
1527 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001528 if (!data_rpl) {
1529 ERRMEM;
1530 return NULL;
1531 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001532 data_rpl->type = NC_RPL_DATA;
1533 data_rpl->data = NULL;
1534 return (struct nc_reply *)data_rpl;
1535 }
1536
Michal Vasko086311b2016-01-08 09:53:11 +01001537 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001538 data = lyd_parse_xml(ctx, &xml->child->child,
1539 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001540 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001541 if (!data) {
1542 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1543 return NULL;
1544 }
1545 break;
1546
1547 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001548 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001549 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001550 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001551 return NULL;
1552 }
1553 break;
1554
1555 case NC_RPC_EDIT:
1556 case NC_RPC_COPY:
1557 case NC_RPC_DELETE:
1558 case NC_RPC_LOCK:
1559 case NC_RPC_UNLOCK:
1560 case NC_RPC_KILL:
1561 case NC_RPC_COMMIT:
1562 case NC_RPC_DISCARD:
1563 case NC_RPC_CANCEL:
1564 case NC_RPC_VALIDATE:
1565 case NC_RPC_SUBSCRIBE:
1566 /* there is no output defined */
1567 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1568 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001569 default:
1570 ERRINT;
1571 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001572 }
1573
1574 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001575 if (!data_rpl) {
1576 ERRMEM;
1577 return NULL;
1578 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001579 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001580 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001581 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001582 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001583 } else {
1584 /* <get>, <get-config> */
1585 data_rpl->data = data;
1586 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001587 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001588 if (!data_rpl->data) {
1589 ERR("Failed to parse <rpc-reply>.");
1590 free(data_rpl);
1591 return NULL;
1592 }
1593 reply = (struct nc_reply *)data_rpl;
1594 }
1595
1596 return reply;
1597}
1598
Radek Krejci53691be2016-02-22 13:58:37 +01001599#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001600
Michal Vasko3031aae2016-01-27 16:07:18 +01001601int
1602nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1603{
1604 int sock;
1605
Michal Vasko45e53ae2016-04-07 11:46:03 +02001606 if (!address) {
1607 ERRARG("address");
1608 return -1;
1609 } else if (!port) {
1610 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001611 return -1;
1612 }
1613
1614 sock = nc_sock_listen(address, port);
1615 if (sock == -1) {
1616 return -1;
1617 }
1618
1619 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001620 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1621 if (!client_opts.ch_binds) {
1622 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001623 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001624 return -1;
1625 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001626
Michal Vasko2e6defd2016-10-07 15:48:15 +02001627 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1628 if (!client_opts.ch_bind_ti) {
1629 ERRMEM;
1630 close(sock);
1631 return -1;
1632 }
1633 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1634
Michal Vasko3031aae2016-01-27 16:07:18 +01001635 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001636 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1637 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001638 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001639 return -1;
1640 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001641 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1642 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001643 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001644
1645 return 0;
1646}
1647
1648int
1649nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1650{
1651 uint32_t i;
1652 int ret = -1;
1653
1654 if (!address && !port && !ti) {
1655 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1656 close(client_opts.ch_binds[i].sock);
1657 free((char *)client_opts.ch_binds[i].address);
1658
1659 ret = 0;
1660 }
1661 free(client_opts.ch_binds);
1662 client_opts.ch_binds = NULL;
1663 client_opts.ch_bind_count = 0;
1664 } else {
1665 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1666 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1667 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001668 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001669 close(client_opts.ch_binds[i].sock);
1670 free((char *)client_opts.ch_binds[i].address);
1671
1672 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001673 if (!client_opts.ch_bind_count) {
1674 free(client_opts.ch_binds);
1675 client_opts.ch_binds = NULL;
1676 } else if (i < client_opts.ch_bind_count) {
1677 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1678 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1679 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001680
1681 ret = 0;
1682 }
1683 }
1684 }
1685
1686 return ret;
1687}
1688
1689API int
1690nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1691{
1692 int sock;
1693 char *host = NULL;
1694 uint16_t port, idx;
1695
Michal Vasko45e53ae2016-04-07 11:46:03 +02001696 if (!client_opts.ch_binds) {
1697 ERRINIT;
1698 return -1;
1699 } else if (!session) {
1700 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001701 return -1;
1702 }
1703
1704 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1705
Michal Vasko50456e82016-02-02 12:16:08 +01001706 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001707 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001708 return sock;
1709 }
1710
Radek Krejci53691be2016-02-22 13:58:37 +01001711#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001712 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001713 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001714 } else
1715#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001716#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001717 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001718 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001719 } else
1720#endif
1721 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001722 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001723 *session = NULL;
1724 }
1725
1726 free(host);
1727
1728 if (!(*session)) {
1729 return -1;
1730 }
1731
1732 return 1;
1733}
1734
Radek Krejci53691be2016-02-22 13:58:37 +01001735#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001736
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001737API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001738nc_session_get_cpblts(const struct nc_session *session)
1739{
1740 if (!session) {
1741 ERRARG("session");
1742 return NULL;
1743 }
1744
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001745 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001746}
1747
1748API const char *
1749nc_session_cpblt(const struct nc_session *session, const char *capab)
1750{
1751 int i, len;
1752
1753 if (!session) {
1754 ERRARG("session");
1755 return NULL;
1756 } else if (!capab) {
1757 ERRARG("capab");
1758 return NULL;
1759 }
1760
1761 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001762 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1763 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1764 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001765 }
1766 }
1767
1768 return NULL;
1769}
1770
Michal Vasko9cd26a82016-05-31 08:58:48 +02001771API int
1772nc_session_ntf_thread_running(const struct nc_session *session)
1773{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001774 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001775 ERRARG("session");
1776 return 0;
1777 }
1778
Michal Vasko2e6defd2016-10-07 15:48:15 +02001779 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001780}
1781
Michal Vaskob7558c52016-02-26 15:04:19 +01001782API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001783nc_client_init(void)
1784{
1785 nc_init();
1786}
1787
1788API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001789nc_client_destroy(void)
1790{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001791 nc_client_set_schema_searchpath(NULL);
1792#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1793 nc_client_ch_del_bind(NULL, 0, 0);
1794#endif
1795#ifdef NC_ENABLED_SSH
1796 nc_client_ssh_destroy_opts();
1797#endif
1798#ifdef NC_ENABLED_TLS
1799 nc_client_tls_destroy_opts();
1800#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001801 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001802}
1803
Michal Vasko086311b2016-01-08 09:53:11 +01001804API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001805nc_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 +01001806{
1807 struct lyxml_elem *xml;
1808 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1809
Michal Vasko45e53ae2016-04-07 11:46:03 +02001810 if (!session) {
1811 ERRARG("session");
1812 return NC_MSG_ERROR;
1813 } else if (!rpc) {
1814 ERRARG("rpc");
1815 return NC_MSG_ERROR;
1816 } else if (!reply) {
1817 ERRARG("reply");
1818 return NC_MSG_ERROR;
1819 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1820 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001821 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001822 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001823 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001824 return NC_MSG_ERROR;
1825 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001826 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001827 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1828 parseroptions &= LYD_OPT_STRICT;
1829 }
Michal Vasko41adf392017-01-17 10:39:04 +01001830 /* no mechanism to check external dependencies is provided */
1831 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001832 *reply = NULL;
1833
1834 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001835
Michal Vasko71ba2da2016-05-04 10:53:16 +02001836 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001837 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001838 lyxml_free(session->ctx, xml);
1839 if (!(*reply)) {
1840 return NC_MSG_ERROR;
1841 }
1842 }
1843
1844 return msgtype;
1845}
1846
1847API NC_MSG_TYPE
1848nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1849{
1850 struct lyxml_elem *xml, *ev_time;
1851 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1852
Michal Vasko45e53ae2016-04-07 11:46:03 +02001853 if (!session) {
1854 ERRARG("session");
1855 return NC_MSG_ERROR;
1856 } else if (!notif) {
1857 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001858 return NC_MSG_ERROR;
1859 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001860 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001861 return NC_MSG_ERROR;
1862 }
1863
1864 msgtype = get_msg(session, timeout, 0, &xml);
1865
1866 if (msgtype == NC_MSG_NOTIF) {
1867 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001868 if (!*notif) {
1869 ERRMEM;
1870 lyxml_free(session->ctx, xml);
1871 return NC_MSG_ERROR;
1872 }
Michal Vasko086311b2016-01-08 09:53:11 +01001873
1874 /* eventTime */
1875 LY_TREE_FOR(xml->child, ev_time) {
1876 if (!strcmp(ev_time->name, "eventTime")) {
1877 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1878 /* lyd_parse does not know this element */
1879 lyxml_free(session->ctx, ev_time);
1880 break;
1881 }
1882 }
1883 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001884 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001885 goto fail;
1886 }
1887
1888 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001889 (*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 +01001890 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001891 lyxml_free(session->ctx, xml);
1892 xml = NULL;
1893 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001894 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001895 goto fail;
1896 }
1897 }
1898
1899 return msgtype;
1900
1901fail:
1902 lydict_remove(session->ctx, (*notif)->datetime);
1903 lyd_free((*notif)->tree);
1904 free(*notif);
1905 *notif = NULL;
1906 lyxml_free(session->ctx, xml);
1907
1908 return NC_MSG_ERROR;
1909}
1910
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001911static void *
1912nc_recv_notif_thread(void *arg)
1913{
1914 struct nc_ntf_thread_arg *ntarg;
1915 struct nc_session *session;
1916 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1917 struct nc_notif *notif;
1918 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02001919 pthread_t *ntf_tid;
1920
1921 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001922
1923 ntarg = (struct nc_ntf_thread_arg *)arg;
1924 session = ntarg->session;
1925 notif_clb = ntarg->notif_clb;
1926 free(ntarg);
1927
Michal Vasko131120a2018-05-29 15:44:02 +02001928 /* remember our allocated tid, we will be freeing it */
1929 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
1930
Michal Vasko2e6defd2016-10-07 15:48:15 +02001931 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001932 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001933 if (msgtype == NC_MSG_NOTIF) {
1934 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001935 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1936 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1937 nc_notif_free(notif);
1938 break;
1939 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001940 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001941 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001942 /* quit this thread once the session is broken */
1943 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001944 }
1945
1946 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1947 }
1948
Michal Vasko0651c902016-05-19 15:55:42 +02001949 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001950 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02001951 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001952 return NULL;
1953}
1954
1955API int
1956nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1957{
1958 struct nc_ntf_thread_arg *ntarg;
1959 int ret;
1960
Michal Vasko45e53ae2016-04-07 11:46:03 +02001961 if (!session) {
1962 ERRARG("session");
1963 return -1;
1964 } else if (!notif_clb) {
1965 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001966 return -1;
1967 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1968 ERR("Session %u: invalid session to receive Notifications.", session->id);
1969 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001970 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001971 ERR("Session %u: separate notification thread is already running.", session->id);
1972 return -1;
1973 }
1974
1975 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001976 if (!ntarg) {
1977 ERRMEM;
1978 return -1;
1979 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001980 ntarg->session = session;
1981 ntarg->notif_clb = notif_clb;
1982
1983 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001984 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1985 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001986 ERRMEM;
1987 free(ntarg);
1988 return -1;
1989 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001990
Michal Vasko2e6defd2016-10-07 15:48:15 +02001991 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001992 if (ret) {
1993 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1994 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001995 free((pthread_t *)session->opts.client.ntf_tid);
1996 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001997 return -1;
1998 }
1999
2000 return 0;
2001}
2002
Michal Vasko086311b2016-01-08 09:53:11 +01002003API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002004nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002005{
2006 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002007 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002008 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002009 struct nc_rpc_getconfig *rpc_gc;
2010 struct nc_rpc_edit *rpc_e;
2011 struct nc_rpc_copy *rpc_cp;
2012 struct nc_rpc_delete *rpc_del;
2013 struct nc_rpc_lock *rpc_lock;
2014 struct nc_rpc_get *rpc_g;
2015 struct nc_rpc_kill *rpc_k;
2016 struct nc_rpc_commit *rpc_com;
2017 struct nc_rpc_cancel *rpc_can;
2018 struct nc_rpc_validate *rpc_val;
2019 struct nc_rpc_getschema *rpc_gs;
2020 struct nc_rpc_subscribe *rpc_sub;
2021 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01002022 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02002023 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002024 uint64_t cur_msgid;
2025
Michal Vasko45e53ae2016-04-07 11:46:03 +02002026 if (!session) {
2027 ERRARG("session");
2028 return NC_MSG_ERROR;
2029 } else if (!rpc) {
2030 ERRARG("rpc");
2031 return NC_MSG_ERROR;
2032 } else if (!msgid) {
2033 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002034 return NC_MSG_ERROR;
2035 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002036 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002037 return NC_MSG_ERROR;
2038 }
2039
Michal Vasko90e8e692016-07-13 12:27:57 +02002040 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002041 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002042 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01002043 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002044 return NC_MSG_ERROR;
2045 }
2046 }
2047
2048 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002049 case NC_RPC_ACT_GENERIC:
2050 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002051
2052 if (rpc_gen->has_data) {
2053 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002054 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002055 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002056 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 +01002057 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002058 if (!data) {
2059 return NC_MSG_ERROR;
2060 }
Michal Vasko086311b2016-01-08 09:53:11 +01002061 }
2062 break;
2063
2064 case NC_RPC_GETCONFIG:
2065 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2066
2067 data = lyd_new(NULL, ietfnc, "get-config");
2068 node = lyd_new(data, ietfnc, "source");
2069 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
2070 if (!node) {
2071 lyd_free(data);
2072 return NC_MSG_ERROR;
2073 }
2074 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002075 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002076 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002077 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002078 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002079 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002080 lyd_insert_attr(node, NULL, "type", "xpath");
2081 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002082 }
2083 if (!node) {
2084 lyd_free(data);
2085 return NC_MSG_ERROR;
2086 }
2087 }
2088
2089 if (rpc_gc->wd_mode) {
2090 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002091 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002092 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002093 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002094 return NC_MSG_ERROR;
2095 }
2096 }
2097 switch (rpc_gc->wd_mode) {
2098 case NC_WD_UNKNOWN:
2099 /* cannot get here */
2100 break;
2101 case NC_WD_ALL:
2102 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2103 break;
2104 case NC_WD_ALL_TAG:
2105 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2106 break;
2107 case NC_WD_TRIM:
2108 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2109 break;
2110 case NC_WD_EXPLICIT:
2111 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2112 break;
2113 }
2114 if (!node) {
2115 lyd_free(data);
2116 return NC_MSG_ERROR;
2117 }
2118 }
2119 break;
2120
2121 case NC_RPC_EDIT:
2122 rpc_e = (struct nc_rpc_edit *)rpc;
2123
2124 data = lyd_new(NULL, ietfnc, "edit-config");
2125 node = lyd_new(data, ietfnc, "target");
2126 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
2127 if (!node) {
2128 lyd_free(data);
2129 return NC_MSG_ERROR;
2130 }
2131
2132 if (rpc_e->default_op) {
2133 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
2134 if (!node) {
2135 lyd_free(data);
2136 return NC_MSG_ERROR;
2137 }
2138 }
2139
2140 if (rpc_e->test_opt) {
2141 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
2142 if (!node) {
2143 lyd_free(data);
2144 return NC_MSG_ERROR;
2145 }
2146 }
2147
2148 if (rpc_e->error_opt) {
2149 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
2150 if (!node) {
2151 lyd_free(data);
2152 return NC_MSG_ERROR;
2153 }
2154 }
2155
Michal Vasko7793bc62016-09-16 11:58:41 +02002156 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002157 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002158 } else {
2159 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
2160 }
2161 if (!node) {
2162 lyd_free(data);
2163 return NC_MSG_ERROR;
2164 }
2165 break;
2166
2167 case NC_RPC_COPY:
2168 rpc_cp = (struct nc_rpc_copy *)rpc;
2169
2170 data = lyd_new(NULL, ietfnc, "copy-config");
2171 node = lyd_new(data, ietfnc, "target");
2172 if (rpc_cp->url_trg) {
2173 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
2174 } else {
2175 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
2176 }
2177 if (!node) {
2178 lyd_free(data);
2179 return NC_MSG_ERROR;
2180 }
2181
2182 node = lyd_new(data, ietfnc, "source");
2183 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002184 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002185 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002186 } else {
2187 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
2188 }
2189 } else {
2190 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
2191 }
2192 if (!node) {
2193 lyd_free(data);
2194 return NC_MSG_ERROR;
2195 }
2196
2197 if (rpc_cp->wd_mode) {
2198 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002199 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002200 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002201 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002202 return NC_MSG_ERROR;
2203 }
2204 }
2205 switch (rpc_cp->wd_mode) {
2206 case NC_WD_UNKNOWN:
2207 /* cannot get here */
2208 break;
2209 case NC_WD_ALL:
2210 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2211 break;
2212 case NC_WD_ALL_TAG:
2213 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2214 break;
2215 case NC_WD_TRIM:
2216 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2217 break;
2218 case NC_WD_EXPLICIT:
2219 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2220 break;
2221 }
2222 if (!node) {
2223 lyd_free(data);
2224 return NC_MSG_ERROR;
2225 }
2226 }
2227 break;
2228
2229 case NC_RPC_DELETE:
2230 rpc_del = (struct nc_rpc_delete *)rpc;
2231
2232 data = lyd_new(NULL, ietfnc, "delete-config");
2233 node = lyd_new(data, ietfnc, "target");
2234 if (rpc_del->url) {
2235 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2236 } else {
2237 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2238 }
2239 if (!node) {
2240 lyd_free(data);
2241 return NC_MSG_ERROR;
2242 }
2243 break;
2244
2245 case NC_RPC_LOCK:
2246 rpc_lock = (struct nc_rpc_lock *)rpc;
2247
2248 data = lyd_new(NULL, ietfnc, "lock");
2249 node = lyd_new(data, ietfnc, "target");
2250 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2251 if (!node) {
2252 lyd_free(data);
2253 return NC_MSG_ERROR;
2254 }
2255 break;
2256
2257 case NC_RPC_UNLOCK:
2258 rpc_lock = (struct nc_rpc_lock *)rpc;
2259
2260 data = lyd_new(NULL, ietfnc, "unlock");
2261 node = lyd_new(data, ietfnc, "target");
2262 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2263 if (!node) {
2264 lyd_free(data);
2265 return NC_MSG_ERROR;
2266 }
2267 break;
2268
2269 case NC_RPC_GET:
2270 rpc_g = (struct nc_rpc_get *)rpc;
2271
2272 data = lyd_new(NULL, ietfnc, "get");
2273 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002274 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002275 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002276 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002277 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002278 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002279 lyd_insert_attr(node, NULL, "type", "xpath");
2280 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002281 }
2282 if (!node) {
2283 lyd_free(data);
2284 return NC_MSG_ERROR;
2285 }
2286 }
2287
2288 if (rpc_g->wd_mode) {
2289 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002290 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002291 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002292 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002293 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002294 return NC_MSG_ERROR;
2295 }
2296 }
2297 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002298 case NC_WD_ALL:
2299 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2300 break;
2301 case NC_WD_ALL_TAG:
2302 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2303 break;
2304 case NC_WD_TRIM:
2305 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2306 break;
2307 case NC_WD_EXPLICIT:
2308 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2309 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002310 default:
2311 /* cannot get here */
2312 node = NULL;
2313 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002314 }
2315 if (!node) {
2316 lyd_free(data);
2317 return NC_MSG_ERROR;
2318 }
2319 }
2320 break;
2321
2322 case NC_RPC_KILL:
2323 rpc_k = (struct nc_rpc_kill *)rpc;
2324
2325 data = lyd_new(NULL, ietfnc, "kill-session");
2326 sprintf(str, "%u", rpc_k->sid);
2327 lyd_new_leaf(data, ietfnc, "session-id", str);
2328 break;
2329
2330 case NC_RPC_COMMIT:
2331 rpc_com = (struct nc_rpc_commit *)rpc;
2332
2333 data = lyd_new(NULL, ietfnc, "commit");
2334 if (rpc_com->confirmed) {
2335 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2336 }
2337
2338 if (rpc_com->confirm_timeout) {
2339 sprintf(str, "%u", rpc_com->confirm_timeout);
2340 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2341 }
2342
2343 if (rpc_com->persist) {
2344 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2345 if (!node) {
2346 lyd_free(data);
2347 return NC_MSG_ERROR;
2348 }
2349 }
2350
2351 if (rpc_com->persist_id) {
2352 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2353 if (!node) {
2354 lyd_free(data);
2355 return NC_MSG_ERROR;
2356 }
2357 }
2358 break;
2359
2360 case NC_RPC_DISCARD:
2361 data = lyd_new(NULL, ietfnc, "discard-changes");
2362 break;
2363
2364 case NC_RPC_CANCEL:
2365 rpc_can = (struct nc_rpc_cancel *)rpc;
2366
2367 data = lyd_new(NULL, ietfnc, "cancel-commit");
2368 if (rpc_can->persist_id) {
2369 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2370 if (!node) {
2371 lyd_free(data);
2372 return NC_MSG_ERROR;
2373 }
2374 }
2375 break;
2376
2377 case NC_RPC_VALIDATE:
2378 rpc_val = (struct nc_rpc_validate *)rpc;
2379
2380 data = lyd_new(NULL, ietfnc, "validate");
2381 node = lyd_new(data, ietfnc, "source");
2382 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002383 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002384 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002385 } else {
2386 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2387 }
2388 } else {
2389 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2390 }
2391 if (!node) {
2392 lyd_free(data);
2393 return NC_MSG_ERROR;
2394 }
2395 break;
2396
2397 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002398 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002399 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002400 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002401 return NC_MSG_ERROR;
2402 }
2403
2404 rpc_gs = (struct nc_rpc_getschema *)rpc;
2405
2406 data = lyd_new(NULL, ietfncmon, "get-schema");
2407 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2408 if (!node) {
2409 lyd_free(data);
2410 return NC_MSG_ERROR;
2411 }
2412 if (rpc_gs->version) {
2413 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2414 if (!node) {
2415 lyd_free(data);
2416 return NC_MSG_ERROR;
2417 }
2418 }
2419 if (rpc_gs->format) {
2420 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2421 if (!node) {
2422 lyd_free(data);
2423 return NC_MSG_ERROR;
2424 }
2425 }
2426 break;
2427
2428 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002429 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002430 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002431 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002432 return NC_MSG_ERROR;
2433 }
2434
2435 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2436
2437 data = lyd_new(NULL, notifs, "create-subscription");
2438 if (rpc_sub->stream) {
2439 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2440 if (!node) {
2441 lyd_free(data);
2442 return NC_MSG_ERROR;
2443 }
2444 }
2445
2446 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002447 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002448 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002449 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002450 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002451 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002452 lyd_insert_attr(node, NULL, "type", "xpath");
2453 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002454 }
2455 if (!node) {
2456 lyd_free(data);
2457 return NC_MSG_ERROR;
2458 }
2459 }
2460
2461 if (rpc_sub->start) {
2462 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2463 if (!node) {
2464 lyd_free(data);
2465 return NC_MSG_ERROR;
2466 }
2467 }
2468
2469 if (rpc_sub->stop) {
2470 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2471 if (!node) {
2472 lyd_free(data);
2473 return NC_MSG_ERROR;
2474 }
2475 }
2476 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002477 default:
2478 ERRINT;
2479 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002480 }
2481
Michal Vasko131120a2018-05-29 15:44:02 +02002482 if (!data) {
2483 /* error was already printed */
2484 return NC_MSG_ERROR;
2485 }
2486
Michal Vasko41adf392017-01-17 10:39:04 +01002487 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2488 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002489 if (dofree) {
2490 lyd_free(data);
2491 }
Michal Vasko086311b2016-01-08 09:53:11 +01002492 return NC_MSG_ERROR;
2493 }
2494
Michal Vasko131120a2018-05-29 15:44:02 +02002495 /* send RPC, store its message ID */
2496 r = nc_send_msg_io(session, timeout, data);
2497 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002498
Radek Krejcib4b19062018-02-07 16:33:06 +01002499 if (dofree) {
2500 lyd_free(data);
2501 }
Michal Vasko086311b2016-01-08 09:53:11 +01002502
Michal Vasko131120a2018-05-29 15:44:02 +02002503 if (r == NC_MSG_RPC) {
2504 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002505 }
Michal Vasko131120a2018-05-29 15:44:02 +02002506 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002507}
Michal Vaskode2946c2017-01-12 12:19:26 +01002508
2509API void
2510nc_client_session_set_not_strict(struct nc_session *session)
2511{
2512 if (session->side != NC_CLIENT) {
2513 ERRARG("session");
2514 return;
2515 }
2516
2517 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2518}