blob: 643c600740b8fa21aa00fb35cbfeec3366ed2c69 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
Radek Krejcifd5b6682017-06-13 15:52:53 +020015#define _GNU_SOURCE
Michal Vasko086311b2016-01-08 09:53:11 +010016#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010020#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010021#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020026#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <sys/types.h>
28#include <unistd.h>
29#include <arpa/inet.h>
30#include <poll.h>
31
32#include <libyang/libyang.h>
33
Michal Vasko086311b2016-01-08 09:53:11 +010034#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010035#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010036#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010037
Michal Vasko80ef5d22016-01-18 09:21:02 +010038static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
39
Radek Krejci62aa0642017-05-25 16:33:49 +020040#ifdef NC_ENABLED_SSH
41int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
42char *sshauth_password(const char *username, const char *hostname, void *priv);
43char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
44char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
45#endif /* NC_ENABLED_SSH */
46
47static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
48static pthread_key_t nc_client_context_key;
49#ifdef __linux__
50static struct nc_client_context context_main = {
51 /* .opts zeroed */
52#ifdef NC_ENABLED_SSH
53 .ssh_opts = {
54 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
55 .auth_hostkey_check = sshauth_hostkey_check,
56 .auth_password = sshauth_password,
57 .auth_interactive = sshauth_interactive,
58 .auth_privkey_passphrase = sshauth_privkey_passphrase
59 },
60 .ssh_ch_opts = {
61 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
62 .auth_hostkey_check = sshauth_hostkey_check,
63 .auth_password = sshauth_password,
64 .auth_interactive = sshauth_interactive,
65 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020066 },
Radek Krejci62aa0642017-05-25 16:33:49 +020067#endif /* NC_ENABLED_SSH */
68 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020069 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020070};
71#endif
72
73static void
74nc_client_context_free(void *ptr)
75{
76 struct nc_client_context *c = (struct nc_client_context *)ptr;
77
Radek Krejci5cebc6b2017-05-26 13:24:38 +020078 if (--(c->refcount)) {
79 /* still used */
80 return;
81 }
82
Radek Krejci62aa0642017-05-25 16:33:49 +020083#ifdef __linux__
84 /* in __linux__ we use static memory in the main thread,
85 * so this check is for programs terminating the main()
86 * function by pthread_exit() :)
87 */
88 if (c != &context_main)
89#endif
90 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +020091 /* for the main thread the same is done in nc_client_destroy() */
Radek Krejci62aa0642017-05-25 16:33:49 +020092 nc_client_set_schema_searchpath(NULL);
93#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
94 nc_client_ch_del_bind(NULL, 0, 0);
95#endif
96#ifdef NC_ENABLED_SSH
97 nc_client_ssh_destroy_opts();
98#endif
99#ifdef NC_ENABLED_TLS
100 nc_client_tls_destroy_opts();
101#endif
102 free(c);
103 }
104}
105
106static void
107nc_client_context_createkey(void)
108{
109 int r;
110
111 /* initiate */
112 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
113 pthread_setspecific(nc_client_context_key, NULL);
114}
115
116struct nc_client_context *
117nc_client_context_location(void)
118{
119 struct nc_client_context *e;
120
121 pthread_once(&nc_client_context_once, nc_client_context_createkey);
122 e = pthread_getspecific(nc_client_context_key);
123 if (!e) {
124 /* prepare ly_err storage */
125#ifdef __linux__
126 if (getpid() == syscall(SYS_gettid)) {
127 /* main thread - use global variable instead of thread-specific variable. */
128 e = &context_main;
129 } else
130#endif /* __linux__ */
131 {
132 e = calloc(1, sizeof *e);
133 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200134 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200135#ifdef NC_ENABLED_SSH
136 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
137 e->ssh_opts.auth_pref[0].value = 3;
138 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
139 e->ssh_opts.auth_pref[1].value = 2;
140 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
141 e->ssh_opts.auth_pref[2].value = 1;
142 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
143 e->ssh_opts.auth_password = sshauth_password;
144 e->ssh_opts.auth_interactive = sshauth_interactive;
145 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
146
147 /* callhome settings are the same except the inverted auth methods preferences */
148 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
149 e->ssh_ch_opts.auth_pref[0].value = 1;
150 e->ssh_ch_opts.auth_pref[1].value = 2;
151 e->ssh_ch_opts.auth_pref[2].value = 3;
152#endif /* NC_ENABLED_SSH */
153 }
154 pthread_setspecific(nc_client_context_key, e);
155 }
156
157 return e;
158}
159
160#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100161
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162API void *
163nc_client_get_thread_context(void)
164{
165 return nc_client_context_location();
166}
167
168API void
169nc_client_set_thread_context(void *context)
170{
171 struct nc_client_context *old, *new;
172
173 if (!context) {
174 ERRARG(context);
175 return;
176 }
177
178 new = (struct nc_client_context *)context;
179 old = nc_client_context_location();
180 if (old == new) {
181 /* nothing to change */
182 return;
183 }
184
185 /* replace old by new, increase reference counter in the newly set context */
186 nc_client_context_free(old);
187 new->refcount++;
188 pthread_setspecific(nc_client_context_key, new);
189}
190
Radek Krejcifd5b6682017-06-13 15:52:53 +0200191int
192nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
193{
194 /* assign context (dicionary needed for handshake) */
195 if (!ctx) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200196 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200197 if (!ctx) {
198 return EXIT_FAILURE;
199 }
200
201 /* user path must be first, the first path is used to store schemas retreived via get-schema */
202 if (client_opts.schema_searchpath) {
203 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
204 }
Michal Vaskoff7e3562018-02-15 13:41:22 +0100205 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200206
207 /* set callback for getting schemas, if provided */
208 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
209 } else {
210 session->flags |= NC_SESSION_SHAREDCTX;
211 }
212
213 session->ctx = ctx;
214
215 return EXIT_SUCCESS;
216}
217
Michal Vasko086311b2016-01-08 09:53:11 +0100218API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100219nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100220{
Michal Vasko3031aae2016-01-27 16:07:18 +0100221 if (client_opts.schema_searchpath) {
222 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 }
Michal Vasko086311b2016-01-08 09:53:11 +0100224
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100225 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100226 client_opts.schema_searchpath = strdup(path);
227 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100228 ERRMEM;
229 return 1;
230 }
231 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100232 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100233 }
234
235 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100236}
237
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100238API const char *
239nc_client_get_schema_searchpath(void)
240{
241 return client_opts.schema_searchpath;
242}
243
Radek Krejcifd5b6682017-06-13 15:52:53 +0200244API int
245nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100246{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200247 client_opts.schema_clb = clb;
248 if (clb) {
249 client_opts.schema_clb_data = user_data;
250 } else {
251 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100252 }
253
254 return 0;
255}
256
Radek Krejcifd5b6682017-06-13 15:52:53 +0200257API ly_module_imp_clb
258nc_client_get_schema_callback(void **user_data)
259{
260 if (user_data) {
261 (*user_data) = client_opts.schema_clb_data;
262 }
263 return client_opts.schema_clb;
264}
265
Radek Krejci65ef6d52018-08-16 16:35:02 +0200266
267struct schema_info {
268 char *name;
269 char *revision;
270 struct {
271 char *name;
272 char *revision;
273 } *submodules;
274 struct ly_set features;
275 int implemented;
276};
277
278struct clb_data_s {
279 void *user_data;
280 ly_module_imp_clb user_clb;
281 struct schema_info *schemas;
282 struct nc_session *session;
283 int has_get_schema;
284};
285
286static char *
287retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
288 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100289{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200290 char *localfile = NULL;
291 FILE *f;
292 long length, l;
293 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100294
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200295 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
296 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
297 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200298 return NULL;
299 }
300 if (localfile) {
301 VRB("Session %u: reading schema from localfile \"%s\".", clb_data->session->id, localfile);
302 f = fopen(localfile, "r");
303 if (!f) {
304 ERR("Session %u: unable to open \"%s\" file to get schema (%s).",
305 clb_data->session->id, localfile, strerror(errno));
306 free(localfile);
307 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100308 }
Michal Vasko086311b2016-01-08 09:53:11 +0100309
Radek Krejci65ef6d52018-08-16 16:35:02 +0200310 fseek(f, 0, SEEK_END);
311 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200312 if (length < 0) {
313 ERR("Session %u: unable to get size of schema file \"%s\".",
314 clb_data->session->id, localfile);
315 free(localfile);
316 fclose(f);
317 return NULL;
318 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200319 fseek(f, 0, SEEK_SET);
320
321 model_data = malloc(length + 1);
322 if (!model_data) {
323 ERRMEM;
324 } else if ((l = fread(model_data, 1, length, f)) != length) {
325 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
326 clb_data->session->id, localfile, l, length);
327 free(model_data);
328 model_data = NULL;
329 } else {
330 /* terminating NULL byte */
331 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100332 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200333 fclose(f);
334 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100335 }
336
Radek Krejci65ef6d52018-08-16 16:35:02 +0200337 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100338}
339
340static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200341retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
342 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100343{
Michal Vasko086311b2016-01-08 09:53:11 +0100344 struct nc_rpc *rpc;
345 struct nc_reply *reply;
346 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200347 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200348 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100349 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100350 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200351 char *localfile = NULL;
352 FILE *f;
353 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100354
Radek Krejci65ef6d52018-08-16 16:35:02 +0200355 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
356 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100357
Radek Krejci65ef6d52018-08-16 16:35:02 +0200358 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100359 usleep(1000);
360 }
361 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200362 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100363 nc_rpc_free(rpc);
364 return NULL;
365 }
366
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200367 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200368 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200369 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100370 nc_rpc_free(rpc);
371 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200372 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100373 return NULL;
374 } else if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200375 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100376 return NULL;
377 }
378
Michal Vasko998ba412016-09-16 12:00:07 +0200379 switch (reply->type) {
380 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200381 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200382 nc_reply_free(reply);
383 return NULL;
384 case NC_RPL_DATA:
385 /* fine */
386 break;
387 case NC_RPL_ERROR:
388 error_rpl = (struct nc_reply_error *)reply;
389 if (error_rpl->count) {
390 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200391 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200392 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200393 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200394 }
395 nc_reply_free(reply);
396 return NULL;
397 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200398 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100399 nc_reply_free(reply);
400 return NULL;
401 }
402
Michal Vasko086311b2016-01-08 09:53:11 +0100403 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200404 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
405 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200406 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200407 nc_reply_free(reply);
408 return NULL;
409 }
Radek Krejci539efb62016-08-24 15:05:16 +0200410 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
411 switch (get_schema_data->value_type) {
412 case LYD_ANYDATA_CONSTSTRING:
413 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200414 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200415 break;
416 case LYD_ANYDATA_DATATREE:
417 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
418 break;
419 case LYD_ANYDATA_XML:
420 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
421 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200422 case LYD_ANYDATA_JSON:
423 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200424 case LYD_ANYDATA_SXML:
425 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200426 case LYD_ANYDATA_LYB:
427 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200428 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200429 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200430 }
Michal Vasko086311b2016-01-08 09:53:11 +0100431 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100432
Radek Krejcifd5b6682017-06-13 15:52:53 +0200433 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200434 if (model_data) {
435 *format = LYS_IN_YANG;
436 if (client_opts.schema_searchpath) {
437 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
438 rev ? "@" : "", rev ? rev : "") == -1) {
439 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100440 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200441 f = fopen(localfile, "w");
442 if (!f) {
443 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
444 localfile, strerror(errno));
445 } else {
446 fputs(model_data, f);
447 fclose(f);
448 }
449 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100450 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200451 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200452 }
453
Michal Vasko086311b2016-01-08 09:53:11 +0100454 return model_data;
455}
456
Radek Krejci65ef6d52018-08-16 16:35:02 +0200457static char *
458retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
459 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data))
460{
461 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
462 unsigned int u, v, match = 1;
463 const char *name = NULL, *rev = NULL;
464 char *model_data = NULL;
465
466 /* get and check the final name and revision of the schema to be retrieved */
467 if (!mod_rev || !mod_rev[0]) {
468 /* newest revision requested - get the newest revision from the list of available modules on server */
469 match = 0;
470 for (u = 0; clb_data->schemas[u].name; ++u) {
471 if (strcmp(mod_name, clb_data->schemas[u].name)) {
472 continue;
473 }
474 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
475 mod_rev = clb_data->schemas[u].revision;
476 }
477 match = u + 1;
478 }
479 if (!match) {
480 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
481 clb_data->session->id, mod_name);
482 }
483 }
484 if (submod_name) {
485 name = submod_name;
486 if (sub_rev) {
487 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200488 } else if (match) {
489 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200490 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 +0200491 clb_data->session->id, submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200492 } else {
493 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
494 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
495 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
496 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200497 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200498 if (!rev) {
499 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
500 clb_data->session->id, submod_name, mod_name);
501 return NULL;
502 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200503 }
504 }
505 } else {
506 name = mod_name;
507 rev = mod_rev;
508 }
509
510 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
511
512 if (match) {
513 /* we have enough information to avoid communication with server and try to get
514 * the schema locally */
515
516 /* 1. try to get data locally */
517 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
518
519 /* 2. try to use <get-schema> */
520 if (!model_data && clb_data->has_get_schema) {
521 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
522 }
523 } else {
524 /* we are unsure which revision of the schema we should load, so first try to get
525 * the newest revision from the server via get-schema and only if the server does not
526 * implement get-schema, try to load the newest revision locally. This is imperfect
527 * solution, but there are situation when a client does not know what revision is
528 * actually implemented by the server. */
529
530 /* 1. try to use <get-schema> */
531 if (clb_data->has_get_schema) {
532 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
533 }
534
535 /* 2. try to get data locally */
536 if (!model_data) {
537 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
538 }
539 }
540
541 /* 3. try to use user callback */
542 if (!model_data && clb_data->user_clb) {
543 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
544 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
545 }
546
547 *free_module_data = free;
548 return model_data;
549}
550
Michal Vaskoceae0152018-02-14 16:03:59 +0100551static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200552nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200553 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, const struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100554{
555 int ret = 0;
556 struct ly_err_item *eitem;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200557 char *module_data = NULL;
558 LYS_INFORMAT format;
559 void (*free_module_data)(void*) = NULL;
560 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100561
Radek Krejci65ef6d52018-08-16 16:35:02 +0200562 *mod = NULL;
563 if (revision) {
564 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
565 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100566 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200567 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100568 /* make the present module implemented */
569 if (lys_set_implemented(*mod)) {
570 ERR("Failed to implement model \"%s\".", (*mod)->name);
571 ret = -1;
572 }
573 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200574 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100575 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200576 clb_data.has_get_schema = has_get_schema;
577 clb_data.schemas = schemas;
578 clb_data.session = session;
579 clb_data.user_clb = user_clb;
580 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100581
582 /* clear all the errors and just collect them for now */
583 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200584 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100585
Radek Krejci65ef6d52018-08-16 16:35:02 +0200586 /* get module data */
587 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100588
Radek Krejci65ef6d52018-08-16 16:35:02 +0200589 if (module_data) {
590 /* parse the schema */
591 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100592
Radek Krejci65ef6d52018-08-16 16:35:02 +0200593 *mod = lys_parse_mem(session->ctx, module_data, format);
594 if (*free_module_data) {
595 (*free_module_data)(module_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100596 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100597
Radek Krejci65ef6d52018-08-16 16:35:02 +0200598 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
599 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100600
Michal Vaskodbf7c272018-02-19 09:07:59 +0100601 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200602 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100603 if (!(*mod)) {
604 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
605 ly_err_print(eitem);
606 }
607 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200608 } else {
609 /* print only warnings */
610 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
611 if (eitem->level == LY_LLWRN) {
612 ly_err_print(eitem);
613 }
614 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100615 }
616
Michal Vaskodbf7c272018-02-19 09:07:59 +0100617 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100618 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100619 }
620
621 return ret;
622}
623
Radek Krejci65ef6d52018-08-16 16:35:02 +0200624static void
625free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200626{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200627 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200628
Radek Krejci65ef6d52018-08-16 16:35:02 +0200629 if (!list) {
630 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200631 }
632
Radek Krejci65ef6d52018-08-16 16:35:02 +0200633 for (u = 0; list[u].name; ++u) {
634 free(list[u].name);
635 free(list[u].revision);
636 for (v = 0; v < list[u].features.number; ++v) {
637 free(list[u].features.set.g[v]);
638 }
639 free(list[u].features.set.g);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200640 if (list[u].submodules) {
641 for (v = 0; list[u].submodules[v].name; ++v) {
642 free(list[u].submodules[v].name);
643 free(list[u].submodules[v].revision);
644 }
645 free(list[u].submodules);
646 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200647 }
648 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200649}
650
Radek Krejci235d8cb2018-08-17 14:04:32 +0200651
652static int
653build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200654{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200655 struct nc_rpc *rpc = NULL;
656 struct nc_reply *reply = NULL;
657 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200658 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200659 NC_MSG_TYPE msg;
660 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200661 struct ly_set *modules = NULL;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200662 unsigned int u, v, c, submodules_count;
663 struct lyd_node *iter, *child;
664 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200665 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200666
667 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200668 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
669 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
670 } else {
671 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0, NC_PARAMTYPE_CONST);
672 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200673 if (!rpc) {
674 goto cleanup;
675 }
676
677 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
678 usleep(1000);
679 }
680 if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200681 WRN("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200682 session->id);
683 goto cleanup;
684 }
685
686 do {
687 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
688 } while (msg == NC_MSG_NOTIF);
689 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200690 WRN("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200691 goto cleanup;
692 } else if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200693 WRN("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200694 goto cleanup;
695 }
696
697 switch (reply->type) {
698 case NC_RPL_OK:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200699 WRN("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200700 goto cleanup;
701 case NC_RPL_DATA:
702 /* fine */
703 break;
704 case NC_RPL_ERROR:
705 error_rpl = (struct nc_reply_error *)reply;
706 if (error_rpl->count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200707 WRN("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200708 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
709 } else {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200710 WRN("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200711 }
712 goto cleanup;
713 case NC_RPL_NOTIF:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200714 WRN("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200715 goto cleanup;
716 }
717
Radek Krejci65ef6d52018-08-16 16:35:02 +0200718 yldata = ((struct nc_reply_data *)reply)->data;
719 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200720 WRN("Session %u: unexpected data in reply 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 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200725 if (!modules) {
726 /* compatibility with older versions of ietf-yang-library */
727 modules = lyd_find_path(yldata, "/ietf-yang-library:modules/module");
728 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200729
730 c = modules ? modules->number : 0;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200731 (*result) = calloc(c + 1, sizeof **result);
732 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200733 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200734 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200735 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200736 }
737
Radek Krejci65ef6d52018-08-16 16:35:02 +0200738 for (u = 0; u < c; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200739 submodules_count = 0;
740 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200741 LY_TREE_FOR(modules->set.d[u]->child, iter) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200742 if (iter->schema->module != mod) {
743 /* ignore node from other schemas (augments) */
744 continue;
745 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200746 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
747 /* ignore empty nodes */
748 continue;
749 }
750 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200751 (*result)[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200752 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200753 (*result)[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200754 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200755 (*result)[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200756 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200757 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 +0200758 } else if (!strcmp(iter->schema->name, "submodule")) {
759 submodules_count++;
760 }
761 }
762
763 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200764 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
765 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200766 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200767 free_schema_info(*result);
768 *result = NULL;
769 ret = EXIT_FAILURE;
770 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200771 } else {
772 v = 0;
773 LY_TREE_FOR(modules->set.d[u]->child, iter) {
774 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
775 if (mod == iter->schema->module && !strcmp(iter->schema->name, "submodule")) {
776 LY_TREE_FOR(iter->child, child) {
777 if (mod != child->schema->module) {
778 continue;
779 } else if (!strcmp(child->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200780 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200781 } else if (!strcmp(child->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200782 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200783 }
784 }
785 }
786 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200787 }
788 }
789 }
790
Radek Krejcifd5b6682017-06-13 15:52:53 +0200791cleanup:
792 nc_rpc_free(rpc);
793 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200794 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200795
Radek Krejci235d8cb2018-08-17 14:04:32 +0200796 if (session->status != NC_STATUS_RUNNING) {
797 /* something bad heppened, discard the session */
798 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
799 ret = EXIT_FAILURE;
800 }
801
802 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200803}
804
Radek Krejci235d8cb2018-08-17 14:04:32 +0200805static int
806build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200807{
808 unsigned int u, v;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200809 char *module_cpblt, *ptr, *ptr2;
810
811 for (u = 0; cpblts[u]; ++u);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200812 (*result) = calloc(u + 1, sizeof **result);
813 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200814 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200815 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200816 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200817
818 for (u = v = 0; cpblts[u]; ++u) {
819 module_cpblt = strstr(cpblts[u], "module=");
820 /* this capability requires a module */
821 if (!module_cpblt) {
822 continue;
823 }
824
825 /* get module's name */
826 ptr = (char *)module_cpblt + 7;
827 ptr2 = strchr(ptr, '&');
828 if (!ptr2) {
829 ptr2 = ptr + strlen(ptr);
830 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200831 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200832
833 /* get module's revision */
834 ptr = strstr(module_cpblt, "revision=");
835 if (ptr) {
836 ptr += 9;
837 ptr2 = strchr(ptr, '&');
838 if (!ptr2) {
839 ptr2 = ptr + strlen(ptr);
840 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200841 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200842 }
843
844 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200845 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200846
847 /* get module's features */
848 ptr = strstr(module_cpblt, "features=");
849 if (ptr) {
850 ptr += 9;
851 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
852 if (*ptr == ',') {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200853 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200854 ptr2 = ptr + 1;
855 }
856 }
857 /* the last one */
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 }
860 ++v;
861 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200862
863 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200864}
865
866static int
867nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
868{
869 int ret = EXIT_FAILURE;
870 const struct lys_module *mod;
871 unsigned int u, v;
872
873 for (u = 0; modules[u].name; ++u) {
874 /* we can continue even if it fails */
875 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
876
877 if (!mod) {
878 if (session->status != NC_STATUS_RUNNING) {
879 /* something bad heppened, discard the session */
880 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
881 goto cleanup;
882 }
883
884 /* all loading ways failed, the schema will be ignored in the received data */
885 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
886 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
887 } else {
888 /* set features - first disable all to enable specified then */
889 lys_features_disable(mod, "*");
890 for (v = 0; v < modules[u].features.number; v++) {
891 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
892 }
893 }
Michal Vasko60f66602017-10-17 13:52:18 +0200894 }
895
Radek Krejci65ef6d52018-08-16 16:35:02 +0200896 /* done */
897 ret = EXIT_SUCCESS;
898
899cleanup:
900
Radek Krejcifd5b6682017-06-13 15:52:53 +0200901 return ret;
902}
903
Radek Krejci65ef6d52018-08-16 16:35:02 +0200904static int
905nc_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)
906{
907 unsigned int u, v;
908 const struct lys_module *ietfnc;
909
910 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
911 if (!ietfnc) {
912 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
913 if (!ietfnc) {
914 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
915 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
916 }
917 }
918 if (!ietfnc) {
919 ERR("Loading base NETCONF schema failed.");
920 return 1;
921 }
922
923 /* set supported capabilities from ietf-netconf */
924 for (u = 0; modules[u].name; ++u) {
925 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
926 continue;
927 }
928
929 lys_features_disable(ietfnc, "*");
930 for (v = 0; v < modules[u].features.number; v++) {
931 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
932 }
933 }
934
935 return 0;
936}
937
Michal Vasko086311b2016-01-08 09:53:11 +0100938int
939nc_ctx_check_and_fill(struct nc_session *session)
940{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200941 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100942 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100943 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200944 const struct lys_module *mod = NULL;
945 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200946 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100947
Michal Vasko2e6defd2016-10-07 15:48:15 +0200948 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100949
Radek Krejci65ef6d52018-08-16 16:35:02 +0200950 /* 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 +0200951 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200952
Radek Krejci65ef6d52018-08-16 16:35:02 +0200953 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
954 * schemas only to those present on the server side */
955 ly_ctx_set_disable_searchdirs(session->ctx);
956
957 /* our callback is set later with appropriate data */
958 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
959
960 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200961 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200962 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200963 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200964 if (yanglib_support) {
965 break;
966 }
967 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-yang-library?", 46)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200968 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200969 if (get_schema_support) {
970 break;
971 }
Michal Vasko086311b2016-01-08 09:53:11 +0100972 }
973 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200974
975 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200976 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
977 ERR("Session %u: unable to get server's schema information from the <hello>'s capabilities.", session->id);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200978 goto cleanup;
979 }
980
Michal Vasko086311b2016-01-08 09:53:11 +0100981 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200982 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200983 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
984 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200985 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100986 }
987 }
988
989 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200990 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200991 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100992 }
993
Radek Krejci65ef6d52018-08-16 16:35:02 +0200994 /* get correct version of ietf-yang-library into context */
995 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200996 /* use get schema to get server's ietf-yang-library */
997 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
998 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200999 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
1000 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001001 yanglib_support = 0;
1002 } else {
1003 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001004 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
1005 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 +02001006 yanglib_support = 0;
1007 }
1008 free(revision);
1009 }
1010 }
1011
Radek Krejci65ef6d52018-08-16 16:35:02 +02001012 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001013 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001014 if (build_schema_info_yl(session, &sm)) {
1015 goto cleanup;
1016 } else if (!sm) {
1017 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
1018 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001019 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
1020 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001021 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001022 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001023 }
Michal Vaskoef578332016-01-25 13:20:09 +01001024
Radek Krejci65ef6d52018-08-16 16:35:02 +02001025 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1026 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001027 }
1028
Radek Krejcifd5b6682017-06-13 15:52:53 +02001029 /* succsess */
1030 ret = 0;
1031
Michal Vaskoeee99412016-11-21 10:19:43 +01001032 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001033 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 +01001034 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001035
1036cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001037 free_schema_info(server_modules);
1038
Radek Krejcifd5b6682017-06-13 15:52:53 +02001039 /* set user callback back */
1040 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001041 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001042
Michal Vaskoef578332016-01-25 13:20:09 +01001043 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001044}
1045
1046API struct nc_session *
1047nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1048{
Michal Vaskod083db62016-01-19 10:31:29 +01001049 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001050
Michal Vasko45e53ae2016-04-07 11:46:03 +02001051 if (fdin < 0) {
1052 ERRARG("fdin");
1053 return NULL;
1054 } else if (fdout < 0) {
1055 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001056 return NULL;
1057 }
1058
1059 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001060 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001061 if (!session) {
1062 ERRMEM;
1063 return NULL;
1064 }
1065 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001066
1067 /* transport specific data */
1068 session->ti_type = NC_TI_FD;
1069 session->ti.fd.in = fdin;
1070 session->ti.fd.out = fdout;
1071
1072 /* assign context (dicionary needed for handshake) */
1073 if (!ctx) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001074 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001075 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001076 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001077 /* that's just it */
1078 goto fail;
1079 }
Michal Vasko086311b2016-01-08 09:53:11 +01001080 } else {
1081 session->flags |= NC_SESSION_SHAREDCTX;
1082 }
1083 session->ctx = ctx;
1084
1085 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001086 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001087 goto fail;
1088 }
1089 session->status = NC_STATUS_RUNNING;
1090
Michal Vaskoef578332016-01-25 13:20:09 +01001091 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001092 goto fail;
1093 }
1094
1095 return session;
1096
1097fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001098 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001099 return NULL;
1100}
1101
Frank Rimpler9f838b02018-07-25 06:44:03 +00001102/*
1103 Helper for a non-blocking connect (which is required because of the locking
1104 concept for e.g. call home settings). For more details see nc_sock_connect().
1105 */
1106static int
1107_non_blocking_connect(int timeout, int* sock_pending, struct addrinfo *res)
Michal Vasko086311b2016-01-08 09:53:11 +01001108{
Frank Rimplerc7a2acb2018-08-06 07:31:08 +00001109 int flags, ret=0;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001110 int sock = -1;
1111 fd_set wset;
1112 struct timeval ts;
1113 int error = 0;
1114 socklen_t len = sizeof(int);
Michal Vasko086311b2016-01-08 09:53:11 +01001115
Frank Rimpler9f838b02018-07-25 06:44:03 +00001116 if (sock_pending && *sock_pending != -1) {
1117 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1118 sock = *sock_pending;
1119 } else {
1120 assert(res);
1121 VRB("Trying to connect via %s.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1122 /* Connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001123 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1124 if (sock == -1) {
Frank Rimpler9f838b02018-07-25 06:44:03 +00001125 ERR("socket couldn't be created.", strerror(errno));
1126 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001127 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001128 /* make the socket non-blocking */
1129 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
1130 ERR("Fcntl failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001131 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001132 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001133 /* non-blocking connect! */
1134 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1135 if (errno != EINPROGRESS) {
1136 /* network connection failed, try another resource */
1137 ERR("connect failed: (%s).", strerror(errno));
1138 goto cleanup;
1139 }
1140 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001141 }
1142 ts.tv_sec = timeout;
1143 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001144
Frank Rimpler9f838b02018-07-25 06:44:03 +00001145 FD_ZERO(&wset);
1146 FD_SET(sock, &wset);
1147
1148 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
1149 ERR("select failed: (%s).", strerror(errno));
1150 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001151 }
1152
Frank Rimpler9f838b02018-07-25 06:44:03 +00001153 if (ret == 0) { //we had a timeout
1154 VRB("timed out after %ds (%s).", timeout, strerror(errno));
1155 if (sock_pending) {
1156 /* no sock-close, we'll try it again */
1157 *sock_pending = sock;
1158 } else {
1159 close(sock);
1160 }
1161 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001162 }
Radek Krejci782041a2018-08-20 10:09:45 +02001163
1164 /* check the usability of the socket */
1165 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1166 ERR("getsockopt failed: (%s).", strerror(errno));
1167 goto cleanup;
1168 }
1169 if (error == ECONNREFUSED) {
1170 /* network connection failed, try another resource */
1171 VRB("getsockopt error: (%s).", strerror(error));
1172 errno = error;
1173 goto cleanup;
1174 }
Michal Vasko086311b2016-01-08 09:53:11 +01001175 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001176
Frank Rimpler9f838b02018-07-25 06:44:03 +00001177cleanup:
1178 if (sock_pending) {
1179 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001180 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001181 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001182 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001183}
1184
Frank Rimpler9f838b02018-07-25 06:44:03 +00001185/* A given timeout value limits the time how long the function blocks. If it has to block
1186 only for some seconds, a socket connection might not yet have been fully established.
1187 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1188 value will be -1. In such a case a subsequent invokation is required, by providing the
1189 stored sock_pending, again.
1190 In general, if this function returns -1, when a timeout has been given, this function
1191 has to be invoked, until it returns a valid socket.
1192 */
1193int
1194nc_sock_connect(const char* host, uint16_t port, int timeout, int* sock_pending)
1195{
1196 int i;
1197 int sock = sock_pending?*sock_pending:-1;
1198 struct addrinfo hints, *res_list, *res;
1199 char port_s[6]; /* length of string representation of short int */
1200
1201 VRB("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
1202
1203 /* no pending socket */
1204 if (sock == -1) {
1205 /* Connect to a server */
1206 snprintf(port_s, 6, "%u", port);
1207 memset(&hints, 0, sizeof hints);
1208 hints.ai_family = AF_UNSPEC;
1209 hints.ai_socktype = SOCK_STREAM;
1210 hints.ai_protocol = IPPROTO_TCP;
1211 i = getaddrinfo(host, port_s, &hints, &res_list);
1212 if (i != 0) {
1213 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1214 return -1;
1215 }
1216
1217 for (res = res_list; res != NULL; res = res->ai_next) {
1218 sock = _non_blocking_connect(timeout, sock_pending, res);
1219 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1220 /* try the next resource */
1221 continue;
1222 }
1223 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1224 break;
1225 }
1226 freeaddrinfo(res_list);
1227
1228 } else {
1229 /* try to get a connection with the pending socket */
1230 assert(sock_pending);
1231 sock = _non_blocking_connect(timeout, sock_pending, NULL);
1232 }
1233
1234 return sock;
1235}
1236
Michal Vasko086311b2016-01-08 09:53:11 +01001237static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001238get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001239{
Michal Vasko086311b2016-01-08 09:53:11 +01001240 char *ptr;
1241 const char *str_msgid;
1242 uint64_t cur_msgid;
1243 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001244 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001245 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1246
Michal Vasko086311b2016-01-08 09:53:11 +01001247 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001248 if (!msgid && session->opts.client.notifs) {
1249 cont = session->opts.client.notifs;
1250 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001251
Michal Vasko71ba2da2016-05-04 10:53:16 +02001252 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001253 free(cont);
1254
Michal Vasko71ba2da2016-05-04 10:53:16 +02001255 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001256 }
1257
1258 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001259 if (msgid && session->opts.client.replies) {
1260 cont = session->opts.client.replies;
1261 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001262
Michal Vasko71ba2da2016-05-04 10:53:16 +02001263 xml = cont->msg;
1264 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001265
Michal Vasko71ba2da2016-05-04 10:53:16 +02001266 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001267 }
1268
Michal Vasko71ba2da2016-05-04 10:53:16 +02001269 if (!msgtype) {
1270 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001271 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001272 }
Michal Vasko086311b2016-01-08 09:53:11 +01001273
1274 /* we read rpc-reply, want a notif */
1275 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001276 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001277 while (*cont_ptr) {
1278 cont_ptr = &((*cont_ptr)->next);
1279 }
1280 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001281 if (!*cont_ptr) {
1282 ERRMEM;
1283 lyxml_free(session->ctx, xml);
1284 return NC_MSG_ERROR;
1285 }
Michal Vasko086311b2016-01-08 09:53:11 +01001286 (*cont_ptr)->msg = xml;
1287 (*cont_ptr)->next = NULL;
1288 }
1289
1290 /* we read notif, want a rpc-reply */
1291 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001292 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001293 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001294 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001295 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001296 }
Michal Vasko086311b2016-01-08 09:53:11 +01001297
Michal Vasko2e6defd2016-10-07 15:48:15 +02001298 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001299 while (*cont_ptr) {
1300 cont_ptr = &((*cont_ptr)->next);
1301 }
1302 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001303 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001304 ERRMEM;
1305 lyxml_free(session->ctx, xml);
1306 return NC_MSG_ERROR;
1307 }
Michal Vasko086311b2016-01-08 09:53:11 +01001308 (*cont_ptr)->msg = xml;
1309 (*cont_ptr)->next = NULL;
1310 }
1311
Michal Vasko086311b2016-01-08 09:53:11 +01001312 switch (msgtype) {
1313 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001314 if (!msgid) {
1315 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001316 }
Michal Vasko086311b2016-01-08 09:53:11 +01001317 break;
1318
1319 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001320 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001321 /* check message-id */
1322 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1323 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001324 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001325 } else {
1326 cur_msgid = strtoul(str_msgid, &ptr, 10);
1327 if (cur_msgid != msgid) {
1328 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1329 session->id, str_msgid);
1330 msgtype = NC_MSG_REPLY_ERR_MSGID;
1331 }
1332 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001333 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001334 }
Michal Vasko086311b2016-01-08 09:53:11 +01001335 break;
1336
1337 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001338 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001339 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001340 msgtype = NC_MSG_ERROR;
1341 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001342
1343 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001344 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001345 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001346 msgtype = NC_MSG_ERROR;
1347 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001348
1349 default:
1350 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1351 * NC_MSG_NONE is not returned by nc_read_msg()
1352 */
1353 break;
1354 }
1355
1356 return msgtype;
1357}
1358
1359/* cannot strictly fail, but does not need to fill any error parameter at all */
1360static void
1361parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1362{
1363 struct lyxml_elem *iter, *next, *info;
1364
1365 LY_TREE_FOR(xml->child, iter) {
1366 if (!iter->ns) {
1367 if (iter->content) {
1368 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1369 } else {
1370 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1371 }
1372 continue;
1373 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1374 if (iter->content) {
1375 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1376 iter->name, iter->content, iter->ns->value);
1377 } else {
1378 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1379 }
1380 continue;
1381 }
1382
1383 if (!strcmp(iter->name, "error-type")) {
1384 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1385 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1386 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1387 } else if (err->type) {
1388 WRN("<rpc-error> <error-type> duplicated.");
1389 } else {
1390 err->type = lydict_insert(ctx, iter->content, 0);
1391 }
1392 } else if (!strcmp(iter->name, "error-tag")) {
1393 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1394 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1395 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1396 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1397 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1398 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1399 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1400 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1401 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1402 && strcmp(iter->content, "malformed-message"))) {
1403 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1404 } else if (err->tag) {
1405 WRN("<rpc-error> <error-tag> duplicated.");
1406 } else {
1407 err->tag = lydict_insert(ctx, iter->content, 0);
1408 }
1409 } else if (!strcmp(iter->name, "error-severity")) {
1410 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1411 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1412 } else if (err->severity) {
1413 WRN("<rpc-error> <error-severity> duplicated.");
1414 } else {
1415 err->severity = lydict_insert(ctx, iter->content, 0);
1416 }
1417 } else if (!strcmp(iter->name, "error-app-tag")) {
1418 if (err->apptag) {
1419 WRN("<rpc-error> <error-app-tag> duplicated.");
1420 } else {
1421 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1422 }
1423 } else if (!strcmp(iter->name, "error-path")) {
1424 if (err->path) {
1425 WRN("<rpc-error> <error-path> duplicated.");
1426 } else {
1427 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1428 }
1429 } else if (!strcmp(iter->name, "error-message")) {
1430 if (err->message) {
1431 WRN("<rpc-error> <error-message> duplicated.");
1432 } else {
1433 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001434 if (err->message_lang) {
1435 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1436 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001437 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1438 }
1439 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1440 }
1441 } else if (!strcmp(iter->name, "error-info")) {
1442 LY_TREE_FOR_SAFE(iter->child, next, info) {
1443 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1444 if (!strcmp(info->name, "session-id")) {
1445 if (err->sid) {
1446 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1447 } else {
1448 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1449 }
Michal Vasko630485f2018-01-03 14:28:32 +01001450 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001451 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001452 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1453 if (!err->attr) {
1454 ERRMEM;
1455 return;
1456 }
Michal Vasko086311b2016-01-08 09:53:11 +01001457 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1458 } else if (!strcmp(info->name, "bad-element")) {
1459 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001460 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1461 if (!err->elem) {
1462 ERRMEM;
1463 return;
1464 }
Michal Vasko086311b2016-01-08 09:53:11 +01001465 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1466 } else if (!strcmp(info->name, "bad-namespace")) {
1467 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001468 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1469 if (!err->ns) {
1470 ERRMEM;
1471 return;
1472 }
Michal Vasko086311b2016-01-08 09:53:11 +01001473 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1474 } else {
1475 if (info->content) {
1476 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1477 info->name, info->content);
1478 } else {
1479 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1480 }
1481 }
1482 } else {
1483 lyxml_unlink(ctx, info);
1484 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001485 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1486 if (!err->other) {
1487 ERRMEM;
1488 return;
1489 }
Michal Vasko086311b2016-01-08 09:53:11 +01001490 err->other[err->other_count - 1] = info;
1491 }
1492 }
1493 } else {
1494 if (iter->content) {
1495 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1496 } else {
1497 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1498 }
1499 }
1500 }
1501}
1502
1503static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001504parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001505{
1506 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001507 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001508 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001509 struct nc_reply_data *data_rpl;
1510 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001511 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001512 int i;
1513
1514 if (!xml->child) {
1515 ERR("An empty <rpc-reply>.");
1516 return NULL;
1517 }
1518
1519 /* rpc-error */
1520 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1521 /* count and check elements */
1522 i = 0;
1523 LY_TREE_FOR(xml->child, iter) {
1524 if (strcmp(iter->name, "rpc-error")) {
1525 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1526 return NULL;
1527 } else if (!iter->ns) {
1528 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1529 return NULL;
1530 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1531 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1532 return NULL;
1533 }
1534 ++i;
1535 }
1536
1537 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001538 if (!error_rpl) {
1539 ERRMEM;
1540 return NULL;
1541 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001542 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001543 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001544 if (!error_rpl->err) {
1545 ERRMEM;
1546 free(error_rpl);
1547 return NULL;
1548 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001549 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001550 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001551 reply = (struct nc_reply *)error_rpl;
1552
1553 i = 0;
1554 LY_TREE_FOR(xml->child, iter) {
1555 parse_rpc_error(ctx, iter, error_rpl->err + i);
1556 ++i;
1557 }
1558
1559 /* ok */
1560 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1561 if (xml->child->next) {
1562 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1563 return NULL;
1564 }
1565 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001566 if (!reply) {
1567 ERRMEM;
1568 return NULL;
1569 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001570 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001571
1572 /* some RPC output */
1573 } else {
1574 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001575 case NC_RPC_ACT_GENERIC:
1576 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001577
1578 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001579 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1580 if (!rpc_act) {
1581 ERR("Failed to duplicate a generic RPC/action.");
1582 return NULL;
1583 }
Michal Vasko086311b2016-01-08 09:53:11 +01001584 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001585 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 +02001586 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001587 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001588 return NULL;
1589 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001590 }
Michal Vasko086311b2016-01-08 09:53:11 +01001591 break;
1592
1593 case NC_RPC_GETCONFIG:
1594 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001595 if (!xml->child->child) {
1596 /* we did not receive any data */
1597 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001598 if (!data_rpl) {
1599 ERRMEM;
1600 return NULL;
1601 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001602 data_rpl->type = NC_RPL_DATA;
1603 data_rpl->data = NULL;
1604 return (struct nc_reply *)data_rpl;
1605 }
1606
Michal Vasko086311b2016-01-08 09:53:11 +01001607 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001608 data = lyd_parse_xml(ctx, &xml->child->child,
1609 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001610 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001611 if (!data) {
1612 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1613 return NULL;
1614 }
1615 break;
1616
1617 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001618 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001619 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001620 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001621 return NULL;
1622 }
1623 break;
1624
1625 case NC_RPC_EDIT:
1626 case NC_RPC_COPY:
1627 case NC_RPC_DELETE:
1628 case NC_RPC_LOCK:
1629 case NC_RPC_UNLOCK:
1630 case NC_RPC_KILL:
1631 case NC_RPC_COMMIT:
1632 case NC_RPC_DISCARD:
1633 case NC_RPC_CANCEL:
1634 case NC_RPC_VALIDATE:
1635 case NC_RPC_SUBSCRIBE:
1636 /* there is no output defined */
1637 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1638 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001639 default:
1640 ERRINT;
1641 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001642 }
1643
1644 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001645 if (!data_rpl) {
1646 ERRMEM;
1647 return NULL;
1648 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001649 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001650 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001651 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001652 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001653 } else {
1654 /* <get>, <get-config> */
1655 data_rpl->data = data;
1656 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001657 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001658 if (!data_rpl->data) {
1659 ERR("Failed to parse <rpc-reply>.");
1660 free(data_rpl);
1661 return NULL;
1662 }
1663 reply = (struct nc_reply *)data_rpl;
1664 }
1665
1666 return reply;
1667}
1668
Radek Krejci53691be2016-02-22 13:58:37 +01001669#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001670
Michal Vasko3031aae2016-01-27 16:07:18 +01001671int
1672nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1673{
1674 int sock;
1675
Michal Vasko45e53ae2016-04-07 11:46:03 +02001676 if (!address) {
1677 ERRARG("address");
1678 return -1;
1679 } else if (!port) {
1680 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001681 return -1;
1682 }
1683
1684 sock = nc_sock_listen(address, port);
1685 if (sock == -1) {
1686 return -1;
1687 }
1688
1689 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001690 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1691 if (!client_opts.ch_binds) {
1692 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001693 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001694 return -1;
1695 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001696
Michal Vasko2e6defd2016-10-07 15:48:15 +02001697 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1698 if (!client_opts.ch_bind_ti) {
1699 ERRMEM;
1700 close(sock);
1701 return -1;
1702 }
1703 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1704
Michal Vasko3031aae2016-01-27 16:07:18 +01001705 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001706 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1707 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001708 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001709 return -1;
1710 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001711 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1712 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001713 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001714
1715 return 0;
1716}
1717
1718int
1719nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1720{
1721 uint32_t i;
1722 int ret = -1;
1723
1724 if (!address && !port && !ti) {
1725 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1726 close(client_opts.ch_binds[i].sock);
1727 free((char *)client_opts.ch_binds[i].address);
1728
1729 ret = 0;
1730 }
1731 free(client_opts.ch_binds);
1732 client_opts.ch_binds = NULL;
1733 client_opts.ch_bind_count = 0;
1734 } else {
1735 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1736 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1737 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001738 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001739 close(client_opts.ch_binds[i].sock);
1740 free((char *)client_opts.ch_binds[i].address);
1741
1742 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001743 if (!client_opts.ch_bind_count) {
1744 free(client_opts.ch_binds);
1745 client_opts.ch_binds = NULL;
1746 } else if (i < client_opts.ch_bind_count) {
1747 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1748 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1749 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001750
1751 ret = 0;
1752 }
1753 }
1754 }
1755
1756 return ret;
1757}
1758
1759API int
1760nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1761{
1762 int sock;
1763 char *host = NULL;
1764 uint16_t port, idx;
1765
Michal Vasko45e53ae2016-04-07 11:46:03 +02001766 if (!client_opts.ch_binds) {
1767 ERRINIT;
1768 return -1;
1769 } else if (!session) {
1770 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001771 return -1;
1772 }
1773
1774 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1775
Michal Vasko50456e82016-02-02 12:16:08 +01001776 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001777 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001778 return sock;
1779 }
1780
Radek Krejci53691be2016-02-22 13:58:37 +01001781#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001782 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001783 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001784 } else
1785#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001786#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001787 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001788 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001789 } else
1790#endif
1791 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001792 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001793 *session = NULL;
1794 }
1795
1796 free(host);
1797
1798 if (!(*session)) {
1799 return -1;
1800 }
1801
1802 return 1;
1803}
1804
Radek Krejci53691be2016-02-22 13:58:37 +01001805#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001806
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001807API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001808nc_session_get_cpblts(const struct nc_session *session)
1809{
1810 if (!session) {
1811 ERRARG("session");
1812 return NULL;
1813 }
1814
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001815 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001816}
1817
1818API const char *
1819nc_session_cpblt(const struct nc_session *session, const char *capab)
1820{
1821 int i, len;
1822
1823 if (!session) {
1824 ERRARG("session");
1825 return NULL;
1826 } else if (!capab) {
1827 ERRARG("capab");
1828 return NULL;
1829 }
1830
1831 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001832 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1833 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1834 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001835 }
1836 }
1837
1838 return NULL;
1839}
1840
Michal Vasko9cd26a82016-05-31 08:58:48 +02001841API int
1842nc_session_ntf_thread_running(const struct nc_session *session)
1843{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001844 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001845 ERRARG("session");
1846 return 0;
1847 }
1848
Michal Vasko2e6defd2016-10-07 15:48:15 +02001849 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001850}
1851
Michal Vaskob7558c52016-02-26 15:04:19 +01001852API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001853nc_client_init(void)
1854{
1855 nc_init();
1856}
1857
1858API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001859nc_client_destroy(void)
1860{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001861 nc_client_set_schema_searchpath(NULL);
1862#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1863 nc_client_ch_del_bind(NULL, 0, 0);
1864#endif
1865#ifdef NC_ENABLED_SSH
1866 nc_client_ssh_destroy_opts();
1867#endif
1868#ifdef NC_ENABLED_TLS
1869 nc_client_tls_destroy_opts();
1870#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001871 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001872}
1873
Michal Vasko086311b2016-01-08 09:53:11 +01001874API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001875nc_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 +01001876{
1877 struct lyxml_elem *xml;
1878 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1879
Michal Vasko45e53ae2016-04-07 11:46:03 +02001880 if (!session) {
1881 ERRARG("session");
1882 return NC_MSG_ERROR;
1883 } else if (!rpc) {
1884 ERRARG("rpc");
1885 return NC_MSG_ERROR;
1886 } else if (!reply) {
1887 ERRARG("reply");
1888 return NC_MSG_ERROR;
1889 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1890 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001891 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001892 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001893 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001894 return NC_MSG_ERROR;
1895 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001896 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001897 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1898 parseroptions &= LYD_OPT_STRICT;
1899 }
Michal Vasko41adf392017-01-17 10:39:04 +01001900 /* no mechanism to check external dependencies is provided */
1901 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001902 *reply = NULL;
1903
1904 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001905
Michal Vasko71ba2da2016-05-04 10:53:16 +02001906 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001907 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001908 lyxml_free(session->ctx, xml);
1909 if (!(*reply)) {
1910 return NC_MSG_ERROR;
1911 }
1912 }
1913
1914 return msgtype;
1915}
1916
1917API NC_MSG_TYPE
1918nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1919{
1920 struct lyxml_elem *xml, *ev_time;
1921 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1922
Michal Vasko45e53ae2016-04-07 11:46:03 +02001923 if (!session) {
1924 ERRARG("session");
1925 return NC_MSG_ERROR;
1926 } else if (!notif) {
1927 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001928 return NC_MSG_ERROR;
1929 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001930 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001931 return NC_MSG_ERROR;
1932 }
1933
1934 msgtype = get_msg(session, timeout, 0, &xml);
1935
1936 if (msgtype == NC_MSG_NOTIF) {
1937 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001938 if (!*notif) {
1939 ERRMEM;
1940 lyxml_free(session->ctx, xml);
1941 return NC_MSG_ERROR;
1942 }
Michal Vasko086311b2016-01-08 09:53:11 +01001943
1944 /* eventTime */
1945 LY_TREE_FOR(xml->child, ev_time) {
1946 if (!strcmp(ev_time->name, "eventTime")) {
1947 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1948 /* lyd_parse does not know this element */
1949 lyxml_free(session->ctx, ev_time);
1950 break;
1951 }
1952 }
1953 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001954 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001955 goto fail;
1956 }
1957
1958 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001959 (*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 +01001960 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001961 lyxml_free(session->ctx, xml);
1962 xml = NULL;
1963 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001964 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001965 goto fail;
1966 }
1967 }
1968
1969 return msgtype;
1970
1971fail:
1972 lydict_remove(session->ctx, (*notif)->datetime);
1973 lyd_free((*notif)->tree);
1974 free(*notif);
1975 *notif = NULL;
1976 lyxml_free(session->ctx, xml);
1977
1978 return NC_MSG_ERROR;
1979}
1980
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001981static void *
1982nc_recv_notif_thread(void *arg)
1983{
1984 struct nc_ntf_thread_arg *ntarg;
1985 struct nc_session *session;
1986 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1987 struct nc_notif *notif;
1988 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02001989 pthread_t *ntf_tid;
1990
1991 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001992
1993 ntarg = (struct nc_ntf_thread_arg *)arg;
1994 session = ntarg->session;
1995 notif_clb = ntarg->notif_clb;
1996 free(ntarg);
1997
Michal Vasko131120a2018-05-29 15:44:02 +02001998 /* remember our allocated tid, we will be freeing it */
1999 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
2000
Michal Vasko2e6defd2016-10-07 15:48:15 +02002001 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002002 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002003 if (msgtype == NC_MSG_NOTIF) {
2004 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002005 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2006 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2007 nc_notif_free(notif);
2008 break;
2009 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002010 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002011 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002012 /* quit this thread once the session is broken */
2013 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002014 }
2015
2016 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2017 }
2018
Michal Vasko0651c902016-05-19 15:55:42 +02002019 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002020 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02002021 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002022 return NULL;
2023}
2024
2025API int
2026nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2027{
2028 struct nc_ntf_thread_arg *ntarg;
2029 int ret;
2030
Michal Vasko45e53ae2016-04-07 11:46:03 +02002031 if (!session) {
2032 ERRARG("session");
2033 return -1;
2034 } else if (!notif_clb) {
2035 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002036 return -1;
2037 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2038 ERR("Session %u: invalid session to receive Notifications.", session->id);
2039 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002040 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002041 ERR("Session %u: separate notification thread is already running.", session->id);
2042 return -1;
2043 }
2044
2045 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002046 if (!ntarg) {
2047 ERRMEM;
2048 return -1;
2049 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002050 ntarg->session = session;
2051 ntarg->notif_clb = notif_clb;
2052
2053 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002054 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2055 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002056 ERRMEM;
2057 free(ntarg);
2058 return -1;
2059 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002060
Michal Vasko2e6defd2016-10-07 15:48:15 +02002061 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002062 if (ret) {
2063 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2064 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002065 free((pthread_t *)session->opts.client.ntf_tid);
2066 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002067 return -1;
2068 }
2069
2070 return 0;
2071}
2072
Michal Vasko086311b2016-01-08 09:53:11 +01002073API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002074nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002075{
2076 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002077 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002078 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002079 struct nc_rpc_getconfig *rpc_gc;
2080 struct nc_rpc_edit *rpc_e;
2081 struct nc_rpc_copy *rpc_cp;
2082 struct nc_rpc_delete *rpc_del;
2083 struct nc_rpc_lock *rpc_lock;
2084 struct nc_rpc_get *rpc_g;
2085 struct nc_rpc_kill *rpc_k;
2086 struct nc_rpc_commit *rpc_com;
2087 struct nc_rpc_cancel *rpc_can;
2088 struct nc_rpc_validate *rpc_val;
2089 struct nc_rpc_getschema *rpc_gs;
2090 struct nc_rpc_subscribe *rpc_sub;
2091 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01002092 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02002093 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002094 uint64_t cur_msgid;
2095
Michal Vasko45e53ae2016-04-07 11:46:03 +02002096 if (!session) {
2097 ERRARG("session");
2098 return NC_MSG_ERROR;
2099 } else if (!rpc) {
2100 ERRARG("rpc");
2101 return NC_MSG_ERROR;
2102 } else if (!msgid) {
2103 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002104 return NC_MSG_ERROR;
2105 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002106 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002107 return NC_MSG_ERROR;
2108 }
2109
Michal Vasko90e8e692016-07-13 12:27:57 +02002110 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002111 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002112 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01002113 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002114 return NC_MSG_ERROR;
2115 }
2116 }
2117
2118 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002119 case NC_RPC_ACT_GENERIC:
2120 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002121
2122 if (rpc_gen->has_data) {
2123 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002124 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002125 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002126 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 +01002127 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002128 if (!data) {
2129 return NC_MSG_ERROR;
2130 }
Michal Vasko086311b2016-01-08 09:53:11 +01002131 }
2132 break;
2133
2134 case NC_RPC_GETCONFIG:
2135 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2136
2137 data = lyd_new(NULL, ietfnc, "get-config");
2138 node = lyd_new(data, ietfnc, "source");
2139 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
2140 if (!node) {
2141 lyd_free(data);
2142 return NC_MSG_ERROR;
2143 }
2144 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002145 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002146 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002147 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002148 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002149 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002150 lyd_insert_attr(node, NULL, "type", "xpath");
2151 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002152 }
2153 if (!node) {
2154 lyd_free(data);
2155 return NC_MSG_ERROR;
2156 }
2157 }
2158
2159 if (rpc_gc->wd_mode) {
2160 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002161 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002162 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002163 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002164 return NC_MSG_ERROR;
2165 }
2166 }
2167 switch (rpc_gc->wd_mode) {
2168 case NC_WD_UNKNOWN:
2169 /* cannot get here */
2170 break;
2171 case NC_WD_ALL:
2172 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2173 break;
2174 case NC_WD_ALL_TAG:
2175 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2176 break;
2177 case NC_WD_TRIM:
2178 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2179 break;
2180 case NC_WD_EXPLICIT:
2181 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2182 break;
2183 }
2184 if (!node) {
2185 lyd_free(data);
2186 return NC_MSG_ERROR;
2187 }
2188 }
2189 break;
2190
2191 case NC_RPC_EDIT:
2192 rpc_e = (struct nc_rpc_edit *)rpc;
2193
2194 data = lyd_new(NULL, ietfnc, "edit-config");
2195 node = lyd_new(data, ietfnc, "target");
2196 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
2197 if (!node) {
2198 lyd_free(data);
2199 return NC_MSG_ERROR;
2200 }
2201
2202 if (rpc_e->default_op) {
2203 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
2204 if (!node) {
2205 lyd_free(data);
2206 return NC_MSG_ERROR;
2207 }
2208 }
2209
2210 if (rpc_e->test_opt) {
2211 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
2212 if (!node) {
2213 lyd_free(data);
2214 return NC_MSG_ERROR;
2215 }
2216 }
2217
2218 if (rpc_e->error_opt) {
2219 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
2220 if (!node) {
2221 lyd_free(data);
2222 return NC_MSG_ERROR;
2223 }
2224 }
2225
Michal Vasko7793bc62016-09-16 11:58:41 +02002226 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002227 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002228 } else {
2229 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
2230 }
2231 if (!node) {
2232 lyd_free(data);
2233 return NC_MSG_ERROR;
2234 }
2235 break;
2236
2237 case NC_RPC_COPY:
2238 rpc_cp = (struct nc_rpc_copy *)rpc;
2239
2240 data = lyd_new(NULL, ietfnc, "copy-config");
2241 node = lyd_new(data, ietfnc, "target");
2242 if (rpc_cp->url_trg) {
2243 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
2244 } else {
2245 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
2246 }
2247 if (!node) {
2248 lyd_free(data);
2249 return NC_MSG_ERROR;
2250 }
2251
2252 node = lyd_new(data, ietfnc, "source");
2253 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002254 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002255 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002256 } else {
2257 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
2258 }
2259 } else {
2260 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
2261 }
2262 if (!node) {
2263 lyd_free(data);
2264 return NC_MSG_ERROR;
2265 }
2266
2267 if (rpc_cp->wd_mode) {
2268 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002269 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002270 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002271 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002272 return NC_MSG_ERROR;
2273 }
2274 }
2275 switch (rpc_cp->wd_mode) {
2276 case NC_WD_UNKNOWN:
2277 /* cannot get here */
2278 break;
2279 case NC_WD_ALL:
2280 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2281 break;
2282 case NC_WD_ALL_TAG:
2283 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2284 break;
2285 case NC_WD_TRIM:
2286 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2287 break;
2288 case NC_WD_EXPLICIT:
2289 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2290 break;
2291 }
2292 if (!node) {
2293 lyd_free(data);
2294 return NC_MSG_ERROR;
2295 }
2296 }
2297 break;
2298
2299 case NC_RPC_DELETE:
2300 rpc_del = (struct nc_rpc_delete *)rpc;
2301
2302 data = lyd_new(NULL, ietfnc, "delete-config");
2303 node = lyd_new(data, ietfnc, "target");
2304 if (rpc_del->url) {
2305 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2306 } else {
2307 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2308 }
2309 if (!node) {
2310 lyd_free(data);
2311 return NC_MSG_ERROR;
2312 }
2313 break;
2314
2315 case NC_RPC_LOCK:
2316 rpc_lock = (struct nc_rpc_lock *)rpc;
2317
2318 data = lyd_new(NULL, ietfnc, "lock");
2319 node = lyd_new(data, ietfnc, "target");
2320 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2321 if (!node) {
2322 lyd_free(data);
2323 return NC_MSG_ERROR;
2324 }
2325 break;
2326
2327 case NC_RPC_UNLOCK:
2328 rpc_lock = (struct nc_rpc_lock *)rpc;
2329
2330 data = lyd_new(NULL, ietfnc, "unlock");
2331 node = lyd_new(data, ietfnc, "target");
2332 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2333 if (!node) {
2334 lyd_free(data);
2335 return NC_MSG_ERROR;
2336 }
2337 break;
2338
2339 case NC_RPC_GET:
2340 rpc_g = (struct nc_rpc_get *)rpc;
2341
2342 data = lyd_new(NULL, ietfnc, "get");
2343 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002344 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002345 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002346 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002347 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002348 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002349 lyd_insert_attr(node, NULL, "type", "xpath");
2350 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002351 }
2352 if (!node) {
2353 lyd_free(data);
2354 return NC_MSG_ERROR;
2355 }
2356 }
2357
2358 if (rpc_g->wd_mode) {
2359 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002360 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002361 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002362 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002363 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002364 return NC_MSG_ERROR;
2365 }
2366 }
2367 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002368 case NC_WD_ALL:
2369 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2370 break;
2371 case NC_WD_ALL_TAG:
2372 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2373 break;
2374 case NC_WD_TRIM:
2375 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2376 break;
2377 case NC_WD_EXPLICIT:
2378 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2379 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002380 default:
2381 /* cannot get here */
2382 node = NULL;
2383 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002384 }
2385 if (!node) {
2386 lyd_free(data);
2387 return NC_MSG_ERROR;
2388 }
2389 }
2390 break;
2391
2392 case NC_RPC_KILL:
2393 rpc_k = (struct nc_rpc_kill *)rpc;
2394
2395 data = lyd_new(NULL, ietfnc, "kill-session");
2396 sprintf(str, "%u", rpc_k->sid);
2397 lyd_new_leaf(data, ietfnc, "session-id", str);
2398 break;
2399
2400 case NC_RPC_COMMIT:
2401 rpc_com = (struct nc_rpc_commit *)rpc;
2402
2403 data = lyd_new(NULL, ietfnc, "commit");
2404 if (rpc_com->confirmed) {
2405 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2406 }
2407
2408 if (rpc_com->confirm_timeout) {
2409 sprintf(str, "%u", rpc_com->confirm_timeout);
2410 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2411 }
2412
2413 if (rpc_com->persist) {
2414 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2415 if (!node) {
2416 lyd_free(data);
2417 return NC_MSG_ERROR;
2418 }
2419 }
2420
2421 if (rpc_com->persist_id) {
2422 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2423 if (!node) {
2424 lyd_free(data);
2425 return NC_MSG_ERROR;
2426 }
2427 }
2428 break;
2429
2430 case NC_RPC_DISCARD:
2431 data = lyd_new(NULL, ietfnc, "discard-changes");
2432 break;
2433
2434 case NC_RPC_CANCEL:
2435 rpc_can = (struct nc_rpc_cancel *)rpc;
2436
2437 data = lyd_new(NULL, ietfnc, "cancel-commit");
2438 if (rpc_can->persist_id) {
2439 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2440 if (!node) {
2441 lyd_free(data);
2442 return NC_MSG_ERROR;
2443 }
2444 }
2445 break;
2446
2447 case NC_RPC_VALIDATE:
2448 rpc_val = (struct nc_rpc_validate *)rpc;
2449
2450 data = lyd_new(NULL, ietfnc, "validate");
2451 node = lyd_new(data, ietfnc, "source");
2452 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002453 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002454 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002455 } else {
2456 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2457 }
2458 } else {
2459 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2460 }
2461 if (!node) {
2462 lyd_free(data);
2463 return NC_MSG_ERROR;
2464 }
2465 break;
2466
2467 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002468 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002469 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002470 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002471 return NC_MSG_ERROR;
2472 }
2473
2474 rpc_gs = (struct nc_rpc_getschema *)rpc;
2475
2476 data = lyd_new(NULL, ietfncmon, "get-schema");
2477 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2478 if (!node) {
2479 lyd_free(data);
2480 return NC_MSG_ERROR;
2481 }
2482 if (rpc_gs->version) {
2483 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2484 if (!node) {
2485 lyd_free(data);
2486 return NC_MSG_ERROR;
2487 }
2488 }
2489 if (rpc_gs->format) {
2490 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2491 if (!node) {
2492 lyd_free(data);
2493 return NC_MSG_ERROR;
2494 }
2495 }
2496 break;
2497
2498 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002499 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002500 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002501 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002502 return NC_MSG_ERROR;
2503 }
2504
2505 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2506
2507 data = lyd_new(NULL, notifs, "create-subscription");
2508 if (rpc_sub->stream) {
2509 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2510 if (!node) {
2511 lyd_free(data);
2512 return NC_MSG_ERROR;
2513 }
2514 }
2515
2516 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002517 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002518 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002519 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002520 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002521 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002522 lyd_insert_attr(node, NULL, "type", "xpath");
2523 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002524 }
2525 if (!node) {
2526 lyd_free(data);
2527 return NC_MSG_ERROR;
2528 }
2529 }
2530
2531 if (rpc_sub->start) {
2532 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2533 if (!node) {
2534 lyd_free(data);
2535 return NC_MSG_ERROR;
2536 }
2537 }
2538
2539 if (rpc_sub->stop) {
2540 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2541 if (!node) {
2542 lyd_free(data);
2543 return NC_MSG_ERROR;
2544 }
2545 }
2546 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002547 default:
2548 ERRINT;
2549 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002550 }
2551
Michal Vasko131120a2018-05-29 15:44:02 +02002552 if (!data) {
2553 /* error was already printed */
2554 return NC_MSG_ERROR;
2555 }
2556
Michal Vasko41adf392017-01-17 10:39:04 +01002557 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2558 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002559 if (dofree) {
2560 lyd_free(data);
2561 }
Michal Vasko086311b2016-01-08 09:53:11 +01002562 return NC_MSG_ERROR;
2563 }
2564
Michal Vasko131120a2018-05-29 15:44:02 +02002565 /* send RPC, store its message ID */
2566 r = nc_send_msg_io(session, timeout, data);
2567 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002568
Radek Krejcib4b19062018-02-07 16:33:06 +01002569 if (dofree) {
2570 lyd_free(data);
2571 }
Michal Vasko086311b2016-01-08 09:53:11 +01002572
Michal Vasko131120a2018-05-29 15:44:02 +02002573 if (r == NC_MSG_RPC) {
2574 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002575 }
Michal Vasko131120a2018-05-29 15:44:02 +02002576 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002577}
Michal Vaskode2946c2017-01-12 12:19:26 +01002578
2579API void
2580nc_client_session_set_not_strict(struct nc_session *session)
2581{
2582 if (session->side != NC_CLIENT) {
2583 ERRARG("session");
2584 return;
2585 }
2586
2587 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2588}