blob: bf9bfeb8c5d63de017385a2671688c6bf437b17a [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
Radek Krejcifd5b6682017-06-13 15:52:53 +020015#define _GNU_SOURCE
Michal Vasko086311b2016-01-08 09:53:11 +010016#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010020#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010021#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020026#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <sys/types.h>
28#include <unistd.h>
29#include <arpa/inet.h>
30#include <poll.h>
31
32#include <libyang/libyang.h>
33
Michal Vasko086311b2016-01-08 09:53:11 +010034#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010035#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010036#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010037
Michal Vasko80ef5d22016-01-18 09:21:02 +010038static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
39
Radek Krejci62aa0642017-05-25 16:33:49 +020040#ifdef NC_ENABLED_SSH
41int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
42char *sshauth_password(const char *username, const char *hostname, void *priv);
43char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
44char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
45#endif /* NC_ENABLED_SSH */
46
47static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
48static pthread_key_t nc_client_context_key;
49#ifdef __linux__
50static struct nc_client_context context_main = {
51 /* .opts zeroed */
52#ifdef NC_ENABLED_SSH
53 .ssh_opts = {
54 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
55 .auth_hostkey_check = sshauth_hostkey_check,
56 .auth_password = sshauth_password,
57 .auth_interactive = sshauth_interactive,
58 .auth_privkey_passphrase = sshauth_privkey_passphrase
59 },
60 .ssh_ch_opts = {
61 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
62 .auth_hostkey_check = sshauth_hostkey_check,
63 .auth_password = sshauth_password,
64 .auth_interactive = sshauth_interactive,
65 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020066 },
Radek Krejci62aa0642017-05-25 16:33:49 +020067#endif /* NC_ENABLED_SSH */
68 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020069 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020070};
71#endif
72
73static void
74nc_client_context_free(void *ptr)
75{
76 struct nc_client_context *c = (struct nc_client_context *)ptr;
77
Radek Krejci5cebc6b2017-05-26 13:24:38 +020078 if (--(c->refcount)) {
79 /* still used */
80 return;
81 }
82
Radek Krejci62aa0642017-05-25 16:33:49 +020083#ifdef __linux__
84 /* in __linux__ we use static memory in the main thread,
85 * so this check is for programs terminating the main()
86 * function by pthread_exit() :)
87 */
88 if (c != &context_main)
89#endif
90 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +020091 /* for the main thread the same is done in nc_client_destroy() */
Radek Krejci62aa0642017-05-25 16:33:49 +020092 nc_client_set_schema_searchpath(NULL);
93#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
94 nc_client_ch_del_bind(NULL, 0, 0);
95#endif
96#ifdef NC_ENABLED_SSH
97 nc_client_ssh_destroy_opts();
98#endif
99#ifdef NC_ENABLED_TLS
100 nc_client_tls_destroy_opts();
101#endif
102 free(c);
103 }
104}
105
106static void
107nc_client_context_createkey(void)
108{
109 int r;
110
111 /* initiate */
112 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
113 pthread_setspecific(nc_client_context_key, NULL);
114}
115
116struct nc_client_context *
117nc_client_context_location(void)
118{
119 struct nc_client_context *e;
120
121 pthread_once(&nc_client_context_once, nc_client_context_createkey);
122 e = pthread_getspecific(nc_client_context_key);
123 if (!e) {
124 /* prepare ly_err storage */
125#ifdef __linux__
126 if (getpid() == syscall(SYS_gettid)) {
127 /* main thread - use global variable instead of thread-specific variable. */
128 e = &context_main;
129 } else
130#endif /* __linux__ */
131 {
132 e = calloc(1, sizeof *e);
133 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200134 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200135#ifdef NC_ENABLED_SSH
136 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
137 e->ssh_opts.auth_pref[0].value = 3;
138 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
139 e->ssh_opts.auth_pref[1].value = 2;
140 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
141 e->ssh_opts.auth_pref[2].value = 1;
142 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
143 e->ssh_opts.auth_password = sshauth_password;
144 e->ssh_opts.auth_interactive = sshauth_interactive;
145 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
146
147 /* callhome settings are the same except the inverted auth methods preferences */
148 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
149 e->ssh_ch_opts.auth_pref[0].value = 1;
150 e->ssh_ch_opts.auth_pref[1].value = 2;
151 e->ssh_ch_opts.auth_pref[2].value = 3;
152#endif /* NC_ENABLED_SSH */
153 }
154 pthread_setspecific(nc_client_context_key, e);
155 }
156
157 return e;
158}
159
160#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100161
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162API void *
163nc_client_get_thread_context(void)
164{
165 return nc_client_context_location();
166}
167
168API void
169nc_client_set_thread_context(void *context)
170{
171 struct nc_client_context *old, *new;
172
173 if (!context) {
174 ERRARG(context);
175 return;
176 }
177
178 new = (struct nc_client_context *)context;
179 old = nc_client_context_location();
180 if (old == new) {
181 /* nothing to change */
182 return;
183 }
184
185 /* replace old by new, increase reference counter in the newly set context */
186 nc_client_context_free(old);
187 new->refcount++;
188 pthread_setspecific(nc_client_context_key, new);
189}
190
Radek Krejcifd5b6682017-06-13 15:52:53 +0200191int
192nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
193{
194 /* assign context (dicionary needed for handshake) */
195 if (!ctx) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200196 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200197 if (!ctx) {
198 return EXIT_FAILURE;
199 }
200
201 /* user path must be first, the first path is used to store schemas retreived via get-schema */
202 if (client_opts.schema_searchpath) {
203 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
204 }
Michal Vaskoff7e3562018-02-15 13:41:22 +0100205 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200206
207 /* set callback for getting schemas, if provided */
208 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
209 } else {
210 session->flags |= NC_SESSION_SHAREDCTX;
211 }
212
213 session->ctx = ctx;
214
215 return EXIT_SUCCESS;
216}
217
Michal Vasko086311b2016-01-08 09:53:11 +0100218API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100219nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100220{
Michal Vasko3031aae2016-01-27 16:07:18 +0100221 if (client_opts.schema_searchpath) {
222 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 }
Michal Vasko086311b2016-01-08 09:53:11 +0100224
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100225 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100226 client_opts.schema_searchpath = strdup(path);
227 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100228 ERRMEM;
229 return 1;
230 }
231 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100232 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100233 }
234
235 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100236}
237
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100238API const char *
239nc_client_get_schema_searchpath(void)
240{
241 return client_opts.schema_searchpath;
242}
243
Radek Krejcifd5b6682017-06-13 15:52:53 +0200244API int
245nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100246{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200247 client_opts.schema_clb = clb;
248 if (clb) {
249 client_opts.schema_clb_data = user_data;
250 } else {
251 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100252 }
253
254 return 0;
255}
256
Radek Krejcifd5b6682017-06-13 15:52:53 +0200257API ly_module_imp_clb
258nc_client_get_schema_callback(void **user_data)
259{
260 if (user_data) {
261 (*user_data) = client_opts.schema_clb_data;
262 }
263 return client_opts.schema_clb;
264}
265
Radek Krejci65ef6d52018-08-16 16:35:02 +0200266
267struct schema_info {
268 char *name;
269 char *revision;
270 struct {
271 char *name;
272 char *revision;
273 } *submodules;
274 struct ly_set features;
275 int implemented;
276};
277
278struct clb_data_s {
279 void *user_data;
280 ly_module_imp_clb user_clb;
281 struct schema_info *schemas;
282 struct nc_session *session;
283 int has_get_schema;
284};
285
286static char *
287retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
288 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100289{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200290 char *localfile = NULL;
291 FILE *f;
292 long length, l;
293 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100294
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200295 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
296 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
297 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200298 return NULL;
299 }
300 if (localfile) {
301 VRB("Session %u: reading schema from localfile \"%s\".", clb_data->session->id, localfile);
302 f = fopen(localfile, "r");
303 if (!f) {
304 ERR("Session %u: unable to open \"%s\" file to get schema (%s).",
305 clb_data->session->id, localfile, strerror(errno));
306 free(localfile);
307 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100308 }
Michal Vasko086311b2016-01-08 09:53:11 +0100309
Radek Krejci65ef6d52018-08-16 16:35:02 +0200310 fseek(f, 0, SEEK_END);
311 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200312 if (length < 0) {
313 ERR("Session %u: unable to get size of schema file \"%s\".",
314 clb_data->session->id, localfile);
315 free(localfile);
316 fclose(f);
317 return NULL;
318 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200319 fseek(f, 0, SEEK_SET);
320
321 model_data = malloc(length + 1);
322 if (!model_data) {
323 ERRMEM;
324 } else if ((l = fread(model_data, 1, length, f)) != length) {
325 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
326 clb_data->session->id, localfile, l, length);
327 free(model_data);
328 model_data = NULL;
329 } else {
330 /* terminating NULL byte */
331 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100332 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200333 fclose(f);
334 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100335 }
336
Radek Krejci65ef6d52018-08-16 16:35:02 +0200337 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100338}
339
340static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200341retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
342 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100343{
Michal Vasko086311b2016-01-08 09:53:11 +0100344 struct nc_rpc *rpc;
345 struct nc_reply *reply;
346 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200347 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200348 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100349 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100350 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200351 char *localfile = NULL;
352 FILE *f;
353 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100354
Radek Krejci65ef6d52018-08-16 16:35:02 +0200355 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
356 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100357
Radek Krejci65ef6d52018-08-16 16:35:02 +0200358 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100359 usleep(1000);
360 }
361 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200362 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100363 nc_rpc_free(rpc);
364 return NULL;
365 }
366
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200367 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200368 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200369 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100370 nc_rpc_free(rpc);
371 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200372 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100373 return NULL;
374 } else if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200375 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100376 return NULL;
377 }
378
Michal Vasko998ba412016-09-16 12:00:07 +0200379 switch (reply->type) {
380 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200381 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200382 nc_reply_free(reply);
383 return NULL;
384 case NC_RPL_DATA:
385 /* fine */
386 break;
387 case NC_RPL_ERROR:
388 error_rpl = (struct nc_reply_error *)reply;
389 if (error_rpl->count) {
390 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200391 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200392 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200393 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200394 }
395 nc_reply_free(reply);
396 return NULL;
397 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200398 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100399 nc_reply_free(reply);
400 return NULL;
401 }
402
Michal Vasko086311b2016-01-08 09:53:11 +0100403 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200404 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
405 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200406 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200407 nc_reply_free(reply);
408 return NULL;
409 }
Radek Krejci539efb62016-08-24 15:05:16 +0200410 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
411 switch (get_schema_data->value_type) {
412 case LYD_ANYDATA_CONSTSTRING:
413 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200414 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200415 break;
416 case LYD_ANYDATA_DATATREE:
417 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
418 break;
419 case LYD_ANYDATA_XML:
420 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
421 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200422 case LYD_ANYDATA_JSON:
423 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200424 case LYD_ANYDATA_SXML:
425 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200426 case LYD_ANYDATA_LYB:
427 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200428 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200429 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200430 }
Michal Vasko086311b2016-01-08 09:53:11 +0100431 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100432
Radek Krejci488b0702018-08-29 14:47:15 +0200433 if (model_data && !model_data[0]) {
434 /* empty data */
435 free(model_data);
436 model_data = NULL;
437 }
438
Radek Krejcifd5b6682017-06-13 15:52:53 +0200439 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200440 if (model_data) {
441 *format = LYS_IN_YANG;
442 if (client_opts.schema_searchpath) {
443 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
444 rev ? "@" : "", rev ? rev : "") == -1) {
445 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100446 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200447 f = fopen(localfile, "w");
448 if (!f) {
449 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
450 localfile, strerror(errno));
451 } else {
452 fputs(model_data, f);
453 fclose(f);
454 }
455 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100456 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200457 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200458 }
459
Michal Vasko086311b2016-01-08 09:53:11 +0100460 return model_data;
461}
462
Jan Kundrát35972df2018-09-06 19:00:01 +0200463static void free_with_user_data(void *data, void *user_data)
464{
465 free(data);
466 (void)user_data;
467}
468
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200469static const char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200470retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
Jan Kundrát35972df2018-09-06 19:00:01 +0200471 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200472{
473 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
474 unsigned int u, v, match = 1;
475 const char *name = NULL, *rev = NULL;
476 char *model_data = NULL;
477
478 /* get and check the final name and revision of the schema to be retrieved */
479 if (!mod_rev || !mod_rev[0]) {
480 /* newest revision requested - get the newest revision from the list of available modules on server */
481 match = 0;
482 for (u = 0; clb_data->schemas[u].name; ++u) {
483 if (strcmp(mod_name, clb_data->schemas[u].name)) {
484 continue;
485 }
486 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
487 mod_rev = clb_data->schemas[u].revision;
488 }
489 match = u + 1;
490 }
491 if (!match) {
492 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
493 clb_data->session->id, mod_name);
494 }
495 }
496 if (submod_name) {
497 name = submod_name;
498 if (sub_rev) {
499 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200500 } else if (match) {
501 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200502 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 +0200503 clb_data->session->id, submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200504 } else {
505 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
506 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
507 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
508 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200509 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200510 if (!rev) {
511 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
512 clb_data->session->id, submod_name, mod_name);
513 return NULL;
514 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200515 }
516 }
517 } else {
518 name = mod_name;
519 rev = mod_rev;
520 }
521
522 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
523
524 if (match) {
525 /* we have enough information to avoid communication with server and try to get
526 * the schema locally */
527
528 /* 1. try to get data locally */
529 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
530
531 /* 2. try to use <get-schema> */
532 if (!model_data && clb_data->has_get_schema) {
533 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
534 }
535 } else {
536 /* we are unsure which revision of the schema we should load, so first try to get
537 * the newest revision from the server via get-schema and only if the server does not
538 * implement get-schema, try to load the newest revision locally. This is imperfect
539 * solution, but there are situation when a client does not know what revision is
540 * actually implemented by the server. */
541
542 /* 1. try to use <get-schema> */
543 if (clb_data->has_get_schema) {
544 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
545 }
546
547 /* 2. try to get data locally */
548 if (!model_data) {
549 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
550 }
551 }
552
553 /* 3. try to use user callback */
554 if (!model_data && clb_data->user_clb) {
555 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
556 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
557 }
558
Jan Kundrát35972df2018-09-06 19:00:01 +0200559 *free_module_data = free_with_user_data;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200560 return model_data;
561}
562
Michal Vaskoceae0152018-02-14 16:03:59 +0100563static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200564nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200565 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 +0100566{
567 int ret = 0;
568 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200569 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200570 LYS_INFORMAT format;
Jan Kundrát35972df2018-09-06 19:00:01 +0200571 void (*free_module_data)(void*, void*) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200572 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100573
Radek Krejci65ef6d52018-08-16 16:35:02 +0200574 *mod = NULL;
575 if (revision) {
576 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
577 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100578 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200579 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100580 /* make the present module implemented */
581 if (lys_set_implemented(*mod)) {
582 ERR("Failed to implement model \"%s\".", (*mod)->name);
583 ret = -1;
584 }
585 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200586 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100587 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200588 clb_data.has_get_schema = has_get_schema;
589 clb_data.schemas = schemas;
590 clb_data.session = session;
591 clb_data.user_clb = user_clb;
592 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100593
594 /* clear all the errors and just collect them for now */
595 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200596 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100597
Radek Krejci65ef6d52018-08-16 16:35:02 +0200598 /* get module data */
599 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100600
Radek Krejci65ef6d52018-08-16 16:35:02 +0200601 if (module_data) {
602 /* parse the schema */
603 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100604
Radek Krejci65ef6d52018-08-16 16:35:02 +0200605 *mod = lys_parse_mem(session->ctx, module_data, format);
606 if (*free_module_data) {
Jan Kundrát35972df2018-09-06 19:00:01 +0200607 (*free_module_data)((char*)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100608 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100609
Radek Krejci65ef6d52018-08-16 16:35:02 +0200610 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
611 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100612
Michal Vaskodbf7c272018-02-19 09:07:59 +0100613 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200614 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100615 if (!(*mod)) {
616 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
617 ly_err_print(eitem);
618 }
619 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200620 } else {
621 /* print only warnings */
622 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
623 if (eitem->level == LY_LLWRN) {
624 ly_err_print(eitem);
625 }
626 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100627 }
628
Michal Vaskodbf7c272018-02-19 09:07:59 +0100629 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100630 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100631 }
632
633 return ret;
634}
635
Radek Krejci65ef6d52018-08-16 16:35:02 +0200636static void
637free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200638{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200639 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200640
Radek Krejci65ef6d52018-08-16 16:35:02 +0200641 if (!list) {
642 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200643 }
644
Radek Krejci65ef6d52018-08-16 16:35:02 +0200645 for (u = 0; list[u].name; ++u) {
646 free(list[u].name);
647 free(list[u].revision);
648 for (v = 0; v < list[u].features.number; ++v) {
649 free(list[u].features.set.g[v]);
650 }
651 free(list[u].features.set.g);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200652 if (list[u].submodules) {
653 for (v = 0; list[u].submodules[v].name; ++v) {
654 free(list[u].submodules[v].name);
655 free(list[u].submodules[v].revision);
656 }
657 free(list[u].submodules);
658 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200659 }
660 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200661}
662
Radek Krejci235d8cb2018-08-17 14:04:32 +0200663
664static int
665build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200666{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200667 struct nc_rpc *rpc = NULL;
668 struct nc_reply *reply = NULL;
669 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200670 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200671 NC_MSG_TYPE msg;
672 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200673 struct ly_set *modules = NULL;
Radek Krejci2d088832018-08-21 13:40:14 +0200674 unsigned int u, v, submodules_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200675 struct lyd_node *iter, *child;
676 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200677 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200678
679 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200680 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
681 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
682 } else {
683 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0, NC_PARAMTYPE_CONST);
684 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200685 if (!rpc) {
686 goto cleanup;
687 }
688
689 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
690 usleep(1000);
691 }
692 if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200693 WRN("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200694 session->id);
695 goto cleanup;
696 }
697
698 do {
699 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
700 } while (msg == NC_MSG_NOTIF);
701 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200702 WRN("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200703 goto cleanup;
704 } else if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200705 WRN("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200706 goto cleanup;
707 }
708
709 switch (reply->type) {
710 case NC_RPL_OK:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200711 WRN("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200712 goto cleanup;
713 case NC_RPL_DATA:
714 /* fine */
715 break;
716 case NC_RPL_ERROR:
717 error_rpl = (struct nc_reply_error *)reply;
718 if (error_rpl->count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200719 WRN("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200720 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
721 } else {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200722 WRN("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200723 }
724 goto cleanup;
725 case NC_RPL_NOTIF:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200726 WRN("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200727 goto cleanup;
728 }
729
Radek Krejci65ef6d52018-08-16 16:35:02 +0200730 yldata = ((struct nc_reply_data *)reply)->data;
731 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200732 WRN("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200733 goto cleanup;
734 }
735
Radek Krejci65ef6d52018-08-16 16:35:02 +0200736 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200737 if (!modules) {
Radek Krejci2d088832018-08-21 13:40:14 +0200738 WRN("Session %u: no module information in reply to a yang-library <get> RPC.", session->id);
739 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200740 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200741
Radek Krejci2d088832018-08-21 13:40:14 +0200742 (*result) = calloc(modules->number + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200743 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200744 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200745 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200746 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200747 }
748
Radek Krejci2d088832018-08-21 13:40:14 +0200749 for (u = 0; u < modules->number; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200750 submodules_count = 0;
751 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200752 LY_TREE_FOR(modules->set.d[u]->child, iter) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200753 if (iter->schema->module != mod) {
754 /* ignore node from other schemas (augments) */
755 continue;
756 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200757 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
758 /* ignore empty nodes */
759 continue;
760 }
761 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200762 (*result)[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200763 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200764 (*result)[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200765 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200766 (*result)[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200767 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200768 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 +0200769 } else if (!strcmp(iter->schema->name, "submodule")) {
770 submodules_count++;
771 }
772 }
773
774 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200775 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
776 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200777 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200778 free_schema_info(*result);
779 *result = NULL;
780 ret = EXIT_FAILURE;
781 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200782 } else {
783 v = 0;
784 LY_TREE_FOR(modules->set.d[u]->child, iter) {
785 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
786 if (mod == iter->schema->module && !strcmp(iter->schema->name, "submodule")) {
787 LY_TREE_FOR(iter->child, child) {
788 if (mod != child->schema->module) {
789 continue;
790 } else if (!strcmp(child->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200791 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200792 } else if (!strcmp(child->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200793 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200794 }
795 }
796 }
797 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200798 }
799 }
800 }
801
Radek Krejcifd5b6682017-06-13 15:52:53 +0200802cleanup:
803 nc_rpc_free(rpc);
804 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200805 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200806
Radek Krejci235d8cb2018-08-17 14:04:32 +0200807 if (session->status != NC_STATUS_RUNNING) {
808 /* something bad heppened, discard the session */
809 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
810 ret = EXIT_FAILURE;
811 }
812
813 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200814}
815
Radek Krejci235d8cb2018-08-17 14:04:32 +0200816static int
817build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200818{
819 unsigned int u, v;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200820 char *module_cpblt, *ptr, *ptr2;
821
822 for (u = 0; cpblts[u]; ++u);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200823 (*result) = calloc(u + 1, sizeof **result);
824 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200825 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200826 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200827 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200828
829 for (u = v = 0; cpblts[u]; ++u) {
830 module_cpblt = strstr(cpblts[u], "module=");
831 /* this capability requires a module */
832 if (!module_cpblt) {
833 continue;
834 }
835
836 /* get module's name */
837 ptr = (char *)module_cpblt + 7;
838 ptr2 = strchr(ptr, '&');
839 if (!ptr2) {
840 ptr2 = ptr + strlen(ptr);
841 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200842 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200843
844 /* get module's revision */
845 ptr = strstr(module_cpblt, "revision=");
846 if (ptr) {
847 ptr += 9;
848 ptr2 = strchr(ptr, '&');
849 if (!ptr2) {
850 ptr2 = ptr + strlen(ptr);
851 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200852 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200853 }
854
855 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200856 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200857
858 /* get module's features */
859 ptr = strstr(module_cpblt, "features=");
860 if (ptr) {
861 ptr += 9;
862 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
863 if (*ptr == ',') {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200864 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200865 ptr2 = ptr + 1;
866 }
867 }
868 /* the last one */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200869 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200870 }
871 ++v;
872 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200873
874 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200875}
876
877static int
878nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
879{
880 int ret = EXIT_FAILURE;
881 const struct lys_module *mod;
882 unsigned int u, v;
883
884 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200885 /* skip import-only modules */
886 if (!modules[u].implemented) {
887 continue;
888 }
889
Radek Krejci65ef6d52018-08-16 16:35:02 +0200890 /* we can continue even if it fails */
891 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
892
893 if (!mod) {
894 if (session->status != NC_STATUS_RUNNING) {
895 /* something bad heppened, discard the session */
896 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
897 goto cleanup;
898 }
899
900 /* all loading ways failed, the schema will be ignored in the received data */
901 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
902 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
903 } else {
904 /* set features - first disable all to enable specified then */
905 lys_features_disable(mod, "*");
906 for (v = 0; v < modules[u].features.number; v++) {
907 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
908 }
909 }
Michal Vasko60f66602017-10-17 13:52:18 +0200910 }
911
Radek Krejci65ef6d52018-08-16 16:35:02 +0200912 /* done */
913 ret = EXIT_SUCCESS;
914
915cleanup:
916
Radek Krejcifd5b6682017-06-13 15:52:53 +0200917 return ret;
918}
919
Radek Krejci65ef6d52018-08-16 16:35:02 +0200920static int
921nc_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)
922{
923 unsigned int u, v;
924 const struct lys_module *ietfnc;
925
926 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
927 if (!ietfnc) {
928 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
929 if (!ietfnc) {
930 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
931 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
932 }
933 }
934 if (!ietfnc) {
935 ERR("Loading base NETCONF schema failed.");
936 return 1;
937 }
938
939 /* set supported capabilities from ietf-netconf */
940 for (u = 0; modules[u].name; ++u) {
941 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
942 continue;
943 }
944
945 lys_features_disable(ietfnc, "*");
946 for (v = 0; v < modules[u].features.number; v++) {
947 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
948 }
949 }
950
951 return 0;
952}
953
Michal Vasko086311b2016-01-08 09:53:11 +0100954int
955nc_ctx_check_and_fill(struct nc_session *session)
956{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200957 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100958 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100959 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200960 const struct lys_module *mod = NULL;
961 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200962 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100963
Michal Vasko2e6defd2016-10-07 15:48:15 +0200964 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100965
Radek Krejci65ef6d52018-08-16 16:35:02 +0200966 /* 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 +0200967 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200968
Radek Krejci65ef6d52018-08-16 16:35:02 +0200969 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
970 * schemas only to those present on the server side */
971 ly_ctx_set_disable_searchdirs(session->ctx);
972
973 /* our callback is set later with appropriate data */
974 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
975
976 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200977 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200978 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200979 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200980 if (yanglib_support) {
981 break;
982 }
Radek Krejcicde410d2018-08-21 09:56:49 +0200983 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:1.0", 51)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200984 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200985 if (get_schema_support) {
986 break;
987 }
Michal Vasko086311b2016-01-08 09:53:11 +0100988 }
989 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200990
991 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200992 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
993 ERR("Session %u: unable to get server's schema information from the <hello>'s capabilities.", session->id);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200994 goto cleanup;
995 }
996
Michal Vasko086311b2016-01-08 09:53:11 +0100997 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200998 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200999 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
1000 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001001 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001002 }
1003 }
1004
1005 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001006 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001007 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001008 }
1009
Radek Krejci65ef6d52018-08-16 16:35:02 +02001010 /* get correct version of ietf-yang-library into context */
1011 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001012 /* use get schema to get server's ietf-yang-library */
1013 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1014 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001015 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
1016 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001017 yanglib_support = 0;
1018 } else {
1019 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001020 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
1021 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 +02001022 yanglib_support = 0;
1023 }
1024 free(revision);
1025 }
1026 }
1027
Radek Krejci65ef6d52018-08-16 16:35:02 +02001028 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001029 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001030 if (build_schema_info_yl(session, &sm)) {
1031 goto cleanup;
1032 } else if (!sm) {
1033 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
1034 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001035 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
1036 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001037 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001038 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001039 }
Michal Vaskoef578332016-01-25 13:20:09 +01001040
Radek Krejci65ef6d52018-08-16 16:35:02 +02001041 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1042 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001043 }
1044
Radek Krejcifd5b6682017-06-13 15:52:53 +02001045 /* succsess */
1046 ret = 0;
1047
Michal Vaskoeee99412016-11-21 10:19:43 +01001048 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001049 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 +01001050 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001051
1052cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001053 free_schema_info(server_modules);
1054
Radek Krejcifd5b6682017-06-13 15:52:53 +02001055 /* set user callback back */
1056 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001057 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001058
Michal Vaskoef578332016-01-25 13:20:09 +01001059 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001060}
1061
1062API struct nc_session *
1063nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1064{
Michal Vaskod083db62016-01-19 10:31:29 +01001065 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001066
Michal Vasko45e53ae2016-04-07 11:46:03 +02001067 if (fdin < 0) {
1068 ERRARG("fdin");
1069 return NULL;
1070 } else if (fdout < 0) {
1071 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001072 return NULL;
1073 }
1074
1075 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001076 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001077 if (!session) {
1078 ERRMEM;
1079 return NULL;
1080 }
1081 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001082
1083 /* transport specific data */
1084 session->ti_type = NC_TI_FD;
1085 session->ti.fd.in = fdin;
1086 session->ti.fd.out = fdout;
1087
1088 /* assign context (dicionary needed for handshake) */
1089 if (!ctx) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001090 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001091 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001092 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001093 /* that's just it */
1094 goto fail;
1095 }
Michal Vasko086311b2016-01-08 09:53:11 +01001096 } else {
1097 session->flags |= NC_SESSION_SHAREDCTX;
1098 }
1099 session->ctx = ctx;
1100
1101 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001102 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001103 goto fail;
1104 }
1105 session->status = NC_STATUS_RUNNING;
1106
Michal Vaskoef578332016-01-25 13:20:09 +01001107 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001108 goto fail;
1109 }
1110
1111 return session;
1112
1113fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001114 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001115 return NULL;
1116}
1117
Frank Rimpler9f838b02018-07-25 06:44:03 +00001118/*
1119 Helper for a non-blocking connect (which is required because of the locking
1120 concept for e.g. call home settings). For more details see nc_sock_connect().
1121 */
1122static int
1123_non_blocking_connect(int timeout, int* sock_pending, struct addrinfo *res)
Michal Vasko086311b2016-01-08 09:53:11 +01001124{
Frank Rimplerc7a2acb2018-08-06 07:31:08 +00001125 int flags, ret=0;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001126 int sock = -1;
1127 fd_set wset;
1128 struct timeval ts;
1129 int error = 0;
1130 socklen_t len = sizeof(int);
Michal Vasko086311b2016-01-08 09:53:11 +01001131
Frank Rimpler9f838b02018-07-25 06:44:03 +00001132 if (sock_pending && *sock_pending != -1) {
1133 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1134 sock = *sock_pending;
1135 } else {
1136 assert(res);
1137 VRB("Trying to connect via %s.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1138 /* Connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001139 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1140 if (sock == -1) {
Frank Rimpler9f838b02018-07-25 06:44:03 +00001141 ERR("socket couldn't be created.", strerror(errno));
1142 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001143 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001144 /* make the socket non-blocking */
1145 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
1146 ERR("Fcntl failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001147 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001148 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001149 /* non-blocking connect! */
1150 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1151 if (errno != EINPROGRESS) {
1152 /* network connection failed, try another resource */
1153 ERR("connect failed: (%s).", strerror(errno));
1154 goto cleanup;
1155 }
1156 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001157 }
1158 ts.tv_sec = timeout;
1159 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001160
Frank Rimpler9f838b02018-07-25 06:44:03 +00001161 FD_ZERO(&wset);
1162 FD_SET(sock, &wset);
1163
1164 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
1165 ERR("select failed: (%s).", strerror(errno));
1166 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001167 }
1168
Frank Rimpler9f838b02018-07-25 06:44:03 +00001169 if (ret == 0) { //we had a timeout
1170 VRB("timed out after %ds (%s).", timeout, strerror(errno));
1171 if (sock_pending) {
1172 /* no sock-close, we'll try it again */
1173 *sock_pending = sock;
1174 } else {
1175 close(sock);
1176 }
1177 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001178 }
Radek Krejci782041a2018-08-20 10:09:45 +02001179
1180 /* check the usability of the socket */
1181 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1182 ERR("getsockopt failed: (%s).", strerror(errno));
1183 goto cleanup;
1184 }
1185 if (error == ECONNREFUSED) {
1186 /* network connection failed, try another resource */
1187 VRB("getsockopt error: (%s).", strerror(error));
1188 errno = error;
1189 goto cleanup;
1190 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001191
1192 /* enable keep-alive */
1193 if (nc_sock_enable_keepalive(sock)) {
1194 goto cleanup;
1195 }
1196
Michal Vasko086311b2016-01-08 09:53:11 +01001197 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001198
Frank Rimpler9f838b02018-07-25 06:44:03 +00001199cleanup:
1200 if (sock_pending) {
1201 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001202 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001203 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001204 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001205}
1206
Frank Rimpler9f838b02018-07-25 06:44:03 +00001207/* A given timeout value limits the time how long the function blocks. If it has to block
1208 only for some seconds, a socket connection might not yet have been fully established.
1209 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1210 value will be -1. In such a case a subsequent invokation is required, by providing the
1211 stored sock_pending, again.
1212 In general, if this function returns -1, when a timeout has been given, this function
1213 has to be invoked, until it returns a valid socket.
1214 */
1215int
1216nc_sock_connect(const char* host, uint16_t port, int timeout, int* sock_pending)
1217{
1218 int i;
1219 int sock = sock_pending?*sock_pending:-1;
1220 struct addrinfo hints, *res_list, *res;
1221 char port_s[6]; /* length of string representation of short int */
1222
1223 VRB("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
1224
1225 /* no pending socket */
1226 if (sock == -1) {
1227 /* Connect to a server */
1228 snprintf(port_s, 6, "%u", port);
1229 memset(&hints, 0, sizeof hints);
1230 hints.ai_family = AF_UNSPEC;
1231 hints.ai_socktype = SOCK_STREAM;
1232 hints.ai_protocol = IPPROTO_TCP;
1233 i = getaddrinfo(host, port_s, &hints, &res_list);
1234 if (i != 0) {
1235 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1236 return -1;
1237 }
1238
1239 for (res = res_list; res != NULL; res = res->ai_next) {
1240 sock = _non_blocking_connect(timeout, sock_pending, res);
1241 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1242 /* try the next resource */
1243 continue;
1244 }
1245 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1246 break;
1247 }
1248 freeaddrinfo(res_list);
1249
1250 } else {
1251 /* try to get a connection with the pending socket */
1252 assert(sock_pending);
1253 sock = _non_blocking_connect(timeout, sock_pending, NULL);
1254 }
1255
1256 return sock;
1257}
1258
Michal Vasko086311b2016-01-08 09:53:11 +01001259static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001260get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001261{
Michal Vasko086311b2016-01-08 09:53:11 +01001262 char *ptr;
1263 const char *str_msgid;
1264 uint64_t cur_msgid;
1265 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001266 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001267 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1268
Michal Vasko086311b2016-01-08 09:53:11 +01001269 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001270 if (!msgid && session->opts.client.notifs) {
1271 cont = session->opts.client.notifs;
1272 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001273
Michal Vasko71ba2da2016-05-04 10:53:16 +02001274 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001275 free(cont);
1276
Michal Vasko71ba2da2016-05-04 10:53:16 +02001277 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001278 }
1279
1280 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001281 if (msgid && session->opts.client.replies) {
1282 cont = session->opts.client.replies;
1283 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001284
Michal Vasko71ba2da2016-05-04 10:53:16 +02001285 xml = cont->msg;
1286 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001287
Michal Vasko71ba2da2016-05-04 10:53:16 +02001288 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001289 }
1290
Michal Vasko71ba2da2016-05-04 10:53:16 +02001291 if (!msgtype) {
1292 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001293 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001294 }
Michal Vasko086311b2016-01-08 09:53:11 +01001295
1296 /* we read rpc-reply, want a notif */
1297 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001298 cont_ptr = &session->opts.client.replies;
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 Vasko4eb3c312016-03-01 14:09:37 +01001303 if (!*cont_ptr) {
1304 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
1312 /* we read notif, want a rpc-reply */
1313 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001314 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001315 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001316 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001317 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001318 }
Michal Vasko086311b2016-01-08 09:53:11 +01001319
Michal Vasko2e6defd2016-10-07 15:48:15 +02001320 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001321 while (*cont_ptr) {
1322 cont_ptr = &((*cont_ptr)->next);
1323 }
1324 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001325 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001326 ERRMEM;
1327 lyxml_free(session->ctx, xml);
1328 return NC_MSG_ERROR;
1329 }
Michal Vasko086311b2016-01-08 09:53:11 +01001330 (*cont_ptr)->msg = xml;
1331 (*cont_ptr)->next = NULL;
1332 }
1333
Michal Vasko086311b2016-01-08 09:53:11 +01001334 switch (msgtype) {
1335 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001336 if (!msgid) {
1337 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001338 }
Michal Vasko086311b2016-01-08 09:53:11 +01001339 break;
1340
1341 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001342 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001343 /* check message-id */
1344 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1345 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001346 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001347 } else {
1348 cur_msgid = strtoul(str_msgid, &ptr, 10);
1349 if (cur_msgid != msgid) {
1350 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1351 session->id, str_msgid);
1352 msgtype = NC_MSG_REPLY_ERR_MSGID;
1353 }
1354 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001355 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001356 }
Michal Vasko086311b2016-01-08 09:53:11 +01001357 break;
1358
1359 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001360 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001361 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001362 msgtype = NC_MSG_ERROR;
1363 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001364
1365 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001366 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001367 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001368 msgtype = NC_MSG_ERROR;
1369 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001370
1371 default:
1372 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1373 * NC_MSG_NONE is not returned by nc_read_msg()
1374 */
1375 break;
1376 }
1377
1378 return msgtype;
1379}
1380
1381/* cannot strictly fail, but does not need to fill any error parameter at all */
1382static void
1383parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1384{
1385 struct lyxml_elem *iter, *next, *info;
1386
1387 LY_TREE_FOR(xml->child, iter) {
1388 if (!iter->ns) {
1389 if (iter->content) {
1390 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1391 } else {
1392 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1393 }
1394 continue;
1395 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1396 if (iter->content) {
1397 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1398 iter->name, iter->content, iter->ns->value);
1399 } else {
1400 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1401 }
1402 continue;
1403 }
1404
1405 if (!strcmp(iter->name, "error-type")) {
1406 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1407 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1408 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1409 } else if (err->type) {
1410 WRN("<rpc-error> <error-type> duplicated.");
1411 } else {
1412 err->type = lydict_insert(ctx, iter->content, 0);
1413 }
1414 } else if (!strcmp(iter->name, "error-tag")) {
1415 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1416 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1417 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1418 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1419 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1420 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1421 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1422 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1423 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1424 && strcmp(iter->content, "malformed-message"))) {
1425 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1426 } else if (err->tag) {
1427 WRN("<rpc-error> <error-tag> duplicated.");
1428 } else {
1429 err->tag = lydict_insert(ctx, iter->content, 0);
1430 }
1431 } else if (!strcmp(iter->name, "error-severity")) {
1432 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1433 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1434 } else if (err->severity) {
1435 WRN("<rpc-error> <error-severity> duplicated.");
1436 } else {
1437 err->severity = lydict_insert(ctx, iter->content, 0);
1438 }
1439 } else if (!strcmp(iter->name, "error-app-tag")) {
1440 if (err->apptag) {
1441 WRN("<rpc-error> <error-app-tag> duplicated.");
1442 } else {
1443 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1444 }
1445 } else if (!strcmp(iter->name, "error-path")) {
1446 if (err->path) {
1447 WRN("<rpc-error> <error-path> duplicated.");
1448 } else {
1449 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1450 }
1451 } else if (!strcmp(iter->name, "error-message")) {
1452 if (err->message) {
1453 WRN("<rpc-error> <error-message> duplicated.");
1454 } else {
1455 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001456 if (err->message_lang) {
1457 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1458 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001459 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1460 }
1461 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1462 }
1463 } else if (!strcmp(iter->name, "error-info")) {
1464 LY_TREE_FOR_SAFE(iter->child, next, info) {
1465 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1466 if (!strcmp(info->name, "session-id")) {
1467 if (err->sid) {
1468 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1469 } else {
1470 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1471 }
Michal Vasko630485f2018-01-03 14:28:32 +01001472 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001473 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001474 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1475 if (!err->attr) {
1476 ERRMEM;
1477 return;
1478 }
Michal Vasko086311b2016-01-08 09:53:11 +01001479 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1480 } else if (!strcmp(info->name, "bad-element")) {
1481 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001482 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1483 if (!err->elem) {
1484 ERRMEM;
1485 return;
1486 }
Michal Vasko086311b2016-01-08 09:53:11 +01001487 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1488 } else if (!strcmp(info->name, "bad-namespace")) {
1489 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001490 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1491 if (!err->ns) {
1492 ERRMEM;
1493 return;
1494 }
Michal Vasko086311b2016-01-08 09:53:11 +01001495 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1496 } else {
1497 if (info->content) {
1498 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1499 info->name, info->content);
1500 } else {
1501 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1502 }
1503 }
1504 } else {
1505 lyxml_unlink(ctx, info);
1506 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001507 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1508 if (!err->other) {
1509 ERRMEM;
1510 return;
1511 }
Michal Vasko086311b2016-01-08 09:53:11 +01001512 err->other[err->other_count - 1] = info;
1513 }
1514 }
1515 } else {
1516 if (iter->content) {
1517 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1518 } else {
1519 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1520 }
1521 }
1522 }
1523}
1524
1525static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001526parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001527{
1528 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001529 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001530 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001531 struct nc_reply_data *data_rpl;
1532 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001533 struct nc_rpc_act_generic *rpc_gen;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001534 int i, data_parsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001535
1536 if (!xml->child) {
1537 ERR("An empty <rpc-reply>.");
1538 return NULL;
1539 }
1540
1541 /* rpc-error */
1542 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1543 /* count and check elements */
1544 i = 0;
1545 LY_TREE_FOR(xml->child, iter) {
1546 if (strcmp(iter->name, "rpc-error")) {
1547 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1548 return NULL;
1549 } else if (!iter->ns) {
1550 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1551 return NULL;
1552 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1553 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1554 return NULL;
1555 }
1556 ++i;
1557 }
1558
1559 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001560 if (!error_rpl) {
1561 ERRMEM;
1562 return NULL;
1563 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001564 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001565 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001566 if (!error_rpl->err) {
1567 ERRMEM;
1568 free(error_rpl);
1569 return NULL;
1570 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001571 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001572 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001573 reply = (struct nc_reply *)error_rpl;
1574
1575 i = 0;
1576 LY_TREE_FOR(xml->child, iter) {
1577 parse_rpc_error(ctx, iter, error_rpl->err + i);
1578 ++i;
1579 }
1580
1581 /* ok */
1582 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1583 if (xml->child->next) {
1584 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1585 return NULL;
1586 }
1587 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001588 if (!reply) {
1589 ERRMEM;
1590 return NULL;
1591 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001592 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001593
1594 /* some RPC output */
1595 } else {
1596 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001597 case NC_RPC_ACT_GENERIC:
1598 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001599
1600 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001601 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1602 if (!rpc_act) {
1603 ERR("Failed to duplicate a generic RPC/action.");
1604 return NULL;
1605 }
Michal Vasko086311b2016-01-08 09:53:11 +01001606 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001607 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 +02001608 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001609 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001610 return NULL;
1611 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001612 }
Michal Vasko086311b2016-01-08 09:53:11 +01001613 break;
1614
1615 case NC_RPC_GETCONFIG:
1616 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001617 if (!xml->child->child) {
1618 /* we did not receive any data */
1619 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001620 if (!data_rpl) {
1621 ERRMEM;
1622 return NULL;
1623 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001624 data_rpl->type = NC_RPL_DATA;
1625 data_rpl->data = NULL;
1626 return (struct nc_reply *)data_rpl;
1627 }
1628
Michal Vasko086311b2016-01-08 09:53:11 +01001629 /* special treatment */
Michal Vaskof45e0c12018-11-28 08:38:23 +01001630 ly_errno = 0;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001631 data = lyd_parse_xml(ctx, &xml->child->child,
1632 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001633 | parseroptions);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001634 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001635 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1636 return NULL;
1637 }
Michal Vaskof45e0c12018-11-28 08:38:23 +01001638 data_parsed = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001639 break;
1640
1641 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001642 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001643 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001644 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001645 return NULL;
1646 }
1647 break;
1648
1649 case NC_RPC_EDIT:
1650 case NC_RPC_COPY:
1651 case NC_RPC_DELETE:
1652 case NC_RPC_LOCK:
1653 case NC_RPC_UNLOCK:
1654 case NC_RPC_KILL:
1655 case NC_RPC_COMMIT:
1656 case NC_RPC_DISCARD:
1657 case NC_RPC_CANCEL:
1658 case NC_RPC_VALIDATE:
1659 case NC_RPC_SUBSCRIBE:
1660 /* there is no output defined */
1661 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1662 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001663 default:
1664 ERRINT;
1665 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001666 }
1667
1668 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001669 if (!data_rpl) {
1670 ERRMEM;
1671 return NULL;
1672 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001673 data_rpl->type = NC_RPL_DATA;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001674
1675 ly_errno = 0;
1676 if (!data_parsed) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001677 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001678 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001679 } else {
1680 /* <get>, <get-config> */
1681 data_rpl->data = data;
1682 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001683 lyd_free_withsiblings(rpc_act);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001684 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001685 ERR("Failed to parse <rpc-reply>.");
1686 free(data_rpl);
1687 return NULL;
1688 }
1689 reply = (struct nc_reply *)data_rpl;
1690 }
1691
1692 return reply;
1693}
1694
Radek Krejci53691be2016-02-22 13:58:37 +01001695#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001696
Michal Vasko3031aae2016-01-27 16:07:18 +01001697int
1698nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1699{
1700 int sock;
1701
Michal Vasko45e53ae2016-04-07 11:46:03 +02001702 if (!address) {
1703 ERRARG("address");
1704 return -1;
1705 } else if (!port) {
1706 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001707 return -1;
1708 }
1709
1710 sock = nc_sock_listen(address, port);
1711 if (sock == -1) {
1712 return -1;
1713 }
1714
1715 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001716 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1717 if (!client_opts.ch_binds) {
1718 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001719 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001720 return -1;
1721 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001722
Michal Vasko2e6defd2016-10-07 15:48:15 +02001723 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1724 if (!client_opts.ch_bind_ti) {
1725 ERRMEM;
1726 close(sock);
1727 return -1;
1728 }
1729 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1730
Michal Vasko3031aae2016-01-27 16:07:18 +01001731 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001732 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1733 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001734 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001735 return -1;
1736 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001737 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1738 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001739 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001740
1741 return 0;
1742}
1743
1744int
1745nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1746{
1747 uint32_t i;
1748 int ret = -1;
1749
1750 if (!address && !port && !ti) {
1751 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1752 close(client_opts.ch_binds[i].sock);
1753 free((char *)client_opts.ch_binds[i].address);
1754
1755 ret = 0;
1756 }
1757 free(client_opts.ch_binds);
1758 client_opts.ch_binds = NULL;
1759 client_opts.ch_bind_count = 0;
1760 } else {
1761 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1762 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1763 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001764 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001765 close(client_opts.ch_binds[i].sock);
1766 free((char *)client_opts.ch_binds[i].address);
1767
1768 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001769 if (!client_opts.ch_bind_count) {
1770 free(client_opts.ch_binds);
1771 client_opts.ch_binds = NULL;
1772 } else if (i < client_opts.ch_bind_count) {
1773 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1774 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1775 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001776
1777 ret = 0;
1778 }
1779 }
1780 }
1781
1782 return ret;
1783}
1784
1785API int
1786nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1787{
1788 int sock;
1789 char *host = NULL;
1790 uint16_t port, idx;
1791
Michal Vasko45e53ae2016-04-07 11:46:03 +02001792 if (!client_opts.ch_binds) {
1793 ERRINIT;
1794 return -1;
1795 } else if (!session) {
1796 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001797 return -1;
1798 }
1799
1800 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1801
Michal Vasko50456e82016-02-02 12:16:08 +01001802 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001803 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001804 return sock;
1805 }
1806
Radek Krejci53691be2016-02-22 13:58:37 +01001807#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001808 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001809 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001810 } else
1811#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001812#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001813 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001814 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001815 } else
1816#endif
1817 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001818 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001819 *session = NULL;
1820 }
1821
1822 free(host);
1823
1824 if (!(*session)) {
1825 return -1;
1826 }
1827
1828 return 1;
1829}
1830
Radek Krejci53691be2016-02-22 13:58:37 +01001831#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001832
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001833API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001834nc_session_get_cpblts(const struct nc_session *session)
1835{
1836 if (!session) {
1837 ERRARG("session");
1838 return NULL;
1839 }
1840
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001841 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001842}
1843
1844API const char *
1845nc_session_cpblt(const struct nc_session *session, const char *capab)
1846{
1847 int i, len;
1848
1849 if (!session) {
1850 ERRARG("session");
1851 return NULL;
1852 } else if (!capab) {
1853 ERRARG("capab");
1854 return NULL;
1855 }
1856
1857 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001858 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1859 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1860 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001861 }
1862 }
1863
1864 return NULL;
1865}
1866
Michal Vasko9cd26a82016-05-31 08:58:48 +02001867API int
1868nc_session_ntf_thread_running(const struct nc_session *session)
1869{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001870 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001871 ERRARG("session");
1872 return 0;
1873 }
1874
Michal Vasko2e6defd2016-10-07 15:48:15 +02001875 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001876}
1877
Michal Vaskob7558c52016-02-26 15:04:19 +01001878API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001879nc_client_init(void)
1880{
1881 nc_init();
1882}
1883
1884API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001885nc_client_destroy(void)
1886{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001887 nc_client_set_schema_searchpath(NULL);
1888#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1889 nc_client_ch_del_bind(NULL, 0, 0);
1890#endif
1891#ifdef NC_ENABLED_SSH
1892 nc_client_ssh_destroy_opts();
1893#endif
1894#ifdef NC_ENABLED_TLS
1895 nc_client_tls_destroy_opts();
1896#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001897 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001898}
1899
Michal Vasko086311b2016-01-08 09:53:11 +01001900API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001901nc_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 +01001902{
1903 struct lyxml_elem *xml;
1904 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1905
Michal Vasko45e53ae2016-04-07 11:46:03 +02001906 if (!session) {
1907 ERRARG("session");
1908 return NC_MSG_ERROR;
1909 } else if (!rpc) {
1910 ERRARG("rpc");
1911 return NC_MSG_ERROR;
1912 } else if (!reply) {
1913 ERRARG("reply");
1914 return NC_MSG_ERROR;
1915 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1916 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001917 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001918 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001919 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001920 return NC_MSG_ERROR;
1921 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001922 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001923 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1924 parseroptions &= LYD_OPT_STRICT;
1925 }
Michal Vasko41adf392017-01-17 10:39:04 +01001926 /* no mechanism to check external dependencies is provided */
1927 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001928 *reply = NULL;
1929
1930 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001931
Michal Vasko71ba2da2016-05-04 10:53:16 +02001932 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001933 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001934 lyxml_free(session->ctx, xml);
1935 if (!(*reply)) {
1936 return NC_MSG_ERROR;
1937 }
1938 }
1939
1940 return msgtype;
1941}
1942
1943API NC_MSG_TYPE
1944nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1945{
1946 struct lyxml_elem *xml, *ev_time;
1947 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1948
Michal Vasko45e53ae2016-04-07 11:46:03 +02001949 if (!session) {
1950 ERRARG("session");
1951 return NC_MSG_ERROR;
1952 } else if (!notif) {
1953 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001954 return NC_MSG_ERROR;
1955 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001956 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001957 return NC_MSG_ERROR;
1958 }
1959
1960 msgtype = get_msg(session, timeout, 0, &xml);
1961
1962 if (msgtype == NC_MSG_NOTIF) {
1963 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001964 if (!*notif) {
1965 ERRMEM;
1966 lyxml_free(session->ctx, xml);
1967 return NC_MSG_ERROR;
1968 }
Michal Vasko086311b2016-01-08 09:53:11 +01001969
1970 /* eventTime */
1971 LY_TREE_FOR(xml->child, ev_time) {
1972 if (!strcmp(ev_time->name, "eventTime")) {
1973 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1974 /* lyd_parse does not know this element */
1975 lyxml_free(session->ctx, ev_time);
1976 break;
1977 }
1978 }
1979 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001980 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001981 goto fail;
1982 }
1983
1984 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001985 (*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 +01001986 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001987 lyxml_free(session->ctx, xml);
1988 xml = NULL;
1989 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001990 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001991 goto fail;
1992 }
1993 }
1994
1995 return msgtype;
1996
1997fail:
1998 lydict_remove(session->ctx, (*notif)->datetime);
1999 lyd_free((*notif)->tree);
2000 free(*notif);
2001 *notif = NULL;
2002 lyxml_free(session->ctx, xml);
2003
2004 return NC_MSG_ERROR;
2005}
2006
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002007static void *
2008nc_recv_notif_thread(void *arg)
2009{
2010 struct nc_ntf_thread_arg *ntarg;
2011 struct nc_session *session;
2012 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
2013 struct nc_notif *notif;
2014 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002015 pthread_t *ntf_tid;
2016
2017 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002018
2019 ntarg = (struct nc_ntf_thread_arg *)arg;
2020 session = ntarg->session;
2021 notif_clb = ntarg->notif_clb;
2022 free(ntarg);
2023
Michal Vasko131120a2018-05-29 15:44:02 +02002024 /* remember our allocated tid, we will be freeing it */
2025 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
2026
Michal Vasko2e6defd2016-10-07 15:48:15 +02002027 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002028 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002029 if (msgtype == NC_MSG_NOTIF) {
2030 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002031 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2032 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2033 nc_notif_free(notif);
2034 break;
2035 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002036 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002037 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002038 /* quit this thread once the session is broken */
2039 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002040 }
2041
2042 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2043 }
2044
Michal Vasko0651c902016-05-19 15:55:42 +02002045 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002046 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02002047 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002048 return NULL;
2049}
2050
2051API int
2052nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2053{
2054 struct nc_ntf_thread_arg *ntarg;
2055 int ret;
2056
Michal Vasko45e53ae2016-04-07 11:46:03 +02002057 if (!session) {
2058 ERRARG("session");
2059 return -1;
2060 } else if (!notif_clb) {
2061 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002062 return -1;
2063 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2064 ERR("Session %u: invalid session to receive Notifications.", session->id);
2065 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002066 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002067 ERR("Session %u: separate notification thread is already running.", session->id);
2068 return -1;
2069 }
2070
2071 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002072 if (!ntarg) {
2073 ERRMEM;
2074 return -1;
2075 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002076 ntarg->session = session;
2077 ntarg->notif_clb = notif_clb;
2078
2079 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002080 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2081 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002082 ERRMEM;
2083 free(ntarg);
2084 return -1;
2085 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002086
Michal Vasko2e6defd2016-10-07 15:48:15 +02002087 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002088 if (ret) {
2089 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2090 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002091 free((pthread_t *)session->opts.client.ntf_tid);
2092 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002093 return -1;
2094 }
2095
2096 return 0;
2097}
2098
Michal Vasko086311b2016-01-08 09:53:11 +01002099API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002100nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002101{
2102 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002103 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002104 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002105 struct nc_rpc_getconfig *rpc_gc;
2106 struct nc_rpc_edit *rpc_e;
2107 struct nc_rpc_copy *rpc_cp;
2108 struct nc_rpc_delete *rpc_del;
2109 struct nc_rpc_lock *rpc_lock;
2110 struct nc_rpc_get *rpc_g;
2111 struct nc_rpc_kill *rpc_k;
2112 struct nc_rpc_commit *rpc_com;
2113 struct nc_rpc_cancel *rpc_can;
2114 struct nc_rpc_validate *rpc_val;
2115 struct nc_rpc_getschema *rpc_gs;
2116 struct nc_rpc_subscribe *rpc_sub;
2117 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01002118 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02002119 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002120 uint64_t cur_msgid;
2121
Michal Vasko45e53ae2016-04-07 11:46:03 +02002122 if (!session) {
2123 ERRARG("session");
2124 return NC_MSG_ERROR;
2125 } else if (!rpc) {
2126 ERRARG("rpc");
2127 return NC_MSG_ERROR;
2128 } else if (!msgid) {
2129 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002130 return NC_MSG_ERROR;
2131 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002132 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002133 return NC_MSG_ERROR;
2134 }
2135
Michal Vasko90e8e692016-07-13 12:27:57 +02002136 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002137 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002138 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01002139 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002140 return NC_MSG_ERROR;
2141 }
2142 }
2143
2144 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002145 case NC_RPC_ACT_GENERIC:
2146 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002147
2148 if (rpc_gen->has_data) {
2149 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002150 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002151 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002152 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 +01002153 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002154 if (!data) {
2155 return NC_MSG_ERROR;
2156 }
Michal Vasko086311b2016-01-08 09:53:11 +01002157 }
2158 break;
2159
2160 case NC_RPC_GETCONFIG:
2161 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2162
2163 data = lyd_new(NULL, ietfnc, "get-config");
2164 node = lyd_new(data, ietfnc, "source");
2165 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
2166 if (!node) {
2167 lyd_free(data);
2168 return NC_MSG_ERROR;
2169 }
2170 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002171 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002172 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002173 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002174 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002175 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002176 lyd_insert_attr(node, NULL, "type", "xpath");
2177 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002178 }
2179 if (!node) {
2180 lyd_free(data);
2181 return NC_MSG_ERROR;
2182 }
2183 }
2184
2185 if (rpc_gc->wd_mode) {
2186 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002187 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002188 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002189 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002190 return NC_MSG_ERROR;
2191 }
2192 }
2193 switch (rpc_gc->wd_mode) {
2194 case NC_WD_UNKNOWN:
2195 /* cannot get here */
2196 break;
2197 case NC_WD_ALL:
2198 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2199 break;
2200 case NC_WD_ALL_TAG:
2201 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2202 break;
2203 case NC_WD_TRIM:
2204 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2205 break;
2206 case NC_WD_EXPLICIT:
2207 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2208 break;
2209 }
2210 if (!node) {
2211 lyd_free(data);
2212 return NC_MSG_ERROR;
2213 }
2214 }
2215 break;
2216
2217 case NC_RPC_EDIT:
2218 rpc_e = (struct nc_rpc_edit *)rpc;
2219
2220 data = lyd_new(NULL, ietfnc, "edit-config");
2221 node = lyd_new(data, ietfnc, "target");
2222 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
2223 if (!node) {
2224 lyd_free(data);
2225 return NC_MSG_ERROR;
2226 }
2227
2228 if (rpc_e->default_op) {
2229 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
2230 if (!node) {
2231 lyd_free(data);
2232 return NC_MSG_ERROR;
2233 }
2234 }
2235
2236 if (rpc_e->test_opt) {
2237 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
2238 if (!node) {
2239 lyd_free(data);
2240 return NC_MSG_ERROR;
2241 }
2242 }
2243
2244 if (rpc_e->error_opt) {
2245 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
2246 if (!node) {
2247 lyd_free(data);
2248 return NC_MSG_ERROR;
2249 }
2250 }
2251
Michal Vasko7793bc62016-09-16 11:58:41 +02002252 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002253 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002254 } else {
2255 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
2256 }
2257 if (!node) {
2258 lyd_free(data);
2259 return NC_MSG_ERROR;
2260 }
2261 break;
2262
2263 case NC_RPC_COPY:
2264 rpc_cp = (struct nc_rpc_copy *)rpc;
2265
2266 data = lyd_new(NULL, ietfnc, "copy-config");
2267 node = lyd_new(data, ietfnc, "target");
2268 if (rpc_cp->url_trg) {
2269 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
2270 } else {
2271 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
2272 }
2273 if (!node) {
2274 lyd_free(data);
2275 return NC_MSG_ERROR;
2276 }
2277
2278 node = lyd_new(data, ietfnc, "source");
2279 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002280 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002281 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002282 } else {
2283 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
2284 }
2285 } else {
2286 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
2287 }
2288 if (!node) {
2289 lyd_free(data);
2290 return NC_MSG_ERROR;
2291 }
2292
2293 if (rpc_cp->wd_mode) {
2294 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002295 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002296 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002297 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002298 return NC_MSG_ERROR;
2299 }
2300 }
2301 switch (rpc_cp->wd_mode) {
2302 case NC_WD_UNKNOWN:
2303 /* cannot get here */
2304 break;
2305 case NC_WD_ALL:
2306 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2307 break;
2308 case NC_WD_ALL_TAG:
2309 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2310 break;
2311 case NC_WD_TRIM:
2312 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2313 break;
2314 case NC_WD_EXPLICIT:
2315 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2316 break;
2317 }
2318 if (!node) {
2319 lyd_free(data);
2320 return NC_MSG_ERROR;
2321 }
2322 }
2323 break;
2324
2325 case NC_RPC_DELETE:
2326 rpc_del = (struct nc_rpc_delete *)rpc;
2327
2328 data = lyd_new(NULL, ietfnc, "delete-config");
2329 node = lyd_new(data, ietfnc, "target");
2330 if (rpc_del->url) {
2331 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2332 } else {
2333 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2334 }
2335 if (!node) {
2336 lyd_free(data);
2337 return NC_MSG_ERROR;
2338 }
2339 break;
2340
2341 case NC_RPC_LOCK:
2342 rpc_lock = (struct nc_rpc_lock *)rpc;
2343
2344 data = lyd_new(NULL, ietfnc, "lock");
2345 node = lyd_new(data, ietfnc, "target");
2346 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2347 if (!node) {
2348 lyd_free(data);
2349 return NC_MSG_ERROR;
2350 }
2351 break;
2352
2353 case NC_RPC_UNLOCK:
2354 rpc_lock = (struct nc_rpc_lock *)rpc;
2355
2356 data = lyd_new(NULL, ietfnc, "unlock");
2357 node = lyd_new(data, ietfnc, "target");
2358 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2359 if (!node) {
2360 lyd_free(data);
2361 return NC_MSG_ERROR;
2362 }
2363 break;
2364
2365 case NC_RPC_GET:
2366 rpc_g = (struct nc_rpc_get *)rpc;
2367
2368 data = lyd_new(NULL, ietfnc, "get");
2369 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002370 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002371 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002372 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002373 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002374 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002375 lyd_insert_attr(node, NULL, "type", "xpath");
2376 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002377 }
2378 if (!node) {
2379 lyd_free(data);
2380 return NC_MSG_ERROR;
2381 }
2382 }
2383
2384 if (rpc_g->wd_mode) {
2385 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002386 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002387 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002388 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002389 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002390 return NC_MSG_ERROR;
2391 }
2392 }
2393 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002394 case NC_WD_ALL:
2395 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2396 break;
2397 case NC_WD_ALL_TAG:
2398 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2399 break;
2400 case NC_WD_TRIM:
2401 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2402 break;
2403 case NC_WD_EXPLICIT:
2404 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2405 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002406 default:
2407 /* cannot get here */
2408 node = NULL;
2409 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002410 }
2411 if (!node) {
2412 lyd_free(data);
2413 return NC_MSG_ERROR;
2414 }
2415 }
2416 break;
2417
2418 case NC_RPC_KILL:
2419 rpc_k = (struct nc_rpc_kill *)rpc;
2420
2421 data = lyd_new(NULL, ietfnc, "kill-session");
2422 sprintf(str, "%u", rpc_k->sid);
2423 lyd_new_leaf(data, ietfnc, "session-id", str);
2424 break;
2425
2426 case NC_RPC_COMMIT:
2427 rpc_com = (struct nc_rpc_commit *)rpc;
2428
2429 data = lyd_new(NULL, ietfnc, "commit");
2430 if (rpc_com->confirmed) {
2431 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2432 }
2433
2434 if (rpc_com->confirm_timeout) {
2435 sprintf(str, "%u", rpc_com->confirm_timeout);
2436 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2437 }
2438
2439 if (rpc_com->persist) {
2440 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2441 if (!node) {
2442 lyd_free(data);
2443 return NC_MSG_ERROR;
2444 }
2445 }
2446
2447 if (rpc_com->persist_id) {
2448 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2449 if (!node) {
2450 lyd_free(data);
2451 return NC_MSG_ERROR;
2452 }
2453 }
2454 break;
2455
2456 case NC_RPC_DISCARD:
2457 data = lyd_new(NULL, ietfnc, "discard-changes");
2458 break;
2459
2460 case NC_RPC_CANCEL:
2461 rpc_can = (struct nc_rpc_cancel *)rpc;
2462
2463 data = lyd_new(NULL, ietfnc, "cancel-commit");
2464 if (rpc_can->persist_id) {
2465 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2466 if (!node) {
2467 lyd_free(data);
2468 return NC_MSG_ERROR;
2469 }
2470 }
2471 break;
2472
2473 case NC_RPC_VALIDATE:
2474 rpc_val = (struct nc_rpc_validate *)rpc;
2475
2476 data = lyd_new(NULL, ietfnc, "validate");
2477 node = lyd_new(data, ietfnc, "source");
2478 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002479 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002480 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002481 } else {
2482 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2483 }
2484 } else {
2485 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2486 }
2487 if (!node) {
2488 lyd_free(data);
2489 return NC_MSG_ERROR;
2490 }
2491 break;
2492
2493 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002494 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002495 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002496 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002497 return NC_MSG_ERROR;
2498 }
2499
2500 rpc_gs = (struct nc_rpc_getschema *)rpc;
2501
2502 data = lyd_new(NULL, ietfncmon, "get-schema");
2503 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2504 if (!node) {
2505 lyd_free(data);
2506 return NC_MSG_ERROR;
2507 }
2508 if (rpc_gs->version) {
2509 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2510 if (!node) {
2511 lyd_free(data);
2512 return NC_MSG_ERROR;
2513 }
2514 }
2515 if (rpc_gs->format) {
2516 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2517 if (!node) {
2518 lyd_free(data);
2519 return NC_MSG_ERROR;
2520 }
2521 }
2522 break;
2523
2524 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002525 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002526 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002527 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002528 return NC_MSG_ERROR;
2529 }
2530
2531 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2532
2533 data = lyd_new(NULL, notifs, "create-subscription");
2534 if (rpc_sub->stream) {
2535 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2536 if (!node) {
2537 lyd_free(data);
2538 return NC_MSG_ERROR;
2539 }
2540 }
2541
2542 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002543 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002544 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002545 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002546 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002547 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002548 lyd_insert_attr(node, NULL, "type", "xpath");
2549 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002550 }
2551 if (!node) {
2552 lyd_free(data);
2553 return NC_MSG_ERROR;
2554 }
2555 }
2556
2557 if (rpc_sub->start) {
2558 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2559 if (!node) {
2560 lyd_free(data);
2561 return NC_MSG_ERROR;
2562 }
2563 }
2564
2565 if (rpc_sub->stop) {
2566 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2567 if (!node) {
2568 lyd_free(data);
2569 return NC_MSG_ERROR;
2570 }
2571 }
2572 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002573 default:
2574 ERRINT;
2575 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002576 }
2577
Michal Vasko131120a2018-05-29 15:44:02 +02002578 if (!data) {
2579 /* error was already printed */
2580 return NC_MSG_ERROR;
2581 }
2582
Michal Vasko41adf392017-01-17 10:39:04 +01002583 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2584 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002585 if (dofree) {
2586 lyd_free(data);
2587 }
Michal Vasko086311b2016-01-08 09:53:11 +01002588 return NC_MSG_ERROR;
2589 }
2590
Michal Vasko131120a2018-05-29 15:44:02 +02002591 /* send RPC, store its message ID */
2592 r = nc_send_msg_io(session, timeout, data);
2593 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002594
Radek Krejcib4b19062018-02-07 16:33:06 +01002595 if (dofree) {
2596 lyd_free(data);
2597 }
Michal Vasko086311b2016-01-08 09:53:11 +01002598
Michal Vasko131120a2018-05-29 15:44:02 +02002599 if (r == NC_MSG_RPC) {
2600 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002601 }
Michal Vasko131120a2018-05-29 15:44:02 +02002602 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002603}
Michal Vaskode2946c2017-01-12 12:19:26 +01002604
2605API void
2606nc_client_session_set_not_strict(struct nc_session *session)
2607{
2608 if (session->side != NC_CLIENT) {
2609 ERRARG("session");
2610 return;
2611 }
2612
2613 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2614}