blob: 5aed2dc6b7a315f29537ebf7de7e705fdaf50098 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_client.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 session client functions
Michal Vasko086311b2016-01-08 09:53:11 +01005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010014 */
15
Radek Krejcifd5b6682017-06-13 15:52:53 +020016#define _GNU_SOURCE
Michal Vaskobbf52c82020-04-07 13:08:50 +020017
18#ifdef __linux__
19# include <sys/syscall.h>
20#endif
21
Michal Vaskob83a3fa2021-05-26 09:53:42 +020022#include <arpa/inet.h>
Michal Vasko086311b2016-01-08 09:53:11 +010023#include <assert.h>
tadeas-vintrlik54f142a2021-08-23 10:36:18 +020024#include <ctype.h>
Michal Vasko086311b2016-01-08 09:53:11 +010025#include <errno.h>
26#include <fcntl.h>
27#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010028#include <netinet/in.h>
Michal Vasko83ad17e2019-01-30 10:11:37 +010029#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include <poll.h>
Michal Vasko086311b2016-01-08 09:53:11 +010031#include <pthread.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020032#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010033#include <stdlib.h>
34#include <string.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020035#include <sys/select.h>
Michal Vasko086311b2016-01-08 09:53:11 +010036#include <sys/socket.h>
37#include <sys/stat.h>
38#include <sys/types.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020039#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010040#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010041
Michal Vasko5788c802024-05-03 16:14:40 +020042#ifdef NC_ENABLED_SSH_TLS
43#include <libssh/libssh.h>
44#endif
Michal Vasko086311b2016-01-08 09:53:11 +010045#include <libyang/libyang.h>
46
Michal Vasko9e8ac262020-04-07 13:06:45 +020047#include "compat.h"
roman3f9b65c2023-06-05 14:26:58 +020048#include "config.h"
49#include "log_p.h"
50#include "messages_p.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020051#include "session_client.h"
roman3f9b65c2023-06-05 14:26:58 +020052#include "session_client_ch.h"
53#include "session_p.h"
Michal Vasko086311b2016-01-08 09:53:11 +010054
Michal Vasko8a4e1462020-05-07 11:32:31 +020055#include "../modules/ietf_netconf@2013-09-29_yang.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020056#include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h"
Michal Vasko8a4e1462020-05-07 11:32:31 +020057
Michal Vasko80ef5d22016-01-18 09:21:02 +010058static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
59
roman2eab4742023-06-06 10:00:26 +020060#ifdef NC_ENABLED_SSH_TLS
Radek Krejci62aa0642017-05-25 16:33:49 +020061char *sshauth_password(const char *username, const char *hostname, void *priv);
62char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020063char *sshauth_privkey_passphrase(const char *privkey_path, void *priv);
roman2eab4742023-06-06 10:00:26 +020064#endif /* NC_ENABLED_SSH_TLS */
Radek Krejci62aa0642017-05-25 16:33:49 +020065
66static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
67static pthread_key_t nc_client_context_key;
68#ifdef __linux__
69static struct nc_client_context context_main = {
Michal Vaskoe49a15f2019-05-27 14:18:36 +020070 .opts.ka = {
71 .enabled = 1,
72 .idle_time = 1,
73 .max_probes = 10,
74 .probe_interval = 5
75 },
roman2eab4742023-06-06 10:00:26 +020076#ifdef NC_ENABLED_SSH_TLS
Radek Krejci62aa0642017-05-25 16:33:49 +020077 .ssh_opts = {
roman41a11e42022-06-22 09:27:08 +020078 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
Radek Krejci62aa0642017-05-25 16:33:49 +020079 .auth_password = sshauth_password,
80 .auth_interactive = sshauth_interactive,
81 .auth_privkey_passphrase = sshauth_privkey_passphrase
82 },
83 .ssh_ch_opts = {
84 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
Radek Krejci62aa0642017-05-25 16:33:49 +020085 .auth_password = sshauth_password,
86 .auth_interactive = sshauth_interactive,
87 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020088 },
roman2eab4742023-06-06 10:00:26 +020089#endif /* NC_ENABLED_SSH_TLS */
Radek Krejci62aa0642017-05-25 16:33:49 +020090 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020091 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020092};
93#endif
94
95static void
96nc_client_context_free(void *ptr)
97{
98 struct nc_client_context *c = (struct nc_client_context *)ptr;
99
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200100 if (--(c->refcount)) {
101 /* still used */
102 return;
103 }
104
Radek Krejci62aa0642017-05-25 16:33:49 +0200105#ifdef __linux__
106 /* in __linux__ we use static memory in the main thread,
107 * so this check is for programs terminating the main()
108 * function by pthread_exit() :)
109 */
110 if (c != &context_main)
111#endif
112 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200113 /* for the main thread the same is done in nc_client_destroy() */
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400114 free(c->opts.schema_searchpath);
115
roman2eab4742023-06-06 10:00:26 +0200116#ifdef NC_ENABLED_SSH_TLS
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400117 int i;
Michal Vasko292c5542023-02-01 14:33:17 +0100118
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400119 for (i = 0; i < c->opts.ch_bind_count; ++i) {
120 close(c->opts.ch_binds[i].sock);
121 free((char *)c->opts.ch_binds[i].address);
122 }
123 free(c->opts.ch_binds);
124 c->opts.ch_binds = NULL;
125 c->opts.ch_bind_count = 0;
roman2eab4742023-06-06 10:00:26 +0200126
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400127 _nc_client_ssh_destroy_opts(&c->ssh_opts);
128 _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
roman2eab4742023-06-06 10:00:26 +0200129
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400130 _nc_client_tls_destroy_opts(&c->tls_opts);
131 _nc_client_tls_destroy_opts(&c->tls_ch_opts);
roman2eab4742023-06-06 10:00:26 +0200132#endif /* NC_ENABLED_SSH_TLS */
Radek Krejci62aa0642017-05-25 16:33:49 +0200133 free(c);
134 }
135}
136
137static void
138nc_client_context_createkey(void)
139{
140 int r;
141
142 /* initiate */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200143 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {}
Radek Krejci62aa0642017-05-25 16:33:49 +0200144 pthread_setspecific(nc_client_context_key, NULL);
145}
146
147struct nc_client_context *
148nc_client_context_location(void)
149{
150 struct nc_client_context *e;
151
152 pthread_once(&nc_client_context_once, nc_client_context_createkey);
153 e = pthread_getspecific(nc_client_context_key);
154 if (!e) {
155 /* prepare ly_err storage */
156#ifdef __linux__
157 if (getpid() == syscall(SYS_gettid)) {
158 /* main thread - use global variable instead of thread-specific variable. */
159 e = &context_main;
160 } else
161#endif /* __linux__ */
162 {
163 e = calloc(1, sizeof *e);
164 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200165 e->refcount = 1;
roman2eab4742023-06-06 10:00:26 +0200166#ifdef NC_ENABLED_SSH_TLS
Michal Vasko487ab802024-04-26 12:20:29 +0200167# ifdef HAVE_TERMIOS
168 e->ssh_opts.knownhosts_mode = NC_SSH_KNOWNHOSTS_ASK;
169# else
170 e->ssh_opts.knownhosts_mode = NC_SSH_KNOWNHOSTS_ACCEPT;
171# endif
Radek Krejci62aa0642017-05-25 16:33:49 +0200172 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
roman41a11e42022-06-22 09:27:08 +0200173 e->ssh_opts.auth_pref[0].value = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200174 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
175 e->ssh_opts.auth_pref[1].value = 2;
176 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
roman41a11e42022-06-22 09:27:08 +0200177 e->ssh_opts.auth_pref[2].value = 3;
Radek Krejci62aa0642017-05-25 16:33:49 +0200178 e->ssh_opts.auth_password = sshauth_password;
179 e->ssh_opts.auth_interactive = sshauth_interactive;
180 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
181
roman41a11e42022-06-22 09:27:08 +0200182 /* callhome settings are the same */
Radek Krejci62aa0642017-05-25 16:33:49 +0200183 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
184 e->ssh_ch_opts.auth_pref[0].value = 1;
185 e->ssh_ch_opts.auth_pref[1].value = 2;
186 e->ssh_ch_opts.auth_pref[2].value = 3;
roman2eab4742023-06-06 10:00:26 +0200187#endif /* NC_ENABLED_SSH_TLS */
Radek Krejci62aa0642017-05-25 16:33:49 +0200188 }
189 pthread_setspecific(nc_client_context_key, e);
190 }
191
192 return e;
193}
194
195#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100196
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200197API void *
198nc_client_get_thread_context(void)
199{
200 return nc_client_context_location();
201}
202
203API void
204nc_client_set_thread_context(void *context)
205{
206 struct nc_client_context *old, *new;
207
208 if (!context) {
roman40672412023-05-04 11:10:22 +0200209 ERRARG(NULL, "context");
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200210 return;
211 }
212
213 new = (struct nc_client_context *)context;
214 old = nc_client_context_location();
215 if (old == new) {
216 /* nothing to change */
217 return;
218 }
219
220 /* replace old by new, increase reference counter in the newly set context */
221 nc_client_context_free(old);
222 new->refcount++;
223 pthread_setspecific(nc_client_context_key, new);
224}
225
Michal Vasko78939072022-12-12 07:43:18 +0100226/**
227 * @brief Ext data callback for a context to provide schema mount data.
228 */
229static LY_ERR
230nc_ly_ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
231{
232 struct nc_session *session = user_data;
233
234 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) {
235 return LY_EINVAL;
236 }
237
238 if (!session->opts.client.ext_data) {
239 ERR(session, "Unable to parse mounted data, no operational schema-mounts data received from the server.");
240 return LY_ENOTFOUND;
241 }
242
243 /* return ext data */
244 *ext_data = session->opts.client.ext_data;
245 *ext_data_free = 0;
246
247 return LY_SUCCESS;
248}
249
Radek Krejcifd5b6682017-06-13 15:52:53 +0200250int
Michal Vasko78939072022-12-12 07:43:18 +0100251nc_client_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200252{
253 /* assign context (dicionary needed for handshake) */
254 if (!ctx) {
Michal Vasko77367452021-02-16 16:32:18 +0100255 if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200256 return EXIT_FAILURE;
257 }
258
Michal Vasko5ca5d972022-09-14 13:51:31 +0200259 /* user path must be first, the first path is used to store modules retreived via get-schema */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200260 if (client_opts.schema_searchpath) {
261 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
Michal Vasko5d0cdc32022-12-12 07:38:21 +0100262 }
Michal Vasko80227222022-12-12 09:05:24 +0100263 if (!access(NC_CLIENT_SEARCH_DIR, F_OK)) {
264 ly_ctx_set_searchdir(ctx, NC_CLIENT_SEARCH_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200265 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200266
Michal Vasko5ca5d972022-09-14 13:51:31 +0200267 /* set callback for getting modules, if provided */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200268 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
Michal Vasko78939072022-12-12 07:43:18 +0100269
270 /* set ext data callback to avoid errors that no callback is set, the data are stored later, if any */
271 ly_ctx_set_ext_data_clb(ctx, nc_ly_ext_data_clb, session);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200272 } else {
273 session->flags |= NC_SESSION_SHAREDCTX;
274 }
275
276 session->ctx = ctx;
277
278 return EXIT_SUCCESS;
279}
280
Michal Vasko086311b2016-01-08 09:53:11 +0100281API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100282nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100283{
Michal Vasko3031aae2016-01-27 16:07:18 +0100284 if (client_opts.schema_searchpath) {
285 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100286 }
Michal Vasko086311b2016-01-08 09:53:11 +0100287
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100288 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100289 client_opts.schema_searchpath = strdup(path);
roman3a95bb22023-10-26 11:07:17 +0200290 NC_CHECK_ERRMEM_RET(!client_opts.schema_searchpath, 1);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100291 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100292 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100293 }
294
295 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100296}
297
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100298API const char *
299nc_client_get_schema_searchpath(void)
300{
301 return client_opts.schema_searchpath;
302}
303
Radek Krejcifd5b6682017-06-13 15:52:53 +0200304API int
305nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100306{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200307 client_opts.schema_clb = clb;
308 if (clb) {
309 client_opts.schema_clb_data = user_data;
310 } else {
311 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100312 }
313
314 return 0;
315}
316
Radek Krejcifd5b6682017-06-13 15:52:53 +0200317API ly_module_imp_clb
318nc_client_get_schema_callback(void **user_data)
319{
320 if (user_data) {
321 (*user_data) = client_opts.schema_clb_data;
322 }
323 return client_opts.schema_clb;
324}
325
Michal Vaskoa272e092023-07-10 14:34:03 +0200326API void
327nc_client_set_new_session_context_autofill(int enabled)
328{
329 client_opts.auto_context_fill_disabled = !enabled;
330}
331
Michal Vasko5ca5d972022-09-14 13:51:31 +0200332struct module_info {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200333 char *name;
334 char *revision;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200335
Radek Krejci65ef6d52018-08-16 16:35:02 +0200336 struct {
337 char *name;
338 char *revision;
339 } *submodules;
Michal Vasko77367452021-02-16 16:32:18 +0100340 char **features;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200341 int implemented;
342};
343
344struct clb_data_s {
345 void *user_data;
346 ly_module_imp_clb user_clb;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200347 struct module_info *modules;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200348 struct nc_session *session;
349 int has_get_schema;
350};
351
Michal Vaskoc088f982021-10-05 12:23:07 +0200352/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200353 * @brief Retrieve YANG module content from a local file.
Michal Vaskoc088f982021-10-05 12:23:07 +0200354 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200355 * @param[in] name Module name.
356 * @param[in] rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200357 * @param[in] clb_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200358 * @param[out] format Module format.
359 * @return Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200360 */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200361static char *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200362retrieve_module_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200363 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100364{
Michal Vaskob13a2242023-12-05 13:29:07 +0100365 char *localfile = NULL, *model_data = NULL;
366 const char *ptr;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200367 FILE *f;
368 long length, l;
Michal Vasko086311b2016-01-08 09:53:11 +0100369
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200370 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200371 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
372 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200373 return NULL;
374 }
Michal Vaskob13a2242023-12-05 13:29:07 +0100375 if (localfile && rev) {
376 ptr = strrchr(localfile, '/');
377 if (!strchr(ptr, '@')) {
378 /* we do not know the revision of the module and we require a specific one, so ignore this module */
379 localfile = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100380 }
Michal Vasko086311b2016-01-08 09:53:11 +0100381 }
382
Michal Vaskob13a2242023-12-05 13:29:07 +0100383 if (!localfile) {
384 return NULL;
385 }
386
387 VRB(clb_data->session, "Reading module \"%s@%s\" from local file \"%s\".", name, rev ? rev : "<latest>",
388 localfile);
389 f = fopen(localfile, "r");
390 if (!f) {
391 ERR(clb_data->session, "Unable to open file \"%s\" (%s).", localfile, strerror(errno));
392 free(localfile);
393 return NULL;
394 }
395
396 fseek(f, 0, SEEK_END);
397 length = ftell(f);
398 if (length < 0) {
399 ERR(clb_data->session, "Unable to get the size of module file \"%s\".", localfile);
400 free(localfile);
401 fclose(f);
402 return NULL;
403 }
404 fseek(f, 0, SEEK_SET);
405
406 model_data = malloc(length + 1);
407 if (!model_data) {
408 ERRMEM;
409 } else if ((l = fread(model_data, 1, length, f)) != length) {
410 ERR(clb_data->session, "Reading module from \"%s\" failed (%d bytes read, but %d expected).", localfile, l,
411 length);
412 free(model_data);
413 model_data = NULL;
414 } else {
415 /* terminating NULL byte */
416 model_data[length] = '\0';
417 }
418 fclose(f);
419 free(localfile);
420
Radek Krejci65ef6d52018-08-16 16:35:02 +0200421 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100422}
423
Michal Vaskoc088f982021-10-05 12:23:07 +0200424/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200425 * @brief Retrieve YANG module content from a reply to get-schema RPC.
Michal Vaskoc088f982021-10-05 12:23:07 +0200426 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200427 * @param[in] name Module name.
428 * @param[in] rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200429 * @param[in] clb_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200430 * @param[out] format Module format.
431 * @return Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200432 */
Michal Vasko086311b2016-01-08 09:53:11 +0100433static char *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200434retrieve_module_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200435 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100436{
Michal Vasko086311b2016-01-08 09:53:11 +0100437 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100438 struct lyd_node *envp = NULL, *op = NULL;
439 struct lyd_node_any *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100440 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100441 uint64_t msgid;
Michal Vasko7502b602022-08-26 11:51:17 +0200442 char *localfile = NULL, *envp_str = NULL, *model_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200443 FILE *f;
Michal Vasko086311b2016-01-08 09:53:11 +0100444
Michal Vasko5ca5d972022-09-14 13:51:31 +0200445 VRB(clb_data->session, "Reading module \"%s@%s\" from server via get-schema.", name, rev ? rev : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200446 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100447
Radek Krejci65ef6d52018-08-16 16:35:02 +0200448 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100449 usleep(1000);
450 }
451 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200452 ERR(clb_data->session, "Failed to send the <get-schema> RPC.");
Michal Vasko086311b2016-01-08 09:53:11 +0100453 nc_rpc_free(rpc);
454 return NULL;
455 }
456
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200457 do {
Michal Vasko77367452021-02-16 16:32:18 +0100458 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200459 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100460 nc_rpc_free(rpc);
461 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko05532772021-06-03 12:12:38 +0200462 ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired.");
Michal Vasko77367452021-02-16 16:32:18 +0100463 goto cleanup;
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100464 } else if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200465 ERR(clb_data->session, "Failed to receive a reply to <get-schema>.");
Michal Vasko77367452021-02-16 16:32:18 +0100466 goto cleanup;
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100467 } else if (!op) {
Michal Vasko7502b602022-08-26 11:51:17 +0200468 assert(envp);
469 lyd_print_mem(&envp_str, envp, LYD_XML, 0);
470 WRN(clb_data->session, "Received an unexpected reply to <get-schema>:\n%s", envp_str);
471 free(envp_str);
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100472 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100473 }
474
Michal Vasko77367452021-02-16 16:32:18 +0100475 if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) {
Michal Vasko05532772021-06-03 12:12:38 +0200476 ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC.");
Michal Vasko77367452021-02-16 16:32:18 +0100477 goto cleanup;
Michal Vaskob2583f12016-05-12 11:40:23 +0200478 }
Michal Vasko77367452021-02-16 16:32:18 +0100479 get_schema_data = (struct lyd_node_any *)lyd_child(op);
Radek Krejci539efb62016-08-24 15:05:16 +0200480 switch (get_schema_data->value_type) {
Radek Krejci539efb62016-08-24 15:05:16 +0200481 case LYD_ANYDATA_STRING:
Michal Vasko77367452021-02-16 16:32:18 +0100482 case LYD_ANYDATA_XML:
Michal Vaskob2583f12016-05-12 11:40:23 +0200483 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200484 break;
485 case LYD_ANYDATA_DATATREE:
Michal Vasko77367452021-02-16 16:32:18 +0100486 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Radek Krejci539efb62016-08-24 15:05:16 +0200487 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200488 case LYD_ANYDATA_JSON:
Michal Vaskod838d292018-08-15 11:39:05 +0200489 case LYD_ANYDATA_LYB:
Radek Krejci03438ec2016-09-21 14:12:26 +0200490 ERRINT;
Michal Vasko77367452021-02-16 16:32:18 +0100491 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200492 }
Michal Vasko086311b2016-01-08 09:53:11 +0100493
Radek Krejci488b0702018-08-29 14:47:15 +0200494 if (model_data && !model_data[0]) {
495 /* empty data */
496 free(model_data);
497 model_data = NULL;
498 }
Michal Vaskoa1721172022-12-12 09:06:53 +0100499 if (!model_data) {
500 goto cleanup;
501 }
502
503 /* set format */
504 *format = LYS_IN_YANG;
Radek Krejci488b0702018-08-29 14:47:15 +0200505
Michal Vasko5ca5d972022-09-14 13:51:31 +0200506 /* try to store the model_data into local module repository */
Michal Vaskoa1721172022-12-12 09:06:53 +0100507 lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx), 0, name, rev, &localfile, NULL);
508 if (client_opts.schema_searchpath && !localfile) {
509 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name, rev ? "@" : "",
510 rev ? rev : "") == -1) {
511 ERRMEM;
512 } else {
513 f = fopen(localfile, "w");
514 if (!f) {
515 WRN(clb_data->session, "Unable to store \"%s\" as a local copy of module retrieved via <get-schema> (%s).",
516 localfile, strerror(errno));
Michal Vaskof945da52018-02-15 08:45:13 +0100517 } else {
Michal Vaskoa1721172022-12-12 09:06:53 +0100518 fputs(model_data, f);
519 fclose(f);
Michal Vaskof945da52018-02-15 08:45:13 +0100520 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200521 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200522 }
Michal Vaskoa1721172022-12-12 09:06:53 +0100523 free(localfile);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200524
Michal Vasko77367452021-02-16 16:32:18 +0100525cleanup:
526 lyd_free_tree(envp);
527 lyd_free_tree(op);
Michal Vasko086311b2016-01-08 09:53:11 +0100528 return model_data;
529}
530
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200531static void
532free_with_user_data(void *data, void *user_data)
Jan Kundrát35972df2018-09-06 19:00:01 +0200533{
534 free(data);
535 (void)user_data;
536}
537
Michal Vaskoc088f982021-10-05 12:23:07 +0200538/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200539 * @brief Retrieve YANG module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200540 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200541 * @param[in] mod_name Module name.
542 * @param[in] mod_rev Module revision.
Michal Vasko5e32c402022-01-12 14:05:09 +0100543 * @param[in] user_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200544 * @param[out] format Module format.
545 * @param[out] module_data Module content.
Michal Vasko5e32c402022-01-12 14:05:09 +0100546 * @param[out] free_module_data Callback for freeing @p module_data.
547 * @return LY_ERR value.
548 */
549static LY_ERR
Michal Vasko5ca5d972022-09-14 13:51:31 +0200550retrieve_module_data(const char *mod_name, const char *mod_rev, void *user_data, LYS_INFORMAT *format,
Michal Vasko5e32c402022-01-12 14:05:09 +0100551 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
552{
553 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
554 char *model_data = NULL;
555
Michal Vasko5e32c402022-01-12 14:05:09 +0100556 /* 1. try to get data locally */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200557 model_data = retrieve_module_data_localfile(mod_name, mod_rev, clb_data, format);
Michal Vasko5e32c402022-01-12 14:05:09 +0100558
559 /* 2. try to use <get-schema> */
560 if (!model_data && clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200561 model_data = retrieve_module_data_getschema(mod_name, mod_rev, clb_data, format);
Michal Vasko5e32c402022-01-12 14:05:09 +0100562 }
563
564 /* 3. try to use user callback */
565 if (!model_data && clb_data->user_clb) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200566 VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", mod_name, mod_rev ? mod_rev : "<latest>");
Michal Vasko5e32c402022-01-12 14:05:09 +0100567 clb_data->user_clb(mod_name, mod_rev, NULL, NULL, clb_data->user_data, format, (const char **)&model_data,
568 free_module_data);
569 }
570
571 *free_module_data = free_with_user_data;
572 *module_data = model_data;
573 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
574}
575
576/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200577 * @brief Retrieve YANG import module content.
Michal Vasko5e32c402022-01-12 14:05:09 +0100578 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200579 * @param[in] mod_name Module name.
580 * @param[in] mod_rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200581 * @param[in] submod_name Optional submodule name.
582 * @param[in] sub_rev Submodule revision.
583 * @param[in] user_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200584 * @param[out] format Module format.
585 * @param[out] module_data Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200586 * @param[out] free_module_data Callback for freeing @p module_data.
587 * @return LY_ERR value.
588 */
Michal Vasko77367452021-02-16 16:32:18 +0100589static LY_ERR
Michal Vasko5ca5d972022-09-14 13:51:31 +0200590retrieve_module_data_imp(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
Michal Vasko77367452021-02-16 16:32:18 +0100591 void *user_data, LYS_INFORMAT *format, const char **module_data,
592 void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200593{
594 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
Michal Vasko5e32c402022-01-12 14:05:09 +0100595 uint32_t u, v, match = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200596 const char *name = NULL, *rev = NULL;
597 char *model_data = NULL;
598
Michal Vasko5ca5d972022-09-14 13:51:31 +0200599 /* get and check the final name and revision of the module to be retrieved */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200600 if (!mod_rev || !mod_rev[0]) {
601 /* newest revision requested - get the newest revision from the list of available modules on server */
602 match = 0;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200603 for (u = 0; clb_data->modules[u].name; ++u) {
604 if (strcmp(mod_name, clb_data->modules[u].name)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200605 continue;
606 }
Michal Vasko5ca5d972022-09-14 13:51:31 +0200607 if (!match || (strcmp(mod_rev, clb_data->modules[u].revision) > 0)) {
608 mod_rev = clb_data->modules[u].revision;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200609 }
610 match = u + 1;
611 }
612 if (!match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200613 /* valid situation if we are retrieving YANG 1.1 module and have only capabilities for now
Michal Vasko1d2665c2021-02-12 09:28:44 +0100614 * (when loading ietf-datastore for ietf-yang-library) */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200615 VRB(clb_data->session, "Unable to identify revision of the import module \"%s\" from "
Michal Vasko05532772021-06-03 12:12:38 +0200616 "the available server side information.", mod_name);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200617 }
618 }
619 if (submod_name) {
620 name = submod_name;
621 if (sub_rev) {
622 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200623 } else if (match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200624 if (!clb_data->modules[match - 1].submodules) {
Michal Vasko05532772021-06-03 12:12:38 +0200625 VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", "
Michal Vasko5ca5d972022-09-14 13:51:31 +0200626 "in import module \"%s\", from the available server side information.", submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200627 } else {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200628 for (v = 0; clb_data->modules[match - 1].submodules[v].name; ++v) {
629 if (!strcmp(submod_name, clb_data->modules[match - 1].submodules[v].name)) {
630 rev = sub_rev = clb_data->modules[match - 1].submodules[v].revision;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200631 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200632 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200633 if (!rev) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200634 ERR(clb_data->session, "Requested submodule \"%s\" is not found in import module \"%s\" on server side.",
Michal Vasko05532772021-06-03 12:12:38 +0200635 submod_name, mod_name);
Michal Vasko77367452021-02-16 16:32:18 +0100636 return LY_ENOTFOUND;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200637 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200638 }
639 }
640 } else {
641 name = mod_name;
642 rev = mod_rev;
643 }
644
Radek Krejci65ef6d52018-08-16 16:35:02 +0200645 if (match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200646 /* we have enough information to avoid communication with server and try to get the module locally */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200647
648 /* 1. try to get data locally */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200649 model_data = retrieve_module_data_localfile(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200650
651 /* 2. try to use <get-schema> */
652 if (!model_data && clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200653 model_data = retrieve_module_data_getschema(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200654 }
655 } else {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200656 /* we are unsure which revision of the module we should load, so first try to get
Radek Krejci65ef6d52018-08-16 16:35:02 +0200657 * the newest revision from the server via get-schema and only if the server does not
658 * implement get-schema, try to load the newest revision locally. This is imperfect
659 * solution, but there are situation when a client does not know what revision is
660 * actually implemented by the server. */
661
662 /* 1. try to use <get-schema> */
663 if (clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200664 model_data = retrieve_module_data_getschema(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200665 }
666
667 /* 2. try to get data locally */
668 if (!model_data) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200669 model_data = retrieve_module_data_localfile(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200670 }
671 }
672
673 /* 3. try to use user callback */
674 if (!model_data && clb_data->user_clb) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200675 VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", name, rev ? rev : "<latest>");
Michal Vasko77367452021-02-16 16:32:18 +0100676 clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format,
677 (const char **)&model_data, free_module_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200678 }
679
Jan Kundrát35972df2018-09-06 19:00:01 +0200680 *free_module_data = free_with_user_data;
Michal Vasko77367452021-02-16 16:32:18 +0100681 *module_data = model_data;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200682 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200683}
684
Michal Vaskoc088f982021-10-05 12:23:07 +0200685/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200686 * @brief Load a YANG module into context.
Michal Vaskoc088f982021-10-05 12:23:07 +0200687 *
688 * @param[in] session NC session.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200689 * @param[in] name Module name.
690 * @param[in] revision Module revision.
691 * @param[in] features Enabled module features.
692 * @param[in] modules Server module info built from capabilities.
693 * @param[in] user_clb User callback for retrieving module data.
Michal Vaskoc088f982021-10-05 12:23:07 +0200694 * @param[in] user_data User data for @p user_clb.
695 * @param[in] has_get_schema Whether the server supports get-schema.
696 * @param[out] mod Loaded module.
697 * @return 0 on success.
698 * @return -1 on error.
699 */
Michal Vaskoceae0152018-02-14 16:03:59 +0100700static int
Michal Vasko5e32c402022-01-12 14:05:09 +0100701nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, const char **features,
Michal Vasko5ca5d972022-09-14 13:51:31 +0200702 struct module_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema, struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100703{
704 int ret = 0;
Michal Vasko58791da2024-02-26 13:52:59 +0100705 const struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200706 const char *module_data = NULL;
Michal Vasko5e32c402022-01-12 14:05:09 +0100707 struct ly_in *in;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200708 LYS_INFORMAT format;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200709
710 void (*free_module_data)(void *, void *) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200711 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100712
Michal Vasko5e32c402022-01-12 14:05:09 +0100713 /* try to use a module from the context */
Michal Vaskoc3e402f2022-09-14 13:34:55 +0200714 *mod = ly_ctx_get_module_implemented(session->ctx, name);
715 if (!*mod) {
716 if (revision) {
717 *mod = ly_ctx_get_module(session->ctx, name, revision);
718 } else {
Michal Vasko5e32c402022-01-12 14:05:09 +0100719 *mod = ly_ctx_get_module_latest(session->ctx, name);
720 }
Michal Vaskoc3e402f2022-09-14 13:34:55 +0200721 } else if (revision && (!(*mod)->revision || strcmp((*mod)->revision, revision))) {
722 WRN(session, "Server implements module \"%s\" in revision \"%s\" but revision \"%s\" is already implemented"
723 " and will be used instead.", name, revision, (*mod)->revision ? (*mod)->revision : "<none>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200724 }
Michal Vasko5e32c402022-01-12 14:05:09 +0100725
Michal Vaskoceae0152018-02-14 16:03:59 +0100726 if (*mod) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100727 /* make the present module implemented and/or enable all its features */
728 if (lys_set_implemented(*mod, features)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200729 ERR(session, "Failed to implement module \"%s\".", (*mod)->name);
Michal Vasko5e32c402022-01-12 14:05:09 +0100730 ret = -1;
Michal Vaskoceae0152018-02-14 16:03:59 +0100731 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200732 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100733 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200734 clb_data.has_get_schema = has_get_schema;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200735 clb_data.modules = modules;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200736 clb_data.session = session;
737 clb_data.user_clb = user_clb;
738 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100739
740 /* clear all the errors and just collect them for now */
741 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200742 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100743
Radek Krejci65ef6d52018-08-16 16:35:02 +0200744 /* get module data */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200745 retrieve_module_data(name, revision, &clb_data, &format, &module_data, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100746
Radek Krejci65ef6d52018-08-16 16:35:02 +0200747 if (module_data) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100748 /* set import callback */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200749 ly_ctx_set_module_imp_clb(session->ctx, retrieve_module_data_imp, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100750
Michal Vasko5ca5d972022-09-14 13:51:31 +0200751 /* parse the module */
Michal Vasko5e32c402022-01-12 14:05:09 +0100752 ly_in_new_memory(module_data, &in);
753 lys_parse(session->ctx, in, format, features, mod);
754 ly_in_free(in, 0);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200755 if (*free_module_data) {
Michal Vasko77367452021-02-16 16:32:18 +0100756 (*free_module_data)((char *)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100757 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100758
Radek Krejci65ef6d52018-08-16 16:35:02 +0200759 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
760 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100761
Michal Vaskodbf7c272018-02-19 09:07:59 +0100762 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200763 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100764 if (!(*mod)) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100765 for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) {
Michal Vasko77367452021-02-16 16:32:18 +0100766 ly_err_print(session->ctx, eitem);
Michal Vaskoceae0152018-02-14 16:03:59 +0100767 }
768 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200769 } else {
770 /* print only warnings */
Michal Vasko5e32c402022-01-12 14:05:09 +0100771 for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200772 if (eitem->level == LY_LLWRN) {
Michal Vasko77367452021-02-16 16:32:18 +0100773 ly_err_print(session->ctx, eitem);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200774 }
775 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100776 }
777
Michal Vaskodbf7c272018-02-19 09:07:59 +0100778 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100779 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100780 }
781
782 return ret;
783}
784
Radek Krejci65ef6d52018-08-16 16:35:02 +0200785static void
Michal Vasko5ca5d972022-09-14 13:51:31 +0200786free_module_info(struct module_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200787{
Michal Vasko77367452021-02-16 16:32:18 +0100788 uint32_t u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200789
Radek Krejci65ef6d52018-08-16 16:35:02 +0200790 if (!list) {
791 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200792 }
793
Radek Krejci65ef6d52018-08-16 16:35:02 +0200794 for (u = 0; list[u].name; ++u) {
795 free(list[u].name);
796 free(list[u].revision);
Michal Vasko77367452021-02-16 16:32:18 +0100797 if (list[u].features) {
798 for (v = 0; list[u].features[v]; ++v) {
799 free(list[u].features[v]);
800 }
801 free(list[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200802 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200803 if (list[u].submodules) {
804 for (v = 0; list[u].submodules[v].name; ++v) {
805 free(list[u].submodules[v].name);
806 free(list[u].submodules[v].revision);
807 }
808 free(list[u].submodules);
809 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200810 }
811 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200812}
813
Michal Vaskoc088f982021-10-05 12:23:07 +0200814/**
Michal Vasko78939072022-12-12 07:43:18 +0100815 * @brief Retrieve yang-library and schema-mounts operational data from the server.
Michal Vaskoc088f982021-10-05 12:23:07 +0200816 *
817 * @param[in] session NC session.
818 * @param[in] has_get_data Whether get-data RPC is available or only get.
Michal Vasko78939072022-12-12 07:43:18 +0100819 * @param[in] filter Filter to use.
820 * @param[out] oper_data Received data.
Michal Vaskoc088f982021-10-05 12:23:07 +0200821 * @return 0 on success.
822 * @return -1 on error.
823 */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200824static int
Michal Vasko78939072022-12-12 07:43:18 +0100825get_oper_data(struct nc_session *session, int has_get_data, const char *filter, struct lyd_node **oper_data)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200826{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200827 struct nc_rpc *rpc = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100828 struct lyd_node *op = NULL, *envp = NULL;
829 struct lyd_node_any *data;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200830 NC_MSG_TYPE msg;
831 uint64_t msgid;
Michal Vasko303263d2021-10-05 12:18:21 +0200832 int ret = 0;
833 const char *rpc_name;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200834
Michal Vasko78939072022-12-12 07:43:18 +0100835 /* get data from the server */
Michal Vasko303263d2021-10-05 12:18:21 +0200836 if (has_get_data) {
Michal Vasko78939072022-12-12 07:43:18 +0100837 rpc_name = "<get-data>";
838 rpc = nc_rpc_getdata("ietf-datastores:operational", filter, "false", NULL, 0, 0, 0, 0, 0, NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200839 } else {
Michal Vasko78939072022-12-12 07:43:18 +0100840 rpc_name = "<get>";
841 rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200842 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200843 if (!rpc) {
844 goto cleanup;
845 }
846
847 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
848 usleep(1000);
849 }
850 if (msg == NC_MSG_ERROR) {
Michal Vasko78939072022-12-12 07:43:18 +0100851 WRN(session, "Failed to send %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200852 goto cleanup;
853 }
854
855 do {
Michal Vasko77367452021-02-16 16:32:18 +0100856 lyd_free_tree(envp);
857 lyd_free_tree(op);
858
859 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200860 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200861 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko78939072022-12-12 07:43:18 +0100862 WRN(session, "Timeout for receiving reply to a %s RPC expired.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200863 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100864 } else if (msg == NC_MSG_ERROR) {
Michal Vasko78939072022-12-12 07:43:18 +0100865 WRN(session, "Failed to receive a reply to %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200866 goto cleanup;
Jan Kundrát1d73a5a2022-03-30 16:54:49 +0200867 } else if (!op || !lyd_child(op) || !lyd_child(op)->schema || strcmp(lyd_child(op)->schema->name, "data")) {
Michal Vasko78939072022-12-12 07:43:18 +0100868 WRN(session, "Unexpected reply without data to a %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200869 goto cleanup;
870 }
871
Michal Vasko77367452021-02-16 16:32:18 +0100872 data = (struct lyd_node_any *)lyd_child(op);
873 if (data->value_type != LYD_ANYDATA_DATATREE) {
Michal Vasko78939072022-12-12 07:43:18 +0100874 WRN(session, "Unexpected data in reply to a %s RPC.", rpc_name);
Michal Vasko40528752021-06-21 15:41:51 +0200875 goto cleanup;
876 } else if (!data->value.tree) {
Michal Vasko78939072022-12-12 07:43:18 +0100877 WRN(session, "No data in reply to a %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200878 goto cleanup;
879 }
880
Michal Vasko78939072022-12-12 07:43:18 +0100881 *oper_data = data->value.tree;
882 data->value.tree = NULL;
883
884cleanup:
885 nc_rpc_free(rpc);
886 lyd_free_tree(envp);
887 lyd_free_tree(op);
888
889 if (session->status != NC_STATUS_RUNNING) {
890 /* something bad happened, discard the session */
891 ERR(session, "Invalid session, discarding.");
892 ret = -1;
893 }
894
895 return ret;
896}
897
898/**
899 * @brief Build server module info from ietf-yang-library data.
900 *
901 * @param[in] session NC session.
902 * @param[in] get_data_sup Whether get-data RPC is available or only get.
903 * @param[in] xpath_sup Whether XPath filter is supported or only subtree filter.
904 * @param[out] result Server modules.
905 * @return 0 on success.
906 * @return -1 on error.
907 */
908static int
909build_module_info_yl(struct nc_session *session, int get_data_sup, int xpath_sup, struct module_info **result)
910{
911 struct ly_set *modules = NULL;
912 uint32_t u, v, submodules_count, feature_count;
913 struct lyd_node *iter, *child, *oper_data = NULL;
914 struct lys_module *mod;
915 int ret = 0;
916
917 /* get yang-library operational data */
918 if (xpath_sup) {
919 if (get_oper_data(session, get_data_sup, "/ietf-yang-library:*", &oper_data)) {
920 goto cleanup;
921 }
922 } else {
923 if (get_oper_data(session, get_data_sup,
924 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", &oper_data)) {
925 goto cleanup;
926 }
927 }
928 if (!oper_data) {
929 goto cleanup;
930 }
931
932 if (lyd_find_xpath(oper_data, "/ietf-yang-library:modules-state/module", &modules)) {
933 WRN(NULL, "No yang-library module information found.");
Radek Krejci2d088832018-08-21 13:40:14 +0200934 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200935 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200936
Michal Vasko77367452021-02-16 16:32:18 +0100937 (*result) = calloc(modules->count + 1, sizeof **result);
roman3a95bb22023-10-26 11:07:17 +0200938 NC_CHECK_ERRMEM_GOTO(!(*result), ret = -1, cleanup);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200939
Michal Vasko77367452021-02-16 16:32:18 +0100940 for (u = 0; u < modules->count; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200941 submodules_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100942 feature_count = 0;
943 mod = ((struct lyd_node *)modules->dnodes[u])->schema->module;
944 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
Michal Vasko5d9d5972021-06-09 14:17:01 +0200945 if (!iter->schema || (iter->schema->module != mod)) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200946 /* ignore node from other schemas (augments) */
947 continue;
948 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200949 if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200950 /* ignore empty nodes */
951 continue;
952 }
953 if (!strcmp(iter->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200954 (*result)[u].name = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200955 } else if (!strcmp(iter->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200956 (*result)[u].revision = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200957 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200958 (*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200959 } else if (!strcmp(iter->schema->name, "feature")) {
Michal Vasko77367452021-02-16 16:32:18 +0100960 (*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features);
roman3a95bb22023-10-26 11:07:17 +0200961 NC_CHECK_ERRMEM_GOTO(!(*result)[u].features, free_module_info(*result); *result = NULL; ret = -1, cleanup);
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200962 (*result)[u].features[feature_count] = strdup(lyd_get_value(iter));
Michal Vasko77367452021-02-16 16:32:18 +0100963 (*result)[u].features[feature_count + 1] = NULL;
964 ++feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200965 } else if (!strcmp(iter->schema->name, "submodule")) {
966 submodules_count++;
967 }
968 }
969
970 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200971 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
roman3a95bb22023-10-26 11:07:17 +0200972 NC_CHECK_ERRMEM_GOTO(!(*result)[u].submodules, free_module_info(*result); *result = NULL; ret = -1, cleanup);
973 v = 0;
974 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
975 mod = modules->dnodes[u]->schema->module;
976 if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) {
977 LY_LIST_FOR(lyd_child(iter), child) {
978 if (mod != child->schema->module) {
979 continue;
980 } else if (!strcmp(child->schema->name, "name")) {
981 (*result)[u].submodules[v].name = strdup(lyd_get_value(child));
982 } else if (!strcmp(child->schema->name, "revision")) {
983 (*result)[u].submodules[v].revision = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200984 }
985 }
986 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200987 }
988 }
989 }
990
Radek Krejcifd5b6682017-06-13 15:52:53 +0200991cleanup:
Michal Vasko78939072022-12-12 07:43:18 +0100992 lyd_free_siblings(oper_data);
Michal Vasko77367452021-02-16 16:32:18 +0100993 ly_set_free(modules, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200994 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200995}
996
Michal Vaskoc088f982021-10-05 12:23:07 +0200997/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200998 * @brief Build server module info from received capabilities.
Michal Vaskoc088f982021-10-05 12:23:07 +0200999 *
1000 * @param[in] cpblts Server capabilities.
Michal Vasko5ca5d972022-09-14 13:51:31 +02001001 * @param[out] result Server modules.
Michal Vaskoc088f982021-10-05 12:23:07 +02001002 * @return 0 on success.
1003 * @return -1 on error.
1004 */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001005static int
Michal Vasko5ca5d972022-09-14 13:51:31 +02001006build_module_info_cpblts(char **cpblts, struct module_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001007{
Michal Vasko77367452021-02-16 16:32:18 +01001008 uint32_t u, v, feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001009 char *module_cpblt, *ptr, *ptr2;
1010
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001011 for (u = 0; cpblts[u]; ++u) {}
Radek Krejci235d8cb2018-08-17 14:04:32 +02001012 (*result) = calloc(u + 1, sizeof **result);
roman3a95bb22023-10-26 11:07:17 +02001013 NC_CHECK_ERRMEM_RET(!(*result), -1);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001014
1015 for (u = v = 0; cpblts[u]; ++u) {
1016 module_cpblt = strstr(cpblts[u], "module=");
1017 /* this capability requires a module */
1018 if (!module_cpblt) {
1019 continue;
1020 }
1021
1022 /* get module's name */
1023 ptr = (char *)module_cpblt + 7;
1024 ptr2 = strchr(ptr, '&');
1025 if (!ptr2) {
1026 ptr2 = ptr + strlen(ptr);
1027 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001028 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001029
1030 /* get module's revision */
1031 ptr = strstr(module_cpblt, "revision=");
1032 if (ptr) {
1033 ptr += 9;
1034 ptr2 = strchr(ptr, '&');
1035 if (!ptr2) {
1036 ptr2 = ptr + strlen(ptr);
1037 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001038 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001039 }
1040
1041 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001042 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001043
1044 /* get module's features */
1045 ptr = strstr(module_cpblt, "features=");
1046 if (ptr) {
1047 ptr += 9;
Michal Vasko77367452021-02-16 16:32:18 +01001048 feature_count = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001049 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
1050 if (*ptr == ',') {
Michal Vasko77367452021-02-16 16:32:18 +01001051 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
1052 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
1053 (*result)[v].features[feature_count + 1] = NULL;
1054 ++feature_count;
1055
Radek Krejci65ef6d52018-08-16 16:35:02 +02001056 ptr2 = ptr + 1;
1057 }
1058 }
1059 /* the last one */
Michal Vasko77367452021-02-16 16:32:18 +01001060 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
1061 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
1062 (*result)[v].features[feature_count + 1] = NULL;
1063 ++feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001064 }
1065 ++v;
1066 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001067
Michal Vasko303263d2021-10-05 12:18:21 +02001068 return 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001069}
1070
Michal Vaskoc088f982021-10-05 12:23:07 +02001071/**
Michal Vasko5ca5d972022-09-14 13:51:31 +02001072 * @brief Fill client context based on server modules info.
Michal Vaskoc088f982021-10-05 12:23:07 +02001073 *
1074 * @param[in] session NC session with the context to modify.
Michal Vasko5ca5d972022-09-14 13:51:31 +02001075 * @param[in] modules Server modules info.
1076 * @param[in] user_clb User callback for retrieving specific modules.
Michal Vaskoc088f982021-10-05 12:23:07 +02001077 * @param[in] user_data User data for @p user_clb.
1078 * @param[in] has_get_schema Whether server supports get-schema RPC.
1079 * @return 0 on success.
1080 * @return -1 on error.
1081 */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001082static int
Michal Vasko5ca5d972022-09-14 13:51:31 +02001083nc_ctx_fill(struct nc_session *session, struct module_info *modules, ly_module_imp_clb user_clb, void *user_data,
Michal Vasko77367452021-02-16 16:32:18 +01001084 int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001085{
Michal Vasko303263d2021-10-05 12:18:21 +02001086 int ret = -1;
Michal Vasko77367452021-02-16 16:32:18 +01001087 struct lys_module *mod;
1088 uint32_t u;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001089
1090 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +02001091 /* skip import-only modules */
1092 if (!modules[u].implemented) {
1093 continue;
1094 }
1095
Radek Krejci65ef6d52018-08-16 16:35:02 +02001096 /* we can continue even if it fails */
Michal Vasko5e32c402022-01-12 14:05:09 +01001097 nc_ctx_load_module(session, modules[u].name, modules[u].revision, (const char **)modules[u].features, modules,
1098 user_clb, user_data, has_get_schema, &mod);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001099
1100 if (!mod) {
1101 if (session->status != NC_STATUS_RUNNING) {
1102 /* something bad heppened, discard the session */
Michal Vasko05532772021-06-03 12:12:38 +02001103 ERR(session, "Invalid session, discarding.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001104 goto cleanup;
1105 }
1106
Michal Vasko5ca5d972022-09-14 13:51:31 +02001107 /* all loading ways failed, the module will be ignored in the received data */
1108 WRN(session, "Failed to load module \"%s@%s\".", modules[u].name, modules[u].revision ?
Michal Vasko05532772021-06-03 12:12:38 +02001109 modules[u].revision : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001110 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001111 }
Michal Vasko60f66602017-10-17 13:52:18 +02001112 }
1113
Michal Vasko77367452021-02-16 16:32:18 +01001114 /* success */
Michal Vasko303263d2021-10-05 12:18:21 +02001115 ret = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001116
1117cleanup:
Radek Krejcifd5b6682017-06-13 15:52:53 +02001118 return ret;
1119}
1120
Michal Vaskoc088f982021-10-05 12:23:07 +02001121/**
Michal Vasko5ca5d972022-09-14 13:51:31 +02001122 * @brief Fill client context with ietf-netconf module.
Michal Vaskoc088f982021-10-05 12:23:07 +02001123 *
1124 * @param[in] session NC session with the context to modify.
Michal Vasko5ca5d972022-09-14 13:51:31 +02001125 * @param[in] modules Server module info.
1126 * @param[in] user_clb User callback for retrieving specific modules.
Michal Vaskoc088f982021-10-05 12:23:07 +02001127 * @param[in] user_data User data for @p user_clb.
1128 * @param[in] has_get_schema Whether server supports get-schema RPC.
1129 * @return 0 on success.
1130 * @return -1 on error.
1131 */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001132static int
Michal Vasko5ca5d972022-09-14 13:51:31 +02001133nc_ctx_fill_ietf_netconf(struct nc_session *session, struct module_info *modules, ly_module_imp_clb user_clb,
Michal Vasko77367452021-02-16 16:32:18 +01001134 void *user_data, int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001135{
Michal Vasko77367452021-02-16 16:32:18 +01001136 uint32_t u;
Michal Vasko5e32c402022-01-12 14:05:09 +01001137 const char **features = NULL;
1138 struct ly_in *in;
Michal Vasko77367452021-02-16 16:32:18 +01001139 struct lys_module *ietfnc;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001140
Michal Vasko5e32c402022-01-12 14:05:09 +01001141 /* find supported features (capabilities) in ietf-netconf */
1142 for (u = 0; modules[u].name; ++u) {
1143 if (!strcmp(modules[u].name, "ietf-netconf")) {
1144 assert(modules[u].implemented);
1145 features = (const char **)modules[u].features;
1146 break;
1147 }
1148 }
1149 if (!modules[u].name) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001150 ERR(session, "Base NETCONF module not supported by the server.");
Michal Vasko5e32c402022-01-12 14:05:09 +01001151 return -1;
1152 }
1153
Michal Vasko77367452021-02-16 16:32:18 +01001154 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vasko5e32c402022-01-12 14:05:09 +01001155 if (ietfnc) {
1156 /* make sure to enable all the features if already loaded */
1157 lys_set_implemented(ietfnc, features);
1158 } else {
1159 /* load the module */
1160 nc_ctx_load_module(session, "ietf-netconf", NULL, features, modules, user_clb, user_data, has_get_schema, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +02001161 if (!ietfnc) {
Michal Vasko5e32c402022-01-12 14:05:09 +01001162 ly_in_new_memory(ietf_netconf_2013_09_29_yang, &in);
1163 lys_parse(session->ctx, in, LYS_IN_YANG, features, &ietfnc);
1164 ly_in_free(in, 0);
Michal Vasko8a4e1462020-05-07 11:32:31 +02001165 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001166 }
1167 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001168 ERR(session, "Loading base NETCONF module failed.");
Michal Vasko303263d2021-10-05 12:18:21 +02001169 return -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001170 }
1171
Radek Krejci65ef6d52018-08-16 16:35:02 +02001172 return 0;
1173}
1174
Michal Vasko78939072022-12-12 07:43:18 +01001175/**
1176 * @brief Set client session context to support schema-mount if possible.
1177 *
1178 * @param[in] session NC session with the context to modify.
1179 * @param[in] get_data_sup Whether get-data RPC is available or only get.
1180 * @param[in] xpath_sup Whether XPath filter is supported or only subtree filter.
1181 * @return 0 on success.
1182 * @return -1 on error.
1183 */
1184static int
1185nc_ctx_schema_mount(struct nc_session *session, int get_data_sup, int xpath_sup)
1186{
1187 int rc = 0;
1188 struct lyd_node *oper_data = NULL;
1189
1190 if (session->flags & NC_SESSION_SHAREDCTX) {
1191 /* context is already fully set up */
1192 goto cleanup;
1193 }
1194
1195 /* get yang-library and schema-mounts operational data */
1196 if (xpath_sup) {
1197 if ((rc = get_oper_data(session, get_data_sup, "/ietf-yang-library:* | /ietf-yang-schema-mount:*", &oper_data))) {
1198 goto cleanup;
1199 }
1200 } else {
1201 if ((rc = get_oper_data(session, get_data_sup,
1202 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>"
1203 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\"/>", &oper_data))) {
1204 goto cleanup;
1205 }
1206 }
1207
1208 if (!oper_data || lyd_find_path(oper_data, "/ietf-yang-schema-mount:schema-mounts", 0, NULL)) {
1209 /* no schema-mounts operational data */
1210 goto cleanup;
1211 }
1212
1213 /* validate the data for the parent reference prefixes to be resolved */
1214 if (lyd_validate_all(&oper_data, NULL, LYD_VALIDATE_PRESENT, NULL)) {
Michal Vasko58791da2024-02-26 13:52:59 +01001215 ERR(session, "Invalid operational data received from the server (%s).", ly_err_last(LYD_CTX(oper_data))->msg);
Michal Vasko78939072022-12-12 07:43:18 +01001216 rc = -1;
1217 goto cleanup;
1218 }
1219
1220 /* store the data in the session */
1221 session->opts.client.ext_data = oper_data;
1222 oper_data = NULL;
1223
1224cleanup:
1225 lyd_free_siblings(oper_data);
1226 return rc;
1227}
1228
Michal Vasko086311b2016-01-08 09:53:11 +01001229int
1230nc_ctx_check_and_fill(struct nc_session *session)
1231{
Michal Vaskod6c6f852022-12-14 15:37:21 +01001232 int i, get_schema_support = 0, yanglib_support = 0, xpath_support = 0, nmda_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +01001233 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001234 void *old_data = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001235 struct lys_module *mod = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +02001236 char *revision;
Michal Vasko5ca5d972022-09-14 13:51:31 +02001237 struct module_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001238
Michal Vasko2e6defd2016-10-07 15:48:15 +02001239 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +01001240
Michal Vaskoa272e092023-07-10 14:34:03 +02001241 if (client_opts.auto_context_fill_disabled) {
1242 VRB(session, "Context of the new session is left only with the default YANG modules.");
1243 return 0;
1244 }
1245
Radek Krejci65ef6d52018-08-16 16:35:02 +02001246 /* 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 +02001247 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001248
Radek Krejci65ef6d52018-08-16 16:35:02 +02001249 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
Michal Vasko5ca5d972022-09-14 13:51:31 +02001250 * modules only to those present on the server side */
Michal Vasko77367452021-02-16 16:32:18 +01001251 ly_ctx_set_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001252
1253 /* our callback is set later with appropriate data */
1254 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
1255
1256 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001257 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001258 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001259 get_schema_support = 1 + i;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001260 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001261 yanglib_support = 1 + i;
Michal Vasko78939072022-12-12 07:43:18 +01001262 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:xpath:1.0", 44)) {
1263 xpath_support = 1 + i;
Michal Vaskod6c6f852022-12-14 15:37:21 +01001264 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-nmda", 45)) {
1265 nmda_support = 1 + i;
Michal Vasko086311b2016-01-08 09:53:11 +01001266 }
1267 }
Michal Vaskod6c6f852022-12-14 15:37:21 +01001268 VRB(session, "Capability for <get-schema> support%s found.", get_schema_support ? "" : " not");
1269 VRB(session, "Capability for yang-library support%s found.", yanglib_support ? "" : " not");
1270 VRB(session, "Capability for XPath filter support%s found.", xpath_support ? "" : " not");
1271 VRB(session, "Capability for NMDA RPCs support%s found.", nmda_support ? "" : " not");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001272
Michal Vasko5ca5d972022-09-14 13:51:31 +02001273 /* get information about server's modules from capabilities list until we will have yang-library */
1274 if (build_module_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
1275 ERR(session, "Unable to get server module information from the <hello>'s capabilities.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001276 goto cleanup;
1277 }
1278
Michal Vasko086311b2016-01-08 09:53:11 +01001279 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Michal Vasko77367452021-02-16 16:32:18 +01001280 if (get_schema_support && lys_parse_mem(session->ctx, ietf_netconf_monitoring_2010_10_04_yang, LYS_IN_YANG, NULL)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001281 WRN(session, "Loading NETCONF monitoring module failed, cannot use <get-schema>.");
Michal Vasko8a4e1462020-05-07 11:32:31 +02001282 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001283 }
1284
1285 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001286 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001287 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001288 }
1289
Radek Krejci65ef6d52018-08-16 16:35:02 +02001290 /* get correct version of ietf-yang-library into context */
1291 if (yanglib_support) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001292 /* use get-schema to get server's ietf-yang-library */
Radek Krejcib1d250e2018-04-12 13:54:18 +02001293 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1294 if (!revision) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001295 WRN(session, "Loading NETCONF ietf-yang-library module failed, missing revision in NETCONF <hello> message.");
Michal Vasko05532772021-06-03 12:12:38 +02001296 WRN(session, "Unable to automatically use <get-schema>.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001297 yanglib_support = 0;
1298 } else {
1299 revision = strndup(&revision[9], 10);
Michal Vasko5e32c402022-01-12 14:05:09 +01001300 if (nc_ctx_load_module(session, "ietf-yang-library", revision, NULL, server_modules, old_clb, old_data,
Michal Vasko0d877f92020-04-02 13:52:43 +02001301 get_schema_support, &mod)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001302 WRN(session, "Loading NETCONF ietf-yang-library module failed, unable to use it to learn all "
Michal Vasko05532772021-06-03 12:12:38 +02001303 "the supported modules.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001304 yanglib_support = 0;
1305 }
Michal Vasko0d877f92020-04-02 13:52:43 +02001306 if (strcmp(revision, "2019-01-04") >= 0) {
1307 /* we also need ietf-datastores to be implemented */
Michal Vasko5e32c402022-01-12 14:05:09 +01001308 if (nc_ctx_load_module(session, "ietf-datastores", NULL, NULL, server_modules, old_clb, old_data,
Michal Vasko0d877f92020-04-02 13:52:43 +02001309 get_schema_support, &mod)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001310 WRN(session, "Loading NETCONF ietf-datastores module failed, unable to use yang-library "
Michal Vasko05532772021-06-03 12:12:38 +02001311 "to learn all the supported modules.");
Michal Vasko0d877f92020-04-02 13:52:43 +02001312 yanglib_support = 0;
1313 }
1314 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001315 free(revision);
1316 }
1317 }
1318
Michal Vaskod6c6f852022-12-14 15:37:21 +01001319 /* ietf-netconf-nmda is needed to issue get-data */
1320 if (nmda_support && nc_ctx_load_module(session, "ietf-netconf-nmda", NULL, NULL, server_modules, old_clb, old_data,
1321 get_schema_support, &mod)) {
1322 WRN(session, "Loading NMDA module failed, unable to use <get-data>.");
1323 nmda_support = 0;
1324 }
1325
Michal Vasko5ca5d972022-09-14 13:51:31 +02001326 /* prepare structured information about server's modules */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001327 if (yanglib_support) {
Michal Vaskod6c6f852022-12-14 15:37:21 +01001328 if (build_module_info_yl(session, nmda_support, xpath_support, &sm)) {
Radek Krejcif0633792018-08-20 15:12:09 +02001329 goto cleanup;
1330 } else if (!sm) {
Michal Vasko05532772021-06-03 12:12:38 +02001331 VRB(session, "Trying to use capabilities instead of ietf-yang-library data.");
Radek Krejcif0633792018-08-20 15:12:09 +02001332 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001333 /* prefer yang-library information, currently we have it from capabilities used for getting correct
Michal Vasko5ca5d972022-09-14 13:51:31 +02001334 * yang-library module */
1335 free_module_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001336 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001337 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001338 }
Michal Vaskoef578332016-01-25 13:20:09 +01001339
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001340 /* compile all modules at once to avoid invalid errors or warnings */
1341 ly_ctx_set_options(session->ctx, LY_CTX_EXPLICIT_COMPILE);
1342
1343 /* fill the context */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001344 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1345 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001346 }
1347
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001348 /* compile it */
1349 if (ly_ctx_compile(session->ctx)) {
1350 goto cleanup;
1351 }
1352
Michal Vasko59762c32024-04-12 12:13:12 +02001353 /* set support for schema-mount, if possible (requires ietf-yang-library support) */
1354 if (yanglib_support && nc_ctx_schema_mount(session, nmda_support, xpath_support)) {
Michal Vasko78939072022-12-12 07:43:18 +01001355 goto cleanup;
1356 }
1357
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001358 /* success */
Radek Krejcifd5b6682017-06-13 15:52:53 +02001359 ret = 0;
1360
Michal Vaskoeee99412016-11-21 10:19:43 +01001361 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001362 WRN(session, "Some modules failed to be loaded, any data from these modules (and any other unknown) will "
Michal Vasko05532772021-06-03 12:12:38 +02001363 "be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +01001364 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001365
1366cleanup:
Michal Vasko5ca5d972022-09-14 13:51:31 +02001367 free_module_info(server_modules);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001368
Radek Krejcifd5b6682017-06-13 15:52:53 +02001369 /* set user callback back */
1370 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vasko77367452021-02-16 16:32:18 +01001371 ly_ctx_unset_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001372 ly_ctx_unset_options(session->ctx, LY_CTX_EXPLICIT_COMPILE);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001373
Michal Vaskoef578332016-01-25 13:20:09 +01001374 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001375}
1376
1377API struct nc_session *
1378nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1379{
Michal Vaskod083db62016-01-19 10:31:29 +01001380 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001381
Michal Vasko45e53ae2016-04-07 11:46:03 +02001382 if (fdin < 0) {
roman40672412023-05-04 11:10:22 +02001383 ERRARG(NULL, "fdin");
Michal Vasko45e53ae2016-04-07 11:46:03 +02001384 return NULL;
1385 } else if (fdout < 0) {
roman40672412023-05-04 11:10:22 +02001386 ERRARG(NULL, "fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001387 return NULL;
1388 }
1389
1390 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001391 session = nc_new_session(NC_CLIENT, 0);
roman3a95bb22023-10-26 11:07:17 +02001392 NC_CHECK_ERRMEM_RET(!session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001393 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001394
1395 /* transport specific data */
1396 session->ti_type = NC_TI_FD;
1397 session->ti.fd.in = fdin;
1398 session->ti.fd.out = fdout;
1399
Michal Vasko78939072022-12-12 07:43:18 +01001400 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Robin Jarry4e38e292019-10-15 17:14:14 +02001401 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001402 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001403 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001404
1405 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001406 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001407 goto fail;
1408 }
1409 session->status = NC_STATUS_RUNNING;
1410
Michal Vaskoef578332016-01-25 13:20:09 +01001411 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001412 goto fail;
1413 }
1414
1415 return session;
1416
1417fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001418 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001419 return NULL;
1420}
1421
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001422API struct nc_session *
1423nc_connect_unix(const char *address, struct ly_ctx *ctx)
1424{
1425 struct nc_session *session = NULL;
1426 struct sockaddr_un sun;
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001427 struct passwd *pw, pw_buf;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001428 char *username;
1429 int sock = -1;
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001430 char *buf = NULL;
1431 size_t buf_size = 0;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001432
roman40672412023-05-04 11:10:22 +02001433 NC_CHECK_ARG_RET(NULL, address, NULL);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001434
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001435 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1436 if (sock < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001437 ERR(NULL, "Failed to create socket (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001438 goto fail;
1439 }
1440
1441 memset(&sun, 0, sizeof(sun));
1442 sun.sun_family = AF_UNIX;
1443 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1444
1445 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001446 ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001447 goto fail;
1448 }
1449
1450 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001451 ERR(NULL, "fcntl failed (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001452 goto fail;
1453 }
1454
1455 /* prepare session structure */
1456 session = nc_new_session(NC_CLIENT, 0);
roman124a4362023-10-26 15:36:22 +02001457 NC_CHECK_ERRMEM_GOTO(!session, , fail);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001458 session->status = NC_STATUS_STARTING;
1459
1460 /* transport specific data */
1461 session->ti_type = NC_TI_UNIX;
1462 session->ti.unixsock.sock = sock;
1463 sock = -1; /* do not close sock in fail label anymore */
1464
Michal Vasko78939072022-12-12 07:43:18 +01001465 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Robin Jarry4e38e292019-10-15 17:14:14 +02001466 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001467 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001468 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001469
Michal Vasko93224072021-11-09 12:14:28 +01001470 session->path = strdup(address);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001471
romanf6e32012023-04-24 15:51:26 +02001472 pw = nc_getpw(geteuid(), NULL, &pw_buf, &buf, &buf_size);
Michal Vaskoccd2dd02021-10-11 09:13:01 +02001473 if (!pw) {
1474 ERR(NULL, "Failed to find username for UID %u.", (unsigned int)geteuid());
1475 goto fail;
1476 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001477 username = strdup(pw->pw_name);
Michal Vaskoccd2dd02021-10-11 09:13:01 +02001478 free(buf);
roman124a4362023-10-26 15:36:22 +02001479 NC_CHECK_ERRMEM_GOTO(!username, , fail);
Michal Vasko93224072021-11-09 12:14:28 +01001480 session->username = username;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001481
1482 /* NETCONF handshake */
1483 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1484 goto fail;
1485 }
1486 session->status = NC_STATUS_RUNNING;
1487
1488 if (nc_ctx_check_and_fill(session) == -1) {
1489 goto fail;
1490 }
1491
1492 return session;
1493
1494fail:
1495 nc_session_free(session, NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001496 if (sock >= 0) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001497 close(sock);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001498 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001499 return NULL;
1500}
1501
Michal Vasko056f53c2022-10-21 13:38:15 +02001502/**
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001503 * @brief Convert socket IP address to string.
1504 *
1505 * @param[in] saddr Sockaddr to convert.
1506 * @param[out] str_ip String IP address.
Michal Vaskoc07d9762023-03-27 10:32:18 +02001507 * @param[out] port Optional port.
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001508 * @return 0 on success.
1509 * @return -1 on error.
1510 */
1511static int
Michal Vaskoc07d9762023-03-27 10:32:18 +02001512nc_saddr2str(const struct sockaddr *saddr, char **str_ip, uint16_t *port)
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001513{
1514 void *addr;
1515 socklen_t str_len;
1516
1517 assert((saddr->sa_family == AF_INET) || (saddr->sa_family == AF_INET6));
1518
1519 str_len = (saddr->sa_family == AF_INET) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
1520 *str_ip = malloc(str_len);
roman3a95bb22023-10-26 11:07:17 +02001521 NC_CHECK_ERRMEM_RET(!(*str_ip), -1);
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001522
1523 if (saddr->sa_family == AF_INET) {
Michal Vaskoc0f85ca2023-03-27 10:19:00 +02001524 addr = &((struct sockaddr_in *)saddr)->sin_addr;
Michal Vaskoc07d9762023-03-27 10:32:18 +02001525 if (port) {
1526 *port = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1527 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001528 } else {
Michal Vaskoc0f85ca2023-03-27 10:19:00 +02001529 addr = &((struct sockaddr_in6 *)saddr)->sin6_addr;
Michal Vaskoc07d9762023-03-27 10:32:18 +02001530 if (port) {
1531 *port = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1532 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001533 }
1534 if (!inet_ntop(saddr->sa_family, addr, *str_ip, str_len)) {
1535 ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno));
1536 free(*str_ip);
1537 return -1;
1538 }
1539
1540 return 0;
1541}
1542
1543/**
Michal Vasko056f53c2022-10-21 13:38:15 +02001544 * @brief Try to connect a socket, optionally a pending one from a previous attempt.
1545 *
1546 * @param[in] timeout_ms Timeout in ms to wait for the connection to be fully established, -1 to block.
1547 * @param[in,out] sock_pending Optional previously created socked that was not fully connected yet. If provided and
1548 * connected, is set to -1.
1549 * @param[in] res Addrinfo resource to use when creating a new socket.
1550 * @param[in] ka Keepalives to set.
1551 * @return Connected socket or -1 on error.
Frank Rimpler9f838b02018-07-25 06:44:03 +00001552 */
1553static int
Michal Vaskoc4550382024-05-03 12:02:38 +02001554sock_connect(int timeout_ms, int *sock_pending, struct addrinfo *res, const struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001555{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001556 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001557 int sock = -1;
Michal Vaskob9336672023-10-11 09:27:30 +02001558 struct pollfd fds = {0};
Frank Rimpler9f838b02018-07-25 06:44:03 +00001559 socklen_t len = sizeof(int);
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001560 uint16_t port;
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001561 char *str;
Michal Vasko086311b2016-01-08 09:53:11 +01001562
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001563 if (sock_pending && (*sock_pending != -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001564 VRB(NULL, "Trying to connect the pending socket %d.", *sock_pending);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001565 sock = *sock_pending;
1566 } else {
1567 assert(res);
Michal Vaskoc07d9762023-03-27 10:32:18 +02001568 if (nc_saddr2str(res->ai_addr, &str, &port)) {
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001569 return -1;
1570 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001571 VRB(NULL, "Trying to connect via %s to %s:%u.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", str, port);
1572 free(str);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001573
1574 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001575 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1576 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001577 ERR(NULL, "Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001578 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001579 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001580 /* make the socket non-blocking */
1581 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001582 ERR(NULL, "fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001583 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001584 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001585 /* non-blocking connect! */
1586 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1587 if (errno != EINPROGRESS) {
1588 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001589 ERR(NULL, "connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001590 goto cleanup;
1591 }
1592 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001593 }
Michal Vasko7d965dc2018-03-12 14:39:23 +01001594
Michal Vaskob9336672023-10-11 09:27:30 +02001595 fds.fd = sock;
1596 fds.events = POLLOUT;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001597
Michal Vaskob9336672023-10-11 09:27:30 +02001598 /* wait until we can write data to the socket */
1599 ret = poll(&fds, 1, timeout_ms);
1600 if (ret == -1) {
1601 /* error */
1602 ERR(NULL, "poll() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001603 goto cleanup;
Michal Vaskob9336672023-10-11 09:27:30 +02001604 } else if (ret == 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001605 /* there was a timeout */
Michal Vasko056f53c2022-10-21 13:38:15 +02001606 VRB(NULL, "Timed out after %d ms (%s).", timeout_ms, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001607 if (sock_pending) {
1608 /* no sock-close, we'll try it again */
1609 *sock_pending = sock;
1610 } else {
1611 close(sock);
1612 }
1613 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001614 }
Radek Krejci782041a2018-08-20 10:09:45 +02001615
1616 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001617 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001618 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001619 ERR(NULL, "getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001620 goto cleanup;
1621 }
Michal Vaskoe27ab4e2020-07-27 11:10:58 +02001622 if (error) {
Radek Krejci782041a2018-08-20 10:09:45 +02001623 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001624 VRB(NULL, "getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001625 errno = error;
1626 goto cleanup;
1627 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001628
roman56acd552024-04-18 16:00:37 +02001629 /* configure keepalives */
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +02001630 if (nc_sock_configure_ka(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001631 goto cleanup;
1632 }
1633
Michal Vasko056f53c2022-10-21 13:38:15 +02001634 /* connected */
1635 if (sock_pending) {
1636 *sock_pending = -1;
1637 }
Michal Vasko086311b2016-01-08 09:53:11 +01001638 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001639
Frank Rimpler9f838b02018-07-25 06:44:03 +00001640cleanup:
1641 if (sock_pending) {
1642 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001643 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001644 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001645 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001646}
1647
Frank Rimpler9f838b02018-07-25 06:44:03 +00001648int
Michal Vasko056f53c2022-10-21 13:38:15 +02001649nc_sock_connect(const char *host, uint16_t port, int timeout_ms, struct nc_keepalives *ka, int *sock_pending, char **ip_host)
Frank Rimpler9f838b02018-07-25 06:44:03 +00001650{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001651 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001652 int sock = sock_pending ? *sock_pending : -1;
Michal Vasko0be85692021-03-02 08:04:57 +01001653 struct addrinfo hints, *res_list = NULL, *res;
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001654 char port_s[6]; /* length of string representation of short int */
1655 struct sockaddr_storage saddr;
1656 socklen_t addr_len = sizeof saddr;
1657
1658 *ip_host = NULL;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001659
Michal Vasko056f53c2022-10-21 13:38:15 +02001660 DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout_ms, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001661
1662 /* no pending socket */
1663 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001664 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001665 snprintf(port_s, 6, "%u", port);
1666 memset(&hints, 0, sizeof hints);
1667 hints.ai_family = AF_UNSPEC;
1668 hints.ai_socktype = SOCK_STREAM;
1669 hints.ai_protocol = IPPROTO_TCP;
1670 i = getaddrinfo(host, port_s, &hints, &res_list);
1671 if (i != 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001672 ERR(NULL, "Unable to translate the host address (%s).", gai_strerror(i));
Michal Vaskocb846632019-12-13 15:12:45 +01001673 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001674 }
1675
1676 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko056f53c2022-10-21 13:38:15 +02001677 sock = sock_connect(timeout_ms, sock_pending, res, ka);
Michal Vasko2af7c382020-07-27 14:21:35 +02001678 if (sock == -1) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001679 if (!sock_pending || (*sock_pending == -1)) {
Michal Vasko2af7c382020-07-27 14:21:35 +02001680 /* try the next resource */
1681 continue;
1682 } else {
1683 /* timeout, keep pending socket */
Michal Vasko0be85692021-03-02 08:04:57 +01001684 break;
Michal Vasko2af7c382020-07-27 14:21:35 +02001685 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001686 }
Michal Vasko05532772021-06-03 12:12:38 +02001687 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 +01001688
1689 opt = 1;
1690 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001691 ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001692 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001693 }
1694
Michal Vaskoc07d9762023-03-27 10:32:18 +02001695 if (nc_saddr2str(res->ai_addr, ip_host, NULL)) {
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001696 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001697 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001698 break;
1699 }
1700 freeaddrinfo(res_list);
1701
1702 } else {
1703 /* try to get a connection with the pending socket */
1704 assert(sock_pending);
Michal Vasko056f53c2022-10-21 13:38:15 +02001705 sock = sock_connect(timeout_ms, sock_pending, NULL, ka);
Tie.Liao9bca73d2023-03-23 17:25:00 +01001706
1707 if (sock > 0) {
Barbaros Tokaoglu96da5912023-06-19 18:57:05 +03001708 if (getpeername(sock, (struct sockaddr *)&saddr, &addr_len)) {
1709 ERR(NULL, "getpeername failed (%s).", strerror(errno));
Tie.Liao9bca73d2023-03-23 17:25:00 +01001710 goto error;
1711 }
1712
Michal Vaskoc07d9762023-03-27 10:32:18 +02001713 if (nc_saddr2str((struct sockaddr *)&saddr, ip_host, NULL)) {
Tie.Liao9bca73d2023-03-23 17:25:00 +01001714 goto error;
1715 }
Tie.Liao9bca73d2023-03-23 17:25:00 +01001716 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001717 }
1718
1719 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001720
1721error:
Michal Vasko0be85692021-03-02 08:04:57 +01001722 if (res_list) {
1723 freeaddrinfo(res_list);
1724 }
Michal Vaskocb846632019-12-13 15:12:45 +01001725 if (sock != -1) {
1726 close(sock);
1727 }
1728 if (sock_pending) {
1729 *sock_pending = -1;
1730 }
1731 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001732}
1733
roman2eab4742023-06-06 10:00:26 +02001734#ifdef NC_ENABLED_SSH_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001735
Michal Vasko3031aae2016-01-27 16:07:18 +01001736int
Michal Vasko9d4cca52022-09-07 11:19:57 +02001737nc_client_ch_add_bind_listen(const char *address, uint16_t port, const char *hostname, NC_TRANSPORT_IMPL ti)
Michal Vasko3031aae2016-01-27 16:07:18 +01001738{
1739 int sock;
1740
roman40672412023-05-04 11:10:22 +02001741 NC_CHECK_ARG_RET(NULL, address, port, -1);
Michal Vasko3031aae2016-01-27 16:07:18 +01001742
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +02001743 sock = nc_sock_listen_inet(address, port);
Michal Vasko3031aae2016-01-27 16:07:18 +01001744 if (sock == -1) {
1745 return -1;
1746 }
1747
1748 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001749 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1750 if (!client_opts.ch_binds) {
1751 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001752 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001753 return -1;
1754 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001755
Michal Vasko9d4cca52022-09-07 11:19:57 +02001756 client_opts.ch_binds_aux = nc_realloc(client_opts.ch_binds_aux, client_opts.ch_bind_count * sizeof *client_opts.ch_binds_aux);
1757 if (!client_opts.ch_binds_aux) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001758 ERRMEM;
1759 close(sock);
1760 return -1;
1761 }
Michal Vasko9d4cca52022-09-07 11:19:57 +02001762 client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].ti = ti;
1763 client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].hostname = hostname ? strdup(hostname) : NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001764
Michal Vasko3031aae2016-01-27 16:07:18 +01001765 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
1766 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1767 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001768 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001769
1770 return 0;
1771}
1772
1773int
1774nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1775{
1776 uint32_t i;
1777 int ret = -1;
1778
1779 if (!address && !port && !ti) {
1780 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1781 close(client_opts.ch_binds[i].sock);
Michal Vasko9d4cca52022-09-07 11:19:57 +02001782 free(client_opts.ch_binds[i].address);
1783
1784 free(client_opts.ch_binds_aux[i].hostname);
Michal Vasko3031aae2016-01-27 16:07:18 +01001785
1786 ret = 0;
1787 }
Michal Vasko9d4cca52022-09-07 11:19:57 +02001788 client_opts.ch_bind_count = 0;
1789
Michal Vasko3031aae2016-01-27 16:07:18 +01001790 free(client_opts.ch_binds);
1791 client_opts.ch_binds = NULL;
Michal Vasko9d4cca52022-09-07 11:19:57 +02001792
1793 free(client_opts.ch_binds_aux);
1794 client_opts.ch_binds_aux = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001795 } else {
1796 for (i = 0; i < client_opts.ch_bind_count; ++i) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001797 if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) &&
1798 (!port || (client_opts.ch_binds[i].port == port)) &&
Michal Vasko9d4cca52022-09-07 11:19:57 +02001799 (!ti || (client_opts.ch_binds_aux[i].ti == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001800 close(client_opts.ch_binds[i].sock);
Michal Vasko9d4cca52022-09-07 11:19:57 +02001801 free(client_opts.ch_binds[i].address);
Michal Vasko3031aae2016-01-27 16:07:18 +01001802
1803 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001804 if (!client_opts.ch_bind_count) {
1805 free(client_opts.ch_binds);
1806 client_opts.ch_binds = NULL;
Michal Vasko9d4cca52022-09-07 11:19:57 +02001807
1808 free(client_opts.ch_binds_aux);
1809 client_opts.ch_binds_aux = NULL;
Michal Vasko66c762a2016-10-13 10:34:14 +02001810 } else if (i < client_opts.ch_bind_count) {
Michal Vasko9d4cca52022-09-07 11:19:57 +02001811 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count],
1812 sizeof *client_opts.ch_binds);
1813
1814 memcpy(&client_opts.ch_binds_aux[i], &client_opts.ch_binds_aux[client_opts.ch_bind_count],
1815 sizeof *client_opts.ch_binds_aux);
Michal Vasko66c762a2016-10-13 10:34:14 +02001816 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001817
1818 ret = 0;
1819 }
1820 }
1821 }
1822
1823 return ret;
1824}
1825
1826API int
1827nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1828{
1829 int sock;
1830 char *host = NULL;
1831 uint16_t port, idx;
1832
roman40672412023-05-04 11:10:22 +02001833 NC_CHECK_ARG_RET(NULL, session, -1);
1834
Michal Vasko45e53ae2016-04-07 11:46:03 +02001835 if (!client_opts.ch_binds) {
romand82caf12024-03-05 14:21:39 +01001836 ERR(NULL, "Call-Home binds not set.");
Michal Vasko45e53ae2016-04-07 11:46:03 +02001837 return -1;
Michal Vasko3031aae2016-01-27 16:07:18 +01001838 }
1839
Michal Vasko5a2c5b92024-02-02 09:11:41 +01001840 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, &client_opts.ch_bind_lock, timeout,
1841 &host, &port, &idx);
Michal Vasko50456e82016-02-02 12:16:08 +01001842 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001843 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001844 return sock;
1845 }
1846
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +02001847 /* configure keepalives */
1848 if (nc_sock_configure_ka(sock, &client_opts.ka)) {
1849 free(host);
1850 close(sock);
1851 return -1;
1852 }
1853
roman506354a2024-04-11 09:37:22 +02001854 if (client_opts.ch_binds_aux[idx].ti == NC_TI_SSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001855 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
roman506354a2024-04-11 09:37:22 +02001856 } else if (client_opts.ch_binds_aux[idx].ti == NC_TI_TLS) {
Michal Vasko9d4cca52022-09-07 11:19:57 +02001857 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT,
1858 client_opts.ch_binds_aux[idx].hostname);
roman2eab4742023-06-06 10:00:26 +02001859 } else {
Michal Vaskofee717c2016-02-01 13:25:43 +01001860 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001861 *session = NULL;
1862 }
1863
1864 free(host);
1865
1866 if (!(*session)) {
1867 return -1;
1868 }
1869
1870 return 1;
1871}
1872
roman2eab4742023-06-06 10:00:26 +02001873#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001874
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001875API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001876nc_session_get_cpblts(const struct nc_session *session)
1877{
roman40672412023-05-04 11:10:22 +02001878 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vaskobdfb5242016-05-24 09:11:01 +02001879
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001880 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001881}
1882
1883API const char *
1884nc_session_cpblt(const struct nc_session *session, const char *capab)
1885{
1886 int i, len;
1887
roman40672412023-05-04 11:10:22 +02001888 NC_CHECK_ARG_RET(session, session, capab, NULL);
Michal Vaskobdfb5242016-05-24 09:11:01 +02001889
1890 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001891 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1892 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1893 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001894 }
1895 }
1896
1897 return NULL;
1898}
1899
Michal Vasko9cd26a82016-05-31 08:58:48 +02001900API int
1901nc_session_ntf_thread_running(const struct nc_session *session)
1902{
roman40672412023-05-04 11:10:22 +02001903 NC_CHECK_ARG_RET(session, session, 0);
1904
1905 if (session->side != NC_CLIENT) {
1906 ERRARG(NULL, "session");
Michal Vasko9cd26a82016-05-31 08:58:48 +02001907 return 0;
1908 }
1909
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02001910 return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running);
Michal Vasko9cd26a82016-05-31 08:58:48 +02001911}
1912
roman9075eab2023-09-14 10:07:26 +02001913API int
1914nc_client_init(void)
1915{
1916 int r;
1917
1918 if ((r = pthread_mutex_init(&client_opts.ch_bind_lock, NULL))) {
1919 ERR(NULL, "%s: failed to init bind lock(%s).", __func__, strerror(r));
1920 return -1;
1921 }
1922
Michal Vasko5788c802024-05-03 16:14:40 +02001923#ifdef NC_ENABLED_SSH_TLS
1924 if (ssh_init()) {
1925 ERR(NULL, "%s: failed to init libssh.", __func__);
1926 return -1;
1927 }
1928#endif
1929
roman9075eab2023-09-14 10:07:26 +02001930 return 0;
1931}
1932
Michal Vaskob7558c52016-02-26 15:04:19 +01001933API void
1934nc_client_destroy(void)
1935{
roman9075eab2023-09-14 10:07:26 +02001936 pthread_mutex_destroy(&client_opts.ch_bind_lock);
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001937 nc_client_set_schema_searchpath(NULL);
roman2eab4742023-06-06 10:00:26 +02001938#ifdef NC_ENABLED_SSH_TLS
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001939 nc_client_ch_del_bind(NULL, 0, 0);
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001940 nc_client_ssh_destroy_opts();
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001941 nc_client_tls_destroy_opts();
Michal Vasko5788c802024-05-03 16:14:40 +02001942 ssh_finalize();
roman2eab4742023-06-06 10:00:26 +02001943#endif /* NC_ENABLED_SSH_TLS */
Michal Vaskob7558c52016-02-26 15:04:19 +01001944}
1945
Michal Vasko77367452021-02-16 16:32:18 +01001946static NC_MSG_TYPE
1947recv_reply_check_msgid(struct nc_session *session, const struct lyd_node *envp, uint64_t msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001948{
Michal Vasko77367452021-02-16 16:32:18 +01001949 char *ptr;
1950 struct lyd_attr *attr;
1951 uint64_t cur_msgid;
1952
1953 assert(envp && !envp->schema);
1954
1955 /* find the message-id attribute */
1956 LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) {
1957 if (!strcmp(attr->name.name, "message-id")) {
1958 break;
1959 }
1960 }
1961
1962 if (!attr) {
Michal Vasko05532772021-06-03 12:12:38 +02001963 ERR(session, "Received a <rpc-reply> without a message-id.");
Michal Vasko77367452021-02-16 16:32:18 +01001964 return NC_MSG_REPLY_ERR_MSGID;
1965 }
1966
1967 cur_msgid = strtoul(attr->value, &ptr, 10);
1968 if (cur_msgid != msgid) {
Michal Vasko05532772021-06-03 12:12:38 +02001969 ERR(session, "Received a <rpc-reply> with an unexpected message-id %" PRIu64 " (expected %" PRIu64 ").",
1970 cur_msgid, msgid);
Michal Vasko77367452021-02-16 16:32:18 +01001971 return NC_MSG_REPLY_ERR_MSGID;
1972 }
1973
1974 return NC_MSG_REPLY;
1975}
1976
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001977/**
1978 * @brief Used to roughly estimate the type of the message, does not actually parse or verify it.
1979 *
1980 * @param[in] session NETCONF session used to send error messages.
1981 * @param[in] msg Message to check for type.
1982 * @return NC_MSG_REPLY If format roughly matches a rpc-reply;
1983 * @return NC_MSG_NOTIF If format roughly matches a notification;
1984 * @return NC_MSG_ERROR If format is malformed or unrecognized.
1985 */
1986static NC_MSG_TYPE
1987get_msg_type(struct nc_session *session, struct ly_in *msg)
1988{
1989 const char *str, *end;
1990
1991 str = ly_in_memory(msg, NULL);
1992
1993 while (*str) {
1994 /* Skip whitespaces */
1995 while (isspace(*str)) {
1996 str++;
1997 }
1998
1999 if (*str == '<') {
2000 str++;
2001 if (!strncmp(str, "!--", 3)) {
2002 /* Skip comments */
2003 end = "-->";
2004 str = strstr(str, end);
2005 } else if (!strncmp(str, "?xml", 4)) {
2006 /* Skip xml declaration */
2007 end = "?>";
2008 str = strstr(str, end);
2009 } else if (!strncmp(str, "rpc-reply", 9)) {
2010 return NC_MSG_REPLY;
2011 } else if (!strncmp(str, "notification", 12)) {
2012 return NC_MSG_NOTIF;
2013 } else {
2014 ERR(session, "Unknown xml element '%.10s'.", str);
2015 return NC_MSG_ERROR;
2016 }
2017 if (!str) {
2018 /* No matching ending tag found */
2019 ERR(session, "No matching ending tag '%s' found in xml message.", end);
2020 return NC_MSG_ERROR;
2021 }
2022 str += strlen(end);
2023 } else {
2024 /* Not a valid xml */
2025 ERR(session, "Unexpected character '%c' in xml message.", *str);
2026 return NC_MSG_ERROR;
2027 }
2028 }
2029
2030 /* Unexpected end of message */
2031 ERR(session, "Unexpected end of xml message.");
2032 return NC_MSG_ERROR;
2033}
2034
2035/**
2036 * @brief Function to receive either replies or notifications.
2037 *
2038 * @param[in] session NETCONF session from which this function receives messages.
2039 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite.
2040 * @param[in] expected Type of the message the caller desired.
2041 * @param[out] message If receiving a message succeeded this is the message, NULL otherwise.
2042 * @return NC_MSG_REPLY If a rpc-reply was received;
2043 * @return NC_MSG_NOTIF If a notification was received;
Michal Vaskofdba4a32022-01-05 12:13:53 +01002044 * @return NC_MSG_ERROR If any error occurred;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002045 * @return NC_MSG_WOULDBLOCK If the timeout was reached.
2046 */
2047static NC_MSG_TYPE
2048recv_msg(struct nc_session *session, int timeout, NC_MSG_TYPE expected, struct ly_in **message)
2049{
2050 struct nc_msg_cont **cont_ptr;
2051 struct ly_in *msg = NULL;
2052 struct nc_msg_cont *cont, *prev;
2053 NC_MSG_TYPE ret = NC_MSG_ERROR;
2054 int r;
2055
2056 *message = NULL;
2057
2058 /* MSGS LOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02002059 r = nc_session_client_msgs_lock(session, &timeout, __func__);
2060 if (!r) {
2061 ret = NC_MSG_WOULDBLOCK;
2062 goto cleanup;
2063 } else if (r == -1) {
2064 ret = NC_MSG_ERROR;
2065 goto cleanup;
2066 }
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002067
2068 /* Find the expected message in the buffer */
2069 prev = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02002070 for (cont = session->opts.client.msgs; cont && (cont->type != expected); cont = cont->next) {
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002071 prev = cont;
2072 }
2073
2074 if (cont) {
2075 /* Remove found message from buffer */
2076 if (prev) {
2077 prev->next = cont->next;
2078 } else {
2079 session->opts.client.msgs = cont->next;
2080 }
2081
2082 /* Use the buffer message */
2083 ret = cont->type;
2084 msg = cont->msg;
2085 free(cont);
Michal Vasko01130bd2021-08-26 11:47:38 +02002086 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002087 }
2088
2089 /* Read a message from the wire */
2090 r = nc_read_msg_poll_io(session, timeout, &msg);
2091 if (!r) {
2092 ret = NC_MSG_WOULDBLOCK;
Michal Vasko01130bd2021-08-26 11:47:38 +02002093 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002094 } else if (r == -1) {
2095 ret = NC_MSG_ERROR;
Michal Vasko01130bd2021-08-26 11:47:38 +02002096 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002097 }
2098
2099 /* Basic check to determine message type */
2100 ret = get_msg_type(session, msg);
2101 if (ret == NC_MSG_ERROR) {
Michal Vasko01130bd2021-08-26 11:47:38 +02002102 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002103 }
2104
2105 /* If received a message of different type store it in the buffer */
2106 if (ret != expected) {
2107 cont_ptr = &session->opts.client.msgs;
2108 while (*cont_ptr) {
2109 cont_ptr = &((*cont_ptr)->next);
2110 }
2111 *cont_ptr = malloc(sizeof **cont_ptr);
roman3a95bb22023-10-26 11:07:17 +02002112 NC_CHECK_ERRMEM_GOTO(!*cont_ptr, ret = NC_MSG_ERROR, cleanup_unlock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002113 (*cont_ptr)->msg = msg;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002114 msg = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02002115 (*cont_ptr)->type = ret;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002116 (*cont_ptr)->next = NULL;
2117 }
2118
Michal Vasko01130bd2021-08-26 11:47:38 +02002119cleanup_unlock:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002120 /* MSGS UNLOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02002121 nc_session_client_msgs_unlock(session, __func__);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002122
Michal Vasko01130bd2021-08-26 11:47:38 +02002123cleanup:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002124 if (ret == expected) {
2125 *message = msg;
2126 } else {
2127 ly_in_free(msg, 1);
2128 }
2129 return ret;
2130}
2131
Michal Vasko77367452021-02-16 16:32:18 +01002132static NC_MSG_TYPE
2133recv_reply(struct nc_session *session, int timeout, struct lyd_node *op, uint64_t msgid, struct lyd_node **envp)
2134{
Michal Vasko77367452021-02-16 16:32:18 +01002135 LY_ERR lyrc;
2136 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002137 NC_MSG_TYPE ret = NC_MSG_ERROR;
Michal Vaskob25c56f2024-03-27 15:45:56 +01002138 uint32_t temp_lo = LY_LOSTORE, *prev_lo;
Michal Vasko77367452021-02-16 16:32:18 +01002139
2140 assert(op && (op->schema->nodetype & (LYS_RPC | LYS_ACTION)));
2141
2142 *envp = NULL;
2143
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002144 /* Receive messages until a rpc-reply is found or a timeout or error reached */
2145 ret = recv_msg(session, timeout, NC_MSG_REPLY, &msg);
2146 if (ret != NC_MSG_REPLY) {
Michal Vasko77367452021-02-16 16:32:18 +01002147 goto cleanup;
2148 }
2149
2150 /* parse */
Michal Vaskob25c56f2024-03-27 15:45:56 +01002151 prev_lo = ly_temp_log_options(&temp_lo);
Michal Vasko77367452021-02-16 16:32:18 +01002152 lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL);
Michal Vaskob25c56f2024-03-27 15:45:56 +01002153 ly_temp_log_options(prev_lo);
2154
2155 if (*envp) {
2156 /* if the envelopes were parsed, check the message-id, even on error */
Michal Vasko77367452021-02-16 16:32:18 +01002157 ret = recv_reply_check_msgid(session, *envp, msgid);
2158 goto cleanup;
Michal Vaskob25c56f2024-03-27 15:45:56 +01002159 }
2160
2161 if (lyrc) {
2162 /* parsing error */
Michal Vasko58791da2024-02-26 13:52:59 +01002163 ERR(session, "Received an invalid message (%s).", ly_err_last(LYD_CTX(op))->msg);
Michal Vasko9a108052022-04-01 12:06:54 +02002164 lyd_free_tree(*envp);
2165 *envp = NULL;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002166 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002167 goto cleanup;
2168 }
2169
Michal Vasko77367452021-02-16 16:32:18 +01002170cleanup:
2171 ly_in_free(msg, 1);
2172 return ret;
2173}
2174
2175static int
2176recv_reply_dup_rpc(struct nc_session *session, struct nc_rpc *rpc, struct lyd_node **op)
2177{
Michal Vasko143aa142021-10-01 15:31:48 +02002178 LY_ERR lyrc = LY_SUCCESS;
Michal Vasko77367452021-02-16 16:32:18 +01002179 struct nc_rpc_act_generic *rpc_gen;
2180 struct ly_in *in;
2181 struct lyd_node *tree, *op2;
2182 const struct lys_module *mod;
Michal Vasko305faca2021-03-25 09:16:02 +01002183 const char *module_name = NULL, *rpc_name = NULL, *module_check = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002184
2185 switch (rpc->type) {
2186 case NC_RPC_ACT_GENERIC:
2187 rpc_gen = (struct nc_rpc_act_generic *)rpc;
2188 if (rpc_gen->has_data) {
2189 tree = rpc_gen->content.data;
2190
2191 /* find the operation node */
2192 lyrc = LY_EINVAL;
2193 LYD_TREE_DFS_BEGIN(tree, op2) {
2194 if (op2->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
2195 lyrc = lyd_dup_single(op2, NULL, 0, op);
2196 break;
2197 }
2198 LYD_TREE_DFS_END(tree, op2);
2199 }
2200 } else {
2201 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2202 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op2);
2203 ly_in_free(in, 0);
2204 if (lyrc) {
Michal Vasko9a108052022-04-01 12:06:54 +02002205 lyd_free_tree(tree);
Michal Vasko77367452021-02-16 16:32:18 +01002206 return -1;
2207 }
2208
2209 /* we want just the operation node */
2210 lyrc = lyd_dup_single(op2, NULL, 0, op);
2211
2212 lyd_free_tree(tree);
2213 }
2214 break;
2215 case NC_RPC_GETCONFIG:
2216 module_name = "ietf-netconf";
2217 rpc_name = "get-config";
2218 break;
2219 case NC_RPC_EDIT:
2220 module_name = "ietf-netconf";
2221 rpc_name = "edit-config";
2222 break;
2223 case NC_RPC_COPY:
2224 module_name = "ietf-netconf";
2225 rpc_name = "copy-config";
2226 break;
2227 case NC_RPC_DELETE:
2228 module_name = "ietf-netconf";
2229 rpc_name = "delete-config";
2230 break;
2231 case NC_RPC_LOCK:
2232 module_name = "ietf-netconf";
2233 rpc_name = "lock";
2234 break;
2235 case NC_RPC_UNLOCK:
2236 module_name = "ietf-netconf";
2237 rpc_name = "unlock";
2238 break;
2239 case NC_RPC_GET:
2240 module_name = "ietf-netconf";
2241 rpc_name = "get";
2242 break;
2243 case NC_RPC_KILL:
2244 module_name = "ietf-netconf";
2245 rpc_name = "kill-session";
2246 break;
2247 case NC_RPC_COMMIT:
2248 module_name = "ietf-netconf";
2249 rpc_name = "commit";
2250 break;
2251 case NC_RPC_DISCARD:
2252 module_name = "ietf-netconf";
2253 rpc_name = "discard-changes";
2254 break;
2255 case NC_RPC_CANCEL:
2256 module_name = "ietf-netconf";
2257 rpc_name = "cancel-commit";
2258 break;
2259 case NC_RPC_VALIDATE:
2260 module_name = "ietf-netconf";
2261 rpc_name = "validate";
2262 break;
2263 case NC_RPC_GETSCHEMA:
2264 module_name = "ietf-netconf-monitoring";
2265 rpc_name = "get-schema";
2266 break;
2267 case NC_RPC_SUBSCRIBE:
2268 module_name = "notifications";
Michal Vaskoeed893d2021-05-25 17:11:08 +02002269 rpc_name = "create-subscription";
Michal Vasko77367452021-02-16 16:32:18 +01002270 break;
2271 case NC_RPC_GETDATA:
2272 module_name = "ietf-netconf-nmda";
2273 rpc_name = "get-data";
2274 break;
2275 case NC_RPC_EDITDATA:
2276 module_name = "ietf-netconf-nmda";
2277 rpc_name = "edit-data";
2278 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002279 case NC_RPC_ESTABLISHSUB:
2280 module_name = "ietf-subscribed-notifications";
2281 rpc_name = "establish-subscription";
2282 break;
2283 case NC_RPC_MODIFYSUB:
2284 module_name = "ietf-subscribed-notifications";
2285 rpc_name = "modify-subscription";
2286 break;
2287 case NC_RPC_DELETESUB:
2288 module_name = "ietf-subscribed-notifications";
2289 rpc_name = "delete-subscription";
2290 break;
2291 case NC_RPC_KILLSUB:
2292 module_name = "ietf-subscribed-notifications";
2293 rpc_name = "kill-subscription";
2294 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002295 case NC_RPC_ESTABLISHPUSH:
2296 module_name = "ietf-subscribed-notifications";
2297 rpc_name = "establish-subscription";
2298 module_check = "ietf-yang-push";
2299 break;
2300 case NC_RPC_MODIFYPUSH:
2301 module_name = "ietf-subscribed-notifications";
2302 rpc_name = "modify-subscription";
2303 module_check = "ietf-yang-push";
2304 break;
2305 case NC_RPC_RESYNCSUB:
2306 module_name = "ietf-yang-push";
2307 rpc_name = "resync-subscription";
2308 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002309 case NC_RPC_UNKNOWN:
Michal Vasko77367452021-02-16 16:32:18 +01002310 lyrc = LY_EINT;
2311 break;
2312 }
2313
2314 if (module_name && rpc_name) {
2315 mod = ly_ctx_get_module_implemented(session->ctx, module_name);
2316 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002317 ERR(session, "Missing \"%s\" module in the context.", module_name);
Michal Vasko77367452021-02-16 16:32:18 +01002318 return -1;
2319 }
2320
2321 /* create the operation node */
2322 lyrc = lyd_new_inner(NULL, mod, rpc_name, 0, op);
2323 }
Michal Vasko305faca2021-03-25 09:16:02 +01002324 if (module_check) {
2325 if (!ly_ctx_get_module_implemented(session->ctx, module_check)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002326 ERR(session, "Missing \"%s\" module in the context.", module_check);
Michal Vasko305faca2021-03-25 09:16:02 +01002327 return -1;
2328 }
2329 }
Michal Vasko77367452021-02-16 16:32:18 +01002330
2331 if (lyrc) {
2332 return -1;
2333 }
2334 return 0;
2335}
2336
2337API NC_MSG_TYPE
2338nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct lyd_node **envp,
2339 struct lyd_node **op)
2340{
2341 NC_MSG_TYPE ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002342
roman40672412023-05-04 11:10:22 +02002343 NC_CHECK_ARG_RET(session, session, rpc, envp, op, NC_MSG_ERROR);
2344
2345 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002346 ERR(session, "Invalid session to receive RPC replies.");
Michal Vasko086311b2016-01-08 09:53:11 +01002347 return NC_MSG_ERROR;
2348 }
Michal Vasko77367452021-02-16 16:32:18 +01002349
2350 /* get a duplicate of the RPC node to append reply to */
2351 if (recv_reply_dup_rpc(session, rpc, op)) {
2352 return NC_MSG_ERROR;
Michal Vaskoeee99412016-11-21 10:19:43 +01002353 }
Michal Vasko086311b2016-01-08 09:53:11 +01002354
Michal Vasko77367452021-02-16 16:32:18 +01002355 /* receive a reply */
2356 ret = recv_reply(session, timeout, *op, msgid, envp);
Michal Vasko086311b2016-01-08 09:53:11 +01002357
Michal Vasko77367452021-02-16 16:32:18 +01002358 /* do not return the RPC copy on error or if the reply includes no data */
2359 if (((ret != NC_MSG_REPLY) && (ret != NC_MSG_REPLY_ERR_MSGID)) || !lyd_child(*op)) {
2360 lyd_free_tree(*op);
2361 *op = NULL;
2362 }
2363 return ret;
2364}
2365
2366static NC_MSG_TYPE
2367recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
2368{
Michal Vasko77367452021-02-16 16:32:18 +01002369 LY_ERR lyrc;
2370 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002371 NC_MSG_TYPE ret = NC_MSG_ERROR;
2372
2373 *op = NULL;
2374 *envp = NULL;
2375
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002376 /* Receive messages until a notification is found or a timeout or error reached */
2377 ret = recv_msg(session, timeout, NC_MSG_NOTIF, &msg);
2378 if (ret != NC_MSG_NOTIF) {
Michal Vasko77367452021-02-16 16:32:18 +01002379 goto cleanup;
2380 }
2381
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002382 /* Parse */
Michal Vasko77367452021-02-16 16:32:18 +01002383 lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op);
2384 if (!lyrc) {
Michal Vasko77367452021-02-16 16:32:18 +01002385 goto cleanup;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002386 } else {
Michal Vasko58791da2024-02-26 13:52:59 +01002387 ERR(session, "Received an invalid message (%s).", ly_err_last(session->ctx)->msg);
Michal Vasko9a108052022-04-01 12:06:54 +02002388 lyd_free_tree(*envp);
2389 *envp = NULL;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002390 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002391 goto cleanup;
2392 }
2393
Michal Vasko77367452021-02-16 16:32:18 +01002394cleanup:
2395 ly_in_free(msg, 1);
2396 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002397}
2398
2399API NC_MSG_TYPE
Michal Vasko77367452021-02-16 16:32:18 +01002400nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
Michal Vasko086311b2016-01-08 09:53:11 +01002401{
roman40672412023-05-04 11:10:22 +02002402 NC_CHECK_ARG_RET(session, session, envp, op, NC_MSG_ERROR);
2403
2404 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002405 ERR(session, "Invalid session to receive Notifications.");
Michal Vasko086311b2016-01-08 09:53:11 +01002406 return NC_MSG_ERROR;
2407 }
2408
Michal Vasko77367452021-02-16 16:32:18 +01002409 /* receive a notification */
2410 return recv_notif(session, timeout, envp, op);
Michal Vasko086311b2016-01-08 09:53:11 +01002411}
2412
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002413static void *
2414nc_recv_notif_thread(void *arg)
2415{
2416 struct nc_ntf_thread_arg *ntarg;
2417 struct nc_session *session;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002418 nc_notif_dispatch_clb notif_clb;
2419 void *user_data;
Michal Vasko743ff9f2022-10-20 10:03:13 +02002420
Michal Vaskoffb35e92022-10-20 09:07:25 +02002421 void (*free_data)(void *);
Michal Vasko77367452021-02-16 16:32:18 +01002422 struct lyd_node *envp, *op;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002423 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002424
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002425 /* detach ourselves */
Michal Vasko131120a2018-05-29 15:44:02 +02002426 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002427
2428 ntarg = (struct nc_ntf_thread_arg *)arg;
2429 session = ntarg->session;
2430 notif_clb = ntarg->notif_clb;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002431 user_data = ntarg->user_data;
2432 free_data = ntarg->free_data;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002433 free(ntarg);
2434
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002435 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
Michal Vasko77367452021-02-16 16:32:18 +01002436 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002437 if (msgtype == NC_MSG_NOTIF) {
Michal Vaskoffb35e92022-10-20 09:07:25 +02002438 notif_clb(session, envp, op, user_data);
Michal Vasko77367452021-02-16 16:32:18 +01002439 if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) {
2440 lyd_free_tree(envp);
Michal Vasko0c0239c2023-02-01 14:31:06 +01002441 lyd_free_all(op);
Michal Vaskof0537d82016-01-29 14:42:38 +01002442 break;
2443 }
Michal Vasko77367452021-02-16 16:32:18 +01002444 lyd_free_tree(envp);
Michal Vasko0c0239c2023-02-01 14:31:06 +01002445 lyd_free_all(op);
Michal Vaskoce326052018-01-04 10:32:03 +01002446 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002447 /* quit this thread once the session is broken */
2448 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002449 }
2450
2451 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2452 }
2453
Michal Vasko05532772021-06-03 12:12:38 +02002454 VRB(session, "Notification thread exit.");
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002455 ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count);
Michal Vaskoffb35e92022-10-20 09:07:25 +02002456 if (free_data) {
2457 free_data(user_data);
2458 }
2459
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002460 return NULL;
2461}
2462
2463API int
Michal Vaskoffb35e92022-10-20 09:07:25 +02002464nc_recv_notif_dispatch(struct nc_session *session, nc_notif_dispatch_clb notif_clb)
2465{
2466 return nc_recv_notif_dispatch_data(session, notif_clb, NULL, NULL);
2467}
2468
2469API int
2470nc_recv_notif_dispatch_data(struct nc_session *session, nc_notif_dispatch_clb notif_clb, void *user_data,
2471 void (*free_data)(void *))
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002472{
2473 struct nc_ntf_thread_arg *ntarg;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002474 pthread_t tid;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002475 int ret;
2476
roman40672412023-05-04 11:10:22 +02002477 NC_CHECK_ARG_RET(session, session, notif_clb, -1);
2478
2479 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002480 ERR(session, "Invalid session to receive Notifications.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002481 return -1;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002482 }
2483
2484 ntarg = malloc(sizeof *ntarg);
roman3a95bb22023-10-26 11:07:17 +02002485 NC_CHECK_ERRMEM_RET(!ntarg, -1);
2486
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002487 ntarg->session = session;
2488 ntarg->notif_clb = notif_clb;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002489 ntarg->user_data = user_data;
2490 ntarg->free_data = free_data;
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002491 ATOMIC_INC_RELAXED(session->opts.client.ntf_thread_count);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002492
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002493 /* just so that nc_recv_notif_thread() does not immediately exit */
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002494 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 1);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002495
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002496 ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002497 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02002498 ERR(session, "Failed to create a new thread (%s).", strerror(errno));
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002499 free(ntarg);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002500 if (ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count) == 1) {
2501 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
2502 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002503 return -1;
2504 }
2505
2506 return 0;
2507}
2508
Michal Vasko77367452021-02-16 16:32:18 +01002509static const char *
2510nc_wd2str(NC_WD_MODE wd)
2511{
2512 switch (wd) {
2513 case NC_WD_ALL:
2514 return "report-all";
2515 case NC_WD_ALL_TAG:
2516 return "report-all-tagged";
2517 case NC_WD_TRIM:
2518 return "trim";
2519 case NC_WD_EXPLICIT:
2520 return "explicit";
2521 default:
2522 break;
2523 }
2524
2525 return NULL;
2526}
2527
Michal Vasko086311b2016-01-08 09:53:11 +01002528API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002529nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002530{
2531 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002532 int dofree = 1;
Michal Vasko77367452021-02-16 16:32:18 +01002533 struct ly_in *in;
Michal Vasko90e8e692016-07-13 12:27:57 +02002534 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002535 struct nc_rpc_getconfig *rpc_gc;
2536 struct nc_rpc_edit *rpc_e;
2537 struct nc_rpc_copy *rpc_cp;
2538 struct nc_rpc_delete *rpc_del;
2539 struct nc_rpc_lock *rpc_lock;
2540 struct nc_rpc_get *rpc_g;
2541 struct nc_rpc_kill *rpc_k;
2542 struct nc_rpc_commit *rpc_com;
2543 struct nc_rpc_cancel *rpc_can;
2544 struct nc_rpc_validate *rpc_val;
2545 struct nc_rpc_getschema *rpc_gs;
2546 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002547 struct nc_rpc_getdata *rpc_getd;
2548 struct nc_rpc_editdata *rpc_editd;
Michal Vasko96f247a2021-03-15 13:32:10 +01002549 struct nc_rpc_establishsub *rpc_estsub;
2550 struct nc_rpc_modifysub *rpc_modsub;
2551 struct nc_rpc_deletesub *rpc_delsub;
2552 struct nc_rpc_killsub *rpc_killsub;
Michal Vasko305faca2021-03-25 09:16:02 +01002553 struct nc_rpc_establishpush *rpc_estpush;
2554 struct nc_rpc_modifypush *rpc_modpush;
2555 struct nc_rpc_resyncsub *rpc_resyncsub;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002556 struct lyd_node *data = NULL, *node, *cont;
Michal Vasko305faca2021-03-25 09:16:02 +01002557 const struct lys_module *mod = NULL, *mod2 = NULL, *ietfncwd;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002558 LY_ERR lyrc = 0;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002559 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002560 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002561 uint64_t cur_msgid;
2562
roman40672412023-05-04 11:10:22 +02002563 NC_CHECK_ARG_RET(session, session, rpc, msgid, NC_MSG_ERROR);
2564
2565 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002566 ERR(session, "Invalid session to send RPCs.");
Michal Vasko086311b2016-01-08 09:53:11 +01002567 return NC_MSG_ERROR;
2568 }
2569
Michal Vaskoc1171a42019-11-05 12:06:46 +01002570 switch (rpc->type) {
2571 case NC_RPC_ACT_GENERIC:
2572 /* checked when parsing */
2573 break;
2574 case NC_RPC_GETCONFIG:
2575 case NC_RPC_EDIT:
2576 case NC_RPC_COPY:
2577 case NC_RPC_DELETE:
2578 case NC_RPC_LOCK:
2579 case NC_RPC_UNLOCK:
2580 case NC_RPC_GET:
2581 case NC_RPC_KILL:
2582 case NC_RPC_COMMIT:
2583 case NC_RPC_DISCARD:
2584 case NC_RPC_CANCEL:
2585 case NC_RPC_VALIDATE:
Michal Vasko77367452021-02-16 16:32:18 +01002586 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002587 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002588 ERR(session, "Missing \"ietf-netconf\" module in the context.");
Michal Vasko086311b2016-01-08 09:53:11 +01002589 return NC_MSG_ERROR;
2590 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002591 break;
2592 case NC_RPC_GETSCHEMA:
Michal Vasko77367452021-02-16 16:32:18 +01002593 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002594 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002595 ERR(session, "Missing \"ietf-netconf-monitoring\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002596 return NC_MSG_ERROR;
2597 }
2598 break;
2599 case NC_RPC_SUBSCRIBE:
Michal Vasko77367452021-02-16 16:32:18 +01002600 mod = ly_ctx_get_module_implemented(session->ctx, "notifications");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002601 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002602 ERR(session, "Missing \"notifications\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002603 return NC_MSG_ERROR;
2604 }
2605 break;
2606 case NC_RPC_GETDATA:
2607 case NC_RPC_EDITDATA:
Michal Vasko77367452021-02-16 16:32:18 +01002608 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-nmda");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002609 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002610 ERR(session, "Missing \"ietf-netconf-nmda\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002611 return NC_MSG_ERROR;
2612 }
2613 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002614 case NC_RPC_ESTABLISHSUB:
2615 case NC_RPC_MODIFYSUB:
2616 case NC_RPC_DELETESUB:
2617 case NC_RPC_KILLSUB:
2618 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2619 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002620 ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context.");
Michal Vasko96f247a2021-03-15 13:32:10 +01002621 return NC_MSG_ERROR;
2622 }
2623 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002624 case NC_RPC_ESTABLISHPUSH:
2625 case NC_RPC_MODIFYPUSH:
2626 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2627 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002628 ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002629 return NC_MSG_ERROR;
2630 }
2631 mod2 = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2632 if (!mod2) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002633 ERR(session, "Missing \"ietf-yang-push\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002634 return NC_MSG_ERROR;
2635 }
2636 break;
2637 case NC_RPC_RESYNCSUB:
2638 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2639 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002640 ERR(session, "Missing \"ietf-yang-push\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002641 return NC_MSG_ERROR;
2642 }
2643 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002644 case NC_RPC_UNKNOWN:
2645 ERRINT;
2646 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002647 }
2648
Michal Vaskoab9deb62021-05-27 11:37:00 +02002649#define CHECK_LYRC_BREAK(func_call) if ((lyrc = func_call)) break;
2650
Michal Vasko086311b2016-01-08 09:53:11 +01002651 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002652 case NC_RPC_ACT_GENERIC:
2653 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002654
2655 if (rpc_gen->has_data) {
2656 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002657 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002658 } else {
Michal Vasko77367452021-02-16 16:32:18 +01002659 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2660 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &data, NULL);
2661 ly_in_free(in, 0);
2662 if (lyrc) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002663 break;
Michal Vaskoeec410f2017-11-24 09:14:55 +01002664 }
Michal Vasko086311b2016-01-08 09:53:11 +01002665 }
2666 break;
2667
2668 case NC_RPC_GETCONFIG:
2669 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2670
Michal Vaskoab9deb62021-05-27 11:37:00 +02002671 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-config", 0, &data));
2672 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
2673 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_gc->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002674 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002675 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002676 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_gc->filter, LYD_ANYDATA_XML, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002677 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002678 } else {
Michal Vasko58791da2024-02-26 13:52:59 +01002679 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002680 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2681 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_gc->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002682 }
2683 }
2684
2685 if (rpc_gc->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002686 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002687 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002688 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002689 lyrc = LY_ENOTFOUND;
2690 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002691 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002692 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 +01002693 }
2694 break;
2695
2696 case NC_RPC_EDIT:
2697 rpc_e = (struct nc_rpc_edit *)rpc;
2698
Michal Vaskoab9deb62021-05-27 11:37:00 +02002699 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-config", 0, &data));
2700 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2701 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_e->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002702
2703 if (rpc_e->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002704 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 +01002705 }
Michal Vasko086311b2016-01-08 09:53:11 +01002706 if (rpc_e->test_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002707 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 +01002708 }
Michal Vasko086311b2016-01-08 09:53:11 +01002709 if (rpc_e->error_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002710 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 +01002711 }
Michal Vasko7793bc62016-09-16 11:58:41 +02002712 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002713 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_e->edit_cont, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002714 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002715 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_e->edit_cont, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002716 }
2717 break;
2718
2719 case NC_RPC_COPY:
2720 rpc_cp = (struct nc_rpc_copy *)rpc;
2721
Michal Vaskoab9deb62021-05-27 11:37:00 +02002722 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "copy-config", 0, &data));
2723 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002724 if (rpc_cp->url_trg) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002725 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_trg, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002726 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002727 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002728 }
2729
Michal Vaskoab9deb62021-05-27 11:37:00 +02002730 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002731 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002732 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002733 CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_cp->url_config_src, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002734 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002735 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002736 }
2737 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002738 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002739 }
2740
2741 if (rpc_cp->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002742 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002743 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002744 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002745 lyrc = LY_ENOTFOUND;
2746 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002747 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002748 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 +01002749 }
2750 break;
2751
2752 case NC_RPC_DELETE:
2753 rpc_del = (struct nc_rpc_delete *)rpc;
2754
Michal Vaskoab9deb62021-05-27 11:37:00 +02002755 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-config", 0, &data));
2756 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002757 if (rpc_del->url) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002758 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_del->url, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002759 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002760 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_del->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002761 }
2762 break;
2763
2764 case NC_RPC_LOCK:
2765 rpc_lock = (struct nc_rpc_lock *)rpc;
2766
Michal Vaskoab9deb62021-05-27 11:37:00 +02002767 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "lock", 0, &data));
2768 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2769 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002770 break;
2771
2772 case NC_RPC_UNLOCK:
2773 rpc_lock = (struct nc_rpc_lock *)rpc;
2774
Michal Vaskoab9deb62021-05-27 11:37:00 +02002775 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "unlock", 0, &data));
2776 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2777 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002778 break;
2779
2780 case NC_RPC_GET:
2781 rpc_g = (struct nc_rpc_get *)rpc;
2782
Michal Vaskoab9deb62021-05-27 11:37:00 +02002783 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002784 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002785 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002786 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_g->filter, LYD_ANYDATA_XML, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002787 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002788 } else {
Michal Vasko58791da2024-02-26 13:52:59 +01002789 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002790 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2791 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_g->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002792 }
2793 }
2794
2795 if (rpc_g->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002796 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002797 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002798 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002799 lyrc = LY_ENOTFOUND;
2800 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002801 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002802 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 +01002803 }
2804 break;
2805
2806 case NC_RPC_KILL:
2807 rpc_k = (struct nc_rpc_kill *)rpc;
2808
Michal Vaskoab9deb62021-05-27 11:37:00 +02002809 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-session", 0, &data));
Michal Vasko16374712024-04-26 14:13:00 +02002810 sprintf(str, "%" PRIu32, rpc_k->sid);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002811 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "session-id", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002812 break;
2813
2814 case NC_RPC_COMMIT:
2815 rpc_com = (struct nc_rpc_commit *)rpc;
2816
Michal Vaskoab9deb62021-05-27 11:37:00 +02002817 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002818 if (rpc_com->confirmed) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002819 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirmed", NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002820 }
2821
2822 if (rpc_com->confirm_timeout) {
Michal Vasko16374712024-04-26 14:13:00 +02002823 sprintf(str, "%" PRIu32, rpc_com->confirm_timeout);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002824 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirm-timeout", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002825 }
Michal Vasko086311b2016-01-08 09:53:11 +01002826 if (rpc_com->persist) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002827 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist", rpc_com->persist, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002828 }
Michal Vasko086311b2016-01-08 09:53:11 +01002829 if (rpc_com->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002830 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_com->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002831 }
2832 break;
2833
2834 case NC_RPC_DISCARD:
Michal Vaskoab9deb62021-05-27 11:37:00 +02002835 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "discard-changes", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002836 break;
2837
2838 case NC_RPC_CANCEL:
2839 rpc_can = (struct nc_rpc_cancel *)rpc;
2840
Michal Vaskoab9deb62021-05-27 11:37:00 +02002841 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "cancel-commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002842 if (rpc_can->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002843 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_can->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002844 }
2845 break;
2846
2847 case NC_RPC_VALIDATE:
2848 rpc_val = (struct nc_rpc_validate *)rpc;
2849
Michal Vaskoab9deb62021-05-27 11:37:00 +02002850 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "validate", 0, &data));
2851 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002852 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002853 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002854 CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_val->url_config_src, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002855 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002856 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_val->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002857 }
2858 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002859 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_val->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002860 }
2861 break;
2862
2863 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002864 rpc_gs = (struct nc_rpc_getschema *)rpc;
2865
Michal Vaskoab9deb62021-05-27 11:37:00 +02002866 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-schema", 0, &data));
2867 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "identifier", rpc_gs->identifier, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002868 if (rpc_gs->version) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002869 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "version", rpc_gs->version, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002870 }
2871 if (rpc_gs->format) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002872 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "format", rpc_gs->format, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002873 }
2874 break;
2875
2876 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002877 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2878
Michal Vaskoab9deb62021-05-27 11:37:00 +02002879 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "create-subscription", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002880 if (rpc_sub->stream) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002881 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_sub->stream, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002882 }
2883
2884 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002885 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002886 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_sub->filter, LYD_ANYDATA_XML, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002887 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002888 } else {
Michal Vasko58791da2024-02-26 13:52:59 +01002889 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002890 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2891 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_sub->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002892 }
2893 }
Michal Vasko086311b2016-01-08 09:53:11 +01002894 if (rpc_sub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002895 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "startTime", rpc_sub->start, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002896 }
Michal Vasko086311b2016-01-08 09:53:11 +01002897 if (rpc_sub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002898 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stopTime", rpc_sub->stop, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002899 }
2900 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002901
2902 case NC_RPC_GETDATA:
2903 rpc_getd = (struct nc_rpc_getdata *)rpc;
2904
Michal Vaskoab9deb62021-05-27 11:37:00 +02002905 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-data", 0, &data));
2906 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_getd->datastore, 0, NULL));
2907
Michal Vaskoc1171a42019-11-05 12:06:46 +01002908 if (rpc_getd->filter) {
2909 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002910 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "subtree-filter", rpc_getd->filter, LYD_ANYDATA_XML, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002911 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002912 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "xpath-filter", rpc_getd->filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002913 }
2914 }
2915 if (rpc_getd->config_filter) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002916 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "config-filter", rpc_getd->config_filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002917 }
2918 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002919 CHECK_LYRC_BREAK(lyd_new_term(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" :
2920 "origin-filter", rpc_getd->origin_filter[i], 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002921 }
2922 if (rpc_getd->max_depth) {
2923 sprintf(str, "%u", rpc_getd->max_depth);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002924 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "max-depth", str, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002925 }
2926 if (rpc_getd->with_origin) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002927 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-origin", NULL, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002928 }
2929
2930 if (rpc_getd->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002931 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
Michal Vaskoab9deb62021-05-27 11:37:00 +02002932 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 +01002933 }
2934 break;
2935
2936 case NC_RPC_EDITDATA:
2937 rpc_editd = (struct nc_rpc_editdata *)rpc;
2938
Michal Vaskoab9deb62021-05-27 11:37:00 +02002939 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-data", 0, &data));
2940 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_editd->datastore, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002941
2942 if (rpc_editd->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002943 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op], 0,
2944 NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002945 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002946 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002947 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_editd->edit_cont, LYD_ANYDATA_XML, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002948 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002949 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_editd->edit_cont, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002950 }
2951 break;
2952
Michal Vasko96f247a2021-03-15 13:32:10 +01002953 case NC_RPC_ESTABLISHSUB:
2954 rpc_estsub = (struct nc_rpc_establishsub *)rpc;
2955
Michal Vaskoab9deb62021-05-27 11:37:00 +02002956 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002957
2958 if (rpc_estsub->filter) {
2959 if (!rpc_estsub->filter[0] || (rpc_estsub->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002960 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_estsub->filter, LYD_ANYDATA_XML,
Michal Vaskoab9deb62021-05-27 11:37:00 +02002961 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002962 } else if (rpc_estsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002963 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002964 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002965 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002966 }
2967 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002968 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_estsub->stream, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002969
2970 if (rpc_estsub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002971 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "replay-start-time", rpc_estsub->start, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002972 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002973 if (rpc_estsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002974 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002975 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002976 if (rpc_estsub->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002977 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estsub->encoding, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002978 }
2979 break;
2980
2981 case NC_RPC_MODIFYSUB:
2982 rpc_modsub = (struct nc_rpc_modifysub *)rpc;
2983
Michal Vaskoab9deb62021-05-27 11:37:00 +02002984 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002985
Michal Vasko16374712024-04-26 14:13:00 +02002986 sprintf(str, "%" PRIu32, rpc_modsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002987 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002988
2989 if (rpc_modsub->filter) {
2990 if (!rpc_modsub->filter[0] || (rpc_modsub->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002991 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_modsub->filter, LYD_ANYDATA_XML,
Michal Vaskoab9deb62021-05-27 11:37:00 +02002992 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002993 } else if (rpc_modsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002994 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002995 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002996 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002997 }
2998 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002999 if (rpc_modsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003000 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01003001 }
3002 break;
3003
3004 case NC_RPC_DELETESUB:
3005 rpc_delsub = (struct nc_rpc_deletesub *)rpc;
3006
Michal Vaskoab9deb62021-05-27 11:37:00 +02003007 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01003008
Michal Vasko16374712024-04-26 14:13:00 +02003009 sprintf(str, "%" PRIu32, rpc_delsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003010 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01003011 break;
3012
3013 case NC_RPC_KILLSUB:
3014 rpc_killsub = (struct nc_rpc_killsub *)rpc;
3015
Michal Vaskoab9deb62021-05-27 11:37:00 +02003016 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01003017
Michal Vasko16374712024-04-26 14:13:00 +02003018 sprintf(str, "%" PRIu32, rpc_killsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003019 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01003020 break;
3021
Michal Vasko305faca2021-03-25 09:16:02 +01003022 case NC_RPC_ESTABLISHPUSH:
3023 rpc_estpush = (struct nc_rpc_establishpush *)rpc;
3024
Michal Vaskoab9deb62021-05-27 11:37:00 +02003025 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
3026 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_estpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003027
3028 if (rpc_estpush->filter) {
3029 if (!rpc_estpush->filter[0] || (rpc_estpush->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01003030 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_estpush->filter,
Michal Vaskoab9deb62021-05-27 11:37:00 +02003031 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003032 } else if (rpc_estpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003033 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003034 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003035 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003036 }
3037 }
3038
3039 if (rpc_estpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003040 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003041 }
Michal Vasko305faca2021-03-25 09:16:02 +01003042 if (rpc_estpush->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003043 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estpush->encoding, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003044 }
3045
3046 if (rpc_estpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003047 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003048 sprintf(str, "%" PRIu32, rpc_estpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003049 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003050 if (rpc_estpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003051 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_estpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003052 }
3053 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003054 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003055 if (rpc_estpush->dampening_period) {
3056 sprintf(str, "%" PRIu32, rpc_estpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003057 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003058 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02003059 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "sync-on-start", rpc_estpush->sync_on_start ? "true" : "false", 0,
3060 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003061 if (rpc_estpush->excluded_change) {
3062 for (i = 0; rpc_estpush->excluded_change[i]; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003063 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "excluded-change", rpc_estpush->excluded_change[i], 0,
3064 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003065 }
3066 }
3067 }
3068 break;
3069
3070 case NC_RPC_MODIFYPUSH:
3071 rpc_modpush = (struct nc_rpc_modifypush *)rpc;
3072
Michal Vaskoab9deb62021-05-27 11:37:00 +02003073 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01003074
Michal Vasko16374712024-04-26 14:13:00 +02003075 sprintf(str, "%" PRIu32, rpc_modpush->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003076 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
3077 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_modpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003078
3079 if (rpc_modpush->filter) {
3080 if (!rpc_modpush->filter[0] || (rpc_modpush->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01003081 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_modpush->filter,
Michal Vaskoab9deb62021-05-27 11:37:00 +02003082 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003083 } else if (rpc_modpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003084 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003085 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003086 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003087 }
3088 }
Michal Vasko305faca2021-03-25 09:16:02 +01003089 if (rpc_modpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003090 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003091 }
3092
3093 if (rpc_modpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003094 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003095 sprintf(str, "%" PRIu32, rpc_modpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003096 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003097 if (rpc_modpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003098 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_modpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003099 }
3100 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003101 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003102 if (rpc_modpush->dampening_period) {
3103 sprintf(str, "%" PRIu32, rpc_modpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003104 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003105 }
3106 }
3107 break;
3108
3109 case NC_RPC_RESYNCSUB:
3110 rpc_resyncsub = (struct nc_rpc_resyncsub *)rpc;
3111
Michal Vaskoab9deb62021-05-27 11:37:00 +02003112 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "resync-subscription", 0, &data));
Michal Vasko16374712024-04-26 14:13:00 +02003113 sprintf(str, "%" PRIu32, rpc_resyncsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003114 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003115 break;
3116
Michal Vasko96f247a2021-03-15 13:32:10 +01003117 case NC_RPC_UNKNOWN:
Michal Vasko7f1c78b2016-01-19 09:52:14 +01003118 ERRINT;
3119 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01003120 }
3121
Michal Vaskoab9deb62021-05-27 11:37:00 +02003122#undef CHECK_LYRC_BREAK
3123
3124 if (lyrc) {
Michal Vasko05532772021-06-03 12:12:38 +02003125 ERR(session, "Failed to create RPC, perhaps a required feature is disabled.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02003126 lyd_free_tree(data);
Michal Vasko131120a2018-05-29 15:44:02 +02003127 return NC_MSG_ERROR;
3128 }
3129
Michal Vasko131120a2018-05-29 15:44:02 +02003130 /* send RPC, store its message ID */
3131 r = nc_send_msg_io(session, timeout, data);
3132 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01003133
Radek Krejcib4b19062018-02-07 16:33:06 +01003134 if (dofree) {
Michal Vasko77367452021-02-16 16:32:18 +01003135 lyd_free_tree(data);
Radek Krejcib4b19062018-02-07 16:33:06 +01003136 }
Michal Vasko086311b2016-01-08 09:53:11 +01003137
Michal Vasko131120a2018-05-29 15:44:02 +02003138 if (r == NC_MSG_RPC) {
3139 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01003140 }
Michal Vasko131120a2018-05-29 15:44:02 +02003141 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01003142}
Michal Vaskode2946c2017-01-12 12:19:26 +01003143
3144API void
3145nc_client_session_set_not_strict(struct nc_session *session)
3146{
3147 if (session->side != NC_CLIENT) {
roman40672412023-05-04 11:10:22 +02003148 ERRARG(NULL, "session");
Michal Vaskode2946c2017-01-12 12:19:26 +01003149 return;
3150 }
3151
3152 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
3153}