blob: b2477e88f70fbcc1a895d19a57b33ea820175642 [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);
Radek Krejci3f499a62018-08-17 10:16:57 +0200312 if (length < 0) {
313 ERR("Session %u: unable to get size of schema file \"%s\".",
314 clb_data->session->id, localfile);
315 free(localfile);
316 fclose(f);
317 return NULL;
318 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200319 fseek(f, 0, SEEK_SET);
320
321 model_data = malloc(length + 1);
322 if (!model_data) {
323 ERRMEM;
324 } else if ((l = fread(model_data, 1, length, f)) != length) {
325 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
326 clb_data->session->id, localfile, l, length);
327 free(model_data);
328 model_data = NULL;
329 } else {
330 /* terminating NULL byte */
331 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100332 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200333 fclose(f);
334 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100335 }
336
Radek Krejci65ef6d52018-08-16 16:35:02 +0200337 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100338}
339
340static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200341retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
342 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100343{
Michal Vasko086311b2016-01-08 09:53:11 +0100344 struct nc_rpc *rpc;
345 struct nc_reply *reply;
346 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200347 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200348 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100349 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100350 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200351 char *localfile = NULL;
352 FILE *f;
353 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100354
Radek Krejci65ef6d52018-08-16 16:35:02 +0200355 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
356 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100357
Radek Krejci65ef6d52018-08-16 16:35:02 +0200358 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100359 usleep(1000);
360 }
361 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200362 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100363 nc_rpc_free(rpc);
364 return NULL;
365 }
366
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200367 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200368 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200369 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100370 nc_rpc_free(rpc);
371 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200372 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100373 return NULL;
374 } else if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200375 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100376 return NULL;
377 }
378
Michal Vasko998ba412016-09-16 12:00:07 +0200379 switch (reply->type) {
380 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200381 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200382 nc_reply_free(reply);
383 return NULL;
384 case NC_RPL_DATA:
385 /* fine */
386 break;
387 case NC_RPL_ERROR:
388 error_rpl = (struct nc_reply_error *)reply;
389 if (error_rpl->count) {
390 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200391 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200392 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200393 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200394 }
395 nc_reply_free(reply);
396 return NULL;
397 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200398 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100399 nc_reply_free(reply);
400 return NULL;
401 }
402
Michal Vasko086311b2016-01-08 09:53:11 +0100403 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200404 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
405 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200406 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200407 nc_reply_free(reply);
408 return NULL;
409 }
Radek Krejci539efb62016-08-24 15:05:16 +0200410 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
411 switch (get_schema_data->value_type) {
412 case LYD_ANYDATA_CONSTSTRING:
413 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200414 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200415 break;
416 case LYD_ANYDATA_DATATREE:
417 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
418 break;
419 case LYD_ANYDATA_XML:
420 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
421 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200422 case LYD_ANYDATA_JSON:
423 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200424 case LYD_ANYDATA_SXML:
425 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200426 case LYD_ANYDATA_LYB:
427 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200428 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200429 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200430 }
Michal Vasko086311b2016-01-08 09:53:11 +0100431 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100432
Radek Krejcifd5b6682017-06-13 15:52:53 +0200433 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200434 if (model_data) {
435 *format = LYS_IN_YANG;
436 if (client_opts.schema_searchpath) {
437 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
438 rev ? "@" : "", rev ? rev : "") == -1) {
439 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100440 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200441 f = fopen(localfile, "w");
442 if (!f) {
443 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
444 localfile, strerror(errno));
445 } else {
446 fputs(model_data, f);
447 fclose(f);
448 }
449 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100450 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200451 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200452 }
453
Michal Vasko086311b2016-01-08 09:53:11 +0100454 return model_data;
455}
456
Radek Krejci65ef6d52018-08-16 16:35:02 +0200457static char *
458retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
459 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data))
460{
461 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
462 unsigned int u, v, match = 1;
463 const char *name = NULL, *rev = NULL;
464 char *model_data = NULL;
465
466 /* get and check the final name and revision of the schema to be retrieved */
467 if (!mod_rev || !mod_rev[0]) {
468 /* newest revision requested - get the newest revision from the list of available modules on server */
469 match = 0;
470 for (u = 0; clb_data->schemas[u].name; ++u) {
471 if (strcmp(mod_name, clb_data->schemas[u].name)) {
472 continue;
473 }
474 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
475 mod_rev = clb_data->schemas[u].revision;
476 }
477 match = u + 1;
478 }
479 if (!match) {
480 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
481 clb_data->session->id, mod_name);
482 }
483 }
484 if (submod_name) {
485 name = submod_name;
486 if (sub_rev) {
487 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200488 } else if (match) {
489 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200490 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
491 clb_data->session->id, submod_name, mod_name);
492 return NULL;
493 }
Radek Krejci6455eb12018-08-17 09:26:29 +0200494 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
495 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
496 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200497 }
498 }
499 if (!rev) {
500 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
501 clb_data->session->id, submod_name, mod_name);
502 return NULL;
503 }
504 }
505 } else {
506 name = mod_name;
507 rev = mod_rev;
508 }
509
510 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
511
512 if (match) {
513 /* we have enough information to avoid communication with server and try to get
514 * the schema locally */
515
516 /* 1. try to get data locally */
517 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
518
519 /* 2. try to use <get-schema> */
520 if (!model_data && clb_data->has_get_schema) {
521 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
522 }
523 } else {
524 /* we are unsure which revision of the schema we should load, so first try to get
525 * the newest revision from the server via get-schema and only if the server does not
526 * implement get-schema, try to load the newest revision locally. This is imperfect
527 * solution, but there are situation when a client does not know what revision is
528 * actually implemented by the server. */
529
530 /* 1. try to use <get-schema> */
531 if (clb_data->has_get_schema) {
532 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
533 }
534
535 /* 2. try to get data locally */
536 if (!model_data) {
537 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
538 }
539 }
540
541 /* 3. try to use user callback */
542 if (!model_data && clb_data->user_clb) {
543 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
544 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
545 }
546
547 *free_module_data = free;
548 return model_data;
549}
550
Michal Vaskoceae0152018-02-14 16:03:59 +0100551static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200552nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200553 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 +0100554{
555 int ret = 0;
556 struct ly_err_item *eitem;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200557 char *module_data = NULL;
558 LYS_INFORMAT format;
559 void (*free_module_data)(void*) = NULL;
560 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100561
Radek Krejci65ef6d52018-08-16 16:35:02 +0200562 *mod = NULL;
563 if (revision) {
564 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
565 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100566 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200567 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100568 /* make the present module implemented */
569 if (lys_set_implemented(*mod)) {
570 ERR("Failed to implement model \"%s\".", (*mod)->name);
571 ret = -1;
572 }
573 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200574 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100575 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200576 clb_data.has_get_schema = has_get_schema;
577 clb_data.schemas = schemas;
578 clb_data.session = session;
579 clb_data.user_clb = user_clb;
580 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100581
582 /* clear all the errors and just collect them for now */
583 ly_err_clean(session->ctx, NULL);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200584 //ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100585
Radek Krejci65ef6d52018-08-16 16:35:02 +0200586 /* get module data */
587 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100588
Radek Krejci65ef6d52018-08-16 16:35:02 +0200589 if (module_data) {
590 /* parse the schema */
591 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100592
Radek Krejci65ef6d52018-08-16 16:35:02 +0200593 *mod = lys_parse_mem(session->ctx, module_data, format);
594 if (*free_module_data) {
595 (*free_module_data)(module_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100596 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100597
Radek Krejci65ef6d52018-08-16 16:35:02 +0200598 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
599 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100600
Michal Vaskodbf7c272018-02-19 09:07:59 +0100601 /* restore logging options, then print errors on definite failure */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200602 //ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100603 if (!(*mod)) {
604 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
605 ly_err_print(eitem);
606 }
607 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200608 } else {
609 /* print only warnings */
610 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
611 if (eitem->level == LY_LLWRN) {
612 ly_err_print(eitem);
613 }
614 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100615 }
616
Michal Vaskodbf7c272018-02-19 09:07:59 +0100617 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100618 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100619 }
620
621 return ret;
622}
623
Radek Krejci65ef6d52018-08-16 16:35:02 +0200624static void
625free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200626{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200627 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200628
Radek Krejci65ef6d52018-08-16 16:35:02 +0200629 if (!list) {
630 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200631 }
632
Radek Krejci65ef6d52018-08-16 16:35:02 +0200633 for (u = 0; list[u].name; ++u) {
634 free(list[u].name);
635 free(list[u].revision);
636 for (v = 0; v < list[u].features.number; ++v) {
637 free(list[u].features.set.g[v]);
638 }
639 free(list[u].features.set.g);
640 }
641 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200642}
643
Radek Krejci65ef6d52018-08-16 16:35:02 +0200644static struct schema_info *
645build_schema_info_yl(struct nc_session *session)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200646{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200647 struct nc_rpc *rpc = NULL;
648 struct nc_reply *reply = NULL;
649 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200650 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200651 NC_MSG_TYPE msg;
652 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200653 struct ly_set *modules = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200654 struct schema_info *result;
655 unsigned int u, c;
656 struct lyd_node *iter;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200657
658 /* get yang-library data from the server */
Michal Vaskoac0356e2018-06-22 13:24:29 +0200659 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200660 if (!rpc) {
661 goto cleanup;
662 }
663
664 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
665 usleep(1000);
666 }
667 if (msg == NC_MSG_ERROR) {
Michal Vasko60f66602017-10-17 13:52:18 +0200668 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200669 session->id);
670 goto cleanup;
671 }
672
673 do {
674 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
675 } while (msg == NC_MSG_NOTIF);
676 if (msg == NC_MSG_WOULDBLOCK) {
677 ERR("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
678 goto cleanup;
679 } else if (msg == NC_MSG_ERROR) {
680 ERR("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
681 goto cleanup;
682 }
683
684 switch (reply->type) {
685 case NC_RPL_OK:
686 ERR("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
687 goto cleanup;
688 case NC_RPL_DATA:
689 /* fine */
690 break;
691 case NC_RPL_ERROR:
692 error_rpl = (struct nc_reply_error *)reply;
693 if (error_rpl->count) {
694 ERR("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
695 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
696 } else {
697 ERR("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
698 }
699 goto cleanup;
700 case NC_RPL_NOTIF:
701 ERR("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
702 goto cleanup;
703 }
704
Radek Krejci65ef6d52018-08-16 16:35:02 +0200705 yldata = ((struct nc_reply_data *)reply)->data;
706 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200707 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
708 goto cleanup;
709 }
710
Radek Krejci65ef6d52018-08-16 16:35:02 +0200711 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200712 if (!modules) {
713 /* compatibility with older versions of ietf-yang-library */
714 modules = lyd_find_path(yldata, "/ietf-yang-library:modules/module");
715 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200716
717 c = modules ? modules->number : 0;
718 result = calloc(c + 1, sizeof *result);
719 if (!result) {
Radek Krejcic9390502018-08-17 10:23:13 +0200720 ERRMEM;
721 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200722 }
723
Radek Krejci65ef6d52018-08-16 16:35:02 +0200724 for (u = 0; u < c; ++u) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200725 LY_TREE_FOR(modules->set.d[u]->child, iter) {
726 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
727 /* ignore empty nodes */
728 continue;
729 }
730 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200731 result[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200732 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200733 result[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200734 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200735 result[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200736 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200737 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 +0200738 }
739 }
740 }
741
Radek Krejcifd5b6682017-06-13 15:52:53 +0200742
743cleanup:
744 nc_rpc_free(rpc);
745 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200746 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200747
Radek Krejci65ef6d52018-08-16 16:35:02 +0200748 return result;
749}
750
751static struct schema_info *
752build_schema_info_cpblts(char **cpblts)
753{
754 unsigned int u, v;
755 struct schema_info *result;
756 char *module_cpblt, *ptr, *ptr2;
757
758 for (u = 0; cpblts[u]; ++u);
759 result = calloc(u + 1, sizeof *result);
Radek Krejcic9390502018-08-17 10:23:13 +0200760 if (!result) {
761 ERRMEM;
762 return NULL;
763 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200764
765 for (u = v = 0; cpblts[u]; ++u) {
766 module_cpblt = strstr(cpblts[u], "module=");
767 /* this capability requires a module */
768 if (!module_cpblt) {
769 continue;
770 }
771
772 /* get module's name */
773 ptr = (char *)module_cpblt + 7;
774 ptr2 = strchr(ptr, '&');
775 if (!ptr2) {
776 ptr2 = ptr + strlen(ptr);
777 }
778 result[v].name = strndup(ptr, ptr2 - ptr);
779
780 /* get module's revision */
781 ptr = strstr(module_cpblt, "revision=");
782 if (ptr) {
783 ptr += 9;
784 ptr2 = strchr(ptr, '&');
785 if (!ptr2) {
786 ptr2 = ptr + strlen(ptr);
787 }
788 result[v].revision = strndup(ptr, ptr2 - ptr);
789 }
790
791 /* all are implemented since there is no better information in capabilities list */
792 result[v].implemented = 1;
793
794 /* get module's features */
795 ptr = strstr(module_cpblt, "features=");
796 if (ptr) {
797 ptr += 9;
798 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
799 if (*ptr == ',') {
800 ly_set_add(&result[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
801 ptr2 = ptr + 1;
802 }
803 }
804 /* the last one */
805 ly_set_add(&result[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
806 }
807 ++v;
808 }
809 return result;
810}
811
812static int
813nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
814{
815 int ret = EXIT_FAILURE;
816 const struct lys_module *mod;
817 unsigned int u, v;
818
819 for (u = 0; modules[u].name; ++u) {
820 /* we can continue even if it fails */
821 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
822
823 if (!mod) {
824 if (session->status != NC_STATUS_RUNNING) {
825 /* something bad heppened, discard the session */
826 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
827 goto cleanup;
828 }
829
830 /* all loading ways failed, the schema will be ignored in the received data */
831 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
832 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
833 } else {
834 /* set features - first disable all to enable specified then */
835 lys_features_disable(mod, "*");
836 for (v = 0; v < modules[u].features.number; v++) {
837 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
838 }
839 }
Michal Vasko60f66602017-10-17 13:52:18 +0200840 }
841
Radek Krejci65ef6d52018-08-16 16:35:02 +0200842 /* done */
843 ret = EXIT_SUCCESS;
844
845cleanup:
846
Radek Krejcifd5b6682017-06-13 15:52:53 +0200847 return ret;
848}
849
Radek Krejci65ef6d52018-08-16 16:35:02 +0200850static int
851nc_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)
852{
853 unsigned int u, v;
854 const struct lys_module *ietfnc;
855
856 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
857 if (!ietfnc) {
858 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
859 if (!ietfnc) {
860 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
861 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
862 }
863 }
864 if (!ietfnc) {
865 ERR("Loading base NETCONF schema failed.");
866 return 1;
867 }
868
869 /* set supported capabilities from ietf-netconf */
870 for (u = 0; modules[u].name; ++u) {
871 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
872 continue;
873 }
874
875 lys_features_disable(ietfnc, "*");
876 for (v = 0; v < modules[u].features.number; v++) {
877 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
878 }
879 }
880
881 return 0;
882}
883
Michal Vasko086311b2016-01-08 09:53:11 +0100884int
885nc_ctx_check_and_fill(struct nc_session *session)
886{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200887 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100888 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100889 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200890 const struct lys_module *mod = NULL;
891 char *revision;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200892 struct schema_info *server_modules = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100893
Michal Vasko2e6defd2016-10-07 15:48:15 +0200894 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100895
Radek Krejci65ef6d52018-08-16 16:35:02 +0200896 /* 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 +0200897 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200898
Radek Krejci65ef6d52018-08-16 16:35:02 +0200899 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
900 * schemas only to those present on the server side */
901 ly_ctx_set_disable_searchdirs(session->ctx);
902
903 /* our callback is set later with appropriate data */
904 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
905
906 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200907 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200908 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200909 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200910 if (yanglib_support) {
911 break;
912 }
913 } 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 +0200914 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200915 if (get_schema_support) {
916 break;
917 }
Michal Vasko086311b2016-01-08 09:53:11 +0100918 }
919 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200920
921 /* get information about server's schemas from capabilities list until we will have yang-library */
922 if (!(server_modules = build_schema_info_cpblts(session->opts.client.cpblts))) {
923 goto cleanup;
924 }
925
Michal Vasko086311b2016-01-08 09:53:11 +0100926 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200927 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200928 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
929 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200930 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100931 }
932 }
933
934 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200935 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200936 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100937 }
938
Radek Krejci65ef6d52018-08-16 16:35:02 +0200939 /* get correct version of ietf-yang-library into context */
940 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200941 /* use get schema to get server's ietf-yang-library */
942 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
943 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200944 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
945 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +0200946 yanglib_support = 0;
947 } else {
948 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200949 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
950 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 +0200951 yanglib_support = 0;
952 }
953 free(revision);
954 }
955 }
956
Radek Krejci65ef6d52018-08-16 16:35:02 +0200957 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200958 if (yanglib_support) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200959 if (server_modules) {
960 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
961 free_schema_info(server_modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200962 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200963 server_modules = build_schema_info_yl(session);
964 if (!server_modules) {
965 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
966 }
967 }
968 if (!server_modules) { /* also in case of error of getting yang-library data */
969 server_modules = build_schema_info_cpblts(session->opts.client.cpblts);
970 }
971 if (!server_modules) {
972 goto cleanup;
973 }
Michal Vaskoef578332016-01-25 13:20:09 +0100974
Radek Krejci65ef6d52018-08-16 16:35:02 +0200975 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
976 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100977 }
978
Radek Krejcifd5b6682017-06-13 15:52:53 +0200979 /* succsess */
980 ret = 0;
981
Michal Vaskoeee99412016-11-21 10:19:43 +0100982 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200983 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 +0100984 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200985
986cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200987 free_schema_info(server_modules);
988
Radek Krejcifd5b6682017-06-13 15:52:53 +0200989 /* set user callback back */
990 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200991 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200992
Michal Vaskoef578332016-01-25 13:20:09 +0100993 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100994}
995
996API struct nc_session *
997nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
998{
Michal Vaskod083db62016-01-19 10:31:29 +0100999 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001000
Michal Vasko45e53ae2016-04-07 11:46:03 +02001001 if (fdin < 0) {
1002 ERRARG("fdin");
1003 return NULL;
1004 } else if (fdout < 0) {
1005 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001006 return NULL;
1007 }
1008
1009 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001010 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001011 if (!session) {
1012 ERRMEM;
1013 return NULL;
1014 }
1015 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001016
1017 /* transport specific data */
1018 session->ti_type = NC_TI_FD;
1019 session->ti.fd.in = fdin;
1020 session->ti.fd.out = fdout;
1021
1022 /* assign context (dicionary needed for handshake) */
1023 if (!ctx) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001024 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001025 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001026 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001027 /* that's just it */
1028 goto fail;
1029 }
Michal Vasko086311b2016-01-08 09:53:11 +01001030 } else {
1031 session->flags |= NC_SESSION_SHAREDCTX;
1032 }
1033 session->ctx = ctx;
1034
1035 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001036 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001037 goto fail;
1038 }
1039 session->status = NC_STATUS_RUNNING;
1040
Michal Vaskoef578332016-01-25 13:20:09 +01001041 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001042 goto fail;
1043 }
1044
1045 return session;
1046
1047fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001048 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001049 return NULL;
1050}
1051
Frank Rimpler9f838b02018-07-25 06:44:03 +00001052/*
1053 Helper for a non-blocking connect (which is required because of the locking
1054 concept for e.g. call home settings). For more details see nc_sock_connect().
1055 */
1056static int
1057_non_blocking_connect(int timeout, int* sock_pending, struct addrinfo *res)
Michal Vasko086311b2016-01-08 09:53:11 +01001058{
Frank Rimplerc7a2acb2018-08-06 07:31:08 +00001059 int flags, ret=0;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001060 int sock = -1;
1061 fd_set wset;
1062 struct timeval ts;
1063 int error = 0;
1064 socklen_t len = sizeof(int);
Michal Vasko086311b2016-01-08 09:53:11 +01001065
Frank Rimpler9f838b02018-07-25 06:44:03 +00001066 if (sock_pending && *sock_pending != -1) {
1067 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1068 sock = *sock_pending;
1069 } else {
1070 assert(res);
1071 VRB("Trying to connect via %s.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1072 /* Connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001073 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1074 if (sock == -1) {
Frank Rimpler9f838b02018-07-25 06:44:03 +00001075 ERR("socket couldn't be created.", strerror(errno));
1076 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001077 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001078 /* make the socket non-blocking */
1079 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
1080 ERR("Fcntl failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001081 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001082 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001083 /* non-blocking connect! */
1084 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1085 if (errno != EINPROGRESS) {
1086 /* network connection failed, try another resource */
1087 ERR("connect failed: (%s).", strerror(errno));
1088 goto cleanup;
1089 }
1090 }
1091 /* check the usability of the socket */
1092 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1093 ERR("getsockopt failed: (%s).", strerror(errno));
1094 goto cleanup;
1095 }
1096 if (error == ECONNREFUSED) {
1097 /* network connection failed, try another resource */
1098 VRB("getsockopt error: (%s).", strerror(error));
1099 goto cleanup;
1100 }
1101 }
1102 ts.tv_sec = timeout;
1103 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001104
Frank Rimpler9f838b02018-07-25 06:44:03 +00001105 FD_ZERO(&wset);
1106 FD_SET(sock, &wset);
1107
1108 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
1109 ERR("select failed: (%s).", strerror(errno));
1110 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001111 }
1112
Frank Rimpler9f838b02018-07-25 06:44:03 +00001113 if (ret == 0) { //we had a timeout
1114 VRB("timed out after %ds (%s).", timeout, strerror(errno));
1115 if (sock_pending) {
1116 /* no sock-close, we'll try it again */
1117 *sock_pending = sock;
1118 } else {
1119 close(sock);
1120 }
1121 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001122 }
Michal Vasko086311b2016-01-08 09:53:11 +01001123 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001124
Frank Rimpler9f838b02018-07-25 06:44:03 +00001125cleanup:
1126 if (sock_pending) {
1127 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001128 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001129 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001130 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001131}
1132
Frank Rimpler9f838b02018-07-25 06:44:03 +00001133/* A given timeout value limits the time how long the function blocks. If it has to block
1134 only for some seconds, a socket connection might not yet have been fully established.
1135 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1136 value will be -1. In such a case a subsequent invokation is required, by providing the
1137 stored sock_pending, again.
1138 In general, if this function returns -1, when a timeout has been given, this function
1139 has to be invoked, until it returns a valid socket.
1140 */
1141int
1142nc_sock_connect(const char* host, uint16_t port, int timeout, int* sock_pending)
1143{
1144 int i;
1145 int sock = sock_pending?*sock_pending:-1;
1146 struct addrinfo hints, *res_list, *res;
1147 char port_s[6]; /* length of string representation of short int */
1148
1149 VRB("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
1150
1151 /* no pending socket */
1152 if (sock == -1) {
1153 /* Connect to a server */
1154 snprintf(port_s, 6, "%u", port);
1155 memset(&hints, 0, sizeof hints);
1156 hints.ai_family = AF_UNSPEC;
1157 hints.ai_socktype = SOCK_STREAM;
1158 hints.ai_protocol = IPPROTO_TCP;
1159 i = getaddrinfo(host, port_s, &hints, &res_list);
1160 if (i != 0) {
1161 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1162 return -1;
1163 }
1164
1165 for (res = res_list; res != NULL; res = res->ai_next) {
1166 sock = _non_blocking_connect(timeout, sock_pending, res);
1167 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1168 /* try the next resource */
1169 continue;
1170 }
1171 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1172 break;
1173 }
1174 freeaddrinfo(res_list);
1175
1176 } else {
1177 /* try to get a connection with the pending socket */
1178 assert(sock_pending);
1179 sock = _non_blocking_connect(timeout, sock_pending, NULL);
1180 }
1181
1182 return sock;
1183}
1184
Michal Vasko086311b2016-01-08 09:53:11 +01001185static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001186get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001187{
Michal Vasko086311b2016-01-08 09:53:11 +01001188 char *ptr;
1189 const char *str_msgid;
1190 uint64_t cur_msgid;
1191 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001192 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001193 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1194
Michal Vasko086311b2016-01-08 09:53:11 +01001195 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001196 if (!msgid && session->opts.client.notifs) {
1197 cont = session->opts.client.notifs;
1198 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001199
Michal Vasko71ba2da2016-05-04 10:53:16 +02001200 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001201 free(cont);
1202
Michal Vasko71ba2da2016-05-04 10:53:16 +02001203 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001204 }
1205
1206 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001207 if (msgid && session->opts.client.replies) {
1208 cont = session->opts.client.replies;
1209 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001210
Michal Vasko71ba2da2016-05-04 10:53:16 +02001211 xml = cont->msg;
1212 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001213
Michal Vasko71ba2da2016-05-04 10:53:16 +02001214 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001215 }
1216
Michal Vasko71ba2da2016-05-04 10:53:16 +02001217 if (!msgtype) {
1218 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001219 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001220 }
Michal Vasko086311b2016-01-08 09:53:11 +01001221
1222 /* we read rpc-reply, want a notif */
1223 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001224 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001225 while (*cont_ptr) {
1226 cont_ptr = &((*cont_ptr)->next);
1227 }
1228 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001229 if (!*cont_ptr) {
1230 ERRMEM;
1231 lyxml_free(session->ctx, xml);
1232 return NC_MSG_ERROR;
1233 }
Michal Vasko086311b2016-01-08 09:53:11 +01001234 (*cont_ptr)->msg = xml;
1235 (*cont_ptr)->next = NULL;
1236 }
1237
1238 /* we read notif, want a rpc-reply */
1239 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001240 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001241 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001242 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001243 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001244 }
Michal Vasko086311b2016-01-08 09:53:11 +01001245
Michal Vasko2e6defd2016-10-07 15:48:15 +02001246 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001247 while (*cont_ptr) {
1248 cont_ptr = &((*cont_ptr)->next);
1249 }
1250 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001251 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001252 ERRMEM;
1253 lyxml_free(session->ctx, xml);
1254 return NC_MSG_ERROR;
1255 }
Michal Vasko086311b2016-01-08 09:53:11 +01001256 (*cont_ptr)->msg = xml;
1257 (*cont_ptr)->next = NULL;
1258 }
1259
Michal Vasko086311b2016-01-08 09:53:11 +01001260 switch (msgtype) {
1261 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001262 if (!msgid) {
1263 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001264 }
Michal Vasko086311b2016-01-08 09:53:11 +01001265 break;
1266
1267 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001268 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001269 /* check message-id */
1270 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1271 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001272 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001273 } else {
1274 cur_msgid = strtoul(str_msgid, &ptr, 10);
1275 if (cur_msgid != msgid) {
1276 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1277 session->id, str_msgid);
1278 msgtype = NC_MSG_REPLY_ERR_MSGID;
1279 }
1280 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001281 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001282 }
Michal Vasko086311b2016-01-08 09:53:11 +01001283 break;
1284
1285 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001286 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001287 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001288 msgtype = NC_MSG_ERROR;
1289 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001290
1291 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001292 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001293 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001294 msgtype = NC_MSG_ERROR;
1295 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001296
1297 default:
1298 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1299 * NC_MSG_NONE is not returned by nc_read_msg()
1300 */
1301 break;
1302 }
1303
1304 return msgtype;
1305}
1306
1307/* cannot strictly fail, but does not need to fill any error parameter at all */
1308static void
1309parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1310{
1311 struct lyxml_elem *iter, *next, *info;
1312
1313 LY_TREE_FOR(xml->child, iter) {
1314 if (!iter->ns) {
1315 if (iter->content) {
1316 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1317 } else {
1318 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1319 }
1320 continue;
1321 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1322 if (iter->content) {
1323 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1324 iter->name, iter->content, iter->ns->value);
1325 } else {
1326 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1327 }
1328 continue;
1329 }
1330
1331 if (!strcmp(iter->name, "error-type")) {
1332 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1333 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1334 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1335 } else if (err->type) {
1336 WRN("<rpc-error> <error-type> duplicated.");
1337 } else {
1338 err->type = lydict_insert(ctx, iter->content, 0);
1339 }
1340 } else if (!strcmp(iter->name, "error-tag")) {
1341 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1342 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1343 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1344 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1345 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1346 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1347 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1348 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1349 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1350 && strcmp(iter->content, "malformed-message"))) {
1351 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1352 } else if (err->tag) {
1353 WRN("<rpc-error> <error-tag> duplicated.");
1354 } else {
1355 err->tag = lydict_insert(ctx, iter->content, 0);
1356 }
1357 } else if (!strcmp(iter->name, "error-severity")) {
1358 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1359 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1360 } else if (err->severity) {
1361 WRN("<rpc-error> <error-severity> duplicated.");
1362 } else {
1363 err->severity = lydict_insert(ctx, iter->content, 0);
1364 }
1365 } else if (!strcmp(iter->name, "error-app-tag")) {
1366 if (err->apptag) {
1367 WRN("<rpc-error> <error-app-tag> duplicated.");
1368 } else {
1369 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1370 }
1371 } else if (!strcmp(iter->name, "error-path")) {
1372 if (err->path) {
1373 WRN("<rpc-error> <error-path> duplicated.");
1374 } else {
1375 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1376 }
1377 } else if (!strcmp(iter->name, "error-message")) {
1378 if (err->message) {
1379 WRN("<rpc-error> <error-message> duplicated.");
1380 } else {
1381 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001382 if (err->message_lang) {
1383 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1384 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001385 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1386 }
1387 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1388 }
1389 } else if (!strcmp(iter->name, "error-info")) {
1390 LY_TREE_FOR_SAFE(iter->child, next, info) {
1391 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1392 if (!strcmp(info->name, "session-id")) {
1393 if (err->sid) {
1394 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1395 } else {
1396 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1397 }
Michal Vasko630485f2018-01-03 14:28:32 +01001398 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001399 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001400 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1401 if (!err->attr) {
1402 ERRMEM;
1403 return;
1404 }
Michal Vasko086311b2016-01-08 09:53:11 +01001405 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1406 } else if (!strcmp(info->name, "bad-element")) {
1407 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001408 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1409 if (!err->elem) {
1410 ERRMEM;
1411 return;
1412 }
Michal Vasko086311b2016-01-08 09:53:11 +01001413 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1414 } else if (!strcmp(info->name, "bad-namespace")) {
1415 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001416 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1417 if (!err->ns) {
1418 ERRMEM;
1419 return;
1420 }
Michal Vasko086311b2016-01-08 09:53:11 +01001421 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1422 } else {
1423 if (info->content) {
1424 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1425 info->name, info->content);
1426 } else {
1427 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1428 }
1429 }
1430 } else {
1431 lyxml_unlink(ctx, info);
1432 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001433 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1434 if (!err->other) {
1435 ERRMEM;
1436 return;
1437 }
Michal Vasko086311b2016-01-08 09:53:11 +01001438 err->other[err->other_count - 1] = info;
1439 }
1440 }
1441 } else {
1442 if (iter->content) {
1443 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1444 } else {
1445 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1446 }
1447 }
1448 }
1449}
1450
1451static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001452parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001453{
1454 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001455 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001456 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001457 struct nc_reply_data *data_rpl;
1458 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001459 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001460 int i;
1461
1462 if (!xml->child) {
1463 ERR("An empty <rpc-reply>.");
1464 return NULL;
1465 }
1466
1467 /* rpc-error */
1468 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1469 /* count and check elements */
1470 i = 0;
1471 LY_TREE_FOR(xml->child, iter) {
1472 if (strcmp(iter->name, "rpc-error")) {
1473 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1474 return NULL;
1475 } else if (!iter->ns) {
1476 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1477 return NULL;
1478 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1479 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1480 return NULL;
1481 }
1482 ++i;
1483 }
1484
1485 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001486 if (!error_rpl) {
1487 ERRMEM;
1488 return NULL;
1489 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001490 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001491 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001492 if (!error_rpl->err) {
1493 ERRMEM;
1494 free(error_rpl);
1495 return NULL;
1496 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001497 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001498 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001499 reply = (struct nc_reply *)error_rpl;
1500
1501 i = 0;
1502 LY_TREE_FOR(xml->child, iter) {
1503 parse_rpc_error(ctx, iter, error_rpl->err + i);
1504 ++i;
1505 }
1506
1507 /* ok */
1508 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1509 if (xml->child->next) {
1510 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1511 return NULL;
1512 }
1513 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001514 if (!reply) {
1515 ERRMEM;
1516 return NULL;
1517 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001518 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001519
1520 /* some RPC output */
1521 } else {
1522 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001523 case NC_RPC_ACT_GENERIC:
1524 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001525
1526 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001527 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1528 if (!rpc_act) {
1529 ERR("Failed to duplicate a generic RPC/action.");
1530 return NULL;
1531 }
Michal Vasko086311b2016-01-08 09:53:11 +01001532 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001533 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 +02001534 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001535 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001536 return NULL;
1537 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001538 }
Michal Vasko086311b2016-01-08 09:53:11 +01001539 break;
1540
1541 case NC_RPC_GETCONFIG:
1542 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001543 if (!xml->child->child) {
1544 /* we did not receive any data */
1545 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001546 if (!data_rpl) {
1547 ERRMEM;
1548 return NULL;
1549 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001550 data_rpl->type = NC_RPL_DATA;
1551 data_rpl->data = NULL;
1552 return (struct nc_reply *)data_rpl;
1553 }
1554
Michal Vasko086311b2016-01-08 09:53:11 +01001555 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001556 data = lyd_parse_xml(ctx, &xml->child->child,
1557 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001558 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001559 if (!data) {
1560 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1561 return NULL;
1562 }
1563 break;
1564
1565 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001566 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001567 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001568 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001569 return NULL;
1570 }
1571 break;
1572
1573 case NC_RPC_EDIT:
1574 case NC_RPC_COPY:
1575 case NC_RPC_DELETE:
1576 case NC_RPC_LOCK:
1577 case NC_RPC_UNLOCK:
1578 case NC_RPC_KILL:
1579 case NC_RPC_COMMIT:
1580 case NC_RPC_DISCARD:
1581 case NC_RPC_CANCEL:
1582 case NC_RPC_VALIDATE:
1583 case NC_RPC_SUBSCRIBE:
1584 /* there is no output defined */
1585 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1586 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001587 default:
1588 ERRINT;
1589 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001590 }
1591
1592 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001593 if (!data_rpl) {
1594 ERRMEM;
1595 return NULL;
1596 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001597 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001598 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001599 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001600 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001601 } else {
1602 /* <get>, <get-config> */
1603 data_rpl->data = data;
1604 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001605 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001606 if (!data_rpl->data) {
1607 ERR("Failed to parse <rpc-reply>.");
1608 free(data_rpl);
1609 return NULL;
1610 }
1611 reply = (struct nc_reply *)data_rpl;
1612 }
1613
1614 return reply;
1615}
1616
Radek Krejci53691be2016-02-22 13:58:37 +01001617#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001618
Michal Vasko3031aae2016-01-27 16:07:18 +01001619int
1620nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1621{
1622 int sock;
1623
Michal Vasko45e53ae2016-04-07 11:46:03 +02001624 if (!address) {
1625 ERRARG("address");
1626 return -1;
1627 } else if (!port) {
1628 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001629 return -1;
1630 }
1631
1632 sock = nc_sock_listen(address, port);
1633 if (sock == -1) {
1634 return -1;
1635 }
1636
1637 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001638 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1639 if (!client_opts.ch_binds) {
1640 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001641 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001642 return -1;
1643 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001644
Michal Vasko2e6defd2016-10-07 15:48:15 +02001645 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1646 if (!client_opts.ch_bind_ti) {
1647 ERRMEM;
1648 close(sock);
1649 return -1;
1650 }
1651 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1652
Michal Vasko3031aae2016-01-27 16:07:18 +01001653 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001654 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1655 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001656 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001657 return -1;
1658 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001659 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1660 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001661 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001662
1663 return 0;
1664}
1665
1666int
1667nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1668{
1669 uint32_t i;
1670 int ret = -1;
1671
1672 if (!address && !port && !ti) {
1673 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1674 close(client_opts.ch_binds[i].sock);
1675 free((char *)client_opts.ch_binds[i].address);
1676
1677 ret = 0;
1678 }
1679 free(client_opts.ch_binds);
1680 client_opts.ch_binds = NULL;
1681 client_opts.ch_bind_count = 0;
1682 } else {
1683 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1684 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1685 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001686 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001687 close(client_opts.ch_binds[i].sock);
1688 free((char *)client_opts.ch_binds[i].address);
1689
1690 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001691 if (!client_opts.ch_bind_count) {
1692 free(client_opts.ch_binds);
1693 client_opts.ch_binds = NULL;
1694 } else if (i < client_opts.ch_bind_count) {
1695 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1696 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1697 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001698
1699 ret = 0;
1700 }
1701 }
1702 }
1703
1704 return ret;
1705}
1706
1707API int
1708nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1709{
1710 int sock;
1711 char *host = NULL;
1712 uint16_t port, idx;
1713
Michal Vasko45e53ae2016-04-07 11:46:03 +02001714 if (!client_opts.ch_binds) {
1715 ERRINIT;
1716 return -1;
1717 } else if (!session) {
1718 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001719 return -1;
1720 }
1721
1722 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1723
Michal Vasko50456e82016-02-02 12:16:08 +01001724 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001725 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001726 return sock;
1727 }
1728
Radek Krejci53691be2016-02-22 13:58:37 +01001729#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001730 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001731 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001732 } else
1733#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001734#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001735 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001736 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001737 } else
1738#endif
1739 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001740 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001741 *session = NULL;
1742 }
1743
1744 free(host);
1745
1746 if (!(*session)) {
1747 return -1;
1748 }
1749
1750 return 1;
1751}
1752
Radek Krejci53691be2016-02-22 13:58:37 +01001753#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001754
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001755API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001756nc_session_get_cpblts(const struct nc_session *session)
1757{
1758 if (!session) {
1759 ERRARG("session");
1760 return NULL;
1761 }
1762
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001763 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001764}
1765
1766API const char *
1767nc_session_cpblt(const struct nc_session *session, const char *capab)
1768{
1769 int i, len;
1770
1771 if (!session) {
1772 ERRARG("session");
1773 return NULL;
1774 } else if (!capab) {
1775 ERRARG("capab");
1776 return NULL;
1777 }
1778
1779 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001780 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1781 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1782 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001783 }
1784 }
1785
1786 return NULL;
1787}
1788
Michal Vasko9cd26a82016-05-31 08:58:48 +02001789API int
1790nc_session_ntf_thread_running(const struct nc_session *session)
1791{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001792 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001793 ERRARG("session");
1794 return 0;
1795 }
1796
Michal Vasko2e6defd2016-10-07 15:48:15 +02001797 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001798}
1799
Michal Vaskob7558c52016-02-26 15:04:19 +01001800API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001801nc_client_init(void)
1802{
1803 nc_init();
1804}
1805
1806API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001807nc_client_destroy(void)
1808{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001809 nc_client_set_schema_searchpath(NULL);
1810#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1811 nc_client_ch_del_bind(NULL, 0, 0);
1812#endif
1813#ifdef NC_ENABLED_SSH
1814 nc_client_ssh_destroy_opts();
1815#endif
1816#ifdef NC_ENABLED_TLS
1817 nc_client_tls_destroy_opts();
1818#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001819 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001820}
1821
Michal Vasko086311b2016-01-08 09:53:11 +01001822API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001823nc_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 +01001824{
1825 struct lyxml_elem *xml;
1826 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1827
Michal Vasko45e53ae2016-04-07 11:46:03 +02001828 if (!session) {
1829 ERRARG("session");
1830 return NC_MSG_ERROR;
1831 } else if (!rpc) {
1832 ERRARG("rpc");
1833 return NC_MSG_ERROR;
1834 } else if (!reply) {
1835 ERRARG("reply");
1836 return NC_MSG_ERROR;
1837 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1838 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001839 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001840 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001841 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001842 return NC_MSG_ERROR;
1843 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001844 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001845 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1846 parseroptions &= LYD_OPT_STRICT;
1847 }
Michal Vasko41adf392017-01-17 10:39:04 +01001848 /* no mechanism to check external dependencies is provided */
1849 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001850 *reply = NULL;
1851
1852 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001853
Michal Vasko71ba2da2016-05-04 10:53:16 +02001854 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001855 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001856 lyxml_free(session->ctx, xml);
1857 if (!(*reply)) {
1858 return NC_MSG_ERROR;
1859 }
1860 }
1861
1862 return msgtype;
1863}
1864
1865API NC_MSG_TYPE
1866nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1867{
1868 struct lyxml_elem *xml, *ev_time;
1869 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1870
Michal Vasko45e53ae2016-04-07 11:46:03 +02001871 if (!session) {
1872 ERRARG("session");
1873 return NC_MSG_ERROR;
1874 } else if (!notif) {
1875 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001876 return NC_MSG_ERROR;
1877 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001878 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001879 return NC_MSG_ERROR;
1880 }
1881
1882 msgtype = get_msg(session, timeout, 0, &xml);
1883
1884 if (msgtype == NC_MSG_NOTIF) {
1885 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001886 if (!*notif) {
1887 ERRMEM;
1888 lyxml_free(session->ctx, xml);
1889 return NC_MSG_ERROR;
1890 }
Michal Vasko086311b2016-01-08 09:53:11 +01001891
1892 /* eventTime */
1893 LY_TREE_FOR(xml->child, ev_time) {
1894 if (!strcmp(ev_time->name, "eventTime")) {
1895 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1896 /* lyd_parse does not know this element */
1897 lyxml_free(session->ctx, ev_time);
1898 break;
1899 }
1900 }
1901 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001902 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001903 goto fail;
1904 }
1905
1906 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001907 (*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 +01001908 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001909 lyxml_free(session->ctx, xml);
1910 xml = NULL;
1911 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001912 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001913 goto fail;
1914 }
1915 }
1916
1917 return msgtype;
1918
1919fail:
1920 lydict_remove(session->ctx, (*notif)->datetime);
1921 lyd_free((*notif)->tree);
1922 free(*notif);
1923 *notif = NULL;
1924 lyxml_free(session->ctx, xml);
1925
1926 return NC_MSG_ERROR;
1927}
1928
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001929static void *
1930nc_recv_notif_thread(void *arg)
1931{
1932 struct nc_ntf_thread_arg *ntarg;
1933 struct nc_session *session;
1934 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1935 struct nc_notif *notif;
1936 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02001937 pthread_t *ntf_tid;
1938
1939 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001940
1941 ntarg = (struct nc_ntf_thread_arg *)arg;
1942 session = ntarg->session;
1943 notif_clb = ntarg->notif_clb;
1944 free(ntarg);
1945
Michal Vasko131120a2018-05-29 15:44:02 +02001946 /* remember our allocated tid, we will be freeing it */
1947 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
1948
Michal Vasko2e6defd2016-10-07 15:48:15 +02001949 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001950 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001951 if (msgtype == NC_MSG_NOTIF) {
1952 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001953 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1954 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1955 nc_notif_free(notif);
1956 break;
1957 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001958 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001959 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001960 /* quit this thread once the session is broken */
1961 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001962 }
1963
1964 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1965 }
1966
Michal Vasko0651c902016-05-19 15:55:42 +02001967 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001968 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02001969 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001970 return NULL;
1971}
1972
1973API int
1974nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1975{
1976 struct nc_ntf_thread_arg *ntarg;
1977 int ret;
1978
Michal Vasko45e53ae2016-04-07 11:46:03 +02001979 if (!session) {
1980 ERRARG("session");
1981 return -1;
1982 } else if (!notif_clb) {
1983 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001984 return -1;
1985 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1986 ERR("Session %u: invalid session to receive Notifications.", session->id);
1987 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001988 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001989 ERR("Session %u: separate notification thread is already running.", session->id);
1990 return -1;
1991 }
1992
1993 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001994 if (!ntarg) {
1995 ERRMEM;
1996 return -1;
1997 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001998 ntarg->session = session;
1999 ntarg->notif_clb = notif_clb;
2000
2001 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002002 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2003 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002004 ERRMEM;
2005 free(ntarg);
2006 return -1;
2007 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002008
Michal Vasko2e6defd2016-10-07 15:48:15 +02002009 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002010 if (ret) {
2011 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2012 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002013 free((pthread_t *)session->opts.client.ntf_tid);
2014 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002015 return -1;
2016 }
2017
2018 return 0;
2019}
2020
Michal Vasko086311b2016-01-08 09:53:11 +01002021API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002022nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002023{
2024 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002025 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002026 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002027 struct nc_rpc_getconfig *rpc_gc;
2028 struct nc_rpc_edit *rpc_e;
2029 struct nc_rpc_copy *rpc_cp;
2030 struct nc_rpc_delete *rpc_del;
2031 struct nc_rpc_lock *rpc_lock;
2032 struct nc_rpc_get *rpc_g;
2033 struct nc_rpc_kill *rpc_k;
2034 struct nc_rpc_commit *rpc_com;
2035 struct nc_rpc_cancel *rpc_can;
2036 struct nc_rpc_validate *rpc_val;
2037 struct nc_rpc_getschema *rpc_gs;
2038 struct nc_rpc_subscribe *rpc_sub;
2039 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01002040 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02002041 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002042 uint64_t cur_msgid;
2043
Michal Vasko45e53ae2016-04-07 11:46:03 +02002044 if (!session) {
2045 ERRARG("session");
2046 return NC_MSG_ERROR;
2047 } else if (!rpc) {
2048 ERRARG("rpc");
2049 return NC_MSG_ERROR;
2050 } else if (!msgid) {
2051 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002052 return NC_MSG_ERROR;
2053 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002054 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002055 return NC_MSG_ERROR;
2056 }
2057
Michal Vasko90e8e692016-07-13 12:27:57 +02002058 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002059 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002060 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01002061 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002062 return NC_MSG_ERROR;
2063 }
2064 }
2065
2066 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002067 case NC_RPC_ACT_GENERIC:
2068 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002069
2070 if (rpc_gen->has_data) {
2071 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002072 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002073 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002074 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 +01002075 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002076 if (!data) {
2077 return NC_MSG_ERROR;
2078 }
Michal Vasko086311b2016-01-08 09:53:11 +01002079 }
2080 break;
2081
2082 case NC_RPC_GETCONFIG:
2083 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2084
2085 data = lyd_new(NULL, ietfnc, "get-config");
2086 node = lyd_new(data, ietfnc, "source");
2087 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
2088 if (!node) {
2089 lyd_free(data);
2090 return NC_MSG_ERROR;
2091 }
2092 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002093 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002094 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002095 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002096 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002097 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002098 lyd_insert_attr(node, NULL, "type", "xpath");
2099 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002100 }
2101 if (!node) {
2102 lyd_free(data);
2103 return NC_MSG_ERROR;
2104 }
2105 }
2106
2107 if (rpc_gc->wd_mode) {
2108 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002109 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002110 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002111 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002112 return NC_MSG_ERROR;
2113 }
2114 }
2115 switch (rpc_gc->wd_mode) {
2116 case NC_WD_UNKNOWN:
2117 /* cannot get here */
2118 break;
2119 case NC_WD_ALL:
2120 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2121 break;
2122 case NC_WD_ALL_TAG:
2123 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2124 break;
2125 case NC_WD_TRIM:
2126 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2127 break;
2128 case NC_WD_EXPLICIT:
2129 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2130 break;
2131 }
2132 if (!node) {
2133 lyd_free(data);
2134 return NC_MSG_ERROR;
2135 }
2136 }
2137 break;
2138
2139 case NC_RPC_EDIT:
2140 rpc_e = (struct nc_rpc_edit *)rpc;
2141
2142 data = lyd_new(NULL, ietfnc, "edit-config");
2143 node = lyd_new(data, ietfnc, "target");
2144 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
2145 if (!node) {
2146 lyd_free(data);
2147 return NC_MSG_ERROR;
2148 }
2149
2150 if (rpc_e->default_op) {
2151 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
2152 if (!node) {
2153 lyd_free(data);
2154 return NC_MSG_ERROR;
2155 }
2156 }
2157
2158 if (rpc_e->test_opt) {
2159 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
2160 if (!node) {
2161 lyd_free(data);
2162 return NC_MSG_ERROR;
2163 }
2164 }
2165
2166 if (rpc_e->error_opt) {
2167 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
2168 if (!node) {
2169 lyd_free(data);
2170 return NC_MSG_ERROR;
2171 }
2172 }
2173
Michal Vasko7793bc62016-09-16 11:58:41 +02002174 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002175 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002176 } else {
2177 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
2178 }
2179 if (!node) {
2180 lyd_free(data);
2181 return NC_MSG_ERROR;
2182 }
2183 break;
2184
2185 case NC_RPC_COPY:
2186 rpc_cp = (struct nc_rpc_copy *)rpc;
2187
2188 data = lyd_new(NULL, ietfnc, "copy-config");
2189 node = lyd_new(data, ietfnc, "target");
2190 if (rpc_cp->url_trg) {
2191 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
2192 } else {
2193 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
2194 }
2195 if (!node) {
2196 lyd_free(data);
2197 return NC_MSG_ERROR;
2198 }
2199
2200 node = lyd_new(data, ietfnc, "source");
2201 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002202 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002203 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002204 } else {
2205 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
2206 }
2207 } else {
2208 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
2209 }
2210 if (!node) {
2211 lyd_free(data);
2212 return NC_MSG_ERROR;
2213 }
2214
2215 if (rpc_cp->wd_mode) {
2216 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002217 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002218 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002219 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002220 return NC_MSG_ERROR;
2221 }
2222 }
2223 switch (rpc_cp->wd_mode) {
2224 case NC_WD_UNKNOWN:
2225 /* cannot get here */
2226 break;
2227 case NC_WD_ALL:
2228 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2229 break;
2230 case NC_WD_ALL_TAG:
2231 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2232 break;
2233 case NC_WD_TRIM:
2234 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2235 break;
2236 case NC_WD_EXPLICIT:
2237 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2238 break;
2239 }
2240 if (!node) {
2241 lyd_free(data);
2242 return NC_MSG_ERROR;
2243 }
2244 }
2245 break;
2246
2247 case NC_RPC_DELETE:
2248 rpc_del = (struct nc_rpc_delete *)rpc;
2249
2250 data = lyd_new(NULL, ietfnc, "delete-config");
2251 node = lyd_new(data, ietfnc, "target");
2252 if (rpc_del->url) {
2253 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2254 } else {
2255 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2256 }
2257 if (!node) {
2258 lyd_free(data);
2259 return NC_MSG_ERROR;
2260 }
2261 break;
2262
2263 case NC_RPC_LOCK:
2264 rpc_lock = (struct nc_rpc_lock *)rpc;
2265
2266 data = lyd_new(NULL, ietfnc, "lock");
2267 node = lyd_new(data, ietfnc, "target");
2268 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2269 if (!node) {
2270 lyd_free(data);
2271 return NC_MSG_ERROR;
2272 }
2273 break;
2274
2275 case NC_RPC_UNLOCK:
2276 rpc_lock = (struct nc_rpc_lock *)rpc;
2277
2278 data = lyd_new(NULL, ietfnc, "unlock");
2279 node = lyd_new(data, ietfnc, "target");
2280 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2281 if (!node) {
2282 lyd_free(data);
2283 return NC_MSG_ERROR;
2284 }
2285 break;
2286
2287 case NC_RPC_GET:
2288 rpc_g = (struct nc_rpc_get *)rpc;
2289
2290 data = lyd_new(NULL, ietfnc, "get");
2291 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002292 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002293 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002294 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002295 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002296 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002297 lyd_insert_attr(node, NULL, "type", "xpath");
2298 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002299 }
2300 if (!node) {
2301 lyd_free(data);
2302 return NC_MSG_ERROR;
2303 }
2304 }
2305
2306 if (rpc_g->wd_mode) {
2307 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002308 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002309 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002310 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002311 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002312 return NC_MSG_ERROR;
2313 }
2314 }
2315 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002316 case NC_WD_ALL:
2317 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2318 break;
2319 case NC_WD_ALL_TAG:
2320 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2321 break;
2322 case NC_WD_TRIM:
2323 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2324 break;
2325 case NC_WD_EXPLICIT:
2326 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2327 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002328 default:
2329 /* cannot get here */
2330 node = NULL;
2331 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002332 }
2333 if (!node) {
2334 lyd_free(data);
2335 return NC_MSG_ERROR;
2336 }
2337 }
2338 break;
2339
2340 case NC_RPC_KILL:
2341 rpc_k = (struct nc_rpc_kill *)rpc;
2342
2343 data = lyd_new(NULL, ietfnc, "kill-session");
2344 sprintf(str, "%u", rpc_k->sid);
2345 lyd_new_leaf(data, ietfnc, "session-id", str);
2346 break;
2347
2348 case NC_RPC_COMMIT:
2349 rpc_com = (struct nc_rpc_commit *)rpc;
2350
2351 data = lyd_new(NULL, ietfnc, "commit");
2352 if (rpc_com->confirmed) {
2353 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2354 }
2355
2356 if (rpc_com->confirm_timeout) {
2357 sprintf(str, "%u", rpc_com->confirm_timeout);
2358 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2359 }
2360
2361 if (rpc_com->persist) {
2362 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2363 if (!node) {
2364 lyd_free(data);
2365 return NC_MSG_ERROR;
2366 }
2367 }
2368
2369 if (rpc_com->persist_id) {
2370 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2371 if (!node) {
2372 lyd_free(data);
2373 return NC_MSG_ERROR;
2374 }
2375 }
2376 break;
2377
2378 case NC_RPC_DISCARD:
2379 data = lyd_new(NULL, ietfnc, "discard-changes");
2380 break;
2381
2382 case NC_RPC_CANCEL:
2383 rpc_can = (struct nc_rpc_cancel *)rpc;
2384
2385 data = lyd_new(NULL, ietfnc, "cancel-commit");
2386 if (rpc_can->persist_id) {
2387 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2388 if (!node) {
2389 lyd_free(data);
2390 return NC_MSG_ERROR;
2391 }
2392 }
2393 break;
2394
2395 case NC_RPC_VALIDATE:
2396 rpc_val = (struct nc_rpc_validate *)rpc;
2397
2398 data = lyd_new(NULL, ietfnc, "validate");
2399 node = lyd_new(data, ietfnc, "source");
2400 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002401 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002402 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002403 } else {
2404 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2405 }
2406 } else {
2407 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2408 }
2409 if (!node) {
2410 lyd_free(data);
2411 return NC_MSG_ERROR;
2412 }
2413 break;
2414
2415 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002416 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002417 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002418 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002419 return NC_MSG_ERROR;
2420 }
2421
2422 rpc_gs = (struct nc_rpc_getschema *)rpc;
2423
2424 data = lyd_new(NULL, ietfncmon, "get-schema");
2425 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2426 if (!node) {
2427 lyd_free(data);
2428 return NC_MSG_ERROR;
2429 }
2430 if (rpc_gs->version) {
2431 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2432 if (!node) {
2433 lyd_free(data);
2434 return NC_MSG_ERROR;
2435 }
2436 }
2437 if (rpc_gs->format) {
2438 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2439 if (!node) {
2440 lyd_free(data);
2441 return NC_MSG_ERROR;
2442 }
2443 }
2444 break;
2445
2446 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002447 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002448 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002449 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002450 return NC_MSG_ERROR;
2451 }
2452
2453 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2454
2455 data = lyd_new(NULL, notifs, "create-subscription");
2456 if (rpc_sub->stream) {
2457 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2458 if (!node) {
2459 lyd_free(data);
2460 return NC_MSG_ERROR;
2461 }
2462 }
2463
2464 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002465 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002466 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002467 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002468 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002469 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002470 lyd_insert_attr(node, NULL, "type", "xpath");
2471 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002472 }
2473 if (!node) {
2474 lyd_free(data);
2475 return NC_MSG_ERROR;
2476 }
2477 }
2478
2479 if (rpc_sub->start) {
2480 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2481 if (!node) {
2482 lyd_free(data);
2483 return NC_MSG_ERROR;
2484 }
2485 }
2486
2487 if (rpc_sub->stop) {
2488 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2489 if (!node) {
2490 lyd_free(data);
2491 return NC_MSG_ERROR;
2492 }
2493 }
2494 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002495 default:
2496 ERRINT;
2497 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002498 }
2499
Michal Vasko131120a2018-05-29 15:44:02 +02002500 if (!data) {
2501 /* error was already printed */
2502 return NC_MSG_ERROR;
2503 }
2504
Michal Vasko41adf392017-01-17 10:39:04 +01002505 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2506 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002507 if (dofree) {
2508 lyd_free(data);
2509 }
Michal Vasko086311b2016-01-08 09:53:11 +01002510 return NC_MSG_ERROR;
2511 }
2512
Michal Vasko131120a2018-05-29 15:44:02 +02002513 /* send RPC, store its message ID */
2514 r = nc_send_msg_io(session, timeout, data);
2515 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002516
Radek Krejcib4b19062018-02-07 16:33:06 +01002517 if (dofree) {
2518 lyd_free(data);
2519 }
Michal Vasko086311b2016-01-08 09:53:11 +01002520
Michal Vasko131120a2018-05-29 15:44:02 +02002521 if (r == NC_MSG_RPC) {
2522 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002523 }
Michal Vasko131120a2018-05-29 15:44:02 +02002524 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002525}
Michal Vaskode2946c2017-01-12 12:19:26 +01002526
2527API void
2528nc_client_session_set_not_strict(struct nc_session *session)
2529{
2530 if (session->side != NC_CLIENT) {
2531 ERRARG("session");
2532 return;
2533 }
2534
2535 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2536}