blob: 880275da7c8d0e8e14a86d6d74e004ef38b212c3 [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 Krejci488b0702018-08-29 14:47:15 +0200433 if (model_data && !model_data[0]) {
434 /* empty data */
435 free(model_data);
436 model_data = NULL;
437 }
438
Radek Krejcifd5b6682017-06-13 15:52:53 +0200439 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200440 if (model_data) {
441 *format = LYS_IN_YANG;
442 if (client_opts.schema_searchpath) {
443 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
444 rev ? "@" : "", rev ? rev : "") == -1) {
445 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100446 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200447 f = fopen(localfile, "w");
448 if (!f) {
449 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
450 localfile, strerror(errno));
451 } else {
452 fputs(model_data, f);
453 fclose(f);
454 }
455 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100456 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200457 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200458 }
459
Michal Vasko086311b2016-01-08 09:53:11 +0100460 return model_data;
461}
462
Radek Krejci65ef6d52018-08-16 16:35:02 +0200463static char *
464retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
465 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data))
466{
467 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
468 unsigned int u, v, match = 1;
469 const char *name = NULL, *rev = NULL;
470 char *model_data = NULL;
471
472 /* get and check the final name and revision of the schema to be retrieved */
473 if (!mod_rev || !mod_rev[0]) {
474 /* newest revision requested - get the newest revision from the list of available modules on server */
475 match = 0;
476 for (u = 0; clb_data->schemas[u].name; ++u) {
477 if (strcmp(mod_name, clb_data->schemas[u].name)) {
478 continue;
479 }
480 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
481 mod_rev = clb_data->schemas[u].revision;
482 }
483 match = u + 1;
484 }
485 if (!match) {
486 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
487 clb_data->session->id, mod_name);
488 }
489 }
490 if (submod_name) {
491 name = submod_name;
492 if (sub_rev) {
493 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200494 } else if (match) {
495 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200496 WRN("Session %u: Unable to identify revision of the requested submodule \"%s\", in schema \"%s\", from the available server side information.",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200497 clb_data->session->id, submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200498 } else {
499 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
500 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
501 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
502 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200503 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200504 if (!rev) {
505 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
506 clb_data->session->id, submod_name, mod_name);
507 return NULL;
508 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200509 }
510 }
511 } else {
512 name = mod_name;
513 rev = mod_rev;
514 }
515
516 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
517
518 if (match) {
519 /* we have enough information to avoid communication with server and try to get
520 * the schema locally */
521
522 /* 1. try to get data locally */
523 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
524
525 /* 2. try to use <get-schema> */
526 if (!model_data && clb_data->has_get_schema) {
527 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
528 }
529 } else {
530 /* we are unsure which revision of the schema we should load, so first try to get
531 * the newest revision from the server via get-schema and only if the server does not
532 * implement get-schema, try to load the newest revision locally. This is imperfect
533 * solution, but there are situation when a client does not know what revision is
534 * actually implemented by the server. */
535
536 /* 1. try to use <get-schema> */
537 if (clb_data->has_get_schema) {
538 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
539 }
540
541 /* 2. try to get data locally */
542 if (!model_data) {
543 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
544 }
545 }
546
547 /* 3. try to use user callback */
548 if (!model_data && clb_data->user_clb) {
549 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
550 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
551 }
552
553 *free_module_data = free;
554 return model_data;
555}
556
Michal Vaskoceae0152018-02-14 16:03:59 +0100557static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200558nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200559 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 +0100560{
561 int ret = 0;
562 struct ly_err_item *eitem;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200563 char *module_data = NULL;
564 LYS_INFORMAT format;
565 void (*free_module_data)(void*) = NULL;
566 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100567
Radek Krejci65ef6d52018-08-16 16:35:02 +0200568 *mod = NULL;
569 if (revision) {
570 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
571 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100572 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200573 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100574 /* make the present module implemented */
575 if (lys_set_implemented(*mod)) {
576 ERR("Failed to implement model \"%s\".", (*mod)->name);
577 ret = -1;
578 }
579 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200580 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100581 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200582 clb_data.has_get_schema = has_get_schema;
583 clb_data.schemas = schemas;
584 clb_data.session = session;
585 clb_data.user_clb = user_clb;
586 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100587
588 /* clear all the errors and just collect them for now */
589 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200590 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100591
Radek Krejci65ef6d52018-08-16 16:35:02 +0200592 /* get module data */
593 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100594
Radek Krejci65ef6d52018-08-16 16:35:02 +0200595 if (module_data) {
596 /* parse the schema */
597 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100598
Radek Krejci65ef6d52018-08-16 16:35:02 +0200599 *mod = lys_parse_mem(session->ctx, module_data, format);
600 if (*free_module_data) {
601 (*free_module_data)(module_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100602 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100603
Radek Krejci65ef6d52018-08-16 16:35:02 +0200604 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
605 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100606
Michal Vaskodbf7c272018-02-19 09:07:59 +0100607 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200608 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100609 if (!(*mod)) {
610 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
611 ly_err_print(eitem);
612 }
613 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200614 } else {
615 /* print only warnings */
616 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
617 if (eitem->level == LY_LLWRN) {
618 ly_err_print(eitem);
619 }
620 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100621 }
622
Michal Vaskodbf7c272018-02-19 09:07:59 +0100623 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100624 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100625 }
626
627 return ret;
628}
629
Radek Krejci65ef6d52018-08-16 16:35:02 +0200630static void
631free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200632{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200633 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200634
Radek Krejci65ef6d52018-08-16 16:35:02 +0200635 if (!list) {
636 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200637 }
638
Radek Krejci65ef6d52018-08-16 16:35:02 +0200639 for (u = 0; list[u].name; ++u) {
640 free(list[u].name);
641 free(list[u].revision);
642 for (v = 0; v < list[u].features.number; ++v) {
643 free(list[u].features.set.g[v]);
644 }
645 free(list[u].features.set.g);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200646 if (list[u].submodules) {
647 for (v = 0; list[u].submodules[v].name; ++v) {
648 free(list[u].submodules[v].name);
649 free(list[u].submodules[v].revision);
650 }
651 free(list[u].submodules);
652 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200653 }
654 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200655}
656
Radek Krejci235d8cb2018-08-17 14:04:32 +0200657
658static int
659build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200660{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200661 struct nc_rpc *rpc = NULL;
662 struct nc_reply *reply = NULL;
663 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200664 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200665 NC_MSG_TYPE msg;
666 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200667 struct ly_set *modules = NULL;
Radek Krejci2d088832018-08-21 13:40:14 +0200668 unsigned int u, v, submodules_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200669 struct lyd_node *iter, *child;
670 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200671 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200672
673 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200674 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
675 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
676 } else {
677 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0, NC_PARAMTYPE_CONST);
678 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200679 if (!rpc) {
680 goto cleanup;
681 }
682
683 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
684 usleep(1000);
685 }
686 if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200687 WRN("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200688 session->id);
689 goto cleanup;
690 }
691
692 do {
693 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
694 } while (msg == NC_MSG_NOTIF);
695 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200696 WRN("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200697 goto cleanup;
698 } else if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200699 WRN("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200700 goto cleanup;
701 }
702
703 switch (reply->type) {
704 case NC_RPL_OK:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200705 WRN("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200706 goto cleanup;
707 case NC_RPL_DATA:
708 /* fine */
709 break;
710 case NC_RPL_ERROR:
711 error_rpl = (struct nc_reply_error *)reply;
712 if (error_rpl->count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200713 WRN("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200714 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
715 } else {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200716 WRN("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200717 }
718 goto cleanup;
719 case NC_RPL_NOTIF:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200720 WRN("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200721 goto cleanup;
722 }
723
Radek Krejci65ef6d52018-08-16 16:35:02 +0200724 yldata = ((struct nc_reply_data *)reply)->data;
725 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200726 WRN("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200727 goto cleanup;
728 }
729
Radek Krejci65ef6d52018-08-16 16:35:02 +0200730 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200731 if (!modules) {
Radek Krejci2d088832018-08-21 13:40:14 +0200732 WRN("Session %u: no module information in reply to a yang-library <get> RPC.", session->id);
733 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200734 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200735
Radek Krejci2d088832018-08-21 13:40:14 +0200736 (*result) = calloc(modules->number + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200737 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200738 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200739 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200740 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200741 }
742
Radek Krejci2d088832018-08-21 13:40:14 +0200743 for (u = 0; u < modules->number; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200744 submodules_count = 0;
745 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200746 LY_TREE_FOR(modules->set.d[u]->child, iter) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200747 if (iter->schema->module != mod) {
748 /* ignore node from other schemas (augments) */
749 continue;
750 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200751 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
752 /* ignore empty nodes */
753 continue;
754 }
755 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200756 (*result)[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200757 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200758 (*result)[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200759 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200760 (*result)[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200761 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200762 ly_set_add(&(*result)[u].features, (void *)strdup(((struct lyd_node_leaf_list *)iter)->value_str), LY_SET_OPT_USEASLIST);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200763 } else if (!strcmp(iter->schema->name, "submodule")) {
764 submodules_count++;
765 }
766 }
767
768 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200769 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
770 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200771 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200772 free_schema_info(*result);
773 *result = NULL;
774 ret = EXIT_FAILURE;
775 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200776 } else {
777 v = 0;
778 LY_TREE_FOR(modules->set.d[u]->child, iter) {
779 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
780 if (mod == iter->schema->module && !strcmp(iter->schema->name, "submodule")) {
781 LY_TREE_FOR(iter->child, child) {
782 if (mod != child->schema->module) {
783 continue;
784 } else if (!strcmp(child->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200785 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200786 } else if (!strcmp(child->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200787 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200788 }
789 }
790 }
791 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200792 }
793 }
794 }
795
Radek Krejcifd5b6682017-06-13 15:52:53 +0200796cleanup:
797 nc_rpc_free(rpc);
798 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200799 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200800
Radek Krejci235d8cb2018-08-17 14:04:32 +0200801 if (session->status != NC_STATUS_RUNNING) {
802 /* something bad heppened, discard the session */
803 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
804 ret = EXIT_FAILURE;
805 }
806
807 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200808}
809
Radek Krejci235d8cb2018-08-17 14:04:32 +0200810static int
811build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200812{
813 unsigned int u, v;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200814 char *module_cpblt, *ptr, *ptr2;
815
816 for (u = 0; cpblts[u]; ++u);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200817 (*result) = calloc(u + 1, sizeof **result);
818 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200819 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200820 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200821 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200822
823 for (u = v = 0; cpblts[u]; ++u) {
824 module_cpblt = strstr(cpblts[u], "module=");
825 /* this capability requires a module */
826 if (!module_cpblt) {
827 continue;
828 }
829
830 /* get module's name */
831 ptr = (char *)module_cpblt + 7;
832 ptr2 = strchr(ptr, '&');
833 if (!ptr2) {
834 ptr2 = ptr + strlen(ptr);
835 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200836 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200837
838 /* get module's revision */
839 ptr = strstr(module_cpblt, "revision=");
840 if (ptr) {
841 ptr += 9;
842 ptr2 = strchr(ptr, '&');
843 if (!ptr2) {
844 ptr2 = ptr + strlen(ptr);
845 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200846 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200847 }
848
849 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200850 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200851
852 /* get module's features */
853 ptr = strstr(module_cpblt, "features=");
854 if (ptr) {
855 ptr += 9;
856 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
857 if (*ptr == ',') {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200858 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200859 ptr2 = ptr + 1;
860 }
861 }
862 /* the last one */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200863 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200864 }
865 ++v;
866 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200867
868 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200869}
870
871static int
872nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
873{
874 int ret = EXIT_FAILURE;
875 const struct lys_module *mod;
876 unsigned int u, v;
877
878 for (u = 0; modules[u].name; ++u) {
879 /* we can continue even if it fails */
880 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
881
882 if (!mod) {
883 if (session->status != NC_STATUS_RUNNING) {
884 /* something bad heppened, discard the session */
885 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
886 goto cleanup;
887 }
888
889 /* all loading ways failed, the schema will be ignored in the received data */
890 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
891 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
892 } else {
893 /* set features - first disable all to enable specified then */
894 lys_features_disable(mod, "*");
895 for (v = 0; v < modules[u].features.number; v++) {
896 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
897 }
898 }
Michal Vasko60f66602017-10-17 13:52:18 +0200899 }
900
Radek Krejci65ef6d52018-08-16 16:35:02 +0200901 /* done */
902 ret = EXIT_SUCCESS;
903
904cleanup:
905
Radek Krejcifd5b6682017-06-13 15:52:53 +0200906 return ret;
907}
908
Radek Krejci65ef6d52018-08-16 16:35:02 +0200909static int
910nc_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)
911{
912 unsigned int u, v;
913 const struct lys_module *ietfnc;
914
915 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
916 if (!ietfnc) {
917 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
918 if (!ietfnc) {
919 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
920 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
921 }
922 }
923 if (!ietfnc) {
924 ERR("Loading base NETCONF schema failed.");
925 return 1;
926 }
927
928 /* set supported capabilities from ietf-netconf */
929 for (u = 0; modules[u].name; ++u) {
930 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
931 continue;
932 }
933
934 lys_features_disable(ietfnc, "*");
935 for (v = 0; v < modules[u].features.number; v++) {
936 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
937 }
938 }
939
940 return 0;
941}
942
Michal Vasko086311b2016-01-08 09:53:11 +0100943int
944nc_ctx_check_and_fill(struct nc_session *session)
945{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200946 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100947 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100948 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200949 const struct lys_module *mod = NULL;
950 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200951 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100952
Michal Vasko2e6defd2016-10-07 15:48:15 +0200953 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100954
Radek Krejci65ef6d52018-08-16 16:35:02 +0200955 /* 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 +0200956 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200957
Radek Krejci65ef6d52018-08-16 16:35:02 +0200958 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
959 * schemas only to those present on the server side */
960 ly_ctx_set_disable_searchdirs(session->ctx);
961
962 /* our callback is set later with appropriate data */
963 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
964
965 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200966 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200967 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200968 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200969 if (yanglib_support) {
970 break;
971 }
Radek Krejcicde410d2018-08-21 09:56:49 +0200972 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:1.0", 51)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200973 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200974 if (get_schema_support) {
975 break;
976 }
Michal Vasko086311b2016-01-08 09:53:11 +0100977 }
978 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200979
980 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200981 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
982 ERR("Session %u: unable to get server's schema information from the <hello>'s capabilities.", session->id);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200983 goto cleanup;
984 }
985
Michal Vasko086311b2016-01-08 09:53:11 +0100986 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200987 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200988 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
989 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200990 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100991 }
992 }
993
994 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200995 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200996 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100997 }
998
Radek Krejci65ef6d52018-08-16 16:35:02 +0200999 /* get correct version of ietf-yang-library into context */
1000 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001001 /* use get schema to get server's ietf-yang-library */
1002 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1003 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001004 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
1005 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001006 yanglib_support = 0;
1007 } else {
1008 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001009 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
1010 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 +02001011 yanglib_support = 0;
1012 }
1013 free(revision);
1014 }
1015 }
1016
Radek Krejci65ef6d52018-08-16 16:35:02 +02001017 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001018 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001019 if (build_schema_info_yl(session, &sm)) {
1020 goto cleanup;
1021 } else if (!sm) {
1022 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
1023 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001024 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
1025 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001026 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001027 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001028 }
Michal Vaskoef578332016-01-25 13:20:09 +01001029
Radek Krejci65ef6d52018-08-16 16:35:02 +02001030 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1031 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001032 }
1033
Radek Krejcifd5b6682017-06-13 15:52:53 +02001034 /* succsess */
1035 ret = 0;
1036
Michal Vaskoeee99412016-11-21 10:19:43 +01001037 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001038 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 +01001039 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001040
1041cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001042 free_schema_info(server_modules);
1043
Radek Krejcifd5b6682017-06-13 15:52:53 +02001044 /* set user callback back */
1045 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001046 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001047
Michal Vaskoef578332016-01-25 13:20:09 +01001048 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001049}
1050
1051API struct nc_session *
1052nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1053{
Michal Vaskod083db62016-01-19 10:31:29 +01001054 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001055
Michal Vasko45e53ae2016-04-07 11:46:03 +02001056 if (fdin < 0) {
1057 ERRARG("fdin");
1058 return NULL;
1059 } else if (fdout < 0) {
1060 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001061 return NULL;
1062 }
1063
1064 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001065 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001066 if (!session) {
1067 ERRMEM;
1068 return NULL;
1069 }
1070 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001071
1072 /* transport specific data */
1073 session->ti_type = NC_TI_FD;
1074 session->ti.fd.in = fdin;
1075 session->ti.fd.out = fdout;
1076
1077 /* assign context (dicionary needed for handshake) */
1078 if (!ctx) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001079 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001080 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001081 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001082 /* that's just it */
1083 goto fail;
1084 }
Michal Vasko086311b2016-01-08 09:53:11 +01001085 } else {
1086 session->flags |= NC_SESSION_SHAREDCTX;
1087 }
1088 session->ctx = ctx;
1089
1090 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001091 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001092 goto fail;
1093 }
1094 session->status = NC_STATUS_RUNNING;
1095
Michal Vaskoef578332016-01-25 13:20:09 +01001096 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001097 goto fail;
1098 }
1099
1100 return session;
1101
1102fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001103 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001104 return NULL;
1105}
1106
Frank Rimpler9f838b02018-07-25 06:44:03 +00001107/*
1108 Helper for a non-blocking connect (which is required because of the locking
1109 concept for e.g. call home settings). For more details see nc_sock_connect().
1110 */
1111static int
1112_non_blocking_connect(int timeout, int* sock_pending, struct addrinfo *res)
Michal Vasko086311b2016-01-08 09:53:11 +01001113{
Frank Rimplerc7a2acb2018-08-06 07:31:08 +00001114 int flags, ret=0;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001115 int sock = -1;
1116 fd_set wset;
1117 struct timeval ts;
1118 int error = 0;
1119 socklen_t len = sizeof(int);
Michal Vasko086311b2016-01-08 09:53:11 +01001120
Frank Rimpler9f838b02018-07-25 06:44:03 +00001121 if (sock_pending && *sock_pending != -1) {
1122 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1123 sock = *sock_pending;
1124 } else {
1125 assert(res);
1126 VRB("Trying to connect via %s.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1127 /* Connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001128 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1129 if (sock == -1) {
Frank Rimpler9f838b02018-07-25 06:44:03 +00001130 ERR("socket couldn't be created.", strerror(errno));
1131 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001132 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001133 /* make the socket non-blocking */
1134 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
1135 ERR("Fcntl failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001136 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001137 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001138 /* non-blocking connect! */
1139 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1140 if (errno != EINPROGRESS) {
1141 /* network connection failed, try another resource */
1142 ERR("connect failed: (%s).", strerror(errno));
1143 goto cleanup;
1144 }
1145 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001146 }
1147 ts.tv_sec = timeout;
1148 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001149
Frank Rimpler9f838b02018-07-25 06:44:03 +00001150 FD_ZERO(&wset);
1151 FD_SET(sock, &wset);
1152
1153 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
1154 ERR("select failed: (%s).", strerror(errno));
1155 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001156 }
1157
Frank Rimpler9f838b02018-07-25 06:44:03 +00001158 if (ret == 0) { //we had a timeout
1159 VRB("timed out after %ds (%s).", timeout, strerror(errno));
1160 if (sock_pending) {
1161 /* no sock-close, we'll try it again */
1162 *sock_pending = sock;
1163 } else {
1164 close(sock);
1165 }
1166 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001167 }
Radek Krejci782041a2018-08-20 10:09:45 +02001168
1169 /* check the usability of the socket */
1170 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1171 ERR("getsockopt failed: (%s).", strerror(errno));
1172 goto cleanup;
1173 }
1174 if (error == ECONNREFUSED) {
1175 /* network connection failed, try another resource */
1176 VRB("getsockopt error: (%s).", strerror(error));
1177 errno = error;
1178 goto cleanup;
1179 }
Michal Vasko086311b2016-01-08 09:53:11 +01001180 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001181
Frank Rimpler9f838b02018-07-25 06:44:03 +00001182cleanup:
1183 if (sock_pending) {
1184 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001185 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001186 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001187 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001188}
1189
Frank Rimpler9f838b02018-07-25 06:44:03 +00001190/* A given timeout value limits the time how long the function blocks. If it has to block
1191 only for some seconds, a socket connection might not yet have been fully established.
1192 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1193 value will be -1. In such a case a subsequent invokation is required, by providing the
1194 stored sock_pending, again.
1195 In general, if this function returns -1, when a timeout has been given, this function
1196 has to be invoked, until it returns a valid socket.
1197 */
1198int
1199nc_sock_connect(const char* host, uint16_t port, int timeout, int* sock_pending)
1200{
1201 int i;
1202 int sock = sock_pending?*sock_pending:-1;
1203 struct addrinfo hints, *res_list, *res;
1204 char port_s[6]; /* length of string representation of short int */
1205
1206 VRB("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
1207
1208 /* no pending socket */
1209 if (sock == -1) {
1210 /* Connect to a server */
1211 snprintf(port_s, 6, "%u", port);
1212 memset(&hints, 0, sizeof hints);
1213 hints.ai_family = AF_UNSPEC;
1214 hints.ai_socktype = SOCK_STREAM;
1215 hints.ai_protocol = IPPROTO_TCP;
1216 i = getaddrinfo(host, port_s, &hints, &res_list);
1217 if (i != 0) {
1218 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1219 return -1;
1220 }
1221
1222 for (res = res_list; res != NULL; res = res->ai_next) {
1223 sock = _non_blocking_connect(timeout, sock_pending, res);
1224 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1225 /* try the next resource */
1226 continue;
1227 }
1228 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1229 break;
1230 }
1231 freeaddrinfo(res_list);
1232
1233 } else {
1234 /* try to get a connection with the pending socket */
1235 assert(sock_pending);
1236 sock = _non_blocking_connect(timeout, sock_pending, NULL);
1237 }
1238
1239 return sock;
1240}
1241
Michal Vasko086311b2016-01-08 09:53:11 +01001242static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001243get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001244{
Michal Vasko086311b2016-01-08 09:53:11 +01001245 char *ptr;
1246 const char *str_msgid;
1247 uint64_t cur_msgid;
1248 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001249 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001250 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1251
Michal Vasko086311b2016-01-08 09:53:11 +01001252 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001253 if (!msgid && session->opts.client.notifs) {
1254 cont = session->opts.client.notifs;
1255 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001256
Michal Vasko71ba2da2016-05-04 10:53:16 +02001257 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001258 free(cont);
1259
Michal Vasko71ba2da2016-05-04 10:53:16 +02001260 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001261 }
1262
1263 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001264 if (msgid && session->opts.client.replies) {
1265 cont = session->opts.client.replies;
1266 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001267
Michal Vasko71ba2da2016-05-04 10:53:16 +02001268 xml = cont->msg;
1269 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001270
Michal Vasko71ba2da2016-05-04 10:53:16 +02001271 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001272 }
1273
Michal Vasko71ba2da2016-05-04 10:53:16 +02001274 if (!msgtype) {
1275 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001276 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001277 }
Michal Vasko086311b2016-01-08 09:53:11 +01001278
1279 /* we read rpc-reply, want a notif */
1280 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001281 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001282 while (*cont_ptr) {
1283 cont_ptr = &((*cont_ptr)->next);
1284 }
1285 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001286 if (!*cont_ptr) {
1287 ERRMEM;
1288 lyxml_free(session->ctx, xml);
1289 return NC_MSG_ERROR;
1290 }
Michal Vasko086311b2016-01-08 09:53:11 +01001291 (*cont_ptr)->msg = xml;
1292 (*cont_ptr)->next = NULL;
1293 }
1294
1295 /* we read notif, want a rpc-reply */
1296 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001297 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001298 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001299 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001300 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001301 }
Michal Vasko086311b2016-01-08 09:53:11 +01001302
Michal Vasko2e6defd2016-10-07 15:48:15 +02001303 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001304 while (*cont_ptr) {
1305 cont_ptr = &((*cont_ptr)->next);
1306 }
1307 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001308 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001309 ERRMEM;
1310 lyxml_free(session->ctx, xml);
1311 return NC_MSG_ERROR;
1312 }
Michal Vasko086311b2016-01-08 09:53:11 +01001313 (*cont_ptr)->msg = xml;
1314 (*cont_ptr)->next = NULL;
1315 }
1316
Michal Vasko086311b2016-01-08 09:53:11 +01001317 switch (msgtype) {
1318 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001319 if (!msgid) {
1320 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001321 }
Michal Vasko086311b2016-01-08 09:53:11 +01001322 break;
1323
1324 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001325 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001326 /* check message-id */
1327 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1328 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001329 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001330 } else {
1331 cur_msgid = strtoul(str_msgid, &ptr, 10);
1332 if (cur_msgid != msgid) {
1333 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1334 session->id, str_msgid);
1335 msgtype = NC_MSG_REPLY_ERR_MSGID;
1336 }
1337 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001338 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001339 }
Michal Vasko086311b2016-01-08 09:53:11 +01001340 break;
1341
1342 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001343 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001344 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001345 msgtype = NC_MSG_ERROR;
1346 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001347
1348 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001349 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001350 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001351 msgtype = NC_MSG_ERROR;
1352 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001353
1354 default:
1355 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1356 * NC_MSG_NONE is not returned by nc_read_msg()
1357 */
1358 break;
1359 }
1360
1361 return msgtype;
1362}
1363
1364/* cannot strictly fail, but does not need to fill any error parameter at all */
1365static void
1366parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1367{
1368 struct lyxml_elem *iter, *next, *info;
1369
1370 LY_TREE_FOR(xml->child, iter) {
1371 if (!iter->ns) {
1372 if (iter->content) {
1373 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1374 } else {
1375 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1376 }
1377 continue;
1378 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1379 if (iter->content) {
1380 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1381 iter->name, iter->content, iter->ns->value);
1382 } else {
1383 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1384 }
1385 continue;
1386 }
1387
1388 if (!strcmp(iter->name, "error-type")) {
1389 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1390 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1391 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1392 } else if (err->type) {
1393 WRN("<rpc-error> <error-type> duplicated.");
1394 } else {
1395 err->type = lydict_insert(ctx, iter->content, 0);
1396 }
1397 } else if (!strcmp(iter->name, "error-tag")) {
1398 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1399 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1400 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1401 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1402 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1403 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1404 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1405 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1406 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1407 && strcmp(iter->content, "malformed-message"))) {
1408 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1409 } else if (err->tag) {
1410 WRN("<rpc-error> <error-tag> duplicated.");
1411 } else {
1412 err->tag = lydict_insert(ctx, iter->content, 0);
1413 }
1414 } else if (!strcmp(iter->name, "error-severity")) {
1415 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1416 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1417 } else if (err->severity) {
1418 WRN("<rpc-error> <error-severity> duplicated.");
1419 } else {
1420 err->severity = lydict_insert(ctx, iter->content, 0);
1421 }
1422 } else if (!strcmp(iter->name, "error-app-tag")) {
1423 if (err->apptag) {
1424 WRN("<rpc-error> <error-app-tag> duplicated.");
1425 } else {
1426 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1427 }
1428 } else if (!strcmp(iter->name, "error-path")) {
1429 if (err->path) {
1430 WRN("<rpc-error> <error-path> duplicated.");
1431 } else {
1432 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1433 }
1434 } else if (!strcmp(iter->name, "error-message")) {
1435 if (err->message) {
1436 WRN("<rpc-error> <error-message> duplicated.");
1437 } else {
1438 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001439 if (err->message_lang) {
1440 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1441 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001442 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1443 }
1444 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1445 }
1446 } else if (!strcmp(iter->name, "error-info")) {
1447 LY_TREE_FOR_SAFE(iter->child, next, info) {
1448 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1449 if (!strcmp(info->name, "session-id")) {
1450 if (err->sid) {
1451 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1452 } else {
1453 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1454 }
Michal Vasko630485f2018-01-03 14:28:32 +01001455 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001456 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001457 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1458 if (!err->attr) {
1459 ERRMEM;
1460 return;
1461 }
Michal Vasko086311b2016-01-08 09:53:11 +01001462 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1463 } else if (!strcmp(info->name, "bad-element")) {
1464 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001465 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1466 if (!err->elem) {
1467 ERRMEM;
1468 return;
1469 }
Michal Vasko086311b2016-01-08 09:53:11 +01001470 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1471 } else if (!strcmp(info->name, "bad-namespace")) {
1472 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001473 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1474 if (!err->ns) {
1475 ERRMEM;
1476 return;
1477 }
Michal Vasko086311b2016-01-08 09:53:11 +01001478 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1479 } else {
1480 if (info->content) {
1481 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1482 info->name, info->content);
1483 } else {
1484 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1485 }
1486 }
1487 } else {
1488 lyxml_unlink(ctx, info);
1489 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001490 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1491 if (!err->other) {
1492 ERRMEM;
1493 return;
1494 }
Michal Vasko086311b2016-01-08 09:53:11 +01001495 err->other[err->other_count - 1] = info;
1496 }
1497 }
1498 } else {
1499 if (iter->content) {
1500 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1501 } else {
1502 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1503 }
1504 }
1505 }
1506}
1507
1508static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001509parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001510{
1511 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001512 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001513 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001514 struct nc_reply_data *data_rpl;
1515 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001516 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001517 int i;
1518
1519 if (!xml->child) {
1520 ERR("An empty <rpc-reply>.");
1521 return NULL;
1522 }
1523
1524 /* rpc-error */
1525 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1526 /* count and check elements */
1527 i = 0;
1528 LY_TREE_FOR(xml->child, iter) {
1529 if (strcmp(iter->name, "rpc-error")) {
1530 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1531 return NULL;
1532 } else if (!iter->ns) {
1533 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1534 return NULL;
1535 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1536 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1537 return NULL;
1538 }
1539 ++i;
1540 }
1541
1542 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001543 if (!error_rpl) {
1544 ERRMEM;
1545 return NULL;
1546 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001547 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001548 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001549 if (!error_rpl->err) {
1550 ERRMEM;
1551 free(error_rpl);
1552 return NULL;
1553 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001554 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001555 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001556 reply = (struct nc_reply *)error_rpl;
1557
1558 i = 0;
1559 LY_TREE_FOR(xml->child, iter) {
1560 parse_rpc_error(ctx, iter, error_rpl->err + i);
1561 ++i;
1562 }
1563
1564 /* ok */
1565 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1566 if (xml->child->next) {
1567 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1568 return NULL;
1569 }
1570 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001571 if (!reply) {
1572 ERRMEM;
1573 return NULL;
1574 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001575 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001576
1577 /* some RPC output */
1578 } else {
1579 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001580 case NC_RPC_ACT_GENERIC:
1581 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001582
1583 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001584 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1585 if (!rpc_act) {
1586 ERR("Failed to duplicate a generic RPC/action.");
1587 return NULL;
1588 }
Michal Vasko086311b2016-01-08 09:53:11 +01001589 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001590 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 +02001591 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001592 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001593 return NULL;
1594 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001595 }
Michal Vasko086311b2016-01-08 09:53:11 +01001596 break;
1597
1598 case NC_RPC_GETCONFIG:
1599 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001600 if (!xml->child->child) {
1601 /* we did not receive any data */
1602 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001603 if (!data_rpl) {
1604 ERRMEM;
1605 return NULL;
1606 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001607 data_rpl->type = NC_RPL_DATA;
1608 data_rpl->data = NULL;
1609 return (struct nc_reply *)data_rpl;
1610 }
1611
Michal Vasko086311b2016-01-08 09:53:11 +01001612 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001613 data = lyd_parse_xml(ctx, &xml->child->child,
1614 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001615 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001616 if (!data) {
1617 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1618 return NULL;
1619 }
1620 break;
1621
1622 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001623 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001624 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001625 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001626 return NULL;
1627 }
1628 break;
1629
1630 case NC_RPC_EDIT:
1631 case NC_RPC_COPY:
1632 case NC_RPC_DELETE:
1633 case NC_RPC_LOCK:
1634 case NC_RPC_UNLOCK:
1635 case NC_RPC_KILL:
1636 case NC_RPC_COMMIT:
1637 case NC_RPC_DISCARD:
1638 case NC_RPC_CANCEL:
1639 case NC_RPC_VALIDATE:
1640 case NC_RPC_SUBSCRIBE:
1641 /* there is no output defined */
1642 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1643 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001644 default:
1645 ERRINT;
1646 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001647 }
1648
1649 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001650 if (!data_rpl) {
1651 ERRMEM;
1652 return NULL;
1653 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001654 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001655 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001656 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001657 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001658 } else {
1659 /* <get>, <get-config> */
1660 data_rpl->data = data;
1661 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001662 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001663 if (!data_rpl->data) {
1664 ERR("Failed to parse <rpc-reply>.");
1665 free(data_rpl);
1666 return NULL;
1667 }
1668 reply = (struct nc_reply *)data_rpl;
1669 }
1670
1671 return reply;
1672}
1673
Radek Krejci53691be2016-02-22 13:58:37 +01001674#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001675
Michal Vasko3031aae2016-01-27 16:07:18 +01001676int
1677nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1678{
1679 int sock;
1680
Michal Vasko45e53ae2016-04-07 11:46:03 +02001681 if (!address) {
1682 ERRARG("address");
1683 return -1;
1684 } else if (!port) {
1685 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001686 return -1;
1687 }
1688
1689 sock = nc_sock_listen(address, port);
1690 if (sock == -1) {
1691 return -1;
1692 }
1693
1694 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001695 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1696 if (!client_opts.ch_binds) {
1697 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001698 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001699 return -1;
1700 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001701
Michal Vasko2e6defd2016-10-07 15:48:15 +02001702 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1703 if (!client_opts.ch_bind_ti) {
1704 ERRMEM;
1705 close(sock);
1706 return -1;
1707 }
1708 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1709
Michal Vasko3031aae2016-01-27 16:07:18 +01001710 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001711 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1712 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001713 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001714 return -1;
1715 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001716 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1717 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001718 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001719
1720 return 0;
1721}
1722
1723int
1724nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1725{
1726 uint32_t i;
1727 int ret = -1;
1728
1729 if (!address && !port && !ti) {
1730 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1731 close(client_opts.ch_binds[i].sock);
1732 free((char *)client_opts.ch_binds[i].address);
1733
1734 ret = 0;
1735 }
1736 free(client_opts.ch_binds);
1737 client_opts.ch_binds = NULL;
1738 client_opts.ch_bind_count = 0;
1739 } else {
1740 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1741 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1742 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001743 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001744 close(client_opts.ch_binds[i].sock);
1745 free((char *)client_opts.ch_binds[i].address);
1746
1747 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001748 if (!client_opts.ch_bind_count) {
1749 free(client_opts.ch_binds);
1750 client_opts.ch_binds = NULL;
1751 } else if (i < client_opts.ch_bind_count) {
1752 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1753 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1754 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001755
1756 ret = 0;
1757 }
1758 }
1759 }
1760
1761 return ret;
1762}
1763
1764API int
1765nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1766{
1767 int sock;
1768 char *host = NULL;
1769 uint16_t port, idx;
1770
Michal Vasko45e53ae2016-04-07 11:46:03 +02001771 if (!client_opts.ch_binds) {
1772 ERRINIT;
1773 return -1;
1774 } else if (!session) {
1775 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001776 return -1;
1777 }
1778
1779 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1780
Michal Vasko50456e82016-02-02 12:16:08 +01001781 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001782 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001783 return sock;
1784 }
1785
Radek Krejci53691be2016-02-22 13:58:37 +01001786#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001787 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001788 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001789 } else
1790#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001791#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001792 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001793 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001794 } else
1795#endif
1796 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001797 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001798 *session = NULL;
1799 }
1800
1801 free(host);
1802
1803 if (!(*session)) {
1804 return -1;
1805 }
1806
1807 return 1;
1808}
1809
Radek Krejci53691be2016-02-22 13:58:37 +01001810#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001811
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001812API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001813nc_session_get_cpblts(const struct nc_session *session)
1814{
1815 if (!session) {
1816 ERRARG("session");
1817 return NULL;
1818 }
1819
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001820 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001821}
1822
1823API const char *
1824nc_session_cpblt(const struct nc_session *session, const char *capab)
1825{
1826 int i, len;
1827
1828 if (!session) {
1829 ERRARG("session");
1830 return NULL;
1831 } else if (!capab) {
1832 ERRARG("capab");
1833 return NULL;
1834 }
1835
1836 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001837 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1838 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1839 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001840 }
1841 }
1842
1843 return NULL;
1844}
1845
Michal Vasko9cd26a82016-05-31 08:58:48 +02001846API int
1847nc_session_ntf_thread_running(const struct nc_session *session)
1848{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001849 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001850 ERRARG("session");
1851 return 0;
1852 }
1853
Michal Vasko2e6defd2016-10-07 15:48:15 +02001854 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001855}
1856
Michal Vaskob7558c52016-02-26 15:04:19 +01001857API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001858nc_client_init(void)
1859{
1860 nc_init();
1861}
1862
1863API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001864nc_client_destroy(void)
1865{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001866 nc_client_set_schema_searchpath(NULL);
1867#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1868 nc_client_ch_del_bind(NULL, 0, 0);
1869#endif
1870#ifdef NC_ENABLED_SSH
1871 nc_client_ssh_destroy_opts();
1872#endif
1873#ifdef NC_ENABLED_TLS
1874 nc_client_tls_destroy_opts();
1875#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001876 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001877}
1878
Michal Vasko086311b2016-01-08 09:53:11 +01001879API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001880nc_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 +01001881{
1882 struct lyxml_elem *xml;
1883 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1884
Michal Vasko45e53ae2016-04-07 11:46:03 +02001885 if (!session) {
1886 ERRARG("session");
1887 return NC_MSG_ERROR;
1888 } else if (!rpc) {
1889 ERRARG("rpc");
1890 return NC_MSG_ERROR;
1891 } else if (!reply) {
1892 ERRARG("reply");
1893 return NC_MSG_ERROR;
1894 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1895 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001896 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001897 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001898 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001899 return NC_MSG_ERROR;
1900 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001901 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001902 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1903 parseroptions &= LYD_OPT_STRICT;
1904 }
Michal Vasko41adf392017-01-17 10:39:04 +01001905 /* no mechanism to check external dependencies is provided */
1906 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001907 *reply = NULL;
1908
1909 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001910
Michal Vasko71ba2da2016-05-04 10:53:16 +02001911 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001912 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001913 lyxml_free(session->ctx, xml);
1914 if (!(*reply)) {
1915 return NC_MSG_ERROR;
1916 }
1917 }
1918
1919 return msgtype;
1920}
1921
1922API NC_MSG_TYPE
1923nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1924{
1925 struct lyxml_elem *xml, *ev_time;
1926 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1927
Michal Vasko45e53ae2016-04-07 11:46:03 +02001928 if (!session) {
1929 ERRARG("session");
1930 return NC_MSG_ERROR;
1931 } else if (!notif) {
1932 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001933 return NC_MSG_ERROR;
1934 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001935 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001936 return NC_MSG_ERROR;
1937 }
1938
1939 msgtype = get_msg(session, timeout, 0, &xml);
1940
1941 if (msgtype == NC_MSG_NOTIF) {
1942 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001943 if (!*notif) {
1944 ERRMEM;
1945 lyxml_free(session->ctx, xml);
1946 return NC_MSG_ERROR;
1947 }
Michal Vasko086311b2016-01-08 09:53:11 +01001948
1949 /* eventTime */
1950 LY_TREE_FOR(xml->child, ev_time) {
1951 if (!strcmp(ev_time->name, "eventTime")) {
1952 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1953 /* lyd_parse does not know this element */
1954 lyxml_free(session->ctx, ev_time);
1955 break;
1956 }
1957 }
1958 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001959 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001960 goto fail;
1961 }
1962
1963 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001964 (*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 +01001965 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001966 lyxml_free(session->ctx, xml);
1967 xml = NULL;
1968 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001969 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001970 goto fail;
1971 }
1972 }
1973
1974 return msgtype;
1975
1976fail:
1977 lydict_remove(session->ctx, (*notif)->datetime);
1978 lyd_free((*notif)->tree);
1979 free(*notif);
1980 *notif = NULL;
1981 lyxml_free(session->ctx, xml);
1982
1983 return NC_MSG_ERROR;
1984}
1985
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001986static void *
1987nc_recv_notif_thread(void *arg)
1988{
1989 struct nc_ntf_thread_arg *ntarg;
1990 struct nc_session *session;
1991 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1992 struct nc_notif *notif;
1993 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02001994 pthread_t *ntf_tid;
1995
1996 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001997
1998 ntarg = (struct nc_ntf_thread_arg *)arg;
1999 session = ntarg->session;
2000 notif_clb = ntarg->notif_clb;
2001 free(ntarg);
2002
Michal Vasko131120a2018-05-29 15:44:02 +02002003 /* remember our allocated tid, we will be freeing it */
2004 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
2005
Michal Vasko2e6defd2016-10-07 15:48:15 +02002006 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002007 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002008 if (msgtype == NC_MSG_NOTIF) {
2009 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002010 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2011 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2012 nc_notif_free(notif);
2013 break;
2014 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002015 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002016 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002017 /* quit this thread once the session is broken */
2018 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002019 }
2020
2021 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2022 }
2023
Michal Vasko0651c902016-05-19 15:55:42 +02002024 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002025 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02002026 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002027 return NULL;
2028}
2029
2030API int
2031nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2032{
2033 struct nc_ntf_thread_arg *ntarg;
2034 int ret;
2035
Michal Vasko45e53ae2016-04-07 11:46:03 +02002036 if (!session) {
2037 ERRARG("session");
2038 return -1;
2039 } else if (!notif_clb) {
2040 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002041 return -1;
2042 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2043 ERR("Session %u: invalid session to receive Notifications.", session->id);
2044 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002045 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002046 ERR("Session %u: separate notification thread is already running.", session->id);
2047 return -1;
2048 }
2049
2050 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002051 if (!ntarg) {
2052 ERRMEM;
2053 return -1;
2054 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002055 ntarg->session = session;
2056 ntarg->notif_clb = notif_clb;
2057
2058 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002059 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2060 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002061 ERRMEM;
2062 free(ntarg);
2063 return -1;
2064 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002065
Michal Vasko2e6defd2016-10-07 15:48:15 +02002066 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002067 if (ret) {
2068 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2069 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002070 free((pthread_t *)session->opts.client.ntf_tid);
2071 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002072 return -1;
2073 }
2074
2075 return 0;
2076}
2077
Michal Vasko086311b2016-01-08 09:53:11 +01002078API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002079nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002080{
2081 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002082 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002083 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002084 struct nc_rpc_getconfig *rpc_gc;
2085 struct nc_rpc_edit *rpc_e;
2086 struct nc_rpc_copy *rpc_cp;
2087 struct nc_rpc_delete *rpc_del;
2088 struct nc_rpc_lock *rpc_lock;
2089 struct nc_rpc_get *rpc_g;
2090 struct nc_rpc_kill *rpc_k;
2091 struct nc_rpc_commit *rpc_com;
2092 struct nc_rpc_cancel *rpc_can;
2093 struct nc_rpc_validate *rpc_val;
2094 struct nc_rpc_getschema *rpc_gs;
2095 struct nc_rpc_subscribe *rpc_sub;
2096 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01002097 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02002098 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002099 uint64_t cur_msgid;
2100
Michal Vasko45e53ae2016-04-07 11:46:03 +02002101 if (!session) {
2102 ERRARG("session");
2103 return NC_MSG_ERROR;
2104 } else if (!rpc) {
2105 ERRARG("rpc");
2106 return NC_MSG_ERROR;
2107 } else if (!msgid) {
2108 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002109 return NC_MSG_ERROR;
2110 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002111 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002112 return NC_MSG_ERROR;
2113 }
2114
Michal Vasko90e8e692016-07-13 12:27:57 +02002115 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002116 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002117 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01002118 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002119 return NC_MSG_ERROR;
2120 }
2121 }
2122
2123 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002124 case NC_RPC_ACT_GENERIC:
2125 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002126
2127 if (rpc_gen->has_data) {
2128 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002129 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002130 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002131 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 +01002132 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002133 if (!data) {
2134 return NC_MSG_ERROR;
2135 }
Michal Vasko086311b2016-01-08 09:53:11 +01002136 }
2137 break;
2138
2139 case NC_RPC_GETCONFIG:
2140 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2141
2142 data = lyd_new(NULL, ietfnc, "get-config");
2143 node = lyd_new(data, ietfnc, "source");
2144 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
2145 if (!node) {
2146 lyd_free(data);
2147 return NC_MSG_ERROR;
2148 }
2149 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002150 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002151 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002152 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002153 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002154 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002155 lyd_insert_attr(node, NULL, "type", "xpath");
2156 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002157 }
2158 if (!node) {
2159 lyd_free(data);
2160 return NC_MSG_ERROR;
2161 }
2162 }
2163
2164 if (rpc_gc->wd_mode) {
2165 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002166 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002167 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002168 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002169 return NC_MSG_ERROR;
2170 }
2171 }
2172 switch (rpc_gc->wd_mode) {
2173 case NC_WD_UNKNOWN:
2174 /* cannot get here */
2175 break;
2176 case NC_WD_ALL:
2177 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2178 break;
2179 case NC_WD_ALL_TAG:
2180 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2181 break;
2182 case NC_WD_TRIM:
2183 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2184 break;
2185 case NC_WD_EXPLICIT:
2186 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2187 break;
2188 }
2189 if (!node) {
2190 lyd_free(data);
2191 return NC_MSG_ERROR;
2192 }
2193 }
2194 break;
2195
2196 case NC_RPC_EDIT:
2197 rpc_e = (struct nc_rpc_edit *)rpc;
2198
2199 data = lyd_new(NULL, ietfnc, "edit-config");
2200 node = lyd_new(data, ietfnc, "target");
2201 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
2202 if (!node) {
2203 lyd_free(data);
2204 return NC_MSG_ERROR;
2205 }
2206
2207 if (rpc_e->default_op) {
2208 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
2209 if (!node) {
2210 lyd_free(data);
2211 return NC_MSG_ERROR;
2212 }
2213 }
2214
2215 if (rpc_e->test_opt) {
2216 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
2217 if (!node) {
2218 lyd_free(data);
2219 return NC_MSG_ERROR;
2220 }
2221 }
2222
2223 if (rpc_e->error_opt) {
2224 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
2225 if (!node) {
2226 lyd_free(data);
2227 return NC_MSG_ERROR;
2228 }
2229 }
2230
Michal Vasko7793bc62016-09-16 11:58:41 +02002231 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002232 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002233 } else {
2234 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
2235 }
2236 if (!node) {
2237 lyd_free(data);
2238 return NC_MSG_ERROR;
2239 }
2240 break;
2241
2242 case NC_RPC_COPY:
2243 rpc_cp = (struct nc_rpc_copy *)rpc;
2244
2245 data = lyd_new(NULL, ietfnc, "copy-config");
2246 node = lyd_new(data, ietfnc, "target");
2247 if (rpc_cp->url_trg) {
2248 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
2249 } else {
2250 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
2251 }
2252 if (!node) {
2253 lyd_free(data);
2254 return NC_MSG_ERROR;
2255 }
2256
2257 node = lyd_new(data, ietfnc, "source");
2258 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002259 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002260 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002261 } else {
2262 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
2263 }
2264 } else {
2265 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
2266 }
2267 if (!node) {
2268 lyd_free(data);
2269 return NC_MSG_ERROR;
2270 }
2271
2272 if (rpc_cp->wd_mode) {
2273 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002274 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002275 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002276 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002277 return NC_MSG_ERROR;
2278 }
2279 }
2280 switch (rpc_cp->wd_mode) {
2281 case NC_WD_UNKNOWN:
2282 /* cannot get here */
2283 break;
2284 case NC_WD_ALL:
2285 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2286 break;
2287 case NC_WD_ALL_TAG:
2288 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2289 break;
2290 case NC_WD_TRIM:
2291 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2292 break;
2293 case NC_WD_EXPLICIT:
2294 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2295 break;
2296 }
2297 if (!node) {
2298 lyd_free(data);
2299 return NC_MSG_ERROR;
2300 }
2301 }
2302 break;
2303
2304 case NC_RPC_DELETE:
2305 rpc_del = (struct nc_rpc_delete *)rpc;
2306
2307 data = lyd_new(NULL, ietfnc, "delete-config");
2308 node = lyd_new(data, ietfnc, "target");
2309 if (rpc_del->url) {
2310 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2311 } else {
2312 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2313 }
2314 if (!node) {
2315 lyd_free(data);
2316 return NC_MSG_ERROR;
2317 }
2318 break;
2319
2320 case NC_RPC_LOCK:
2321 rpc_lock = (struct nc_rpc_lock *)rpc;
2322
2323 data = lyd_new(NULL, ietfnc, "lock");
2324 node = lyd_new(data, ietfnc, "target");
2325 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2326 if (!node) {
2327 lyd_free(data);
2328 return NC_MSG_ERROR;
2329 }
2330 break;
2331
2332 case NC_RPC_UNLOCK:
2333 rpc_lock = (struct nc_rpc_lock *)rpc;
2334
2335 data = lyd_new(NULL, ietfnc, "unlock");
2336 node = lyd_new(data, ietfnc, "target");
2337 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2338 if (!node) {
2339 lyd_free(data);
2340 return NC_MSG_ERROR;
2341 }
2342 break;
2343
2344 case NC_RPC_GET:
2345 rpc_g = (struct nc_rpc_get *)rpc;
2346
2347 data = lyd_new(NULL, ietfnc, "get");
2348 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002349 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002350 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002351 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002352 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002353 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002354 lyd_insert_attr(node, NULL, "type", "xpath");
2355 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002356 }
2357 if (!node) {
2358 lyd_free(data);
2359 return NC_MSG_ERROR;
2360 }
2361 }
2362
2363 if (rpc_g->wd_mode) {
2364 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002365 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002366 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002367 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002368 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002369 return NC_MSG_ERROR;
2370 }
2371 }
2372 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002373 case NC_WD_ALL:
2374 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2375 break;
2376 case NC_WD_ALL_TAG:
2377 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2378 break;
2379 case NC_WD_TRIM:
2380 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2381 break;
2382 case NC_WD_EXPLICIT:
2383 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2384 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002385 default:
2386 /* cannot get here */
2387 node = NULL;
2388 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002389 }
2390 if (!node) {
2391 lyd_free(data);
2392 return NC_MSG_ERROR;
2393 }
2394 }
2395 break;
2396
2397 case NC_RPC_KILL:
2398 rpc_k = (struct nc_rpc_kill *)rpc;
2399
2400 data = lyd_new(NULL, ietfnc, "kill-session");
2401 sprintf(str, "%u", rpc_k->sid);
2402 lyd_new_leaf(data, ietfnc, "session-id", str);
2403 break;
2404
2405 case NC_RPC_COMMIT:
2406 rpc_com = (struct nc_rpc_commit *)rpc;
2407
2408 data = lyd_new(NULL, ietfnc, "commit");
2409 if (rpc_com->confirmed) {
2410 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2411 }
2412
2413 if (rpc_com->confirm_timeout) {
2414 sprintf(str, "%u", rpc_com->confirm_timeout);
2415 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2416 }
2417
2418 if (rpc_com->persist) {
2419 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2420 if (!node) {
2421 lyd_free(data);
2422 return NC_MSG_ERROR;
2423 }
2424 }
2425
2426 if (rpc_com->persist_id) {
2427 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2428 if (!node) {
2429 lyd_free(data);
2430 return NC_MSG_ERROR;
2431 }
2432 }
2433 break;
2434
2435 case NC_RPC_DISCARD:
2436 data = lyd_new(NULL, ietfnc, "discard-changes");
2437 break;
2438
2439 case NC_RPC_CANCEL:
2440 rpc_can = (struct nc_rpc_cancel *)rpc;
2441
2442 data = lyd_new(NULL, ietfnc, "cancel-commit");
2443 if (rpc_can->persist_id) {
2444 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2445 if (!node) {
2446 lyd_free(data);
2447 return NC_MSG_ERROR;
2448 }
2449 }
2450 break;
2451
2452 case NC_RPC_VALIDATE:
2453 rpc_val = (struct nc_rpc_validate *)rpc;
2454
2455 data = lyd_new(NULL, ietfnc, "validate");
2456 node = lyd_new(data, ietfnc, "source");
2457 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002458 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002459 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002460 } else {
2461 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2462 }
2463 } else {
2464 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2465 }
2466 if (!node) {
2467 lyd_free(data);
2468 return NC_MSG_ERROR;
2469 }
2470 break;
2471
2472 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002473 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002474 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002475 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002476 return NC_MSG_ERROR;
2477 }
2478
2479 rpc_gs = (struct nc_rpc_getschema *)rpc;
2480
2481 data = lyd_new(NULL, ietfncmon, "get-schema");
2482 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2483 if (!node) {
2484 lyd_free(data);
2485 return NC_MSG_ERROR;
2486 }
2487 if (rpc_gs->version) {
2488 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2489 if (!node) {
2490 lyd_free(data);
2491 return NC_MSG_ERROR;
2492 }
2493 }
2494 if (rpc_gs->format) {
2495 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2496 if (!node) {
2497 lyd_free(data);
2498 return NC_MSG_ERROR;
2499 }
2500 }
2501 break;
2502
2503 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002504 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002505 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002506 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002507 return NC_MSG_ERROR;
2508 }
2509
2510 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2511
2512 data = lyd_new(NULL, notifs, "create-subscription");
2513 if (rpc_sub->stream) {
2514 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2515 if (!node) {
2516 lyd_free(data);
2517 return NC_MSG_ERROR;
2518 }
2519 }
2520
2521 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002522 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002523 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002524 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002525 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002526 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002527 lyd_insert_attr(node, NULL, "type", "xpath");
2528 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002529 }
2530 if (!node) {
2531 lyd_free(data);
2532 return NC_MSG_ERROR;
2533 }
2534 }
2535
2536 if (rpc_sub->start) {
2537 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2538 if (!node) {
2539 lyd_free(data);
2540 return NC_MSG_ERROR;
2541 }
2542 }
2543
2544 if (rpc_sub->stop) {
2545 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2546 if (!node) {
2547 lyd_free(data);
2548 return NC_MSG_ERROR;
2549 }
2550 }
2551 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002552 default:
2553 ERRINT;
2554 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002555 }
2556
Michal Vasko131120a2018-05-29 15:44:02 +02002557 if (!data) {
2558 /* error was already printed */
2559 return NC_MSG_ERROR;
2560 }
2561
Michal Vasko41adf392017-01-17 10:39:04 +01002562 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2563 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002564 if (dofree) {
2565 lyd_free(data);
2566 }
Michal Vasko086311b2016-01-08 09:53:11 +01002567 return NC_MSG_ERROR;
2568 }
2569
Michal Vasko131120a2018-05-29 15:44:02 +02002570 /* send RPC, store its message ID */
2571 r = nc_send_msg_io(session, timeout, data);
2572 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002573
Radek Krejcib4b19062018-02-07 16:33:06 +01002574 if (dofree) {
2575 lyd_free(data);
2576 }
Michal Vasko086311b2016-01-08 09:53:11 +01002577
Michal Vasko131120a2018-05-29 15:44:02 +02002578 if (r == NC_MSG_RPC) {
2579 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002580 }
Michal Vasko131120a2018-05-29 15:44:02 +02002581 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002582}
Michal Vaskode2946c2017-01-12 12:19:26 +01002583
2584API void
2585nc_client_session_set_not_strict(struct nc_session *session)
2586{
2587 if (session->side != NC_CLIENT) {
2588 ERRARG("session");
2589 return;
2590 }
2591
2592 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2593}