blob: 51b6c77684a17ef3bdbfbf769f1835e543e3d6a7 [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 Krejci3b60e5c2018-08-17 10:10:16 +0200295 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
296 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
297 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200298 return NULL;
299 }
300 if (localfile) {
301 VRB("Session %u: reading schema from localfile \"%s\".", clb_data->session->id, localfile);
302 f = fopen(localfile, "r");
303 if (!f) {
304 ERR("Session %u: unable to open \"%s\" file to get schema (%s).",
305 clb_data->session->id, localfile, strerror(errno));
306 free(localfile);
307 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100308 }
Michal Vasko086311b2016-01-08 09:53:11 +0100309
Radek Krejci65ef6d52018-08-16 16:35:02 +0200310 fseek(f, 0, SEEK_END);
311 length = ftell(f);
312 fseek(f, 0, SEEK_SET);
313
314 model_data = malloc(length + 1);
315 if (!model_data) {
316 ERRMEM;
317 } else if ((l = fread(model_data, 1, length, f)) != length) {
318 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
319 clb_data->session->id, localfile, l, length);
320 free(model_data);
321 model_data = NULL;
322 } else {
323 /* terminating NULL byte */
324 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100325 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200326 fclose(f);
327 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100328 }
329
Radek Krejci65ef6d52018-08-16 16:35:02 +0200330 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100331}
332
333static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200334retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
335 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100336{
Michal Vasko086311b2016-01-08 09:53:11 +0100337 struct nc_rpc *rpc;
338 struct nc_reply *reply;
339 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200340 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200341 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100342 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100343 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200344 char *localfile = NULL;
345 FILE *f;
346 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100347
Radek Krejci65ef6d52018-08-16 16:35:02 +0200348 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
349 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100350
Radek Krejci65ef6d52018-08-16 16:35:02 +0200351 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100352 usleep(1000);
353 }
354 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200355 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100356 nc_rpc_free(rpc);
357 return NULL;
358 }
359
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200360 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200361 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200362 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100363 nc_rpc_free(rpc);
364 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200365 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100366 return NULL;
367 } else if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200368 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100369 return NULL;
370 }
371
Michal Vasko998ba412016-09-16 12:00:07 +0200372 switch (reply->type) {
373 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200374 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200375 nc_reply_free(reply);
376 return NULL;
377 case NC_RPL_DATA:
378 /* fine */
379 break;
380 case NC_RPL_ERROR:
381 error_rpl = (struct nc_reply_error *)reply;
382 if (error_rpl->count) {
383 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200384 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200385 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200386 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200387 }
388 nc_reply_free(reply);
389 return NULL;
390 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200391 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100392 nc_reply_free(reply);
393 return NULL;
394 }
395
Michal Vasko086311b2016-01-08 09:53:11 +0100396 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200397 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
398 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200399 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200400 nc_reply_free(reply);
401 return NULL;
402 }
Radek Krejci539efb62016-08-24 15:05:16 +0200403 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
404 switch (get_schema_data->value_type) {
405 case LYD_ANYDATA_CONSTSTRING:
406 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200407 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200408 break;
409 case LYD_ANYDATA_DATATREE:
410 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
411 break;
412 case LYD_ANYDATA_XML:
413 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
414 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200415 case LYD_ANYDATA_JSON:
416 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200417 case LYD_ANYDATA_SXML:
418 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200419 case LYD_ANYDATA_LYB:
420 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200421 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200422 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200423 }
Michal Vasko086311b2016-01-08 09:53:11 +0100424 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100425
Radek Krejcifd5b6682017-06-13 15:52:53 +0200426 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200427 if (model_data) {
428 *format = LYS_IN_YANG;
429 if (client_opts.schema_searchpath) {
430 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
431 rev ? "@" : "", rev ? rev : "") == -1) {
432 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100433 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200434 f = fopen(localfile, "w");
435 if (!f) {
436 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
437 localfile, strerror(errno));
438 } else {
439 fputs(model_data, f);
440 fclose(f);
441 }
442 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100443 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200444 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200445 }
446
Michal Vasko086311b2016-01-08 09:53:11 +0100447 return model_data;
448}
449
Radek Krejci65ef6d52018-08-16 16:35:02 +0200450static char *
451retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
452 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data))
453{
454 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
455 unsigned int u, v, match = 1;
456 const char *name = NULL, *rev = NULL;
457 char *model_data = NULL;
458
459 /* get and check the final name and revision of the schema to be retrieved */
460 if (!mod_rev || !mod_rev[0]) {
461 /* newest revision requested - get the newest revision from the list of available modules on server */
462 match = 0;
463 for (u = 0; clb_data->schemas[u].name; ++u) {
464 if (strcmp(mod_name, clb_data->schemas[u].name)) {
465 continue;
466 }
467 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
468 mod_rev = clb_data->schemas[u].revision;
469 }
470 match = u + 1;
471 }
472 if (!match) {
473 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
474 clb_data->session->id, mod_name);
475 }
476 }
477 if (submod_name) {
478 name = submod_name;
479 if (sub_rev) {
480 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200481 } else if (match) {
482 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200483 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
484 clb_data->session->id, submod_name, mod_name);
485 return NULL;
486 }
Radek Krejci6455eb12018-08-17 09:26:29 +0200487 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
488 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
489 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200490 }
491 }
492 if (!rev) {
493 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
494 clb_data->session->id, submod_name, mod_name);
495 return NULL;
496 }
497 }
498 } else {
499 name = mod_name;
500 rev = mod_rev;
501 }
502
503 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
504
505 if (match) {
506 /* we have enough information to avoid communication with server and try to get
507 * the schema locally */
508
509 /* 1. try to get data locally */
510 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
511
512 /* 2. try to use <get-schema> */
513 if (!model_data && clb_data->has_get_schema) {
514 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
515 }
516 } else {
517 /* we are unsure which revision of the schema we should load, so first try to get
518 * the newest revision from the server via get-schema and only if the server does not
519 * implement get-schema, try to load the newest revision locally. This is imperfect
520 * solution, but there are situation when a client does not know what revision is
521 * actually implemented by the server. */
522
523 /* 1. try to use <get-schema> */
524 if (clb_data->has_get_schema) {
525 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
526 }
527
528 /* 2. try to get data locally */
529 if (!model_data) {
530 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
531 }
532 }
533
534 /* 3. try to use user callback */
535 if (!model_data && clb_data->user_clb) {
536 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
537 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
538 }
539
540 *free_module_data = free;
541 return model_data;
542}
543
Michal Vaskoceae0152018-02-14 16:03:59 +0100544static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200545nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200546 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 +0100547{
548 int ret = 0;
549 struct ly_err_item *eitem;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200550 char *module_data = NULL;
551 LYS_INFORMAT format;
552 void (*free_module_data)(void*) = NULL;
553 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100554
Radek Krejci65ef6d52018-08-16 16:35:02 +0200555 *mod = NULL;
556 if (revision) {
557 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
558 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100559 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200560 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100561 /* make the present module implemented */
562 if (lys_set_implemented(*mod)) {
563 ERR("Failed to implement model \"%s\".", (*mod)->name);
564 ret = -1;
565 }
566 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200567 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100568 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200569 clb_data.has_get_schema = has_get_schema;
570 clb_data.schemas = schemas;
571 clb_data.session = session;
572 clb_data.user_clb = user_clb;
573 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100574
575 /* clear all the errors and just collect them for now */
576 ly_err_clean(session->ctx, NULL);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200577 //ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100578
Radek Krejci65ef6d52018-08-16 16:35:02 +0200579 /* get module data */
580 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100581
Radek Krejci65ef6d52018-08-16 16:35:02 +0200582 if (module_data) {
583 /* parse the schema */
584 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100585
Radek Krejci65ef6d52018-08-16 16:35:02 +0200586 *mod = lys_parse_mem(session->ctx, module_data, format);
587 if (*free_module_data) {
588 (*free_module_data)(module_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100589 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100590
Radek Krejci65ef6d52018-08-16 16:35:02 +0200591 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
592 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100593
Michal Vaskodbf7c272018-02-19 09:07:59 +0100594 /* restore logging options, then print errors on definite failure */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200595 //ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100596 if (!(*mod)) {
597 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
598 ly_err_print(eitem);
599 }
600 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200601 } else {
602 /* print only warnings */
603 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
604 if (eitem->level == LY_LLWRN) {
605 ly_err_print(eitem);
606 }
607 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100608 }
609
Michal Vaskodbf7c272018-02-19 09:07:59 +0100610 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100611 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100612 }
613
614 return ret;
615}
616
Radek Krejci65ef6d52018-08-16 16:35:02 +0200617static void
618free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200619{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200620 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200621
Radek Krejci65ef6d52018-08-16 16:35:02 +0200622 if (!list) {
623 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200624 }
625
Radek Krejci65ef6d52018-08-16 16:35:02 +0200626 for (u = 0; list[u].name; ++u) {
627 free(list[u].name);
628 free(list[u].revision);
629 for (v = 0; v < list[u].features.number; ++v) {
630 free(list[u].features.set.g[v]);
631 }
632 free(list[u].features.set.g);
633 }
634 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200635}
636
Radek Krejci65ef6d52018-08-16 16:35:02 +0200637static struct schema_info *
638build_schema_info_yl(struct nc_session *session)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200639{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200640 struct nc_rpc *rpc = NULL;
641 struct nc_reply *reply = NULL;
642 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200643 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200644 NC_MSG_TYPE msg;
645 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200646 struct ly_set *modules;
647 struct schema_info *result;
648 unsigned int u, c;
649 struct lyd_node *iter;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200650
651 /* get yang-library data from the server */
Michal Vaskoac0356e2018-06-22 13:24:29 +0200652 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200653 if (!rpc) {
654 goto cleanup;
655 }
656
657 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
658 usleep(1000);
659 }
660 if (msg == NC_MSG_ERROR) {
Michal Vasko60f66602017-10-17 13:52:18 +0200661 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200662 session->id);
663 goto cleanup;
664 }
665
666 do {
667 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
668 } while (msg == NC_MSG_NOTIF);
669 if (msg == NC_MSG_WOULDBLOCK) {
670 ERR("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
671 goto cleanup;
672 } else if (msg == NC_MSG_ERROR) {
673 ERR("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
674 goto cleanup;
675 }
676
677 switch (reply->type) {
678 case NC_RPL_OK:
679 ERR("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
680 goto cleanup;
681 case NC_RPL_DATA:
682 /* fine */
683 break;
684 case NC_RPL_ERROR:
685 error_rpl = (struct nc_reply_error *)reply;
686 if (error_rpl->count) {
687 ERR("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
688 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
689 } else {
690 ERR("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
691 }
692 goto cleanup;
693 case NC_RPL_NOTIF:
694 ERR("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
695 goto cleanup;
696 }
697
Radek Krejci65ef6d52018-08-16 16:35:02 +0200698 yldata = ((struct nc_reply_data *)reply)->data;
699 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200700 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
701 goto cleanup;
702 }
703
Radek Krejci65ef6d52018-08-16 16:35:02 +0200704 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
705
706 c = modules ? modules->number : 0;
707 result = calloc(c + 1, sizeof *result);
708 if (!result) {
709 return NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200710 }
711
Radek Krejci65ef6d52018-08-16 16:35:02 +0200712 for (u = 0; u < c; ++u) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200713 LY_TREE_FOR(modules->set.d[u]->child, iter) {
714 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
715 /* ignore empty nodes */
716 continue;
717 }
718 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200719 result[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200720 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200721 result[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200722 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200723 result[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200724 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200725 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 +0200726 }
727 }
728 }
729
Radek Krejcifd5b6682017-06-13 15:52:53 +0200730
731cleanup:
732 nc_rpc_free(rpc);
733 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200734 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200735
Radek Krejci65ef6d52018-08-16 16:35:02 +0200736 return result;
737}
738
739static struct schema_info *
740build_schema_info_cpblts(char **cpblts)
741{
742 unsigned int u, v;
743 struct schema_info *result;
744 char *module_cpblt, *ptr, *ptr2;
745
746 for (u = 0; cpblts[u]; ++u);
747 result = calloc(u + 1, sizeof *result);
748
749 for (u = v = 0; cpblts[u]; ++u) {
750 module_cpblt = strstr(cpblts[u], "module=");
751 /* this capability requires a module */
752 if (!module_cpblt) {
753 continue;
754 }
755
756 /* get module's name */
757 ptr = (char *)module_cpblt + 7;
758 ptr2 = strchr(ptr, '&');
759 if (!ptr2) {
760 ptr2 = ptr + strlen(ptr);
761 }
762 result[v].name = strndup(ptr, ptr2 - ptr);
763
764 /* get module's revision */
765 ptr = strstr(module_cpblt, "revision=");
766 if (ptr) {
767 ptr += 9;
768 ptr2 = strchr(ptr, '&');
769 if (!ptr2) {
770 ptr2 = ptr + strlen(ptr);
771 }
772 result[v].revision = strndup(ptr, ptr2 - ptr);
773 }
774
775 /* all are implemented since there is no better information in capabilities list */
776 result[v].implemented = 1;
777
778 /* get module's features */
779 ptr = strstr(module_cpblt, "features=");
780 if (ptr) {
781 ptr += 9;
782 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
783 if (*ptr == ',') {
784 ly_set_add(&result[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
785 ptr2 = ptr + 1;
786 }
787 }
788 /* the last one */
789 ly_set_add(&result[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
790 }
791 ++v;
792 }
793 return result;
794}
795
796static int
797nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
798{
799 int ret = EXIT_FAILURE;
800 const struct lys_module *mod;
801 unsigned int u, v;
802
803 for (u = 0; modules[u].name; ++u) {
804 /* we can continue even if it fails */
805 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
806
807 if (!mod) {
808 if (session->status != NC_STATUS_RUNNING) {
809 /* something bad heppened, discard the session */
810 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
811 goto cleanup;
812 }
813
814 /* all loading ways failed, the schema will be ignored in the received data */
815 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
816 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
817 } else {
818 /* set features - first disable all to enable specified then */
819 lys_features_disable(mod, "*");
820 for (v = 0; v < modules[u].features.number; v++) {
821 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
822 }
823 }
Michal Vasko60f66602017-10-17 13:52:18 +0200824 }
825
Radek Krejci65ef6d52018-08-16 16:35:02 +0200826 /* done */
827 ret = EXIT_SUCCESS;
828
829cleanup:
830
Radek Krejcifd5b6682017-06-13 15:52:53 +0200831 return ret;
832}
833
Radek Krejci65ef6d52018-08-16 16:35:02 +0200834static int
835nc_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)
836{
837 unsigned int u, v;
838 const struct lys_module *ietfnc;
839
840 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
841 if (!ietfnc) {
842 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
843 if (!ietfnc) {
844 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
845 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
846 }
847 }
848 if (!ietfnc) {
849 ERR("Loading base NETCONF schema failed.");
850 return 1;
851 }
852
853 /* set supported capabilities from ietf-netconf */
854 for (u = 0; modules[u].name; ++u) {
855 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
856 continue;
857 }
858
859 lys_features_disable(ietfnc, "*");
860 for (v = 0; v < modules[u].features.number; v++) {
861 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
862 }
863 }
864
865 return 0;
866}
867
Michal Vasko086311b2016-01-08 09:53:11 +0100868int
869nc_ctx_check_and_fill(struct nc_session *session)
870{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200871 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100872 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100873 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200874 const struct lys_module *mod = NULL;
875 char *revision;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200876 struct schema_info *server_modules = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100877
Michal Vasko2e6defd2016-10-07 15:48:15 +0200878 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100879
Radek Krejci65ef6d52018-08-16 16:35:02 +0200880 /* 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 +0200881 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200882
Radek Krejci65ef6d52018-08-16 16:35:02 +0200883 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
884 * schemas only to those present on the server side */
885 ly_ctx_set_disable_searchdirs(session->ctx);
886
887 /* our callback is set later with appropriate data */
888 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
889
890 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200891 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200892 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200893 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200894 if (yanglib_support) {
895 break;
896 }
897 } 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 +0200898 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200899 if (get_schema_support) {
900 break;
901 }
Michal Vasko086311b2016-01-08 09:53:11 +0100902 }
903 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200904
905 /* get information about server's schemas from capabilities list until we will have yang-library */
906 if (!(server_modules = build_schema_info_cpblts(session->opts.client.cpblts))) {
907 goto cleanup;
908 }
909
Michal Vasko086311b2016-01-08 09:53:11 +0100910 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200911 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200912 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
913 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200914 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100915 }
916 }
917
918 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200919 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200920 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100921 }
922
Radek Krejci65ef6d52018-08-16 16:35:02 +0200923 /* get correct version of ietf-yang-library into context */
924 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200925 /* use get schema to get server's ietf-yang-library */
926 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
927 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200928 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
929 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +0200930 yanglib_support = 0;
931 } else {
932 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200933 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
934 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 +0200935 yanglib_support = 0;
936 }
937 free(revision);
938 }
939 }
940
Radek Krejci65ef6d52018-08-16 16:35:02 +0200941 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200942 if (yanglib_support) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200943 if (server_modules) {
944 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
945 free_schema_info(server_modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200946 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200947 server_modules = build_schema_info_yl(session);
948 if (!server_modules) {
949 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
950 }
951 }
952 if (!server_modules) { /* also in case of error of getting yang-library data */
953 server_modules = build_schema_info_cpblts(session->opts.client.cpblts);
954 }
955 if (!server_modules) {
956 goto cleanup;
957 }
Michal Vaskoef578332016-01-25 13:20:09 +0100958
Radek Krejci65ef6d52018-08-16 16:35:02 +0200959 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
960 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100961 }
962
Radek Krejcifd5b6682017-06-13 15:52:53 +0200963 /* succsess */
964 ret = 0;
965
Michal Vaskoeee99412016-11-21 10:19:43 +0100966 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200967 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 +0100968 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200969
970cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200971 free_schema_info(server_modules);
972
Radek Krejcifd5b6682017-06-13 15:52:53 +0200973 /* set user callback back */
974 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200975 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200976
Michal Vaskoef578332016-01-25 13:20:09 +0100977 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100978}
979
980API struct nc_session *
981nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
982{
Michal Vaskod083db62016-01-19 10:31:29 +0100983 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100984
Michal Vasko45e53ae2016-04-07 11:46:03 +0200985 if (fdin < 0) {
986 ERRARG("fdin");
987 return NULL;
988 } else if (fdout < 0) {
989 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100990 return NULL;
991 }
992
993 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200994 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100995 if (!session) {
996 ERRMEM;
997 return NULL;
998 }
999 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001000
1001 /* transport specific data */
1002 session->ti_type = NC_TI_FD;
1003 session->ti.fd.in = fdin;
1004 session->ti.fd.out = fdout;
1005
1006 /* assign context (dicionary needed for handshake) */
1007 if (!ctx) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001008 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001009 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001010 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001011 /* that's just it */
1012 goto fail;
1013 }
Michal Vasko086311b2016-01-08 09:53:11 +01001014 } else {
1015 session->flags |= NC_SESSION_SHAREDCTX;
1016 }
1017 session->ctx = ctx;
1018
1019 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001020 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001021 goto fail;
1022 }
1023 session->status = NC_STATUS_RUNNING;
1024
Michal Vaskoef578332016-01-25 13:20:09 +01001025 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001026 goto fail;
1027 }
1028
1029 return session;
1030
1031fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001032 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001033 return NULL;
1034}
1035
Frank Rimpler9f838b02018-07-25 06:44:03 +00001036/*
1037 Helper for a non-blocking connect (which is required because of the locking
1038 concept for e.g. call home settings). For more details see nc_sock_connect().
1039 */
1040static int
1041_non_blocking_connect(int timeout, int* sock_pending, struct addrinfo *res)
Michal Vasko086311b2016-01-08 09:53:11 +01001042{
Frank Rimplerc7a2acb2018-08-06 07:31:08 +00001043 int flags, ret=0;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001044 int sock = -1;
1045 fd_set wset;
1046 struct timeval ts;
1047 int error = 0;
1048 socklen_t len = sizeof(int);
Michal Vasko086311b2016-01-08 09:53:11 +01001049
Frank Rimpler9f838b02018-07-25 06:44:03 +00001050 if (sock_pending && *sock_pending != -1) {
1051 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1052 sock = *sock_pending;
1053 } else {
1054 assert(res);
1055 VRB("Trying to connect via %s.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1056 /* Connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001057 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1058 if (sock == -1) {
Frank Rimpler9f838b02018-07-25 06:44:03 +00001059 ERR("socket couldn't be created.", strerror(errno));
1060 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001061 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001062 /* make the socket non-blocking */
1063 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
1064 ERR("Fcntl failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001065 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001066 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001067 /* non-blocking connect! */
1068 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1069 if (errno != EINPROGRESS) {
1070 /* network connection failed, try another resource */
1071 ERR("connect failed: (%s).", strerror(errno));
1072 goto cleanup;
1073 }
1074 }
1075 /* check the usability of the socket */
1076 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1077 ERR("getsockopt failed: (%s).", strerror(errno));
1078 goto cleanup;
1079 }
1080 if (error == ECONNREFUSED) {
1081 /* network connection failed, try another resource */
1082 VRB("getsockopt error: (%s).", strerror(error));
1083 goto cleanup;
1084 }
1085 }
1086 ts.tv_sec = timeout;
1087 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001088
Frank Rimpler9f838b02018-07-25 06:44:03 +00001089 FD_ZERO(&wset);
1090 FD_SET(sock, &wset);
1091
1092 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
1093 ERR("select failed: (%s).", strerror(errno));
1094 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001095 }
1096
Frank Rimpler9f838b02018-07-25 06:44:03 +00001097 if (ret == 0) { //we had a timeout
1098 VRB("timed out after %ds (%s).", timeout, strerror(errno));
1099 if (sock_pending) {
1100 /* no sock-close, we'll try it again */
1101 *sock_pending = sock;
1102 } else {
1103 close(sock);
1104 }
1105 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001106 }
Michal Vasko086311b2016-01-08 09:53:11 +01001107 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001108
Frank Rimpler9f838b02018-07-25 06:44:03 +00001109cleanup:
1110 if (sock_pending) {
1111 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001112 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001113 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001114 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001115}
1116
Frank Rimpler9f838b02018-07-25 06:44:03 +00001117/* A given timeout value limits the time how long the function blocks. If it has to block
1118 only for some seconds, a socket connection might not yet have been fully established.
1119 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1120 value will be -1. In such a case a subsequent invokation is required, by providing the
1121 stored sock_pending, again.
1122 In general, if this function returns -1, when a timeout has been given, this function
1123 has to be invoked, until it returns a valid socket.
1124 */
1125int
1126nc_sock_connect(const char* host, uint16_t port, int timeout, int* sock_pending)
1127{
1128 int i;
1129 int sock = sock_pending?*sock_pending:-1;
1130 struct addrinfo hints, *res_list, *res;
1131 char port_s[6]; /* length of string representation of short int */
1132
1133 VRB("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
1134
1135 /* no pending socket */
1136 if (sock == -1) {
1137 /* Connect to a server */
1138 snprintf(port_s, 6, "%u", port);
1139 memset(&hints, 0, sizeof hints);
1140 hints.ai_family = AF_UNSPEC;
1141 hints.ai_socktype = SOCK_STREAM;
1142 hints.ai_protocol = IPPROTO_TCP;
1143 i = getaddrinfo(host, port_s, &hints, &res_list);
1144 if (i != 0) {
1145 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1146 return -1;
1147 }
1148
1149 for (res = res_list; res != NULL; res = res->ai_next) {
1150 sock = _non_blocking_connect(timeout, sock_pending, res);
1151 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1152 /* try the next resource */
1153 continue;
1154 }
1155 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1156 break;
1157 }
1158 freeaddrinfo(res_list);
1159
1160 } else {
1161 /* try to get a connection with the pending socket */
1162 assert(sock_pending);
1163 sock = _non_blocking_connect(timeout, sock_pending, NULL);
1164 }
1165
1166 return sock;
1167}
1168
Michal Vasko086311b2016-01-08 09:53:11 +01001169static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001170get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001171{
Michal Vasko086311b2016-01-08 09:53:11 +01001172 char *ptr;
1173 const char *str_msgid;
1174 uint64_t cur_msgid;
1175 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001176 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001177 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1178
Michal Vasko086311b2016-01-08 09:53:11 +01001179 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001180 if (!msgid && session->opts.client.notifs) {
1181 cont = session->opts.client.notifs;
1182 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001183
Michal Vasko71ba2da2016-05-04 10:53:16 +02001184 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001185 free(cont);
1186
Michal Vasko71ba2da2016-05-04 10:53:16 +02001187 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001188 }
1189
1190 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001191 if (msgid && session->opts.client.replies) {
1192 cont = session->opts.client.replies;
1193 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001194
Michal Vasko71ba2da2016-05-04 10:53:16 +02001195 xml = cont->msg;
1196 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001197
Michal Vasko71ba2da2016-05-04 10:53:16 +02001198 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001199 }
1200
Michal Vasko71ba2da2016-05-04 10:53:16 +02001201 if (!msgtype) {
1202 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001203 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001204 }
Michal Vasko086311b2016-01-08 09:53:11 +01001205
1206 /* we read rpc-reply, want a notif */
1207 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001208 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001209 while (*cont_ptr) {
1210 cont_ptr = &((*cont_ptr)->next);
1211 }
1212 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001213 if (!*cont_ptr) {
1214 ERRMEM;
1215 lyxml_free(session->ctx, xml);
1216 return NC_MSG_ERROR;
1217 }
Michal Vasko086311b2016-01-08 09:53:11 +01001218 (*cont_ptr)->msg = xml;
1219 (*cont_ptr)->next = NULL;
1220 }
1221
1222 /* we read notif, want a rpc-reply */
1223 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001224 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001225 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001226 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001227 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001228 }
Michal Vasko086311b2016-01-08 09:53:11 +01001229
Michal Vasko2e6defd2016-10-07 15:48:15 +02001230 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001231 while (*cont_ptr) {
1232 cont_ptr = &((*cont_ptr)->next);
1233 }
1234 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001235 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001236 ERRMEM;
1237 lyxml_free(session->ctx, xml);
1238 return NC_MSG_ERROR;
1239 }
Michal Vasko086311b2016-01-08 09:53:11 +01001240 (*cont_ptr)->msg = xml;
1241 (*cont_ptr)->next = NULL;
1242 }
1243
Michal Vasko086311b2016-01-08 09:53:11 +01001244 switch (msgtype) {
1245 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001246 if (!msgid) {
1247 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001248 }
Michal Vasko086311b2016-01-08 09:53:11 +01001249 break;
1250
1251 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001252 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001253 /* check message-id */
1254 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1255 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001256 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001257 } else {
1258 cur_msgid = strtoul(str_msgid, &ptr, 10);
1259 if (cur_msgid != msgid) {
1260 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1261 session->id, str_msgid);
1262 msgtype = NC_MSG_REPLY_ERR_MSGID;
1263 }
1264 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001265 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001266 }
Michal Vasko086311b2016-01-08 09:53:11 +01001267 break;
1268
1269 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001270 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001271 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001272 msgtype = NC_MSG_ERROR;
1273 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001274
1275 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001276 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001277 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001278 msgtype = NC_MSG_ERROR;
1279 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001280
1281 default:
1282 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1283 * NC_MSG_NONE is not returned by nc_read_msg()
1284 */
1285 break;
1286 }
1287
1288 return msgtype;
1289}
1290
1291/* cannot strictly fail, but does not need to fill any error parameter at all */
1292static void
1293parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1294{
1295 struct lyxml_elem *iter, *next, *info;
1296
1297 LY_TREE_FOR(xml->child, iter) {
1298 if (!iter->ns) {
1299 if (iter->content) {
1300 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1301 } else {
1302 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1303 }
1304 continue;
1305 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1306 if (iter->content) {
1307 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1308 iter->name, iter->content, iter->ns->value);
1309 } else {
1310 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1311 }
1312 continue;
1313 }
1314
1315 if (!strcmp(iter->name, "error-type")) {
1316 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1317 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1318 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1319 } else if (err->type) {
1320 WRN("<rpc-error> <error-type> duplicated.");
1321 } else {
1322 err->type = lydict_insert(ctx, iter->content, 0);
1323 }
1324 } else if (!strcmp(iter->name, "error-tag")) {
1325 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1326 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1327 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1328 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1329 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1330 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1331 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1332 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1333 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1334 && strcmp(iter->content, "malformed-message"))) {
1335 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1336 } else if (err->tag) {
1337 WRN("<rpc-error> <error-tag> duplicated.");
1338 } else {
1339 err->tag = lydict_insert(ctx, iter->content, 0);
1340 }
1341 } else if (!strcmp(iter->name, "error-severity")) {
1342 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1343 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1344 } else if (err->severity) {
1345 WRN("<rpc-error> <error-severity> duplicated.");
1346 } else {
1347 err->severity = lydict_insert(ctx, iter->content, 0);
1348 }
1349 } else if (!strcmp(iter->name, "error-app-tag")) {
1350 if (err->apptag) {
1351 WRN("<rpc-error> <error-app-tag> duplicated.");
1352 } else {
1353 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1354 }
1355 } else if (!strcmp(iter->name, "error-path")) {
1356 if (err->path) {
1357 WRN("<rpc-error> <error-path> duplicated.");
1358 } else {
1359 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1360 }
1361 } else if (!strcmp(iter->name, "error-message")) {
1362 if (err->message) {
1363 WRN("<rpc-error> <error-message> duplicated.");
1364 } else {
1365 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001366 if (err->message_lang) {
1367 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1368 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001369 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1370 }
1371 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1372 }
1373 } else if (!strcmp(iter->name, "error-info")) {
1374 LY_TREE_FOR_SAFE(iter->child, next, info) {
1375 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1376 if (!strcmp(info->name, "session-id")) {
1377 if (err->sid) {
1378 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1379 } else {
1380 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1381 }
Michal Vasko630485f2018-01-03 14:28:32 +01001382 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001383 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001384 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1385 if (!err->attr) {
1386 ERRMEM;
1387 return;
1388 }
Michal Vasko086311b2016-01-08 09:53:11 +01001389 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1390 } else if (!strcmp(info->name, "bad-element")) {
1391 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001392 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1393 if (!err->elem) {
1394 ERRMEM;
1395 return;
1396 }
Michal Vasko086311b2016-01-08 09:53:11 +01001397 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1398 } else if (!strcmp(info->name, "bad-namespace")) {
1399 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001400 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1401 if (!err->ns) {
1402 ERRMEM;
1403 return;
1404 }
Michal Vasko086311b2016-01-08 09:53:11 +01001405 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1406 } else {
1407 if (info->content) {
1408 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1409 info->name, info->content);
1410 } else {
1411 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1412 }
1413 }
1414 } else {
1415 lyxml_unlink(ctx, info);
1416 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001417 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1418 if (!err->other) {
1419 ERRMEM;
1420 return;
1421 }
Michal Vasko086311b2016-01-08 09:53:11 +01001422 err->other[err->other_count - 1] = info;
1423 }
1424 }
1425 } else {
1426 if (iter->content) {
1427 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1428 } else {
1429 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1430 }
1431 }
1432 }
1433}
1434
1435static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001436parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001437{
1438 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001439 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001440 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001441 struct nc_reply_data *data_rpl;
1442 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001443 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001444 int i;
1445
1446 if (!xml->child) {
1447 ERR("An empty <rpc-reply>.");
1448 return NULL;
1449 }
1450
1451 /* rpc-error */
1452 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1453 /* count and check elements */
1454 i = 0;
1455 LY_TREE_FOR(xml->child, iter) {
1456 if (strcmp(iter->name, "rpc-error")) {
1457 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1458 return NULL;
1459 } else if (!iter->ns) {
1460 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1461 return NULL;
1462 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1463 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1464 return NULL;
1465 }
1466 ++i;
1467 }
1468
1469 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001470 if (!error_rpl) {
1471 ERRMEM;
1472 return NULL;
1473 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001474 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001475 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001476 if (!error_rpl->err) {
1477 ERRMEM;
1478 free(error_rpl);
1479 return NULL;
1480 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001481 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001482 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001483 reply = (struct nc_reply *)error_rpl;
1484
1485 i = 0;
1486 LY_TREE_FOR(xml->child, iter) {
1487 parse_rpc_error(ctx, iter, error_rpl->err + i);
1488 ++i;
1489 }
1490
1491 /* ok */
1492 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1493 if (xml->child->next) {
1494 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1495 return NULL;
1496 }
1497 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001498 if (!reply) {
1499 ERRMEM;
1500 return NULL;
1501 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001502 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001503
1504 /* some RPC output */
1505 } else {
1506 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001507 case NC_RPC_ACT_GENERIC:
1508 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001509
1510 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001511 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1512 if (!rpc_act) {
1513 ERR("Failed to duplicate a generic RPC/action.");
1514 return NULL;
1515 }
Michal Vasko086311b2016-01-08 09:53:11 +01001516 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001517 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 +02001518 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001519 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001520 return NULL;
1521 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001522 }
Michal Vasko086311b2016-01-08 09:53:11 +01001523 break;
1524
1525 case NC_RPC_GETCONFIG:
1526 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001527 if (!xml->child->child) {
1528 /* we did not receive any data */
1529 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001530 if (!data_rpl) {
1531 ERRMEM;
1532 return NULL;
1533 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001534 data_rpl->type = NC_RPL_DATA;
1535 data_rpl->data = NULL;
1536 return (struct nc_reply *)data_rpl;
1537 }
1538
Michal Vasko086311b2016-01-08 09:53:11 +01001539 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001540 data = lyd_parse_xml(ctx, &xml->child->child,
1541 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001542 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001543 if (!data) {
1544 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1545 return NULL;
1546 }
1547 break;
1548
1549 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001550 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001551 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001552 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001553 return NULL;
1554 }
1555 break;
1556
1557 case NC_RPC_EDIT:
1558 case NC_RPC_COPY:
1559 case NC_RPC_DELETE:
1560 case NC_RPC_LOCK:
1561 case NC_RPC_UNLOCK:
1562 case NC_RPC_KILL:
1563 case NC_RPC_COMMIT:
1564 case NC_RPC_DISCARD:
1565 case NC_RPC_CANCEL:
1566 case NC_RPC_VALIDATE:
1567 case NC_RPC_SUBSCRIBE:
1568 /* there is no output defined */
1569 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1570 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001571 default:
1572 ERRINT;
1573 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001574 }
1575
1576 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001577 if (!data_rpl) {
1578 ERRMEM;
1579 return NULL;
1580 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001581 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001582 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001583 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001584 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001585 } else {
1586 /* <get>, <get-config> */
1587 data_rpl->data = data;
1588 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001589 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001590 if (!data_rpl->data) {
1591 ERR("Failed to parse <rpc-reply>.");
1592 free(data_rpl);
1593 return NULL;
1594 }
1595 reply = (struct nc_reply *)data_rpl;
1596 }
1597
1598 return reply;
1599}
1600
Radek Krejci53691be2016-02-22 13:58:37 +01001601#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001602
Michal Vasko3031aae2016-01-27 16:07:18 +01001603int
1604nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1605{
1606 int sock;
1607
Michal Vasko45e53ae2016-04-07 11:46:03 +02001608 if (!address) {
1609 ERRARG("address");
1610 return -1;
1611 } else if (!port) {
1612 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001613 return -1;
1614 }
1615
1616 sock = nc_sock_listen(address, port);
1617 if (sock == -1) {
1618 return -1;
1619 }
1620
1621 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001622 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1623 if (!client_opts.ch_binds) {
1624 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001625 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001626 return -1;
1627 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001628
Michal Vasko2e6defd2016-10-07 15:48:15 +02001629 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1630 if (!client_opts.ch_bind_ti) {
1631 ERRMEM;
1632 close(sock);
1633 return -1;
1634 }
1635 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1636
Michal Vasko3031aae2016-01-27 16:07:18 +01001637 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001638 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1639 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001640 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001641 return -1;
1642 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001643 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1644 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001645 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001646
1647 return 0;
1648}
1649
1650int
1651nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1652{
1653 uint32_t i;
1654 int ret = -1;
1655
1656 if (!address && !port && !ti) {
1657 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1658 close(client_opts.ch_binds[i].sock);
1659 free((char *)client_opts.ch_binds[i].address);
1660
1661 ret = 0;
1662 }
1663 free(client_opts.ch_binds);
1664 client_opts.ch_binds = NULL;
1665 client_opts.ch_bind_count = 0;
1666 } else {
1667 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1668 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1669 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001670 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001671 close(client_opts.ch_binds[i].sock);
1672 free((char *)client_opts.ch_binds[i].address);
1673
1674 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001675 if (!client_opts.ch_bind_count) {
1676 free(client_opts.ch_binds);
1677 client_opts.ch_binds = NULL;
1678 } else if (i < client_opts.ch_bind_count) {
1679 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1680 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1681 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001682
1683 ret = 0;
1684 }
1685 }
1686 }
1687
1688 return ret;
1689}
1690
1691API int
1692nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1693{
1694 int sock;
1695 char *host = NULL;
1696 uint16_t port, idx;
1697
Michal Vasko45e53ae2016-04-07 11:46:03 +02001698 if (!client_opts.ch_binds) {
1699 ERRINIT;
1700 return -1;
1701 } else if (!session) {
1702 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001703 return -1;
1704 }
1705
1706 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1707
Michal Vasko50456e82016-02-02 12:16:08 +01001708 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001709 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001710 return sock;
1711 }
1712
Radek Krejci53691be2016-02-22 13:58:37 +01001713#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001714 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001715 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001716 } else
1717#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001718#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001719 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001720 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001721 } else
1722#endif
1723 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001724 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001725 *session = NULL;
1726 }
1727
1728 free(host);
1729
1730 if (!(*session)) {
1731 return -1;
1732 }
1733
1734 return 1;
1735}
1736
Radek Krejci53691be2016-02-22 13:58:37 +01001737#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001738
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001739API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001740nc_session_get_cpblts(const struct nc_session *session)
1741{
1742 if (!session) {
1743 ERRARG("session");
1744 return NULL;
1745 }
1746
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001747 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001748}
1749
1750API const char *
1751nc_session_cpblt(const struct nc_session *session, const char *capab)
1752{
1753 int i, len;
1754
1755 if (!session) {
1756 ERRARG("session");
1757 return NULL;
1758 } else if (!capab) {
1759 ERRARG("capab");
1760 return NULL;
1761 }
1762
1763 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001764 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1765 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1766 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001767 }
1768 }
1769
1770 return NULL;
1771}
1772
Michal Vasko9cd26a82016-05-31 08:58:48 +02001773API int
1774nc_session_ntf_thread_running(const struct nc_session *session)
1775{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001776 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001777 ERRARG("session");
1778 return 0;
1779 }
1780
Michal Vasko2e6defd2016-10-07 15:48:15 +02001781 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001782}
1783
Michal Vaskob7558c52016-02-26 15:04:19 +01001784API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001785nc_client_init(void)
1786{
1787 nc_init();
1788}
1789
1790API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001791nc_client_destroy(void)
1792{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001793 nc_client_set_schema_searchpath(NULL);
1794#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1795 nc_client_ch_del_bind(NULL, 0, 0);
1796#endif
1797#ifdef NC_ENABLED_SSH
1798 nc_client_ssh_destroy_opts();
1799#endif
1800#ifdef NC_ENABLED_TLS
1801 nc_client_tls_destroy_opts();
1802#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001803 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001804}
1805
Michal Vasko086311b2016-01-08 09:53:11 +01001806API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001807nc_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 +01001808{
1809 struct lyxml_elem *xml;
1810 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1811
Michal Vasko45e53ae2016-04-07 11:46:03 +02001812 if (!session) {
1813 ERRARG("session");
1814 return NC_MSG_ERROR;
1815 } else if (!rpc) {
1816 ERRARG("rpc");
1817 return NC_MSG_ERROR;
1818 } else if (!reply) {
1819 ERRARG("reply");
1820 return NC_MSG_ERROR;
1821 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1822 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001823 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001824 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001825 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001826 return NC_MSG_ERROR;
1827 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001828 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001829 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1830 parseroptions &= LYD_OPT_STRICT;
1831 }
Michal Vasko41adf392017-01-17 10:39:04 +01001832 /* no mechanism to check external dependencies is provided */
1833 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001834 *reply = NULL;
1835
1836 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001837
Michal Vasko71ba2da2016-05-04 10:53:16 +02001838 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001839 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001840 lyxml_free(session->ctx, xml);
1841 if (!(*reply)) {
1842 return NC_MSG_ERROR;
1843 }
1844 }
1845
1846 return msgtype;
1847}
1848
1849API NC_MSG_TYPE
1850nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1851{
1852 struct lyxml_elem *xml, *ev_time;
1853 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1854
Michal Vasko45e53ae2016-04-07 11:46:03 +02001855 if (!session) {
1856 ERRARG("session");
1857 return NC_MSG_ERROR;
1858 } else if (!notif) {
1859 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001860 return NC_MSG_ERROR;
1861 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001862 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001863 return NC_MSG_ERROR;
1864 }
1865
1866 msgtype = get_msg(session, timeout, 0, &xml);
1867
1868 if (msgtype == NC_MSG_NOTIF) {
1869 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001870 if (!*notif) {
1871 ERRMEM;
1872 lyxml_free(session->ctx, xml);
1873 return NC_MSG_ERROR;
1874 }
Michal Vasko086311b2016-01-08 09:53:11 +01001875
1876 /* eventTime */
1877 LY_TREE_FOR(xml->child, ev_time) {
1878 if (!strcmp(ev_time->name, "eventTime")) {
1879 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1880 /* lyd_parse does not know this element */
1881 lyxml_free(session->ctx, ev_time);
1882 break;
1883 }
1884 }
1885 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001886 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001887 goto fail;
1888 }
1889
1890 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001891 (*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 +01001892 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001893 lyxml_free(session->ctx, xml);
1894 xml = NULL;
1895 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001896 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001897 goto fail;
1898 }
1899 }
1900
1901 return msgtype;
1902
1903fail:
1904 lydict_remove(session->ctx, (*notif)->datetime);
1905 lyd_free((*notif)->tree);
1906 free(*notif);
1907 *notif = NULL;
1908 lyxml_free(session->ctx, xml);
1909
1910 return NC_MSG_ERROR;
1911}
1912
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001913static void *
1914nc_recv_notif_thread(void *arg)
1915{
1916 struct nc_ntf_thread_arg *ntarg;
1917 struct nc_session *session;
1918 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1919 struct nc_notif *notif;
1920 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02001921 pthread_t *ntf_tid;
1922
1923 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001924
1925 ntarg = (struct nc_ntf_thread_arg *)arg;
1926 session = ntarg->session;
1927 notif_clb = ntarg->notif_clb;
1928 free(ntarg);
1929
Michal Vasko131120a2018-05-29 15:44:02 +02001930 /* remember our allocated tid, we will be freeing it */
1931 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
1932
Michal Vasko2e6defd2016-10-07 15:48:15 +02001933 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001934 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001935 if (msgtype == NC_MSG_NOTIF) {
1936 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001937 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1938 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1939 nc_notif_free(notif);
1940 break;
1941 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001942 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001943 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001944 /* quit this thread once the session is broken */
1945 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001946 }
1947
1948 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1949 }
1950
Michal Vasko0651c902016-05-19 15:55:42 +02001951 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001952 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02001953 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001954 return NULL;
1955}
1956
1957API int
1958nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1959{
1960 struct nc_ntf_thread_arg *ntarg;
1961 int ret;
1962
Michal Vasko45e53ae2016-04-07 11:46:03 +02001963 if (!session) {
1964 ERRARG("session");
1965 return -1;
1966 } else if (!notif_clb) {
1967 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001968 return -1;
1969 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1970 ERR("Session %u: invalid session to receive Notifications.", session->id);
1971 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001972 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001973 ERR("Session %u: separate notification thread is already running.", session->id);
1974 return -1;
1975 }
1976
1977 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001978 if (!ntarg) {
1979 ERRMEM;
1980 return -1;
1981 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001982 ntarg->session = session;
1983 ntarg->notif_clb = notif_clb;
1984
1985 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001986 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1987 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001988 ERRMEM;
1989 free(ntarg);
1990 return -1;
1991 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001992
Michal Vasko2e6defd2016-10-07 15:48:15 +02001993 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001994 if (ret) {
1995 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1996 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001997 free((pthread_t *)session->opts.client.ntf_tid);
1998 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001999 return -1;
2000 }
2001
2002 return 0;
2003}
2004
Michal Vasko086311b2016-01-08 09:53:11 +01002005API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002006nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002007{
2008 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002009 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002010 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002011 struct nc_rpc_getconfig *rpc_gc;
2012 struct nc_rpc_edit *rpc_e;
2013 struct nc_rpc_copy *rpc_cp;
2014 struct nc_rpc_delete *rpc_del;
2015 struct nc_rpc_lock *rpc_lock;
2016 struct nc_rpc_get *rpc_g;
2017 struct nc_rpc_kill *rpc_k;
2018 struct nc_rpc_commit *rpc_com;
2019 struct nc_rpc_cancel *rpc_can;
2020 struct nc_rpc_validate *rpc_val;
2021 struct nc_rpc_getschema *rpc_gs;
2022 struct nc_rpc_subscribe *rpc_sub;
2023 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01002024 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02002025 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002026 uint64_t cur_msgid;
2027
Michal Vasko45e53ae2016-04-07 11:46:03 +02002028 if (!session) {
2029 ERRARG("session");
2030 return NC_MSG_ERROR;
2031 } else if (!rpc) {
2032 ERRARG("rpc");
2033 return NC_MSG_ERROR;
2034 } else if (!msgid) {
2035 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002036 return NC_MSG_ERROR;
2037 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002038 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002039 return NC_MSG_ERROR;
2040 }
2041
Michal Vasko90e8e692016-07-13 12:27:57 +02002042 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002043 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002044 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01002045 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002046 return NC_MSG_ERROR;
2047 }
2048 }
2049
2050 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002051 case NC_RPC_ACT_GENERIC:
2052 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002053
2054 if (rpc_gen->has_data) {
2055 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002056 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002057 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002058 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 +01002059 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002060 if (!data) {
2061 return NC_MSG_ERROR;
2062 }
Michal Vasko086311b2016-01-08 09:53:11 +01002063 }
2064 break;
2065
2066 case NC_RPC_GETCONFIG:
2067 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2068
2069 data = lyd_new(NULL, ietfnc, "get-config");
2070 node = lyd_new(data, ietfnc, "source");
2071 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
2072 if (!node) {
2073 lyd_free(data);
2074 return NC_MSG_ERROR;
2075 }
2076 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002077 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002078 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002079 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002080 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002081 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002082 lyd_insert_attr(node, NULL, "type", "xpath");
2083 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002084 }
2085 if (!node) {
2086 lyd_free(data);
2087 return NC_MSG_ERROR;
2088 }
2089 }
2090
2091 if (rpc_gc->wd_mode) {
2092 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002093 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002094 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002095 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002096 return NC_MSG_ERROR;
2097 }
2098 }
2099 switch (rpc_gc->wd_mode) {
2100 case NC_WD_UNKNOWN:
2101 /* cannot get here */
2102 break;
2103 case NC_WD_ALL:
2104 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2105 break;
2106 case NC_WD_ALL_TAG:
2107 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2108 break;
2109 case NC_WD_TRIM:
2110 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2111 break;
2112 case NC_WD_EXPLICIT:
2113 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2114 break;
2115 }
2116 if (!node) {
2117 lyd_free(data);
2118 return NC_MSG_ERROR;
2119 }
2120 }
2121 break;
2122
2123 case NC_RPC_EDIT:
2124 rpc_e = (struct nc_rpc_edit *)rpc;
2125
2126 data = lyd_new(NULL, ietfnc, "edit-config");
2127 node = lyd_new(data, ietfnc, "target");
2128 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
2129 if (!node) {
2130 lyd_free(data);
2131 return NC_MSG_ERROR;
2132 }
2133
2134 if (rpc_e->default_op) {
2135 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
2136 if (!node) {
2137 lyd_free(data);
2138 return NC_MSG_ERROR;
2139 }
2140 }
2141
2142 if (rpc_e->test_opt) {
2143 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
2144 if (!node) {
2145 lyd_free(data);
2146 return NC_MSG_ERROR;
2147 }
2148 }
2149
2150 if (rpc_e->error_opt) {
2151 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
2152 if (!node) {
2153 lyd_free(data);
2154 return NC_MSG_ERROR;
2155 }
2156 }
2157
Michal Vasko7793bc62016-09-16 11:58:41 +02002158 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002159 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002160 } else {
2161 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
2162 }
2163 if (!node) {
2164 lyd_free(data);
2165 return NC_MSG_ERROR;
2166 }
2167 break;
2168
2169 case NC_RPC_COPY:
2170 rpc_cp = (struct nc_rpc_copy *)rpc;
2171
2172 data = lyd_new(NULL, ietfnc, "copy-config");
2173 node = lyd_new(data, ietfnc, "target");
2174 if (rpc_cp->url_trg) {
2175 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
2176 } else {
2177 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
2178 }
2179 if (!node) {
2180 lyd_free(data);
2181 return NC_MSG_ERROR;
2182 }
2183
2184 node = lyd_new(data, ietfnc, "source");
2185 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002186 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002187 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002188 } else {
2189 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
2190 }
2191 } else {
2192 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
2193 }
2194 if (!node) {
2195 lyd_free(data);
2196 return NC_MSG_ERROR;
2197 }
2198
2199 if (rpc_cp->wd_mode) {
2200 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002201 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002202 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002203 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002204 return NC_MSG_ERROR;
2205 }
2206 }
2207 switch (rpc_cp->wd_mode) {
2208 case NC_WD_UNKNOWN:
2209 /* cannot get here */
2210 break;
2211 case NC_WD_ALL:
2212 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2213 break;
2214 case NC_WD_ALL_TAG:
2215 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2216 break;
2217 case NC_WD_TRIM:
2218 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2219 break;
2220 case NC_WD_EXPLICIT:
2221 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2222 break;
2223 }
2224 if (!node) {
2225 lyd_free(data);
2226 return NC_MSG_ERROR;
2227 }
2228 }
2229 break;
2230
2231 case NC_RPC_DELETE:
2232 rpc_del = (struct nc_rpc_delete *)rpc;
2233
2234 data = lyd_new(NULL, ietfnc, "delete-config");
2235 node = lyd_new(data, ietfnc, "target");
2236 if (rpc_del->url) {
2237 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2238 } else {
2239 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2240 }
2241 if (!node) {
2242 lyd_free(data);
2243 return NC_MSG_ERROR;
2244 }
2245 break;
2246
2247 case NC_RPC_LOCK:
2248 rpc_lock = (struct nc_rpc_lock *)rpc;
2249
2250 data = lyd_new(NULL, ietfnc, "lock");
2251 node = lyd_new(data, ietfnc, "target");
2252 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2253 if (!node) {
2254 lyd_free(data);
2255 return NC_MSG_ERROR;
2256 }
2257 break;
2258
2259 case NC_RPC_UNLOCK:
2260 rpc_lock = (struct nc_rpc_lock *)rpc;
2261
2262 data = lyd_new(NULL, ietfnc, "unlock");
2263 node = lyd_new(data, ietfnc, "target");
2264 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2265 if (!node) {
2266 lyd_free(data);
2267 return NC_MSG_ERROR;
2268 }
2269 break;
2270
2271 case NC_RPC_GET:
2272 rpc_g = (struct nc_rpc_get *)rpc;
2273
2274 data = lyd_new(NULL, ietfnc, "get");
2275 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002276 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002277 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002278 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002279 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002280 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002281 lyd_insert_attr(node, NULL, "type", "xpath");
2282 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002283 }
2284 if (!node) {
2285 lyd_free(data);
2286 return NC_MSG_ERROR;
2287 }
2288 }
2289
2290 if (rpc_g->wd_mode) {
2291 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002292 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002293 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002294 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002295 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002296 return NC_MSG_ERROR;
2297 }
2298 }
2299 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002300 case NC_WD_ALL:
2301 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2302 break;
2303 case NC_WD_ALL_TAG:
2304 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2305 break;
2306 case NC_WD_TRIM:
2307 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2308 break;
2309 case NC_WD_EXPLICIT:
2310 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2311 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002312 default:
2313 /* cannot get here */
2314 node = NULL;
2315 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002316 }
2317 if (!node) {
2318 lyd_free(data);
2319 return NC_MSG_ERROR;
2320 }
2321 }
2322 break;
2323
2324 case NC_RPC_KILL:
2325 rpc_k = (struct nc_rpc_kill *)rpc;
2326
2327 data = lyd_new(NULL, ietfnc, "kill-session");
2328 sprintf(str, "%u", rpc_k->sid);
2329 lyd_new_leaf(data, ietfnc, "session-id", str);
2330 break;
2331
2332 case NC_RPC_COMMIT:
2333 rpc_com = (struct nc_rpc_commit *)rpc;
2334
2335 data = lyd_new(NULL, ietfnc, "commit");
2336 if (rpc_com->confirmed) {
2337 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2338 }
2339
2340 if (rpc_com->confirm_timeout) {
2341 sprintf(str, "%u", rpc_com->confirm_timeout);
2342 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2343 }
2344
2345 if (rpc_com->persist) {
2346 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2347 if (!node) {
2348 lyd_free(data);
2349 return NC_MSG_ERROR;
2350 }
2351 }
2352
2353 if (rpc_com->persist_id) {
2354 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2355 if (!node) {
2356 lyd_free(data);
2357 return NC_MSG_ERROR;
2358 }
2359 }
2360 break;
2361
2362 case NC_RPC_DISCARD:
2363 data = lyd_new(NULL, ietfnc, "discard-changes");
2364 break;
2365
2366 case NC_RPC_CANCEL:
2367 rpc_can = (struct nc_rpc_cancel *)rpc;
2368
2369 data = lyd_new(NULL, ietfnc, "cancel-commit");
2370 if (rpc_can->persist_id) {
2371 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2372 if (!node) {
2373 lyd_free(data);
2374 return NC_MSG_ERROR;
2375 }
2376 }
2377 break;
2378
2379 case NC_RPC_VALIDATE:
2380 rpc_val = (struct nc_rpc_validate *)rpc;
2381
2382 data = lyd_new(NULL, ietfnc, "validate");
2383 node = lyd_new(data, ietfnc, "source");
2384 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002385 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002386 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002387 } else {
2388 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2389 }
2390 } else {
2391 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2392 }
2393 if (!node) {
2394 lyd_free(data);
2395 return NC_MSG_ERROR;
2396 }
2397 break;
2398
2399 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002400 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002401 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002402 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002403 return NC_MSG_ERROR;
2404 }
2405
2406 rpc_gs = (struct nc_rpc_getschema *)rpc;
2407
2408 data = lyd_new(NULL, ietfncmon, "get-schema");
2409 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2410 if (!node) {
2411 lyd_free(data);
2412 return NC_MSG_ERROR;
2413 }
2414 if (rpc_gs->version) {
2415 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2416 if (!node) {
2417 lyd_free(data);
2418 return NC_MSG_ERROR;
2419 }
2420 }
2421 if (rpc_gs->format) {
2422 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2423 if (!node) {
2424 lyd_free(data);
2425 return NC_MSG_ERROR;
2426 }
2427 }
2428 break;
2429
2430 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002431 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002432 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002433 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002434 return NC_MSG_ERROR;
2435 }
2436
2437 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2438
2439 data = lyd_new(NULL, notifs, "create-subscription");
2440 if (rpc_sub->stream) {
2441 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2442 if (!node) {
2443 lyd_free(data);
2444 return NC_MSG_ERROR;
2445 }
2446 }
2447
2448 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002449 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002450 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002451 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002452 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002453 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002454 lyd_insert_attr(node, NULL, "type", "xpath");
2455 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002456 }
2457 if (!node) {
2458 lyd_free(data);
2459 return NC_MSG_ERROR;
2460 }
2461 }
2462
2463 if (rpc_sub->start) {
2464 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2465 if (!node) {
2466 lyd_free(data);
2467 return NC_MSG_ERROR;
2468 }
2469 }
2470
2471 if (rpc_sub->stop) {
2472 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2473 if (!node) {
2474 lyd_free(data);
2475 return NC_MSG_ERROR;
2476 }
2477 }
2478 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002479 default:
2480 ERRINT;
2481 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002482 }
2483
Michal Vasko131120a2018-05-29 15:44:02 +02002484 if (!data) {
2485 /* error was already printed */
2486 return NC_MSG_ERROR;
2487 }
2488
Michal Vasko41adf392017-01-17 10:39:04 +01002489 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2490 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002491 if (dofree) {
2492 lyd_free(data);
2493 }
Michal Vasko086311b2016-01-08 09:53:11 +01002494 return NC_MSG_ERROR;
2495 }
2496
Michal Vasko131120a2018-05-29 15:44:02 +02002497 /* send RPC, store its message ID */
2498 r = nc_send_msg_io(session, timeout, data);
2499 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002500
Radek Krejcib4b19062018-02-07 16:33:06 +01002501 if (dofree) {
2502 lyd_free(data);
2503 }
Michal Vasko086311b2016-01-08 09:53:11 +01002504
Michal Vasko131120a2018-05-29 15:44:02 +02002505 if (r == NC_MSG_RPC) {
2506 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002507 }
Michal Vasko131120a2018-05-29 15:44:02 +02002508 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002509}
Michal Vaskode2946c2017-01-12 12:19:26 +01002510
2511API void
2512nc_client_session_set_not_strict(struct nc_session *session)
2513{
2514 if (session->side != NC_CLIENT) {
2515 ERRARG("session");
2516 return;
2517 }
2518
2519 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2520}