blob: b2a18543e54e196631d905b54ff00ee7e3e718cc [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 Vaskobbf52c82020-04-07 13:08:50 +020016
17#ifdef __linux__
18# include <sys/syscall.h>
19#endif
20
Michal Vaskob83a3fa2021-05-26 09:53:42 +020021#include <arpa/inet.h>
Michal Vasko086311b2016-01-08 09:53:11 +010022#include <assert.h>
tadeas-vintrlik54f142a2021-08-23 10:36:18 +020023#include <ctype.h>
Michal Vasko086311b2016-01-08 09:53:11 +010024#include <errno.h>
25#include <fcntl.h>
26#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010027#include <netinet/in.h>
Michal Vasko83ad17e2019-01-30 10:11:37 +010028#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020029#include <poll.h>
Michal Vasko086311b2016-01-08 09:53:11 +010030#include <pthread.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020031#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010032#include <stdlib.h>
33#include <string.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020034#include <sys/select.h>
Michal Vasko086311b2016-01-08 09:53:11 +010035#include <sys/socket.h>
36#include <sys/stat.h>
37#include <sys/types.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020038#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010039#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010040
41#include <libyang/libyang.h>
42
Michal Vasko9e8ac262020-04-07 13:06:45 +020043#include "compat.h"
Michal Vasko086311b2016-01-08 09:53:11 +010044#include "libnetconf.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010045#include "messages_client.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020046#include "session_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010047
Michal Vasko8a4e1462020-05-07 11:32:31 +020048#include "../modules/ietf_netconf@2013-09-29_yang.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020049#include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h"
Michal Vasko8a4e1462020-05-07 11:32:31 +020050
Michal Vasko80ef5d22016-01-18 09:21:02 +010051static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
52
Radek Krejci62aa0642017-05-25 16:33:49 +020053#ifdef NC_ENABLED_SSH
54int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
55char *sshauth_password(const char *username, const char *hostname, void *priv);
56char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020057char *sshauth_privkey_passphrase(const char *privkey_path, void *priv);
Radek Krejci62aa0642017-05-25 16:33:49 +020058#endif /* NC_ENABLED_SSH */
59
60static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
61static pthread_key_t nc_client_context_key;
62#ifdef __linux__
63static struct nc_client_context context_main = {
Michal Vaskoe49a15f2019-05-27 14:18:36 +020064 .opts.ka = {
65 .enabled = 1,
66 .idle_time = 1,
67 .max_probes = 10,
68 .probe_interval = 5
69 },
Radek Krejci62aa0642017-05-25 16:33:49 +020070#ifdef NC_ENABLED_SSH
71 .ssh_opts = {
72 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
73 .auth_hostkey_check = sshauth_hostkey_check,
74 .auth_password = sshauth_password,
75 .auth_interactive = sshauth_interactive,
76 .auth_privkey_passphrase = sshauth_privkey_passphrase
77 },
78 .ssh_ch_opts = {
79 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
80 .auth_hostkey_check = sshauth_hostkey_check,
81 .auth_password = sshauth_password,
82 .auth_interactive = sshauth_interactive,
83 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020084 },
Radek Krejci62aa0642017-05-25 16:33:49 +020085#endif /* NC_ENABLED_SSH */
86 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020087 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020088};
89#endif
90
91static void
92nc_client_context_free(void *ptr)
93{
94 struct nc_client_context *c = (struct nc_client_context *)ptr;
95
Radek Krejci5cebc6b2017-05-26 13:24:38 +020096 if (--(c->refcount)) {
97 /* still used */
98 return;
99 }
100
Radek Krejci62aa0642017-05-25 16:33:49 +0200101#ifdef __linux__
102 /* in __linux__ we use static memory in the main thread,
103 * so this check is for programs terminating the main()
104 * function by pthread_exit() :)
105 */
106 if (c != &context_main)
107#endif
108 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200109 /* for the main thread the same is done in nc_client_destroy() */
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400110 free(c->opts.schema_searchpath);
111
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200112#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400113 int i;
114 for (i = 0; i < c->opts.ch_bind_count; ++i) {
115 close(c->opts.ch_binds[i].sock);
116 free((char *)c->opts.ch_binds[i].address);
117 }
118 free(c->opts.ch_binds);
119 c->opts.ch_binds = NULL;
120 c->opts.ch_bind_count = 0;
Radek Krejci62aa0642017-05-25 16:33:49 +0200121#endif
122#ifdef NC_ENABLED_SSH
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400123 _nc_client_ssh_destroy_opts(&c->ssh_opts);
124 _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200125#endif
126#ifdef NC_ENABLED_TLS
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400127 _nc_client_tls_destroy_opts(&c->tls_opts);
128 _nc_client_tls_destroy_opts(&c->tls_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200129#endif
130 free(c);
131 }
132}
133
134static void
135nc_client_context_createkey(void)
136{
137 int r;
138
139 /* initiate */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200140 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {}
Radek Krejci62aa0642017-05-25 16:33:49 +0200141 pthread_setspecific(nc_client_context_key, NULL);
142}
143
144struct nc_client_context *
145nc_client_context_location(void)
146{
147 struct nc_client_context *e;
148
149 pthread_once(&nc_client_context_once, nc_client_context_createkey);
150 e = pthread_getspecific(nc_client_context_key);
151 if (!e) {
152 /* prepare ly_err storage */
153#ifdef __linux__
154 if (getpid() == syscall(SYS_gettid)) {
155 /* main thread - use global variable instead of thread-specific variable. */
156 e = &context_main;
157 } else
158#endif /* __linux__ */
159 {
160 e = calloc(1, sizeof *e);
161 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200163#ifdef NC_ENABLED_SSH
164 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
165 e->ssh_opts.auth_pref[0].value = 3;
166 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
167 e->ssh_opts.auth_pref[1].value = 2;
168 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
169 e->ssh_opts.auth_pref[2].value = 1;
170 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
171 e->ssh_opts.auth_password = sshauth_password;
172 e->ssh_opts.auth_interactive = sshauth_interactive;
173 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
174
175 /* callhome settings are the same except the inverted auth methods preferences */
176 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
177 e->ssh_ch_opts.auth_pref[0].value = 1;
178 e->ssh_ch_opts.auth_pref[1].value = 2;
179 e->ssh_ch_opts.auth_pref[2].value = 3;
180#endif /* NC_ENABLED_SSH */
181 }
182 pthread_setspecific(nc_client_context_key, e);
183 }
184
185 return e;
186}
187
188#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100189
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200190API void *
191nc_client_get_thread_context(void)
192{
193 return nc_client_context_location();
194}
195
196API void
197nc_client_set_thread_context(void *context)
198{
199 struct nc_client_context *old, *new;
200
201 if (!context) {
202 ERRARG(context);
203 return;
204 }
205
206 new = (struct nc_client_context *)context;
207 old = nc_client_context_location();
208 if (old == new) {
209 /* nothing to change */
210 return;
211 }
212
213 /* replace old by new, increase reference counter in the newly set context */
214 nc_client_context_free(old);
215 new->refcount++;
216 pthread_setspecific(nc_client_context_key, new);
217}
218
Radek Krejcifd5b6682017-06-13 15:52:53 +0200219int
220nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
221{
222 /* assign context (dicionary needed for handshake) */
223 if (!ctx) {
Michal Vasko77367452021-02-16 16:32:18 +0100224 if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200225 return EXIT_FAILURE;
226 }
227
228 /* user path must be first, the first path is used to store schemas retreived via get-schema */
229 if (client_opts.schema_searchpath) {
230 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200231 } else if (!access(NC_YANG_DIR, F_OK)) {
232 ly_ctx_set_searchdir(ctx, NC_YANG_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200233 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200234
235 /* set callback for getting schemas, if provided */
236 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
237 } else {
238 session->flags |= NC_SESSION_SHAREDCTX;
239 }
240
241 session->ctx = ctx;
242
243 return EXIT_SUCCESS;
244}
245
Michal Vasko086311b2016-01-08 09:53:11 +0100246API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100247nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100248{
Michal Vasko3031aae2016-01-27 16:07:18 +0100249 if (client_opts.schema_searchpath) {
250 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100251 }
Michal Vasko086311b2016-01-08 09:53:11 +0100252
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100253 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100254 client_opts.schema_searchpath = strdup(path);
255 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100256 ERRMEM;
257 return 1;
258 }
259 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100260 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100261 }
262
263 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100264}
265
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100266API const char *
267nc_client_get_schema_searchpath(void)
268{
269 return client_opts.schema_searchpath;
270}
271
Radek Krejcifd5b6682017-06-13 15:52:53 +0200272API int
273nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100274{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200275 client_opts.schema_clb = clb;
276 if (clb) {
277 client_opts.schema_clb_data = user_data;
278 } else {
279 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100280 }
281
282 return 0;
283}
284
Radek Krejcifd5b6682017-06-13 15:52:53 +0200285API ly_module_imp_clb
286nc_client_get_schema_callback(void **user_data)
287{
288 if (user_data) {
289 (*user_data) = client_opts.schema_clb_data;
290 }
291 return client_opts.schema_clb;
292}
293
Radek Krejci65ef6d52018-08-16 16:35:02 +0200294struct schema_info {
295 char *name;
296 char *revision;
297 struct {
298 char *name;
299 char *revision;
300 } *submodules;
Michal Vasko77367452021-02-16 16:32:18 +0100301 char **features;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200302 int implemented;
303};
304
305struct clb_data_s {
306 void *user_data;
307 ly_module_imp_clb user_clb;
308 struct schema_info *schemas;
309 struct nc_session *session;
310 int has_get_schema;
311};
312
Michal Vaskoc088f982021-10-05 12:23:07 +0200313/**
314 * @brief Retrieve YANG schema content from a local file.
315 *
316 * @param[in] name Schema name.
317 * @param[in] rev Schema revision.
318 * @param[in] clb_data get-schema callback data.
319 * @param[out] format Schema format.
320 * @return Schema content.
321 */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200322static char *
323retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200324 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100325{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200326 char *localfile = NULL;
327 FILE *f;
328 long length, l;
329 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100330
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200331 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200332 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
333 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200334 return NULL;
335 }
336 if (localfile) {
Michal Vasko05532772021-06-03 12:12:38 +0200337 VRB(clb_data->session, "Reading schema from localfile \"%s\".", localfile);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200338 f = fopen(localfile, "r");
339 if (!f) {
Michal Vasko05532772021-06-03 12:12:38 +0200340 ERR(clb_data->session, "Unable to open \"%s\" file to get schema (%s).", localfile, strerror(errno));
Radek Krejci65ef6d52018-08-16 16:35:02 +0200341 free(localfile);
342 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100343 }
Michal Vasko086311b2016-01-08 09:53:11 +0100344
Radek Krejci65ef6d52018-08-16 16:35:02 +0200345 fseek(f, 0, SEEK_END);
346 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200347 if (length < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200348 ERR(clb_data->session, "Unable to get size of schema file \"%s\".", localfile);
Radek Krejci3f499a62018-08-17 10:16:57 +0200349 free(localfile);
350 fclose(f);
351 return NULL;
352 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200353 fseek(f, 0, SEEK_SET);
354
355 model_data = malloc(length + 1);
356 if (!model_data) {
357 ERRMEM;
358 } else if ((l = fread(model_data, 1, length, f)) != length) {
Michal Vasko05532772021-06-03 12:12:38 +0200359 ERR(clb_data->session, "Reading schema from \"%s\" failed (%d bytes read, but %d expected).", localfile, l,
360 length);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200361 free(model_data);
362 model_data = NULL;
363 } else {
364 /* terminating NULL byte */
365 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100366 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200367 fclose(f);
368 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100369 }
370
Radek Krejci65ef6d52018-08-16 16:35:02 +0200371 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100372}
373
Michal Vaskoc088f982021-10-05 12:23:07 +0200374/**
375 * @brief Retrieve YANG schema content from a reply to get-schema RPC.
376 *
377 * @param[in] name Schema name.
378 * @param[in] rev Schema revision.
379 * @param[in] clb_data get-schema callback data.
380 * @param[out] format Schema format.
381 * @return Schema content.
382 */
Michal Vasko086311b2016-01-08 09:53:11 +0100383static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200384retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200385 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100386{
Michal Vasko086311b2016-01-08 09:53:11 +0100387 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100388 struct lyd_node *envp = NULL, *op = NULL;
389 struct lyd_node_any *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100390 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100391 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200392 char *localfile = NULL;
393 FILE *f;
394 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100395
Michal Vasko05532772021-06-03 12:12:38 +0200396 VRB(clb_data->session, "Reading schema from server via get-schema.");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200397 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100398
Radek Krejci65ef6d52018-08-16 16:35:02 +0200399 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100400 usleep(1000);
401 }
402 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200403 ERR(clb_data->session, "Failed to send the <get-schema> RPC.");
Michal Vasko086311b2016-01-08 09:53:11 +0100404 nc_rpc_free(rpc);
405 return NULL;
406 }
407
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200408 do {
Michal Vasko77367452021-02-16 16:32:18 +0100409 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200410 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100411 nc_rpc_free(rpc);
412 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko05532772021-06-03 12:12:38 +0200413 ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired.");
Michal Vasko77367452021-02-16 16:32:18 +0100414 goto cleanup;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200415 } else if ((msg == NC_MSG_ERROR) || !op) {
Michal Vasko05532772021-06-03 12:12:38 +0200416 ERR(clb_data->session, "Failed to receive a reply to <get-schema>.");
Michal Vasko77367452021-02-16 16:32:18 +0100417 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100418 }
419
Michal Vasko77367452021-02-16 16:32:18 +0100420 if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) {
Michal Vasko05532772021-06-03 12:12:38 +0200421 ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC.");
Michal Vasko77367452021-02-16 16:32:18 +0100422 goto cleanup;
Michal Vaskob2583f12016-05-12 11:40:23 +0200423 }
Michal Vasko77367452021-02-16 16:32:18 +0100424 get_schema_data = (struct lyd_node_any *)lyd_child(op);
Radek Krejci539efb62016-08-24 15:05:16 +0200425 switch (get_schema_data->value_type) {
Radek Krejci539efb62016-08-24 15:05:16 +0200426 case LYD_ANYDATA_STRING:
Michal Vasko77367452021-02-16 16:32:18 +0100427 case LYD_ANYDATA_XML:
Michal Vaskob2583f12016-05-12 11:40:23 +0200428 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200429 break;
430 case LYD_ANYDATA_DATATREE:
Michal Vasko77367452021-02-16 16:32:18 +0100431 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Radek Krejci539efb62016-08-24 15:05:16 +0200432 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200433 case LYD_ANYDATA_JSON:
Michal Vaskod838d292018-08-15 11:39:05 +0200434 case LYD_ANYDATA_LYB:
Radek Krejci03438ec2016-09-21 14:12:26 +0200435 ERRINT;
Michal Vasko77367452021-02-16 16:32:18 +0100436 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200437 }
Michal Vasko086311b2016-01-08 09:53:11 +0100438
Radek Krejci488b0702018-08-29 14:47:15 +0200439 if (model_data && !model_data[0]) {
440 /* empty data */
441 free(model_data);
442 model_data = NULL;
443 }
444
Radek Krejcifd5b6682017-06-13 15:52:53 +0200445 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200446 if (model_data) {
447 *format = LYS_IN_YANG;
448 if (client_opts.schema_searchpath) {
Michal Vasko77367452021-02-16 16:32:18 +0100449 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name, rev ? "@" : "",
450 rev ? rev : "") == -1) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200451 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100452 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200453 f = fopen(localfile, "w");
454 if (!f) {
Michal Vasko05532772021-06-03 12:12:38 +0200455 WRN(clb_data->session, "Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200456 localfile, strerror(errno));
Radek Krejci65ef6d52018-08-16 16:35:02 +0200457 } else {
458 fputs(model_data, f);
459 fclose(f);
460 }
461 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100462 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200463 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200464 }
465
Michal Vasko77367452021-02-16 16:32:18 +0100466cleanup:
467 lyd_free_tree(envp);
468 lyd_free_tree(op);
Michal Vasko086311b2016-01-08 09:53:11 +0100469 return model_data;
470}
471
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200472static void
473free_with_user_data(void *data, void *user_data)
Jan Kundrát35972df2018-09-06 19:00:01 +0200474{
475 free(data);
476 (void)user_data;
477}
478
Michal Vaskoc088f982021-10-05 12:23:07 +0200479/**
480 * @brief Retrieve YANG schema content.
481 *
482 * @param[in] mod_name Schema name.
483 * @param[in] mod_rev Schema revision.
484 * @param[in] submod_name Optional submodule name.
485 * @param[in] sub_rev Submodule revision.
486 * @param[in] user_data get-schema callback data.
487 * @param[out] format Schema format.
488 * @param[out] module_data Schema content.
489 * @param[out] free_module_data Callback for freeing @p module_data.
490 * @return LY_ERR value.
491 */
Michal Vasko77367452021-02-16 16:32:18 +0100492static LY_ERR
Radek Krejci65ef6d52018-08-16 16:35:02 +0200493retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
Michal Vasko77367452021-02-16 16:32:18 +0100494 void *user_data, LYS_INFORMAT *format, const char **module_data,
495 void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200496{
497 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
498 unsigned int u, v, match = 1;
499 const char *name = NULL, *rev = NULL;
500 char *model_data = NULL;
501
502 /* get and check the final name and revision of the schema to be retrieved */
503 if (!mod_rev || !mod_rev[0]) {
504 /* newest revision requested - get the newest revision from the list of available modules on server */
505 match = 0;
506 for (u = 0; clb_data->schemas[u].name; ++u) {
507 if (strcmp(mod_name, clb_data->schemas[u].name)) {
508 continue;
509 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200510 if (!match || (strcmp(mod_rev, clb_data->schemas[u].revision) > 0)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200511 mod_rev = clb_data->schemas[u].revision;
512 }
513 match = u + 1;
514 }
515 if (!match) {
Michal Vasko1d2665c2021-02-12 09:28:44 +0100516 /* valid situation if we are retrieving YANG 1.1 schema and have only capabilities for now
517 * (when loading ietf-datastore for ietf-yang-library) */
Michal Vasko05532772021-06-03 12:12:38 +0200518 VRB(clb_data->session, "Unable to identify revision of the schema \"%s\" from "
519 "the available server side information.", mod_name);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200520 }
521 }
522 if (submod_name) {
523 name = submod_name;
524 if (sub_rev) {
525 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200526 } else if (match) {
527 if (!clb_data->schemas[match - 1].submodules) {
Michal Vasko05532772021-06-03 12:12:38 +0200528 VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", "
529 "in schema \"%s\", from the available server side information.", submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200530 } else {
531 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
532 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
533 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
534 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200535 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200536 if (!rev) {
Michal Vasko05532772021-06-03 12:12:38 +0200537 ERR(clb_data->session, "Requested submodule \"%s\" is not known for schema \"%s\" on server side.",
538 submod_name, mod_name);
Michal Vasko77367452021-02-16 16:32:18 +0100539 return LY_ENOTFOUND;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200540 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200541 }
542 }
543 } else {
544 name = mod_name;
545 rev = mod_rev;
546 }
547
Michal Vasko05532772021-06-03 12:12:38 +0200548 VRB(clb_data->session, "Retrieving data for schema \"%s\", revision \"%s\".", name, rev ? rev : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200549
550 if (match) {
551 /* we have enough information to avoid communication with server and try to get
552 * the schema locally */
553
554 /* 1. try to get data locally */
555 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
556
557 /* 2. try to use <get-schema> */
558 if (!model_data && clb_data->has_get_schema) {
559 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
560 }
561 } else {
562 /* we are unsure which revision of the schema we should load, so first try to get
563 * the newest revision from the server via get-schema and only if the server does not
564 * implement get-schema, try to load the newest revision locally. This is imperfect
565 * solution, but there are situation when a client does not know what revision is
566 * actually implemented by the server. */
567
568 /* 1. try to use <get-schema> */
569 if (clb_data->has_get_schema) {
570 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
571 }
572
573 /* 2. try to get data locally */
574 if (!model_data) {
575 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
576 }
577 }
578
579 /* 3. try to use user callback */
580 if (!model_data && clb_data->user_clb) {
Michal Vasko05532772021-06-03 12:12:38 +0200581 VRB(clb_data->session, "Reading schema via user callback.");
Michal Vasko77367452021-02-16 16:32:18 +0100582 clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format,
583 (const char **)&model_data, free_module_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200584 }
585
Jan Kundrát35972df2018-09-06 19:00:01 +0200586 *free_module_data = free_with_user_data;
Michal Vasko77367452021-02-16 16:32:18 +0100587 *module_data = model_data;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200588 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200589}
590
Michal Vaskoc088f982021-10-05 12:23:07 +0200591/**
592 * @brief Load a YANG schema into context.
593 *
594 * @param[in] session NC session.
595 * @param[in] name Schema name.
596 * @param[in] revision Schema revision.
597 * @param[in] schemas Server schema info built from capabilities.
598 * @param[in] user_clb User callback for retireving schema data.
599 * @param[in] user_data User data for @p user_clb.
600 * @param[in] has_get_schema Whether the server supports get-schema.
601 * @param[out] mod Loaded module.
602 * @return 0 on success.
603 * @return -1 on error.
604 */
Michal Vaskoceae0152018-02-14 16:03:59 +0100605static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200606nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Michal Vasko77367452021-02-16 16:32:18 +0100607 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100608{
609 int ret = 0;
610 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200611 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200612 LYS_INFORMAT format;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200613
614 void (*free_module_data)(void *, void *) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200615 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100616
Radek Krejci65ef6d52018-08-16 16:35:02 +0200617 *mod = NULL;
618 if (revision) {
Michal Vasko77367452021-02-16 16:32:18 +0100619 *mod = ly_ctx_get_module(session->ctx, name, revision);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200620 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100621 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200622 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100623 /* make the present module implemented */
Michal Vasko77367452021-02-16 16:32:18 +0100624 if (lys_set_implemented(*mod, NULL)) {
Michal Vasko05532772021-06-03 12:12:38 +0200625 ERR(session, "Failed to implement model \"%s\".", (*mod)->name);
Michal Vaskoceae0152018-02-14 16:03:59 +0100626 ret = -1;
627 }
628 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200629 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100630 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200631 clb_data.has_get_schema = has_get_schema;
632 clb_data.schemas = schemas;
633 clb_data.session = session;
634 clb_data.user_clb = user_clb;
635 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100636
637 /* clear all the errors and just collect them for now */
638 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200639 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100640
Radek Krejci65ef6d52018-08-16 16:35:02 +0200641 /* get module data */
Michal Vasko77367452021-02-16 16:32:18 +0100642 retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &module_data, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100643
Radek Krejci65ef6d52018-08-16 16:35:02 +0200644 if (module_data) {
645 /* parse the schema */
646 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100647
Michal Vasko029c5372021-07-09 10:54:21 +0200648 lys_parse_mem(session->ctx, module_data, format, mod);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200649 if (*free_module_data) {
Michal Vasko77367452021-02-16 16:32:18 +0100650 (*free_module_data)((char *)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100651 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100652
Radek Krejci65ef6d52018-08-16 16:35:02 +0200653 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
654 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100655
Michal Vaskodbf7c272018-02-19 09:07:59 +0100656 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200657 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100658 if (!(*mod)) {
659 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
Michal Vasko77367452021-02-16 16:32:18 +0100660 ly_err_print(session->ctx, eitem);
Michal Vaskoceae0152018-02-14 16:03:59 +0100661 }
662 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200663 } else {
664 /* print only warnings */
665 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
666 if (eitem->level == LY_LLWRN) {
Michal Vasko77367452021-02-16 16:32:18 +0100667 ly_err_print(session->ctx, eitem);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200668 }
669 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100670 }
671
Michal Vaskodbf7c272018-02-19 09:07:59 +0100672 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100673 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100674 }
675
676 return ret;
677}
678
Radek Krejci65ef6d52018-08-16 16:35:02 +0200679static void
680free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200681{
Michal Vasko77367452021-02-16 16:32:18 +0100682 uint32_t u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200683
Radek Krejci65ef6d52018-08-16 16:35:02 +0200684 if (!list) {
685 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200686 }
687
Radek Krejci65ef6d52018-08-16 16:35:02 +0200688 for (u = 0; list[u].name; ++u) {
689 free(list[u].name);
690 free(list[u].revision);
Michal Vasko77367452021-02-16 16:32:18 +0100691 if (list[u].features) {
692 for (v = 0; list[u].features[v]; ++v) {
693 free(list[u].features[v]);
694 }
695 free(list[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200696 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200697 if (list[u].submodules) {
698 for (v = 0; list[u].submodules[v].name; ++v) {
699 free(list[u].submodules[v].name);
700 free(list[u].submodules[v].revision);
701 }
702 free(list[u].submodules);
703 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200704 }
705 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200706}
707
Michal Vaskoc088f982021-10-05 12:23:07 +0200708/**
709 * @brief Build server schema info from ietf-yang-library data.
710 *
711 * @param[in] session NC session.
712 * @param[in] has_get_data Whether get-data RPC is available or only get.
713 * @param[out] result Server schemas.
714 * @return 0 on success.
715 * @return -1 on error.
716 */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200717static int
Michal Vasko303263d2021-10-05 12:18:21 +0200718build_schema_info_yl(struct nc_session *session, int has_get_data, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200719{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200720 struct nc_rpc *rpc = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100721 struct lyd_node *op = NULL, *envp = NULL;
722 struct lyd_node_any *data;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200723 NC_MSG_TYPE msg;
724 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200725 struct ly_set *modules = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100726 uint32_t u, v, submodules_count, feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200727 struct lyd_node *iter, *child;
728 struct lys_module *mod;
Michal Vasko303263d2021-10-05 12:18:21 +0200729 int ret = 0;
730 const char *rpc_name;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200731
732 /* get yang-library data from the server */
Michal Vasko303263d2021-10-05 12:18:21 +0200733 if (has_get_data) {
734 rpc_name = "get-data";
735 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
736 rpc = nc_rpc_getdata("ietf-datastores:operational", "/ietf-yang-library:*", "false", NULL, 0, 0, 0, 0, 0,
737 NC_PARAMTYPE_CONST);
738 } else {
739 rpc = nc_rpc_getdata("ietf-datastores:operational",
740 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", "false", NULL, 0, 0, 0, 0,
741 0, NC_PARAMTYPE_CONST);
742 }
Radek Krejci261737f2018-08-21 09:22:34 +0200743 } else {
Michal Vasko303263d2021-10-05 12:18:21 +0200744 rpc_name = "get";
745 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
746 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
747 } else {
748 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0,
749 NC_PARAMTYPE_CONST);
750 }
Radek Krejci261737f2018-08-21 09:22:34 +0200751 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200752 if (!rpc) {
753 goto cleanup;
754 }
755
756 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
757 usleep(1000);
758 }
759 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200760 WRN(session, "Failed to send request for yang-library data.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200761 goto cleanup;
762 }
763
764 do {
Michal Vasko77367452021-02-16 16:32:18 +0100765 lyd_free_tree(envp);
766 lyd_free_tree(op);
767
768 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200769 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200770 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko303263d2021-10-05 12:18:21 +0200771 WRN(session, "Timeout for receiving reply to a <%s> yang-library data expired.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200772 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100773 } else if (msg == NC_MSG_ERROR) {
Michal Vasko303263d2021-10-05 12:18:21 +0200774 WRN(session, "Failed to receive a reply to <%s> of yang-library data.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200775 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100776 } else if (!op || !lyd_child(op) || strcmp(lyd_child(op)->schema->name, "data")) {
Michal Vasko303263d2021-10-05 12:18:21 +0200777 WRN(session, "Unexpected reply without data to a yang-library <%s> RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200778 goto cleanup;
779 }
780
Michal Vasko77367452021-02-16 16:32:18 +0100781 data = (struct lyd_node_any *)lyd_child(op);
782 if (data->value_type != LYD_ANYDATA_DATATREE) {
Michal Vasko303263d2021-10-05 12:18:21 +0200783 WRN(session, "Unexpected data in reply to a yang-library <%s> RPC.", rpc_name);
Michal Vasko40528752021-06-21 15:41:51 +0200784 goto cleanup;
785 } else if (!data->value.tree) {
Michal Vasko303263d2021-10-05 12:18:21 +0200786 WRN(session, "No data in reply to a yang-library <%s> RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200787 goto cleanup;
788 }
789
Michal Vasko77367452021-02-16 16:32:18 +0100790 if (lyd_find_xpath(data->value.tree, "/ietf-yang-library:modules-state/module", &modules)) {
Michal Vasko303263d2021-10-05 12:18:21 +0200791 WRN(session, "No module information in reply to a yang-library <%s> RPC.", rpc_name);
Radek Krejci2d088832018-08-21 13:40:14 +0200792 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200793 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200794
Michal Vasko77367452021-02-16 16:32:18 +0100795 (*result) = calloc(modules->count + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200796 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200797 ERRMEM;
Michal Vasko303263d2021-10-05 12:18:21 +0200798 ret = -1;
Radek Krejcic9390502018-08-17 10:23:13 +0200799 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200800 }
801
Michal Vasko77367452021-02-16 16:32:18 +0100802 for (u = 0; u < modules->count; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200803 submodules_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100804 feature_count = 0;
805 mod = ((struct lyd_node *)modules->dnodes[u])->schema->module;
806 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
Michal Vasko5d9d5972021-06-09 14:17:01 +0200807 if (!iter->schema || (iter->schema->module != mod)) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200808 /* ignore node from other schemas (augments) */
809 continue;
810 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200811 if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200812 /* ignore empty nodes */
813 continue;
814 }
815 if (!strcmp(iter->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200816 (*result)[u].name = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200817 } else if (!strcmp(iter->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200818 (*result)[u].revision = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200819 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200820 (*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200821 } else if (!strcmp(iter->schema->name, "feature")) {
Michal Vasko77367452021-02-16 16:32:18 +0100822 (*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features);
823 if (!(*result)[u].features) {
824 ERRMEM;
825 free_schema_info(*result);
826 *result = NULL;
Michal Vasko303263d2021-10-05 12:18:21 +0200827 ret = -1;
Michal Vasko77367452021-02-16 16:32:18 +0100828 goto cleanup;
829 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200830 (*result)[u].features[feature_count] = strdup(lyd_get_value(iter));
Michal Vasko77367452021-02-16 16:32:18 +0100831 (*result)[u].features[feature_count + 1] = NULL;
832 ++feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200833 } else if (!strcmp(iter->schema->name, "submodule")) {
834 submodules_count++;
835 }
836 }
837
838 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200839 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
840 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200841 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200842 free_schema_info(*result);
843 *result = NULL;
Michal Vasko303263d2021-10-05 12:18:21 +0200844 ret = -1;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200845 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200846 } else {
847 v = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100848 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
849 mod = modules->dnodes[u]->schema->module;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200850 if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) {
Michal Vasko77367452021-02-16 16:32:18 +0100851 LY_LIST_FOR(lyd_child(iter), child) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200852 if (mod != child->schema->module) {
853 continue;
854 } else if (!strcmp(child->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200855 (*result)[u].submodules[v].name = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200856 } else if (!strcmp(child->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200857 (*result)[u].submodules[v].revision = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200858 }
859 }
860 }
861 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200862 }
863 }
864 }
865
Radek Krejcifd5b6682017-06-13 15:52:53 +0200866cleanup:
867 nc_rpc_free(rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100868 lyd_free_tree(envp);
869 lyd_free_tree(op);
870 ly_set_free(modules, NULL);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200871
Radek Krejci235d8cb2018-08-17 14:04:32 +0200872 if (session->status != NC_STATUS_RUNNING) {
Michal Vasko05532772021-06-03 12:12:38 +0200873 /* something bad happened, discard the session */
874 ERR(session, "Invalid session, discarding.");
Michal Vaskoc088f982021-10-05 12:23:07 +0200875 ret = -1;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200876 }
877
878 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200879}
880
Michal Vaskoc088f982021-10-05 12:23:07 +0200881/**
882 * @brief Build server schema info from received capabilities.
883 *
884 * @param[in] cpblts Server capabilities.
885 * @param[out] result Server schemas.
886 * @return 0 on success.
887 * @return -1 on error.
888 */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200889static int
890build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200891{
Michal Vasko77367452021-02-16 16:32:18 +0100892 uint32_t u, v, feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200893 char *module_cpblt, *ptr, *ptr2;
894
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200895 for (u = 0; cpblts[u]; ++u) {}
Radek Krejci235d8cb2018-08-17 14:04:32 +0200896 (*result) = calloc(u + 1, sizeof **result);
897 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200898 ERRMEM;
Michal Vasko303263d2021-10-05 12:18:21 +0200899 return -1;
Radek Krejcic9390502018-08-17 10:23:13 +0200900 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200901
902 for (u = v = 0; cpblts[u]; ++u) {
903 module_cpblt = strstr(cpblts[u], "module=");
904 /* this capability requires a module */
905 if (!module_cpblt) {
906 continue;
907 }
908
909 /* get module's name */
910 ptr = (char *)module_cpblt + 7;
911 ptr2 = strchr(ptr, '&');
912 if (!ptr2) {
913 ptr2 = ptr + strlen(ptr);
914 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200915 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200916
917 /* get module's revision */
918 ptr = strstr(module_cpblt, "revision=");
919 if (ptr) {
920 ptr += 9;
921 ptr2 = strchr(ptr, '&');
922 if (!ptr2) {
923 ptr2 = ptr + strlen(ptr);
924 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200925 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200926 }
927
928 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200929 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200930
931 /* get module's features */
932 ptr = strstr(module_cpblt, "features=");
933 if (ptr) {
934 ptr += 9;
Michal Vasko77367452021-02-16 16:32:18 +0100935 feature_count = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200936 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
937 if (*ptr == ',') {
Michal Vasko77367452021-02-16 16:32:18 +0100938 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
939 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
940 (*result)[v].features[feature_count + 1] = NULL;
941 ++feature_count;
942
Radek Krejci65ef6d52018-08-16 16:35:02 +0200943 ptr2 = ptr + 1;
944 }
945 }
946 /* the last one */
Michal Vasko77367452021-02-16 16:32:18 +0100947 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
948 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
949 (*result)[v].features[feature_count + 1] = NULL;
950 ++feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200951 }
952 ++v;
953 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200954
Michal Vasko303263d2021-10-05 12:18:21 +0200955 return 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200956}
957
Michal Vaskoc088f982021-10-05 12:23:07 +0200958/**
959 * @brief Fill client context based on server schema info.
960 *
961 * @param[in] session NC session with the context to modify.
962 * @param[in] modules Server schema info.
963 * @param[in] user_clb User callback for retrieving specific schemas.
964 * @param[in] user_data User data for @p user_clb.
965 * @param[in] has_get_schema Whether server supports get-schema RPC.
966 * @return 0 on success.
967 * @return -1 on error.
968 */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200969static int
Michal Vasko77367452021-02-16 16:32:18 +0100970nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data,
971 int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200972{
Michal Vasko303263d2021-10-05 12:18:21 +0200973 int ret = -1;
Michal Vasko77367452021-02-16 16:32:18 +0100974 struct lys_module *mod;
975 uint32_t u;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200976
977 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200978 /* skip import-only modules */
979 if (!modules[u].implemented) {
980 continue;
981 }
982
Radek Krejci65ef6d52018-08-16 16:35:02 +0200983 /* we can continue even if it fails */
984 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
985
986 if (!mod) {
987 if (session->status != NC_STATUS_RUNNING) {
988 /* something bad heppened, discard the session */
Michal Vasko05532772021-06-03 12:12:38 +0200989 ERR(session, "Invalid session, discarding.");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200990 goto cleanup;
991 }
992
993 /* all loading ways failed, the schema will be ignored in the received data */
Michal Vasko05532772021-06-03 12:12:38 +0200994 WRN(session, "Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ?
995 modules[u].revision : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200996 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
997 } else {
Michal Vasko77367452021-02-16 16:32:18 +0100998 /* set the features */
999 lys_set_implemented(mod, (const char **)modules[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001000 }
Michal Vasko60f66602017-10-17 13:52:18 +02001001 }
1002
Michal Vasko77367452021-02-16 16:32:18 +01001003 /* success */
Michal Vasko303263d2021-10-05 12:18:21 +02001004 ret = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001005
1006cleanup:
Radek Krejcifd5b6682017-06-13 15:52:53 +02001007 return ret;
1008}
1009
Michal Vaskoc088f982021-10-05 12:23:07 +02001010/**
1011 * @brief Fill client context with ietf-netconf schema.
1012 *
1013 * @param[in] session NC session with the context to modify.
1014 * @param[in] modules Server schema info.
1015 * @param[in] user_clb User callback for retrieving specific schemas.
1016 * @param[in] user_data User data for @p user_clb.
1017 * @param[in] has_get_schema Whether server supports get-schema RPC.
1018 * @return 0 on success.
1019 * @return -1 on error.
1020 */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001021static int
Michal Vasko77367452021-02-16 16:32:18 +01001022nc_ctx_fill_ietf_netconf(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb,
1023 void *user_data, int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001024{
Michal Vasko77367452021-02-16 16:32:18 +01001025 uint32_t u;
1026 struct lys_module *ietfnc;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001027
Michal Vasko77367452021-02-16 16:32:18 +01001028 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001029 if (!ietfnc) {
1030 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +02001031 if (!ietfnc) {
Michal Vasko029c5372021-07-09 10:54:21 +02001032 lys_parse_mem(session->ctx, ietf_netconf_2013_09_29_yang, LYS_IN_YANG, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +02001033 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001034 }
1035 if (!ietfnc) {
Michal Vasko05532772021-06-03 12:12:38 +02001036 ERR(session, "Loading base NETCONF schema failed.");
Michal Vasko303263d2021-10-05 12:18:21 +02001037 return -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001038 }
1039
1040 /* set supported capabilities from ietf-netconf */
1041 for (u = 0; modules[u].name; ++u) {
1042 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
1043 continue;
1044 }
1045
Michal Vasko77367452021-02-16 16:32:18 +01001046 lys_set_implemented(ietfnc, (const char **)modules[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001047 }
1048
1049 return 0;
1050}
1051
Michal Vasko086311b2016-01-08 09:53:11 +01001052int
1053nc_ctx_check_and_fill(struct nc_session *session)
1054{
Michal Vasko303263d2021-10-05 12:18:21 +02001055 int i, get_schema_support = 0, yanglib_support = 0, get_data_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +01001056 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001057 void *old_data = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001058 struct lys_module *mod = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +02001059 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +02001060 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001061
Michal Vasko2e6defd2016-10-07 15:48:15 +02001062 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +01001063
Radek Krejci65ef6d52018-08-16 16:35:02 +02001064 /* 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 +02001065 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001066
Radek Krejci65ef6d52018-08-16 16:35:02 +02001067 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
1068 * schemas only to those present on the server side */
Michal Vasko77367452021-02-16 16:32:18 +01001069 ly_ctx_set_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001070
1071 /* our callback is set later with appropriate data */
1072 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
1073
1074 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001075 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001076 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001077 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +02001078 if (yanglib_support) {
1079 break;
1080 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001081 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001082 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +02001083 if (get_schema_support) {
1084 break;
1085 }
Michal Vasko086311b2016-01-08 09:53:11 +01001086 }
1087 }
Michal Vasko31dd4c52020-04-17 10:29:44 +02001088 if (get_schema_support) {
Michal Vasko05532772021-06-03 12:12:38 +02001089 VRB(session, "Capability for <get-schema> support found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +02001090 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001091 VRB(session, "Capability for <get-schema> support not found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +02001092 }
1093 if (yanglib_support) {
Michal Vasko05532772021-06-03 12:12:38 +02001094 VRB(session, "Capability for yang-library support found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +02001095 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001096 VRB(session, "Capability for yang-library support not found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +02001097 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001098
1099 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001100 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
Michal Vasko05532772021-06-03 12:12:38 +02001101 ERR(session, "Unable to get server's schema information from the <hello>'s capabilities.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001102 goto cleanup;
1103 }
1104
Michal Vasko086311b2016-01-08 09:53:11 +01001105 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Michal Vasko77367452021-02-16 16:32:18 +01001106 if (get_schema_support && lys_parse_mem(session->ctx, ietf_netconf_monitoring_2010_10_04_yang, LYS_IN_YANG, NULL)) {
Michal Vasko05532772021-06-03 12:12:38 +02001107 WRN(session, "Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Michal Vasko8a4e1462020-05-07 11:32:31 +02001108 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001109 }
1110
1111 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001112 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001113 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001114 }
1115
Radek Krejci65ef6d52018-08-16 16:35:02 +02001116 /* get correct version of ietf-yang-library into context */
1117 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001118 /* use get schema to get server's ietf-yang-library */
1119 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1120 if (!revision) {
Michal Vasko05532772021-06-03 12:12:38 +02001121 WRN(session, "Loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.");
1122 WRN(session, "Unable to automatically use <get-schema>.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001123 yanglib_support = 0;
1124 } else {
1125 revision = strndup(&revision[9], 10);
Michal Vasko0d877f92020-04-02 13:52:43 +02001126 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data,
1127 get_schema_support, &mod)) {
Michal Vasko05532772021-06-03 12:12:38 +02001128 WRN(session, "Loading NETCONF ietf-yang-library schema failed, unable to use it to learn all "
1129 "the supported modules.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001130 yanglib_support = 0;
1131 }
Michal Vasko0d877f92020-04-02 13:52:43 +02001132 if (strcmp(revision, "2019-01-04") >= 0) {
1133 /* we also need ietf-datastores to be implemented */
1134 if (nc_ctx_load_module(session, "ietf-datastores", NULL, server_modules, old_clb, old_data,
1135 get_schema_support, &mod)) {
Michal Vasko05532772021-06-03 12:12:38 +02001136 WRN(session, "Loading NETCONF ietf-datastores schema failed, unable to use yang-library "
1137 "to learn all the supported modules.");
Michal Vasko0d877f92020-04-02 13:52:43 +02001138 yanglib_support = 0;
1139 }
1140 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001141 free(revision);
Michal Vasko77367452021-02-16 16:32:18 +01001142
1143 /* ietf-netconf-nmda is needed to issue get-data */
Michal Vasko303263d2021-10-05 12:18:21 +02001144 if (!nc_ctx_load_module(session, "ietf-netconf-nmda", NULL, server_modules, old_clb, old_data,
Michal Vasko77367452021-02-16 16:32:18 +01001145 get_schema_support, &mod)) {
Michal Vasko303263d2021-10-05 12:18:21 +02001146 VRB(session, "Support for <get-data> from ietf-netcon-nmda found.");
1147 get_data_support = 1;
Michal Vasko77367452021-02-16 16:32:18 +01001148 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001149 }
1150 }
1151
Radek Krejci65ef6d52018-08-16 16:35:02 +02001152 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001153 if (yanglib_support) {
Michal Vasko303263d2021-10-05 12:18:21 +02001154 if (build_schema_info_yl(session, get_data_support, &sm)) {
Radek Krejcif0633792018-08-20 15:12:09 +02001155 goto cleanup;
1156 } else if (!sm) {
Michal Vasko05532772021-06-03 12:12:38 +02001157 VRB(session, "Trying to use capabilities instead of ietf-yang-library data.");
Radek Krejcif0633792018-08-20 15:12:09 +02001158 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001159 /* prefer yang-library information, currently we have it from capabilities used for getting correct
1160 * yang-library schema */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001161 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001162 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001163 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001164 }
Michal Vaskoef578332016-01-25 13:20:09 +01001165
Radek Krejci65ef6d52018-08-16 16:35:02 +02001166 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1167 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001168 }
1169
Radek Krejcifd5b6682017-06-13 15:52:53 +02001170 /* succsess */
1171 ret = 0;
1172
Michal Vaskoeee99412016-11-21 10:19:43 +01001173 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Michal Vasko05532772021-06-03 12:12:38 +02001174 WRN(session, "Some models failed to be loaded, any data from these models (and any other unknown) will "
1175 "be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +01001176 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001177
1178cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001179 free_schema_info(server_modules);
1180
Radek Krejcifd5b6682017-06-13 15:52:53 +02001181 /* set user callback back */
1182 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vasko77367452021-02-16 16:32:18 +01001183 ly_ctx_unset_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001184
Michal Vaskoef578332016-01-25 13:20:09 +01001185 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001186}
1187
1188API struct nc_session *
1189nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1190{
Michal Vaskod083db62016-01-19 10:31:29 +01001191 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001192
Michal Vasko45e53ae2016-04-07 11:46:03 +02001193 if (fdin < 0) {
1194 ERRARG("fdin");
1195 return NULL;
1196 } else if (fdout < 0) {
1197 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001198 return NULL;
1199 }
1200
1201 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001202 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001203 if (!session) {
1204 ERRMEM;
1205 return NULL;
1206 }
1207 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001208
1209 /* transport specific data */
1210 session->ti_type = NC_TI_FD;
1211 session->ti.fd.in = fdin;
1212 session->ti.fd.out = fdout;
1213
Robin Jarry4e38e292019-10-15 17:14:14 +02001214 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1215 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001216 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001217 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001218
1219 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001220 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001221 goto fail;
1222 }
1223 session->status = NC_STATUS_RUNNING;
1224
Michal Vaskoef578332016-01-25 13:20:09 +01001225 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001226 goto fail;
1227 }
1228
1229 return session;
1230
1231fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001232 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001233 return NULL;
1234}
1235
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001236API struct nc_session *
1237nc_connect_unix(const char *address, struct ly_ctx *ctx)
1238{
1239 struct nc_session *session = NULL;
1240 struct sockaddr_un sun;
1241 const struct passwd *pw;
1242 char *username;
1243 int sock = -1;
1244
1245 if (address == NULL) {
1246 ERRARG("address");
1247 return NULL;
1248 }
1249
1250 pw = getpwuid(geteuid());
1251 if (pw == NULL) {
Michal Vasko05532772021-06-03 12:12:38 +02001252 ERR(NULL, "Failed to find username for euid=%u.\n", geteuid());
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001253 goto fail;
1254 }
1255
1256 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1257 if (sock < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001258 ERR(NULL, "Failed to create socket (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001259 goto fail;
1260 }
1261
1262 memset(&sun, 0, sizeof(sun));
1263 sun.sun_family = AF_UNIX;
1264 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1265
1266 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001267 ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001268 goto fail;
1269 }
1270
1271 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001272 ERR(NULL, "fcntl failed (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001273 goto fail;
1274 }
1275
1276 /* prepare session structure */
1277 session = nc_new_session(NC_CLIENT, 0);
1278 if (!session) {
1279 ERRMEM;
1280 goto fail;
1281 }
1282 session->status = NC_STATUS_STARTING;
1283
1284 /* transport specific data */
1285 session->ti_type = NC_TI_UNIX;
1286 session->ti.unixsock.sock = sock;
1287 sock = -1; /* do not close sock in fail label anymore */
1288
Robin Jarry4e38e292019-10-15 17:14:14 +02001289 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1290 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001291 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001292 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001293
Michal Vasko77367452021-02-16 16:32:18 +01001294 lydict_insert(ctx, address, 0, &session->path);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001295
1296 username = strdup(pw->pw_name);
1297 if (username == NULL) {
1298 ERRMEM;
1299 goto fail;
1300 }
Michal Vasko77367452021-02-16 16:32:18 +01001301 lydict_insert_zc(ctx, username, &session->username);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001302
1303 /* NETCONF handshake */
1304 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1305 goto fail;
1306 }
1307 session->status = NC_STATUS_RUNNING;
1308
1309 if (nc_ctx_check_and_fill(session) == -1) {
1310 goto fail;
1311 }
1312
1313 return session;
1314
1315fail:
1316 nc_session_free(session, NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001317 if (sock >= 0) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001318 close(sock);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001319 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001320 return NULL;
1321}
1322
Frank Rimpler9f838b02018-07-25 06:44:03 +00001323/*
1324 Helper for a non-blocking connect (which is required because of the locking
1325 concept for e.g. call home settings). For more details see nc_sock_connect().
1326 */
1327static int
Michal Vasko5e8d0192019-06-24 19:19:49 +02001328_non_blocking_connect(int timeout, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001329{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001330 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001331 int sock = -1;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001332 fd_set wset;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001333 struct timeval ts;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001334 socklen_t len = sizeof(int);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001335 struct in_addr *addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001336 uint16_t port;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001337 char str[INET6_ADDRSTRLEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001338
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001339 if (sock_pending && (*sock_pending != -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001340 VRB(NULL, "Trying to connect the pending socket %d.", *sock_pending);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001341 sock = *sock_pending;
1342 } else {
1343 assert(res);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001344 if (res->ai_family == AF_INET6) {
1345 addr = (struct in_addr *) &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001346 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001347 } else {
1348 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001349 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001350 }
1351 if (!inet_ntop(res->ai_family, addr, str, res->ai_addrlen)) {
Michal Vasko05532772021-06-03 12:12:38 +02001352 WRN(NULL, "inet_ntop() failed (%s).", strerror(errno));
Michal Vasko5e8d0192019-06-24 19:19:49 +02001353 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001354 VRB(NULL, "Trying to connect via %s to %s:%u.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", str, port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001355 }
1356
1357 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001358 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1359 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001360 ERR(NULL, "Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001361 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001362 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001363 /* make the socket non-blocking */
1364 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001365 ERR(NULL, "fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001366 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001367 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001368 /* non-blocking connect! */
1369 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1370 if (errno != EINPROGRESS) {
1371 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001372 ERR(NULL, "connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001373 goto cleanup;
1374 }
1375 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001376 }
1377 ts.tv_sec = timeout;
1378 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001379
Frank Rimpler9f838b02018-07-25 06:44:03 +00001380 FD_ZERO(&wset);
1381 FD_SET(sock, &wset);
1382
1383 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001384 ERR(NULL, "select() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001385 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001386 }
1387
Michal Vasko5e8d0192019-06-24 19:19:49 +02001388 if (ret == 0) {
1389 /* there was a timeout */
Michal Vasko05532772021-06-03 12:12:38 +02001390 VRB(NULL, "Timed out after %ds (%s).", timeout, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001391 if (sock_pending) {
1392 /* no sock-close, we'll try it again */
1393 *sock_pending = sock;
1394 } else {
1395 close(sock);
1396 }
1397 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001398 }
Radek Krejci782041a2018-08-20 10:09:45 +02001399
1400 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001401 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001402 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001403 ERR(NULL, "getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001404 goto cleanup;
1405 }
Michal Vaskoe27ab4e2020-07-27 11:10:58 +02001406 if (error) {
Radek Krejci782041a2018-08-20 10:09:45 +02001407 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001408 VRB(NULL, "getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001409 errno = error;
1410 goto cleanup;
1411 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001412
1413 /* enable keep-alive */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001414 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001415 goto cleanup;
1416 }
1417
Michal Vasko086311b2016-01-08 09:53:11 +01001418 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001419
Frank Rimpler9f838b02018-07-25 06:44:03 +00001420cleanup:
1421 if (sock_pending) {
1422 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001423 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001424 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001425 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001426}
1427
Frank Rimpler9f838b02018-07-25 06:44:03 +00001428/* A given timeout value limits the time how long the function blocks. If it has to block
1429 only for some seconds, a socket connection might not yet have been fully established.
1430 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1431 value will be -1. In such a case a subsequent invokation is required, by providing the
1432 stored sock_pending, again.
1433 In general, if this function returns -1, when a timeout has been given, this function
1434 has to be invoked, until it returns a valid socket.
1435 */
1436int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001437nc_sock_connect(const char *host, uint16_t port, int timeout, struct nc_keepalives *ka, int *sock_pending, char **ip_host)
Frank Rimpler9f838b02018-07-25 06:44:03 +00001438{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001439 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001440 int sock = sock_pending ? *sock_pending : -1;
Michal Vasko0be85692021-03-02 08:04:57 +01001441 struct addrinfo hints, *res_list = NULL, *res;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001442 char *buf, port_s[6]; /* length of string representation of short int */
Michal Vasko66032bc2019-01-22 15:03:12 +01001443 void *addr;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001444
Michal Vasko05532772021-06-03 12:12:38 +02001445 DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001446
1447 /* no pending socket */
1448 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001449 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001450 snprintf(port_s, 6, "%u", port);
1451 memset(&hints, 0, sizeof hints);
1452 hints.ai_family = AF_UNSPEC;
1453 hints.ai_socktype = SOCK_STREAM;
1454 hints.ai_protocol = IPPROTO_TCP;
1455 i = getaddrinfo(host, port_s, &hints, &res_list);
1456 if (i != 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001457 ERR(NULL, "Unable to translate the host address (%s).", gai_strerror(i));
Michal Vaskocb846632019-12-13 15:12:45 +01001458 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001459 }
1460
1461 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001462 sock = _non_blocking_connect(timeout, sock_pending, res, ka);
Michal Vasko2af7c382020-07-27 14:21:35 +02001463 if (sock == -1) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001464 if (!sock_pending || (*sock_pending == -1)) {
Michal Vasko2af7c382020-07-27 14:21:35 +02001465 /* try the next resource */
1466 continue;
1467 } else {
1468 /* timeout, keep pending socket */
Michal Vasko0be85692021-03-02 08:04:57 +01001469 break;
Michal Vasko2af7c382020-07-27 14:21:35 +02001470 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001471 }
Michal Vasko05532772021-06-03 12:12:38 +02001472 VRB(NULL, "Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
Michal Vasko83ad17e2019-01-30 10:11:37 +01001473
1474 opt = 1;
1475 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001476 ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001477 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001478 }
1479
Michal Vasko66032bc2019-01-22 15:03:12 +01001480 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1481 buf = malloc(INET6_ADDRSTRLEN);
1482 if (!buf) {
1483 ERRMEM;
Michal Vaskocb846632019-12-13 15:12:45 +01001484 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001485 }
1486 if (res->ai_family == AF_INET) {
1487 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1488 } else {
1489 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1490 }
1491 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
Michal Vasko05532772021-06-03 12:12:38 +02001492 ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno));
Michal Vasko66032bc2019-01-22 15:03:12 +01001493 free(buf);
Michal Vaskocb846632019-12-13 15:12:45 +01001494 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001495 }
1496
1497 *ip_host = buf;
1498 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001499 break;
1500 }
1501 freeaddrinfo(res_list);
1502
1503 } else {
1504 /* try to get a connection with the pending socket */
1505 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001506 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001507 }
1508
1509 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001510
1511error:
Michal Vasko0be85692021-03-02 08:04:57 +01001512 if (res_list) {
1513 freeaddrinfo(res_list);
1514 }
Michal Vaskocb846632019-12-13 15:12:45 +01001515 if (sock != -1) {
1516 close(sock);
1517 }
1518 if (sock_pending) {
1519 *sock_pending = -1;
1520 }
1521 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001522}
1523
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001524#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001525
Michal Vasko3031aae2016-01-27 16:07:18 +01001526int
1527nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1528{
1529 int sock;
1530
Michal Vasko45e53ae2016-04-07 11:46:03 +02001531 if (!address) {
1532 ERRARG("address");
1533 return -1;
1534 } else if (!port) {
1535 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001536 return -1;
1537 }
1538
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001539 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001540 if (sock == -1) {
1541 return -1;
1542 }
1543
1544 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001545 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1546 if (!client_opts.ch_binds) {
1547 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001548 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001549 return -1;
1550 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001551
Michal Vasko2e6defd2016-10-07 15:48:15 +02001552 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1553 if (!client_opts.ch_bind_ti) {
1554 ERRMEM;
1555 close(sock);
1556 return -1;
1557 }
1558 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1559
Michal Vasko3031aae2016-01-27 16:07:18 +01001560 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001561 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1562 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001563 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001564 return -1;
1565 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001566 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1567 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001568 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001569
1570 return 0;
1571}
1572
1573int
1574nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1575{
1576 uint32_t i;
1577 int ret = -1;
1578
1579 if (!address && !port && !ti) {
1580 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1581 close(client_opts.ch_binds[i].sock);
1582 free((char *)client_opts.ch_binds[i].address);
1583
1584 ret = 0;
1585 }
1586 free(client_opts.ch_binds);
1587 client_opts.ch_binds = NULL;
1588 client_opts.ch_bind_count = 0;
1589 } else {
1590 for (i = 0; i < client_opts.ch_bind_count; ++i) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001591 if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) &&
1592 (!port || (client_opts.ch_binds[i].port == port)) &&
1593 (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001594 close(client_opts.ch_binds[i].sock);
1595 free((char *)client_opts.ch_binds[i].address);
1596
1597 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001598 if (!client_opts.ch_bind_count) {
1599 free(client_opts.ch_binds);
1600 client_opts.ch_binds = NULL;
1601 } else if (i < client_opts.ch_bind_count) {
1602 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1603 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1604 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001605
1606 ret = 0;
1607 }
1608 }
1609 }
1610
1611 return ret;
1612}
1613
1614API int
1615nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1616{
1617 int sock;
1618 char *host = NULL;
1619 uint16_t port, idx;
1620
Michal Vasko45e53ae2016-04-07 11:46:03 +02001621 if (!client_opts.ch_binds) {
1622 ERRINIT;
1623 return -1;
1624 } else if (!session) {
1625 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001626 return -1;
1627 }
1628
1629 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1630
Michal Vasko50456e82016-02-02 12:16:08 +01001631 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001632 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001633 return sock;
1634 }
1635
Radek Krejci53691be2016-02-22 13:58:37 +01001636#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001637 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001638 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001639 } else
1640#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001641#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001642 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001643 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001644 } else
1645#endif
1646 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001647 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001648 *session = NULL;
1649 }
1650
1651 free(host);
1652
1653 if (!(*session)) {
1654 return -1;
1655 }
1656
1657 return 1;
1658}
1659
Radek Krejci53691be2016-02-22 13:58:37 +01001660#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001661
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001662API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001663nc_session_get_cpblts(const struct nc_session *session)
1664{
1665 if (!session) {
1666 ERRARG("session");
1667 return NULL;
1668 }
1669
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001670 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001671}
1672
1673API const char *
1674nc_session_cpblt(const struct nc_session *session, const char *capab)
1675{
1676 int i, len;
1677
1678 if (!session) {
1679 ERRARG("session");
1680 return NULL;
1681 } else if (!capab) {
1682 ERRARG("capab");
1683 return NULL;
1684 }
1685
1686 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001687 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1688 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1689 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001690 }
1691 }
1692
1693 return NULL;
1694}
1695
Michal Vasko9cd26a82016-05-31 08:58:48 +02001696API int
1697nc_session_ntf_thread_running(const struct nc_session *session)
1698{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001699 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001700 ERRARG("session");
1701 return 0;
1702 }
1703
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001704 return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread) ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001705}
1706
Michal Vaskob7558c52016-02-26 15:04:19 +01001707API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001708nc_client_init(void)
1709{
1710 nc_init();
1711}
1712
1713API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001714nc_client_destroy(void)
1715{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001716 nc_client_set_schema_searchpath(NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001717#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001718 nc_client_ch_del_bind(NULL, 0, 0);
1719#endif
1720#ifdef NC_ENABLED_SSH
1721 nc_client_ssh_destroy_opts();
1722#endif
1723#ifdef NC_ENABLED_TLS
1724 nc_client_tls_destroy_opts();
1725#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001726 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001727}
1728
Michal Vasko77367452021-02-16 16:32:18 +01001729static NC_MSG_TYPE
1730recv_reply_check_msgid(struct nc_session *session, const struct lyd_node *envp, uint64_t msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001731{
Michal Vasko77367452021-02-16 16:32:18 +01001732 char *ptr;
1733 struct lyd_attr *attr;
1734 uint64_t cur_msgid;
1735
1736 assert(envp && !envp->schema);
1737
1738 /* find the message-id attribute */
1739 LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) {
1740 if (!strcmp(attr->name.name, "message-id")) {
1741 break;
1742 }
1743 }
1744
1745 if (!attr) {
Michal Vasko05532772021-06-03 12:12:38 +02001746 ERR(session, "Received a <rpc-reply> without a message-id.");
Michal Vasko77367452021-02-16 16:32:18 +01001747 return NC_MSG_REPLY_ERR_MSGID;
1748 }
1749
1750 cur_msgid = strtoul(attr->value, &ptr, 10);
1751 if (cur_msgid != msgid) {
Michal Vasko05532772021-06-03 12:12:38 +02001752 ERR(session, "Received a <rpc-reply> with an unexpected message-id %" PRIu64 " (expected %" PRIu64 ").",
1753 cur_msgid, msgid);
Michal Vasko77367452021-02-16 16:32:18 +01001754 return NC_MSG_REPLY_ERR_MSGID;
1755 }
1756
1757 return NC_MSG_REPLY;
1758}
1759
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001760/**
1761 * @brief Used to roughly estimate the type of the message, does not actually parse or verify it.
1762 *
1763 * @param[in] session NETCONF session used to send error messages.
1764 * @param[in] msg Message to check for type.
1765 * @return NC_MSG_REPLY If format roughly matches a rpc-reply;
1766 * @return NC_MSG_NOTIF If format roughly matches a notification;
1767 * @return NC_MSG_ERROR If format is malformed or unrecognized.
1768 */
1769static NC_MSG_TYPE
1770get_msg_type(struct nc_session *session, struct ly_in *msg)
1771{
1772 const char *str, *end;
1773
1774 str = ly_in_memory(msg, NULL);
1775
1776 while (*str) {
1777 /* Skip whitespaces */
1778 while (isspace(*str)) {
1779 str++;
1780 }
1781
1782 if (*str == '<') {
1783 str++;
1784 if (!strncmp(str, "!--", 3)) {
1785 /* Skip comments */
1786 end = "-->";
1787 str = strstr(str, end);
1788 } else if (!strncmp(str, "?xml", 4)) {
1789 /* Skip xml declaration */
1790 end = "?>";
1791 str = strstr(str, end);
1792 } else if (!strncmp(str, "rpc-reply", 9)) {
1793 return NC_MSG_REPLY;
1794 } else if (!strncmp(str, "notification", 12)) {
1795 return NC_MSG_NOTIF;
1796 } else {
1797 ERR(session, "Unknown xml element '%.10s'.", str);
1798 return NC_MSG_ERROR;
1799 }
1800 if (!str) {
1801 /* No matching ending tag found */
1802 ERR(session, "No matching ending tag '%s' found in xml message.", end);
1803 return NC_MSG_ERROR;
1804 }
1805 str += strlen(end);
1806 } else {
1807 /* Not a valid xml */
1808 ERR(session, "Unexpected character '%c' in xml message.", *str);
1809 return NC_MSG_ERROR;
1810 }
1811 }
1812
1813 /* Unexpected end of message */
1814 ERR(session, "Unexpected end of xml message.");
1815 return NC_MSG_ERROR;
1816}
1817
1818/**
1819 * @brief Function to receive either replies or notifications.
1820 *
1821 * @param[in] session NETCONF session from which this function receives messages.
1822 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite.
1823 * @param[in] expected Type of the message the caller desired.
1824 * @param[out] message If receiving a message succeeded this is the message, NULL otherwise.
1825 * @return NC_MSG_REPLY If a rpc-reply was received;
1826 * @return NC_MSG_NOTIF If a notification was received;
1827 * @return NC_MSG_ERROR If any error occured;
1828 * @return NC_MSG_WOULDBLOCK If the timeout was reached.
1829 */
1830static NC_MSG_TYPE
1831recv_msg(struct nc_session *session, int timeout, NC_MSG_TYPE expected, struct ly_in **message)
1832{
1833 struct nc_msg_cont **cont_ptr;
1834 struct ly_in *msg = NULL;
1835 struct nc_msg_cont *cont, *prev;
1836 NC_MSG_TYPE ret = NC_MSG_ERROR;
1837 int r;
1838
1839 *message = NULL;
1840
1841 /* MSGS LOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02001842 r = nc_session_client_msgs_lock(session, &timeout, __func__);
1843 if (!r) {
1844 ret = NC_MSG_WOULDBLOCK;
1845 goto cleanup;
1846 } else if (r == -1) {
1847 ret = NC_MSG_ERROR;
1848 goto cleanup;
1849 }
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001850
1851 /* Find the expected message in the buffer */
1852 prev = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02001853 for (cont = session->opts.client.msgs; cont && (cont->type != expected); cont = cont->next) {
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001854 prev = cont;
1855 }
1856
1857 if (cont) {
1858 /* Remove found message from buffer */
1859 if (prev) {
1860 prev->next = cont->next;
1861 } else {
1862 session->opts.client.msgs = cont->next;
1863 }
1864
1865 /* Use the buffer message */
1866 ret = cont->type;
1867 msg = cont->msg;
1868 free(cont);
Michal Vasko01130bd2021-08-26 11:47:38 +02001869 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001870 }
1871
1872 /* Read a message from the wire */
1873 r = nc_read_msg_poll_io(session, timeout, &msg);
1874 if (!r) {
1875 ret = NC_MSG_WOULDBLOCK;
Michal Vasko01130bd2021-08-26 11:47:38 +02001876 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001877 } else if (r == -1) {
1878 ret = NC_MSG_ERROR;
Michal Vasko01130bd2021-08-26 11:47:38 +02001879 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001880 }
1881
1882 /* Basic check to determine message type */
1883 ret = get_msg_type(session, msg);
1884 if (ret == NC_MSG_ERROR) {
Michal Vasko01130bd2021-08-26 11:47:38 +02001885 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001886 }
1887
1888 /* If received a message of different type store it in the buffer */
1889 if (ret != expected) {
1890 cont_ptr = &session->opts.client.msgs;
1891 while (*cont_ptr) {
1892 cont_ptr = &((*cont_ptr)->next);
1893 }
1894 *cont_ptr = malloc(sizeof **cont_ptr);
1895 if (!*cont_ptr) {
1896 ERRMEM;
1897 ret = NC_MSG_ERROR;
Michal Vasko01130bd2021-08-26 11:47:38 +02001898 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001899 }
1900 (*cont_ptr)->msg = msg;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001901 msg = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02001902 (*cont_ptr)->type = ret;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001903 (*cont_ptr)->next = NULL;
1904 }
1905
Michal Vasko01130bd2021-08-26 11:47:38 +02001906cleanup_unlock:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001907 /* MSGS UNLOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02001908 nc_session_client_msgs_unlock(session, __func__);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001909
Michal Vasko01130bd2021-08-26 11:47:38 +02001910cleanup:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001911 if (ret == expected) {
1912 *message = msg;
1913 } else {
1914 ly_in_free(msg, 1);
1915 }
1916 return ret;
1917}
1918
Michal Vasko77367452021-02-16 16:32:18 +01001919static NC_MSG_TYPE
1920recv_reply(struct nc_session *session, int timeout, struct lyd_node *op, uint64_t msgid, struct lyd_node **envp)
1921{
Michal Vasko77367452021-02-16 16:32:18 +01001922 LY_ERR lyrc;
1923 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001924 NC_MSG_TYPE ret = NC_MSG_ERROR;
1925
1926 assert(op && (op->schema->nodetype & (LYS_RPC | LYS_ACTION)));
1927
1928 *envp = NULL;
1929
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001930 /* Receive messages until a rpc-reply is found or a timeout or error reached */
1931 ret = recv_msg(session, timeout, NC_MSG_REPLY, &msg);
1932 if (ret != NC_MSG_REPLY) {
Michal Vasko77367452021-02-16 16:32:18 +01001933 goto cleanup;
1934 }
1935
1936 /* parse */
1937 lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL);
1938 if (!lyrc) {
1939 ret = recv_reply_check_msgid(session, *envp, msgid);
1940 goto cleanup;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001941 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001942 ERR(session, "Received an invalid message (%s).", ly_errmsg(LYD_CTX(op)));
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001943 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01001944 goto cleanup;
1945 }
1946
Michal Vasko77367452021-02-16 16:32:18 +01001947cleanup:
1948 ly_in_free(msg, 1);
1949 return ret;
1950}
1951
1952static int
1953recv_reply_dup_rpc(struct nc_session *session, struct nc_rpc *rpc, struct lyd_node **op)
1954{
Michal Vasko143aa142021-10-01 15:31:48 +02001955 LY_ERR lyrc = LY_SUCCESS;
Michal Vasko77367452021-02-16 16:32:18 +01001956 struct nc_rpc_act_generic *rpc_gen;
1957 struct ly_in *in;
1958 struct lyd_node *tree, *op2;
1959 const struct lys_module *mod;
Michal Vasko305faca2021-03-25 09:16:02 +01001960 const char *module_name = NULL, *rpc_name = NULL, *module_check = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001961
1962 switch (rpc->type) {
1963 case NC_RPC_ACT_GENERIC:
1964 rpc_gen = (struct nc_rpc_act_generic *)rpc;
1965 if (rpc_gen->has_data) {
1966 tree = rpc_gen->content.data;
1967
1968 /* find the operation node */
1969 lyrc = LY_EINVAL;
1970 LYD_TREE_DFS_BEGIN(tree, op2) {
1971 if (op2->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1972 lyrc = lyd_dup_single(op2, NULL, 0, op);
1973 break;
1974 }
1975 LYD_TREE_DFS_END(tree, op2);
1976 }
1977 } else {
1978 ly_in_new_memory(rpc_gen->content.xml_str, &in);
1979 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op2);
1980 ly_in_free(in, 0);
1981 if (lyrc) {
1982 return -1;
1983 }
1984
1985 /* we want just the operation node */
1986 lyrc = lyd_dup_single(op2, NULL, 0, op);
1987
1988 lyd_free_tree(tree);
1989 }
1990 break;
1991 case NC_RPC_GETCONFIG:
1992 module_name = "ietf-netconf";
1993 rpc_name = "get-config";
1994 break;
1995 case NC_RPC_EDIT:
1996 module_name = "ietf-netconf";
1997 rpc_name = "edit-config";
1998 break;
1999 case NC_RPC_COPY:
2000 module_name = "ietf-netconf";
2001 rpc_name = "copy-config";
2002 break;
2003 case NC_RPC_DELETE:
2004 module_name = "ietf-netconf";
2005 rpc_name = "delete-config";
2006 break;
2007 case NC_RPC_LOCK:
2008 module_name = "ietf-netconf";
2009 rpc_name = "lock";
2010 break;
2011 case NC_RPC_UNLOCK:
2012 module_name = "ietf-netconf";
2013 rpc_name = "unlock";
2014 break;
2015 case NC_RPC_GET:
2016 module_name = "ietf-netconf";
2017 rpc_name = "get";
2018 break;
2019 case NC_RPC_KILL:
2020 module_name = "ietf-netconf";
2021 rpc_name = "kill-session";
2022 break;
2023 case NC_RPC_COMMIT:
2024 module_name = "ietf-netconf";
2025 rpc_name = "commit";
2026 break;
2027 case NC_RPC_DISCARD:
2028 module_name = "ietf-netconf";
2029 rpc_name = "discard-changes";
2030 break;
2031 case NC_RPC_CANCEL:
2032 module_name = "ietf-netconf";
2033 rpc_name = "cancel-commit";
2034 break;
2035 case NC_RPC_VALIDATE:
2036 module_name = "ietf-netconf";
2037 rpc_name = "validate";
2038 break;
2039 case NC_RPC_GETSCHEMA:
2040 module_name = "ietf-netconf-monitoring";
2041 rpc_name = "get-schema";
2042 break;
2043 case NC_RPC_SUBSCRIBE:
2044 module_name = "notifications";
Michal Vaskoeed893d2021-05-25 17:11:08 +02002045 rpc_name = "create-subscription";
Michal Vasko77367452021-02-16 16:32:18 +01002046 break;
2047 case NC_RPC_GETDATA:
2048 module_name = "ietf-netconf-nmda";
2049 rpc_name = "get-data";
2050 break;
2051 case NC_RPC_EDITDATA:
2052 module_name = "ietf-netconf-nmda";
2053 rpc_name = "edit-data";
2054 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002055 case NC_RPC_ESTABLISHSUB:
2056 module_name = "ietf-subscribed-notifications";
2057 rpc_name = "establish-subscription";
2058 break;
2059 case NC_RPC_MODIFYSUB:
2060 module_name = "ietf-subscribed-notifications";
2061 rpc_name = "modify-subscription";
2062 break;
2063 case NC_RPC_DELETESUB:
2064 module_name = "ietf-subscribed-notifications";
2065 rpc_name = "delete-subscription";
2066 break;
2067 case NC_RPC_KILLSUB:
2068 module_name = "ietf-subscribed-notifications";
2069 rpc_name = "kill-subscription";
2070 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002071 case NC_RPC_ESTABLISHPUSH:
2072 module_name = "ietf-subscribed-notifications";
2073 rpc_name = "establish-subscription";
2074 module_check = "ietf-yang-push";
2075 break;
2076 case NC_RPC_MODIFYPUSH:
2077 module_name = "ietf-subscribed-notifications";
2078 rpc_name = "modify-subscription";
2079 module_check = "ietf-yang-push";
2080 break;
2081 case NC_RPC_RESYNCSUB:
2082 module_name = "ietf-yang-push";
2083 rpc_name = "resync-subscription";
2084 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002085 case NC_RPC_UNKNOWN:
Michal Vasko77367452021-02-16 16:32:18 +01002086 lyrc = LY_EINT;
2087 break;
2088 }
2089
2090 if (module_name && rpc_name) {
2091 mod = ly_ctx_get_module_implemented(session->ctx, module_name);
2092 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002093 ERR(session, "Missing \"%s\" schema in the context.", module_name);
Michal Vasko77367452021-02-16 16:32:18 +01002094 return -1;
2095 }
2096
2097 /* create the operation node */
2098 lyrc = lyd_new_inner(NULL, mod, rpc_name, 0, op);
2099 }
Michal Vasko305faca2021-03-25 09:16:02 +01002100 if (module_check) {
2101 if (!ly_ctx_get_module_implemented(session->ctx, module_check)) {
Michal Vasko05532772021-06-03 12:12:38 +02002102 ERR(session, "Missing \"%s\" schema in the context.", module_check);
Michal Vasko305faca2021-03-25 09:16:02 +01002103 return -1;
2104 }
2105 }
Michal Vasko77367452021-02-16 16:32:18 +01002106
2107 if (lyrc) {
2108 return -1;
2109 }
2110 return 0;
2111}
2112
2113API NC_MSG_TYPE
2114nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct lyd_node **envp,
2115 struct lyd_node **op)
2116{
2117 NC_MSG_TYPE ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002118
Michal Vasko45e53ae2016-04-07 11:46:03 +02002119 if (!session) {
2120 ERRARG("session");
2121 return NC_MSG_ERROR;
2122 } else if (!rpc) {
2123 ERRARG("rpc");
2124 return NC_MSG_ERROR;
Michal Vasko5a2c7162020-01-30 11:24:17 +01002125 } else if (!msgid) {
2126 ERRARG("msgid");
2127 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002128 } else if (!envp) {
2129 ERRARG("envp");
Michal Vasko45e53ae2016-04-07 11:46:03 +02002130 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002131 } else if (!op) {
2132 ERRARG("op");
Michal Vasko086311b2016-01-08 09:53:11 +01002133 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002134 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002135 ERR(session, "Invalid session to receive RPC replies.");
Michal Vasko086311b2016-01-08 09:53:11 +01002136 return NC_MSG_ERROR;
2137 }
Michal Vasko77367452021-02-16 16:32:18 +01002138
2139 /* get a duplicate of the RPC node to append reply to */
2140 if (recv_reply_dup_rpc(session, rpc, op)) {
2141 return NC_MSG_ERROR;
Michal Vaskoeee99412016-11-21 10:19:43 +01002142 }
Michal Vasko086311b2016-01-08 09:53:11 +01002143
Michal Vasko77367452021-02-16 16:32:18 +01002144 /* receive a reply */
2145 ret = recv_reply(session, timeout, *op, msgid, envp);
Michal Vasko086311b2016-01-08 09:53:11 +01002146
Michal Vasko77367452021-02-16 16:32:18 +01002147 /* do not return the RPC copy on error or if the reply includes no data */
2148 if (((ret != NC_MSG_REPLY) && (ret != NC_MSG_REPLY_ERR_MSGID)) || !lyd_child(*op)) {
2149 lyd_free_tree(*op);
2150 *op = NULL;
2151 }
2152 return ret;
2153}
2154
2155static NC_MSG_TYPE
2156recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
2157{
Michal Vasko77367452021-02-16 16:32:18 +01002158 LY_ERR lyrc;
2159 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002160 NC_MSG_TYPE ret = NC_MSG_ERROR;
2161
2162 *op = NULL;
2163 *envp = NULL;
2164
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002165 /* Receive messages until a notification is found or a timeout or error reached */
2166 ret = recv_msg(session, timeout, NC_MSG_NOTIF, &msg);
2167 if (ret != NC_MSG_NOTIF) {
Michal Vasko77367452021-02-16 16:32:18 +01002168 goto cleanup;
2169 }
2170
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002171 /* Parse */
Michal Vasko77367452021-02-16 16:32:18 +01002172 lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op);
2173 if (!lyrc) {
Michal Vasko77367452021-02-16 16:32:18 +01002174 goto cleanup;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002175 } else {
Michal Vasko05532772021-06-03 12:12:38 +02002176 ERR(session, "Received an invalid message (%s).", ly_errmsg(session->ctx));
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002177 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002178 goto cleanup;
2179 }
2180
Michal Vasko77367452021-02-16 16:32:18 +01002181cleanup:
2182 ly_in_free(msg, 1);
2183 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002184}
2185
2186API NC_MSG_TYPE
Michal Vasko77367452021-02-16 16:32:18 +01002187nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
Michal Vasko086311b2016-01-08 09:53:11 +01002188{
Michal Vasko45e53ae2016-04-07 11:46:03 +02002189 if (!session) {
2190 ERRARG("session");
2191 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002192 } else if (!envp) {
2193 ERRARG("envp");
2194 return NC_MSG_ERROR;
2195 } else if (!op) {
2196 ERRARG("op");
Michal Vasko086311b2016-01-08 09:53:11 +01002197 return NC_MSG_ERROR;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002198 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002199 ERR(session, "Invalid session to receive Notifications.");
Michal Vasko086311b2016-01-08 09:53:11 +01002200 return NC_MSG_ERROR;
2201 }
2202
Michal Vasko77367452021-02-16 16:32:18 +01002203 /* receive a notification */
2204 return recv_notif(session, timeout, envp, op);
Michal Vasko086311b2016-01-08 09:53:11 +01002205}
2206
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002207static void *
2208nc_recv_notif_thread(void *arg)
2209{
2210 struct nc_ntf_thread_arg *ntarg;
2211 struct nc_session *session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002212
Michal Vasko77367452021-02-16 16:32:18 +01002213 void (*notif_clb)(struct nc_session *session, const struct lyd_node *envp, const struct lyd_node *op);
2214 struct lyd_node *envp, *op;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002215 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002216
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002217 /* detach ourselves */
Michal Vasko131120a2018-05-29 15:44:02 +02002218 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002219
2220 ntarg = (struct nc_ntf_thread_arg *)arg;
2221 session = ntarg->session;
2222 notif_clb = ntarg->notif_clb;
2223 free(ntarg);
2224
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002225 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread) == 1) {
Michal Vasko77367452021-02-16 16:32:18 +01002226 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002227 if (msgtype == NC_MSG_NOTIF) {
Michal Vasko77367452021-02-16 16:32:18 +01002228 notif_clb(session, envp, op);
2229 if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) {
2230 lyd_free_tree(envp);
2231 lyd_free_tree(op);
Michal Vaskof0537d82016-01-29 14:42:38 +01002232 break;
2233 }
Michal Vasko77367452021-02-16 16:32:18 +01002234 lyd_free_tree(envp);
2235 lyd_free_tree(op);
Michal Vaskoce326052018-01-04 10:32:03 +01002236 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002237 /* quit this thread once the session is broken */
2238 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002239 }
2240
2241 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2242 }
2243
Michal Vasko05532772021-06-03 12:12:38 +02002244 VRB(session, "Notification thread exit.");
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002245 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 0);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002246 return NULL;
2247}
2248
2249API int
Michal Vasko77367452021-02-16 16:32:18 +01002250nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session,
2251 const struct lyd_node *envp, const struct lyd_node *op))
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002252{
2253 struct nc_ntf_thread_arg *ntarg;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002254 pthread_t tid;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002255 int ret;
2256
Michal Vasko45e53ae2016-04-07 11:46:03 +02002257 if (!session) {
2258 ERRARG("session");
2259 return -1;
2260 } else if (!notif_clb) {
2261 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002262 return -1;
2263 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002264 ERR(session, "Invalid session to receive Notifications.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002265 return -1;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002266 } else if (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
Michal Vasko05532772021-06-03 12:12:38 +02002267 ERR(session, "Separate notification thread is already running.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002268 return -1;
2269 }
2270
2271 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002272 if (!ntarg) {
2273 ERRMEM;
2274 return -1;
2275 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002276 ntarg->session = session;
2277 ntarg->notif_clb = notif_clb;
2278
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002279 /* just so that nc_recv_notif_thread() does not immediately exit */
2280 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 1);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002281
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002282 ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002283 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02002284 ERR(session, "Failed to create a new thread (%s).", strerror(errno));
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002285 free(ntarg);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002286 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 0);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002287 return -1;
2288 }
2289
2290 return 0;
2291}
2292
Michal Vasko77367452021-02-16 16:32:18 +01002293static const char *
2294nc_wd2str(NC_WD_MODE wd)
2295{
2296 switch (wd) {
2297 case NC_WD_ALL:
2298 return "report-all";
2299 case NC_WD_ALL_TAG:
2300 return "report-all-tagged";
2301 case NC_WD_TRIM:
2302 return "trim";
2303 case NC_WD_EXPLICIT:
2304 return "explicit";
2305 default:
2306 break;
2307 }
2308
2309 return NULL;
2310}
2311
Michal Vasko086311b2016-01-08 09:53:11 +01002312API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002313nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002314{
2315 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002316 int dofree = 1;
Michal Vasko77367452021-02-16 16:32:18 +01002317 struct ly_in *in;
Michal Vasko90e8e692016-07-13 12:27:57 +02002318 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002319 struct nc_rpc_getconfig *rpc_gc;
2320 struct nc_rpc_edit *rpc_e;
2321 struct nc_rpc_copy *rpc_cp;
2322 struct nc_rpc_delete *rpc_del;
2323 struct nc_rpc_lock *rpc_lock;
2324 struct nc_rpc_get *rpc_g;
2325 struct nc_rpc_kill *rpc_k;
2326 struct nc_rpc_commit *rpc_com;
2327 struct nc_rpc_cancel *rpc_can;
2328 struct nc_rpc_validate *rpc_val;
2329 struct nc_rpc_getschema *rpc_gs;
2330 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002331 struct nc_rpc_getdata *rpc_getd;
2332 struct nc_rpc_editdata *rpc_editd;
Michal Vasko96f247a2021-03-15 13:32:10 +01002333 struct nc_rpc_establishsub *rpc_estsub;
2334 struct nc_rpc_modifysub *rpc_modsub;
2335 struct nc_rpc_deletesub *rpc_delsub;
2336 struct nc_rpc_killsub *rpc_killsub;
Michal Vasko305faca2021-03-25 09:16:02 +01002337 struct nc_rpc_establishpush *rpc_estpush;
2338 struct nc_rpc_modifypush *rpc_modpush;
2339 struct nc_rpc_resyncsub *rpc_resyncsub;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002340 struct lyd_node *data = NULL, *node, *cont;
Michal Vasko305faca2021-03-25 09:16:02 +01002341 const struct lys_module *mod = NULL, *mod2 = NULL, *ietfncwd;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002342 LY_ERR lyrc = 0;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002343 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002344 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002345 uint64_t cur_msgid;
2346
Michal Vasko45e53ae2016-04-07 11:46:03 +02002347 if (!session) {
2348 ERRARG("session");
2349 return NC_MSG_ERROR;
2350 } else if (!rpc) {
2351 ERRARG("rpc");
2352 return NC_MSG_ERROR;
2353 } else if (!msgid) {
2354 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002355 return NC_MSG_ERROR;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002356 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002357 ERR(session, "Invalid session to send RPCs.");
Michal Vasko086311b2016-01-08 09:53:11 +01002358 return NC_MSG_ERROR;
2359 }
2360
Michal Vaskoc1171a42019-11-05 12:06:46 +01002361 switch (rpc->type) {
2362 case NC_RPC_ACT_GENERIC:
2363 /* checked when parsing */
2364 break;
2365 case NC_RPC_GETCONFIG:
2366 case NC_RPC_EDIT:
2367 case NC_RPC_COPY:
2368 case NC_RPC_DELETE:
2369 case NC_RPC_LOCK:
2370 case NC_RPC_UNLOCK:
2371 case NC_RPC_GET:
2372 case NC_RPC_KILL:
2373 case NC_RPC_COMMIT:
2374 case NC_RPC_DISCARD:
2375 case NC_RPC_CANCEL:
2376 case NC_RPC_VALIDATE:
Michal Vasko77367452021-02-16 16:32:18 +01002377 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002378 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002379 ERR(session, "Missing \"ietf-netconf\" schema in the context.");
Michal Vasko086311b2016-01-08 09:53:11 +01002380 return NC_MSG_ERROR;
2381 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002382 break;
2383 case NC_RPC_GETSCHEMA:
Michal Vasko77367452021-02-16 16:32:18 +01002384 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002385 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002386 ERR(session, "Missing \"ietf-netconf-monitoring\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002387 return NC_MSG_ERROR;
2388 }
2389 break;
2390 case NC_RPC_SUBSCRIBE:
Michal Vasko77367452021-02-16 16:32:18 +01002391 mod = ly_ctx_get_module_implemented(session->ctx, "notifications");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002392 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002393 ERR(session, "Missing \"notifications\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002394 return NC_MSG_ERROR;
2395 }
2396 break;
2397 case NC_RPC_GETDATA:
2398 case NC_RPC_EDITDATA:
Michal Vasko77367452021-02-16 16:32:18 +01002399 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-nmda");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002400 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002401 ERR(session, "Missing \"ietf-netconf-nmda\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002402 return NC_MSG_ERROR;
2403 }
2404 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002405 case NC_RPC_ESTABLISHSUB:
2406 case NC_RPC_MODIFYSUB:
2407 case NC_RPC_DELETESUB:
2408 case NC_RPC_KILLSUB:
2409 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2410 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002411 ERR(session, "Missing \"ietf-subscribed-notifications\" schema in the context.");
Michal Vasko96f247a2021-03-15 13:32:10 +01002412 return NC_MSG_ERROR;
2413 }
2414 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002415 case NC_RPC_ESTABLISHPUSH:
2416 case NC_RPC_MODIFYPUSH:
2417 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2418 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002419 ERR(session, "Missing \"ietf-subscribed-notifications\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002420 return NC_MSG_ERROR;
2421 }
2422 mod2 = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2423 if (!mod2) {
Michal Vasko05532772021-06-03 12:12:38 +02002424 ERR(session, "Missing \"ietf-yang-push\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002425 return NC_MSG_ERROR;
2426 }
2427 break;
2428 case NC_RPC_RESYNCSUB:
2429 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2430 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002431 ERR(session, "Missing \"ietf-yang-push\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002432 return NC_MSG_ERROR;
2433 }
2434 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002435 case NC_RPC_UNKNOWN:
2436 ERRINT;
2437 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002438 }
2439
Michal Vaskoab9deb62021-05-27 11:37:00 +02002440#define CHECK_LYRC_BREAK(func_call) if ((lyrc = func_call)) break;
2441
Michal Vasko086311b2016-01-08 09:53:11 +01002442 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002443 case NC_RPC_ACT_GENERIC:
2444 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002445
2446 if (rpc_gen->has_data) {
2447 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002448 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002449 } else {
Michal Vasko77367452021-02-16 16:32:18 +01002450 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2451 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &data, NULL);
2452 ly_in_free(in, 0);
2453 if (lyrc) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002454 break;
Michal Vaskoeec410f2017-11-24 09:14:55 +01002455 }
Michal Vasko086311b2016-01-08 09:53:11 +01002456 }
2457 break;
2458
2459 case NC_RPC_GETCONFIG:
2460 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2461
Michal Vaskoab9deb62021-05-27 11:37:00 +02002462 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-config", 0, &data));
2463 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
2464 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_gc->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002465 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002466 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002467 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_gc->filter, 0, LYD_ANYDATA_XML, 0, &node));
2468 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002469 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002470 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2471 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2472 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_gc->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002473 }
2474 }
2475
2476 if (rpc_gc->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002477 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002478 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002479 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002480 lyrc = LY_ENOTFOUND;
2481 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002482 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002483 CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_gc->wd_mode), 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002484 }
2485 break;
2486
2487 case NC_RPC_EDIT:
2488 rpc_e = (struct nc_rpc_edit *)rpc;
2489
Michal Vaskoab9deb62021-05-27 11:37:00 +02002490 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-config", 0, &data));
2491 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2492 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_e->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002493
2494 if (rpc_e->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002495 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op], 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002496 }
Michal Vasko086311b2016-01-08 09:53:11 +01002497 if (rpc_e->test_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002498 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt], 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002499 }
Michal Vasko086311b2016-01-08 09:53:11 +01002500 if (rpc_e->error_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002501 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt], 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002502 }
Michal Vasko7793bc62016-09-16 11:58:41 +02002503 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002504 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_e->edit_cont, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002505 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002506 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_e->edit_cont, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002507 }
2508 break;
2509
2510 case NC_RPC_COPY:
2511 rpc_cp = (struct nc_rpc_copy *)rpc;
2512
Michal Vaskoab9deb62021-05-27 11:37:00 +02002513 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "copy-config", 0, &data));
2514 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002515 if (rpc_cp->url_trg) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002516 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_trg, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002517 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002518 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002519 }
2520
Michal Vaskoab9deb62021-05-27 11:37:00 +02002521 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002522 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002523 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002524 CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_cp->url_config_src, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002525 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002526 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002527 }
2528 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002529 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002530 }
2531
2532 if (rpc_cp->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002533 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002534 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002535 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002536 lyrc = LY_ENOTFOUND;
2537 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002538 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002539 CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_cp->wd_mode), 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002540 }
2541 break;
2542
2543 case NC_RPC_DELETE:
2544 rpc_del = (struct nc_rpc_delete *)rpc;
2545
Michal Vaskoab9deb62021-05-27 11:37:00 +02002546 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-config", 0, &data));
2547 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002548 if (rpc_del->url) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002549 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_del->url, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002550 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002551 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_del->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002552 }
2553 break;
2554
2555 case NC_RPC_LOCK:
2556 rpc_lock = (struct nc_rpc_lock *)rpc;
2557
Michal Vaskoab9deb62021-05-27 11:37:00 +02002558 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "lock", 0, &data));
2559 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2560 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002561 break;
2562
2563 case NC_RPC_UNLOCK:
2564 rpc_lock = (struct nc_rpc_lock *)rpc;
2565
Michal Vaskoab9deb62021-05-27 11:37:00 +02002566 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "unlock", 0, &data));
2567 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2568 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002569 break;
2570
2571 case NC_RPC_GET:
2572 rpc_g = (struct nc_rpc_get *)rpc;
2573
Michal Vaskoab9deb62021-05-27 11:37:00 +02002574 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002575 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002576 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002577 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_g->filter, 0, LYD_ANYDATA_XML, 0, &node));
2578 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002579 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002580 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2581 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2582 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_g->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002583 }
2584 }
2585
2586 if (rpc_g->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002587 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002588 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002589 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002590 lyrc = LY_ENOTFOUND;
2591 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002592 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002593 CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_g->wd_mode), 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002594 }
2595 break;
2596
2597 case NC_RPC_KILL:
2598 rpc_k = (struct nc_rpc_kill *)rpc;
2599
Michal Vaskoab9deb62021-05-27 11:37:00 +02002600 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-session", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002601 sprintf(str, "%u", rpc_k->sid);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002602 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "session-id", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002603 break;
2604
2605 case NC_RPC_COMMIT:
2606 rpc_com = (struct nc_rpc_commit *)rpc;
2607
Michal Vaskoab9deb62021-05-27 11:37:00 +02002608 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002609 if (rpc_com->confirmed) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002610 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirmed", NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002611 }
2612
2613 if (rpc_com->confirm_timeout) {
2614 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002615 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirm-timeout", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002616 }
Michal Vasko086311b2016-01-08 09:53:11 +01002617 if (rpc_com->persist) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002618 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist", rpc_com->persist, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002619 }
Michal Vasko086311b2016-01-08 09:53:11 +01002620 if (rpc_com->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002621 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_com->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002622 }
2623 break;
2624
2625 case NC_RPC_DISCARD:
Michal Vaskoab9deb62021-05-27 11:37:00 +02002626 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "discard-changes", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002627 break;
2628
2629 case NC_RPC_CANCEL:
2630 rpc_can = (struct nc_rpc_cancel *)rpc;
2631
Michal Vaskoab9deb62021-05-27 11:37:00 +02002632 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "cancel-commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002633 if (rpc_can->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002634 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_can->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002635 }
2636 break;
2637
2638 case NC_RPC_VALIDATE:
2639 rpc_val = (struct nc_rpc_validate *)rpc;
2640
Michal Vaskoab9deb62021-05-27 11:37:00 +02002641 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "validate", 0, &data));
2642 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002643 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002644 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002645 CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_val->url_config_src, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002646 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002647 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_val->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002648 }
2649 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002650 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_val->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002651 }
2652 break;
2653
2654 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002655 rpc_gs = (struct nc_rpc_getschema *)rpc;
2656
Michal Vaskoab9deb62021-05-27 11:37:00 +02002657 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-schema", 0, &data));
2658 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "identifier", rpc_gs->identifier, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002659 if (rpc_gs->version) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002660 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "version", rpc_gs->version, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002661 }
2662 if (rpc_gs->format) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002663 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "format", rpc_gs->format, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002664 }
2665 break;
2666
2667 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002668 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2669
Michal Vaskoab9deb62021-05-27 11:37:00 +02002670 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "create-subscription", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002671 if (rpc_sub->stream) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002672 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_sub->stream, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002673 }
2674
2675 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002676 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002677 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_sub->filter, 0, LYD_ANYDATA_XML, 0, &node));
2678 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002679 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002680 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2681 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2682 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_sub->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002683 }
2684 }
Michal Vasko086311b2016-01-08 09:53:11 +01002685 if (rpc_sub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002686 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "startTime", rpc_sub->start, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002687 }
Michal Vasko086311b2016-01-08 09:53:11 +01002688 if (rpc_sub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002689 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stopTime", rpc_sub->stop, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002690 }
2691 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002692
2693 case NC_RPC_GETDATA:
2694 rpc_getd = (struct nc_rpc_getdata *)rpc;
2695
Michal Vaskoab9deb62021-05-27 11:37:00 +02002696 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-data", 0, &data));
2697 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_getd->datastore, 0, NULL));
2698
Michal Vaskoc1171a42019-11-05 12:06:46 +01002699 if (rpc_getd->filter) {
2700 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002701 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "subtree-filter", rpc_getd->filter, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002702 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002703 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "xpath-filter", rpc_getd->filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002704 }
2705 }
2706 if (rpc_getd->config_filter) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002707 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "config-filter", rpc_getd->config_filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002708 }
2709 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002710 CHECK_LYRC_BREAK(lyd_new_term(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" :
2711 "origin-filter", rpc_getd->origin_filter[i], 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002712 }
2713 if (rpc_getd->max_depth) {
2714 sprintf(str, "%u", rpc_getd->max_depth);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002715 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "max-depth", str, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002716 }
2717 if (rpc_getd->with_origin) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002718 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-origin", NULL, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002719 }
2720
2721 if (rpc_getd->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002722 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
Michal Vaskoab9deb62021-05-27 11:37:00 +02002723 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-defaults", nc_wd2str(rpc_getd->wd_mode), 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002724 }
2725 break;
2726
2727 case NC_RPC_EDITDATA:
2728 rpc_editd = (struct nc_rpc_editdata *)rpc;
2729
Michal Vaskoab9deb62021-05-27 11:37:00 +02002730 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-data", 0, &data));
2731 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_editd->datastore, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002732
2733 if (rpc_editd->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002734 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op], 0,
2735 NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002736 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002737 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002738 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_editd->edit_cont, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002739 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002740 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_editd->edit_cont, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002741 }
2742 break;
2743
Michal Vasko96f247a2021-03-15 13:32:10 +01002744 case NC_RPC_ESTABLISHSUB:
2745 rpc_estsub = (struct nc_rpc_establishsub *)rpc;
2746
Michal Vaskoab9deb62021-05-27 11:37:00 +02002747 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002748
2749 if (rpc_estsub->filter) {
2750 if (!rpc_estsub->filter[0] || (rpc_estsub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002751 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_estsub->filter, 0, LYD_ANYDATA_XML,
2752 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002753 } else if (rpc_estsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002754 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002755 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002756 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002757 }
2758 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002759 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_estsub->stream, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002760
2761 if (rpc_estsub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002762 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "replay-start-time", rpc_estsub->start, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002763 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002764 if (rpc_estsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002765 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002766 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002767 if (rpc_estsub->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002768 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estsub->encoding, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002769 }
2770 break;
2771
2772 case NC_RPC_MODIFYSUB:
2773 rpc_modsub = (struct nc_rpc_modifysub *)rpc;
2774
Michal Vaskoab9deb62021-05-27 11:37:00 +02002775 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002776
2777 sprintf(str, "%u", rpc_modsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002778 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002779
2780 if (rpc_modsub->filter) {
2781 if (!rpc_modsub->filter[0] || (rpc_modsub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002782 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_modsub->filter, 0, LYD_ANYDATA_XML,
2783 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002784 } else if (rpc_modsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002785 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002786 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002787 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002788 }
2789 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002790 if (rpc_modsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002791 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002792 }
2793 break;
2794
2795 case NC_RPC_DELETESUB:
2796 rpc_delsub = (struct nc_rpc_deletesub *)rpc;
2797
Michal Vaskoab9deb62021-05-27 11:37:00 +02002798 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002799
2800 sprintf(str, "%u", rpc_delsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002801 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002802 break;
2803
2804 case NC_RPC_KILLSUB:
2805 rpc_killsub = (struct nc_rpc_killsub *)rpc;
2806
Michal Vaskoab9deb62021-05-27 11:37:00 +02002807 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002808
2809 sprintf(str, "%u", rpc_killsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002810 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002811 break;
2812
Michal Vasko305faca2021-03-25 09:16:02 +01002813 case NC_RPC_ESTABLISHPUSH:
2814 rpc_estpush = (struct nc_rpc_establishpush *)rpc;
2815
Michal Vaskoab9deb62021-05-27 11:37:00 +02002816 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
2817 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_estpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002818
2819 if (rpc_estpush->filter) {
2820 if (!rpc_estpush->filter[0] || (rpc_estpush->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002821 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_estpush->filter, 0,
2822 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002823 } else if (rpc_estpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002824 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002825 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002826 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002827 }
2828 }
2829
2830 if (rpc_estpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002831 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002832 }
Michal Vasko305faca2021-03-25 09:16:02 +01002833 if (rpc_estpush->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002834 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estpush->encoding, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002835 }
2836
2837 if (rpc_estpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002838 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002839 sprintf(str, "%" PRIu32, rpc_estpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002840 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002841 if (rpc_estpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002842 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_estpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002843 }
2844 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002845 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002846 if (rpc_estpush->dampening_period) {
2847 sprintf(str, "%" PRIu32, rpc_estpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002848 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002849 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002850 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "sync-on-start", rpc_estpush->sync_on_start ? "true" : "false", 0,
2851 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002852 if (rpc_estpush->excluded_change) {
2853 for (i = 0; rpc_estpush->excluded_change[i]; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002854 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "excluded-change", rpc_estpush->excluded_change[i], 0,
2855 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002856 }
2857 }
2858 }
2859 break;
2860
2861 case NC_RPC_MODIFYPUSH:
2862 rpc_modpush = (struct nc_rpc_modifypush *)rpc;
2863
Michal Vaskoab9deb62021-05-27 11:37:00 +02002864 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01002865
2866 sprintf(str, "%u", rpc_modpush->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002867 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
2868 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_modpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002869
2870 if (rpc_modpush->filter) {
2871 if (!rpc_modpush->filter[0] || (rpc_modpush->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002872 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_modpush->filter, 0,
2873 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002874 } else if (rpc_modpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002875 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002876 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002877 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002878 }
2879 }
Michal Vasko305faca2021-03-25 09:16:02 +01002880 if (rpc_modpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002881 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002882 }
2883
2884 if (rpc_modpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002885 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002886 sprintf(str, "%" PRIu32, rpc_modpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002887 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002888 if (rpc_modpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002889 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_modpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002890 }
2891 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002892 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002893 if (rpc_modpush->dampening_period) {
2894 sprintf(str, "%" PRIu32, rpc_modpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002895 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002896 }
2897 }
2898 break;
2899
2900 case NC_RPC_RESYNCSUB:
2901 rpc_resyncsub = (struct nc_rpc_resyncsub *)rpc;
2902
Michal Vaskoab9deb62021-05-27 11:37:00 +02002903 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "resync-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01002904 sprintf(str, "%u", rpc_resyncsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002905 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002906 break;
2907
Michal Vasko96f247a2021-03-15 13:32:10 +01002908 case NC_RPC_UNKNOWN:
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002909 ERRINT;
2910 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002911 }
2912
Michal Vaskoab9deb62021-05-27 11:37:00 +02002913#undef CHECK_LYRC_BREAK
2914
2915 if (lyrc) {
Michal Vasko05532772021-06-03 12:12:38 +02002916 ERR(session, "Failed to create RPC, perhaps a required feature is disabled.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002917 lyd_free_tree(data);
Michal Vasko131120a2018-05-29 15:44:02 +02002918 return NC_MSG_ERROR;
2919 }
2920
Michal Vasko131120a2018-05-29 15:44:02 +02002921 /* send RPC, store its message ID */
2922 r = nc_send_msg_io(session, timeout, data);
2923 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002924
Radek Krejcib4b19062018-02-07 16:33:06 +01002925 if (dofree) {
Michal Vasko77367452021-02-16 16:32:18 +01002926 lyd_free_tree(data);
Radek Krejcib4b19062018-02-07 16:33:06 +01002927 }
Michal Vasko086311b2016-01-08 09:53:11 +01002928
Michal Vasko131120a2018-05-29 15:44:02 +02002929 if (r == NC_MSG_RPC) {
2930 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002931 }
Michal Vasko131120a2018-05-29 15:44:02 +02002932 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002933}
Michal Vaskode2946c2017-01-12 12:19:26 +01002934
2935API void
2936nc_client_session_set_not_strict(struct nc_session *session)
2937{
2938 if (session->side != NC_CLIENT) {
2939 ERRARG("session");
2940 return;
2941 }
2942
2943 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2944}