blob: 504068bcfaef73290da04eb40e62f47b2c462c93 [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
42#include <libyang/libyang.h>
43
Michal Vasko9e8ac262020-04-07 13:06:45 +020044#include "compat.h"
roman3f9b65c2023-06-05 14:26:58 +020045#include "config.h"
46#include "log_p.h"
47#include "messages_p.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020048#include "session_client.h"
roman3f9b65c2023-06-05 14:26:58 +020049#include "session_client_ch.h"
50#include "session_p.h"
Michal Vasko086311b2016-01-08 09:53:11 +010051
Michal Vasko8a4e1462020-05-07 11:32:31 +020052#include "../modules/ietf_netconf@2013-09-29_yang.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020053#include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h"
Michal Vasko8a4e1462020-05-07 11:32:31 +020054
Michal Vasko80ef5d22016-01-18 09:21:02 +010055static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
56
roman2eab4742023-06-06 10:00:26 +020057#ifdef NC_ENABLED_SSH_TLS
Radek Krejci62aa0642017-05-25 16:33:49 +020058char *sshauth_password(const char *username, const char *hostname, void *priv);
59char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020060char *sshauth_privkey_passphrase(const char *privkey_path, void *priv);
roman2eab4742023-06-06 10:00:26 +020061#endif /* NC_ENABLED_SSH_TLS */
Radek Krejci62aa0642017-05-25 16:33:49 +020062
63static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
64static pthread_key_t nc_client_context_key;
65#ifdef __linux__
66static struct nc_client_context context_main = {
Michal Vaskoe49a15f2019-05-27 14:18:36 +020067 .opts.ka = {
68 .enabled = 1,
69 .idle_time = 1,
70 .max_probes = 10,
71 .probe_interval = 5
72 },
roman2eab4742023-06-06 10:00:26 +020073#ifdef NC_ENABLED_SSH_TLS
Radek Krejci62aa0642017-05-25 16:33:49 +020074 .ssh_opts = {
roman41a11e42022-06-22 09:27:08 +020075 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
Radek Krejci62aa0642017-05-25 16:33:49 +020076 .auth_password = sshauth_password,
77 .auth_interactive = sshauth_interactive,
78 .auth_privkey_passphrase = sshauth_privkey_passphrase
79 },
80 .ssh_ch_opts = {
81 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
Radek Krejci62aa0642017-05-25 16:33:49 +020082 .auth_password = sshauth_password,
83 .auth_interactive = sshauth_interactive,
84 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020085 },
roman2eab4742023-06-06 10:00:26 +020086#endif /* NC_ENABLED_SSH_TLS */
Radek Krejci62aa0642017-05-25 16:33:49 +020087 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020088 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020089};
90#endif
91
92static void
93nc_client_context_free(void *ptr)
94{
95 struct nc_client_context *c = (struct nc_client_context *)ptr;
96
Radek Krejci5cebc6b2017-05-26 13:24:38 +020097 if (--(c->refcount)) {
98 /* still used */
99 return;
100 }
101
Radek Krejci62aa0642017-05-25 16:33:49 +0200102#ifdef __linux__
103 /* in __linux__ we use static memory in the main thread,
104 * so this check is for programs terminating the main()
105 * function by pthread_exit() :)
106 */
107 if (c != &context_main)
108#endif
109 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200110 /* for the main thread the same is done in nc_client_destroy() */
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400111 free(c->opts.schema_searchpath);
112
roman2eab4742023-06-06 10:00:26 +0200113#ifdef NC_ENABLED_SSH_TLS
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400114 int i;
Michal Vasko292c5542023-02-01 14:33:17 +0100115
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400116 for (i = 0; i < c->opts.ch_bind_count; ++i) {
117 close(c->opts.ch_binds[i].sock);
118 free((char *)c->opts.ch_binds[i].address);
119 }
120 free(c->opts.ch_binds);
121 c->opts.ch_binds = NULL;
122 c->opts.ch_bind_count = 0;
roman2eab4742023-06-06 10:00:26 +0200123
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400124 _nc_client_ssh_destroy_opts(&c->ssh_opts);
125 _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
roman2eab4742023-06-06 10:00:26 +0200126
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400127 _nc_client_tls_destroy_opts(&c->tls_opts);
128 _nc_client_tls_destroy_opts(&c->tls_ch_opts);
roman2eab4742023-06-06 10:00:26 +0200129#endif /* NC_ENABLED_SSH_TLS */
Radek Krejci62aa0642017-05-25 16:33:49 +0200130 free(c);
131 }
132}
133
134static void
135nc_client_context_createkey(void)
136{
137 int r;
138
139 /* initiate */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200140 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {}
Radek Krejci62aa0642017-05-25 16:33:49 +0200141 pthread_setspecific(nc_client_context_key, NULL);
142}
143
144struct nc_client_context *
145nc_client_context_location(void)
146{
147 struct nc_client_context *e;
148
149 pthread_once(&nc_client_context_once, nc_client_context_createkey);
150 e = pthread_getspecific(nc_client_context_key);
151 if (!e) {
152 /* prepare ly_err storage */
153#ifdef __linux__
154 if (getpid() == syscall(SYS_gettid)) {
155 /* main thread - use global variable instead of thread-specific variable. */
156 e = &context_main;
157 } else
158#endif /* __linux__ */
159 {
160 e = calloc(1, sizeof *e);
161 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162 e->refcount = 1;
roman2eab4742023-06-06 10:00:26 +0200163#ifdef NC_ENABLED_SSH_TLS
Radek Krejci62aa0642017-05-25 16:33:49 +0200164 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
roman41a11e42022-06-22 09:27:08 +0200165 e->ssh_opts.auth_pref[0].value = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200166 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
167 e->ssh_opts.auth_pref[1].value = 2;
168 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
roman41a11e42022-06-22 09:27:08 +0200169 e->ssh_opts.auth_pref[2].value = 3;
Radek Krejci62aa0642017-05-25 16:33:49 +0200170 e->ssh_opts.auth_password = sshauth_password;
171 e->ssh_opts.auth_interactive = sshauth_interactive;
172 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
173
roman41a11e42022-06-22 09:27:08 +0200174 /* callhome settings are the same */
Radek Krejci62aa0642017-05-25 16:33:49 +0200175 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
176 e->ssh_ch_opts.auth_pref[0].value = 1;
177 e->ssh_ch_opts.auth_pref[1].value = 2;
178 e->ssh_ch_opts.auth_pref[2].value = 3;
roman2eab4742023-06-06 10:00:26 +0200179#endif /* NC_ENABLED_SSH_TLS */
Radek Krejci62aa0642017-05-25 16:33:49 +0200180 }
181 pthread_setspecific(nc_client_context_key, e);
182 }
183
184 return e;
185}
186
187#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100188
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200189API void *
190nc_client_get_thread_context(void)
191{
192 return nc_client_context_location();
193}
194
195API void
196nc_client_set_thread_context(void *context)
197{
198 struct nc_client_context *old, *new;
199
200 if (!context) {
roman40672412023-05-04 11:10:22 +0200201 ERRARG(NULL, "context");
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200202 return;
203 }
204
205 new = (struct nc_client_context *)context;
206 old = nc_client_context_location();
207 if (old == new) {
208 /* nothing to change */
209 return;
210 }
211
212 /* replace old by new, increase reference counter in the newly set context */
213 nc_client_context_free(old);
214 new->refcount++;
215 pthread_setspecific(nc_client_context_key, new);
216}
217
Michal Vasko78939072022-12-12 07:43:18 +0100218/**
219 * @brief Ext data callback for a context to provide schema mount data.
220 */
221static LY_ERR
222nc_ly_ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
223{
224 struct nc_session *session = user_data;
225
226 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) {
227 return LY_EINVAL;
228 }
229
230 if (!session->opts.client.ext_data) {
231 ERR(session, "Unable to parse mounted data, no operational schema-mounts data received from the server.");
232 return LY_ENOTFOUND;
233 }
234
235 /* return ext data */
236 *ext_data = session->opts.client.ext_data;
237 *ext_data_free = 0;
238
239 return LY_SUCCESS;
240}
241
Radek Krejcifd5b6682017-06-13 15:52:53 +0200242int
Michal Vasko78939072022-12-12 07:43:18 +0100243nc_client_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200244{
245 /* assign context (dicionary needed for handshake) */
246 if (!ctx) {
Michal Vasko77367452021-02-16 16:32:18 +0100247 if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200248 return EXIT_FAILURE;
249 }
250
Michal Vasko5ca5d972022-09-14 13:51:31 +0200251 /* user path must be first, the first path is used to store modules retreived via get-schema */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200252 if (client_opts.schema_searchpath) {
253 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
Michal Vasko5d0cdc32022-12-12 07:38:21 +0100254 }
Michal Vasko80227222022-12-12 09:05:24 +0100255 if (!access(NC_CLIENT_SEARCH_DIR, F_OK)) {
256 ly_ctx_set_searchdir(ctx, NC_CLIENT_SEARCH_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200257 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200258
Michal Vasko5ca5d972022-09-14 13:51:31 +0200259 /* set callback for getting modules, if provided */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200260 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
Michal Vasko78939072022-12-12 07:43:18 +0100261
262 /* set ext data callback to avoid errors that no callback is set, the data are stored later, if any */
263 ly_ctx_set_ext_data_clb(ctx, nc_ly_ext_data_clb, session);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200264 } else {
265 session->flags |= NC_SESSION_SHAREDCTX;
266 }
267
268 session->ctx = ctx;
269
270 return EXIT_SUCCESS;
271}
272
Michal Vasko086311b2016-01-08 09:53:11 +0100273API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100274nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100275{
Michal Vasko3031aae2016-01-27 16:07:18 +0100276 if (client_opts.schema_searchpath) {
277 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100278 }
Michal Vasko086311b2016-01-08 09:53:11 +0100279
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100280 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100281 client_opts.schema_searchpath = strdup(path);
roman3a95bb22023-10-26 11:07:17 +0200282 NC_CHECK_ERRMEM_RET(!client_opts.schema_searchpath, 1);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100283 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100284 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100285 }
286
287 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100288}
289
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100290API const char *
291nc_client_get_schema_searchpath(void)
292{
293 return client_opts.schema_searchpath;
294}
295
Radek Krejcifd5b6682017-06-13 15:52:53 +0200296API int
297nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100298{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200299 client_opts.schema_clb = clb;
300 if (clb) {
301 client_opts.schema_clb_data = user_data;
302 } else {
303 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100304 }
305
306 return 0;
307}
308
Radek Krejcifd5b6682017-06-13 15:52:53 +0200309API ly_module_imp_clb
310nc_client_get_schema_callback(void **user_data)
311{
312 if (user_data) {
313 (*user_data) = client_opts.schema_clb_data;
314 }
315 return client_opts.schema_clb;
316}
317
Michal Vaskoa272e092023-07-10 14:34:03 +0200318API void
319nc_client_set_new_session_context_autofill(int enabled)
320{
321 client_opts.auto_context_fill_disabled = !enabled;
322}
323
Michal Vasko5ca5d972022-09-14 13:51:31 +0200324struct module_info {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200325 char *name;
326 char *revision;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200327
Radek Krejci65ef6d52018-08-16 16:35:02 +0200328 struct {
329 char *name;
330 char *revision;
331 } *submodules;
Michal Vasko77367452021-02-16 16:32:18 +0100332 char **features;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200333 int implemented;
334};
335
336struct clb_data_s {
337 void *user_data;
338 ly_module_imp_clb user_clb;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200339 struct module_info *modules;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200340 struct nc_session *session;
341 int has_get_schema;
342};
343
Michal Vaskoc088f982021-10-05 12:23:07 +0200344/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200345 * @brief Retrieve YANG module content from a local file.
Michal Vaskoc088f982021-10-05 12:23:07 +0200346 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200347 * @param[in] name Module name.
348 * @param[in] rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200349 * @param[in] clb_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200350 * @param[out] format Module format.
351 * @return Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200352 */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200353static char *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200354retrieve_module_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200355 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100356{
Michal Vaskob13a2242023-12-05 13:29:07 +0100357 char *localfile = NULL, *model_data = NULL;
358 const char *ptr;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200359 FILE *f;
360 long length, l;
Michal Vasko086311b2016-01-08 09:53:11 +0100361
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200362 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200363 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
364 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200365 return NULL;
366 }
Michal Vaskob13a2242023-12-05 13:29:07 +0100367 if (localfile && rev) {
368 ptr = strrchr(localfile, '/');
369 if (!strchr(ptr, '@')) {
370 /* we do not know the revision of the module and we require a specific one, so ignore this module */
371 localfile = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100372 }
Michal Vasko086311b2016-01-08 09:53:11 +0100373 }
374
Michal Vaskob13a2242023-12-05 13:29:07 +0100375 if (!localfile) {
376 return NULL;
377 }
378
379 VRB(clb_data->session, "Reading module \"%s@%s\" from local file \"%s\".", name, rev ? rev : "<latest>",
380 localfile);
381 f = fopen(localfile, "r");
382 if (!f) {
383 ERR(clb_data->session, "Unable to open file \"%s\" (%s).", localfile, strerror(errno));
384 free(localfile);
385 return NULL;
386 }
387
388 fseek(f, 0, SEEK_END);
389 length = ftell(f);
390 if (length < 0) {
391 ERR(clb_data->session, "Unable to get the size of module file \"%s\".", localfile);
392 free(localfile);
393 fclose(f);
394 return NULL;
395 }
396 fseek(f, 0, SEEK_SET);
397
398 model_data = malloc(length + 1);
399 if (!model_data) {
400 ERRMEM;
401 } else if ((l = fread(model_data, 1, length, f)) != length) {
402 ERR(clb_data->session, "Reading module from \"%s\" failed (%d bytes read, but %d expected).", localfile, l,
403 length);
404 free(model_data);
405 model_data = NULL;
406 } else {
407 /* terminating NULL byte */
408 model_data[length] = '\0';
409 }
410 fclose(f);
411 free(localfile);
412
Radek Krejci65ef6d52018-08-16 16:35:02 +0200413 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100414}
415
Michal Vaskoc088f982021-10-05 12:23:07 +0200416/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200417 * @brief Retrieve YANG module content from a reply to get-schema RPC.
Michal Vaskoc088f982021-10-05 12:23:07 +0200418 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200419 * @param[in] name Module name.
420 * @param[in] rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200421 * @param[in] clb_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200422 * @param[out] format Module format.
423 * @return Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200424 */
Michal Vasko086311b2016-01-08 09:53:11 +0100425static char *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200426retrieve_module_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200427 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100428{
Michal Vasko086311b2016-01-08 09:53:11 +0100429 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100430 struct lyd_node *envp = NULL, *op = NULL;
431 struct lyd_node_any *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100432 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100433 uint64_t msgid;
Michal Vasko7502b602022-08-26 11:51:17 +0200434 char *localfile = NULL, *envp_str = NULL, *model_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200435 FILE *f;
Michal Vasko086311b2016-01-08 09:53:11 +0100436
Michal Vasko5ca5d972022-09-14 13:51:31 +0200437 VRB(clb_data->session, "Reading module \"%s@%s\" from server via get-schema.", name, rev ? rev : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200438 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100439
Radek Krejci65ef6d52018-08-16 16:35:02 +0200440 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100441 usleep(1000);
442 }
443 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200444 ERR(clb_data->session, "Failed to send the <get-schema> RPC.");
Michal Vasko086311b2016-01-08 09:53:11 +0100445 nc_rpc_free(rpc);
446 return NULL;
447 }
448
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200449 do {
Michal Vasko77367452021-02-16 16:32:18 +0100450 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200451 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100452 nc_rpc_free(rpc);
453 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko05532772021-06-03 12:12:38 +0200454 ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired.");
Michal Vasko77367452021-02-16 16:32:18 +0100455 goto cleanup;
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100456 } else if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200457 ERR(clb_data->session, "Failed to receive a reply to <get-schema>.");
Michal Vasko77367452021-02-16 16:32:18 +0100458 goto cleanup;
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100459 } else if (!op) {
Michal Vasko7502b602022-08-26 11:51:17 +0200460 assert(envp);
461 lyd_print_mem(&envp_str, envp, LYD_XML, 0);
462 WRN(clb_data->session, "Received an unexpected reply to <get-schema>:\n%s", envp_str);
463 free(envp_str);
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100464 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100465 }
466
Michal Vasko77367452021-02-16 16:32:18 +0100467 if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) {
Michal Vasko05532772021-06-03 12:12:38 +0200468 ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC.");
Michal Vasko77367452021-02-16 16:32:18 +0100469 goto cleanup;
Michal Vaskob2583f12016-05-12 11:40:23 +0200470 }
Michal Vasko77367452021-02-16 16:32:18 +0100471 get_schema_data = (struct lyd_node_any *)lyd_child(op);
Radek Krejci539efb62016-08-24 15:05:16 +0200472 switch (get_schema_data->value_type) {
Radek Krejci539efb62016-08-24 15:05:16 +0200473 case LYD_ANYDATA_STRING:
Michal Vasko77367452021-02-16 16:32:18 +0100474 case LYD_ANYDATA_XML:
Michal Vaskob2583f12016-05-12 11:40:23 +0200475 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200476 break;
477 case LYD_ANYDATA_DATATREE:
Michal Vasko77367452021-02-16 16:32:18 +0100478 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Radek Krejci539efb62016-08-24 15:05:16 +0200479 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200480 case LYD_ANYDATA_JSON:
Michal Vaskod838d292018-08-15 11:39:05 +0200481 case LYD_ANYDATA_LYB:
Radek Krejci03438ec2016-09-21 14:12:26 +0200482 ERRINT;
Michal Vasko77367452021-02-16 16:32:18 +0100483 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200484 }
Michal Vasko086311b2016-01-08 09:53:11 +0100485
Radek Krejci488b0702018-08-29 14:47:15 +0200486 if (model_data && !model_data[0]) {
487 /* empty data */
488 free(model_data);
489 model_data = NULL;
490 }
Michal Vaskoa1721172022-12-12 09:06:53 +0100491 if (!model_data) {
492 goto cleanup;
493 }
494
495 /* set format */
496 *format = LYS_IN_YANG;
Radek Krejci488b0702018-08-29 14:47:15 +0200497
Michal Vasko5ca5d972022-09-14 13:51:31 +0200498 /* try to store the model_data into local module repository */
Michal Vaskoa1721172022-12-12 09:06:53 +0100499 lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx), 0, name, rev, &localfile, NULL);
500 if (client_opts.schema_searchpath && !localfile) {
501 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name, rev ? "@" : "",
502 rev ? rev : "") == -1) {
503 ERRMEM;
504 } else {
505 f = fopen(localfile, "w");
506 if (!f) {
507 WRN(clb_data->session, "Unable to store \"%s\" as a local copy of module retrieved via <get-schema> (%s).",
508 localfile, strerror(errno));
Michal Vaskof945da52018-02-15 08:45:13 +0100509 } else {
Michal Vaskoa1721172022-12-12 09:06:53 +0100510 fputs(model_data, f);
511 fclose(f);
Michal Vaskof945da52018-02-15 08:45:13 +0100512 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200513 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200514 }
Michal Vaskoa1721172022-12-12 09:06:53 +0100515 free(localfile);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200516
Michal Vasko77367452021-02-16 16:32:18 +0100517cleanup:
518 lyd_free_tree(envp);
519 lyd_free_tree(op);
Michal Vasko086311b2016-01-08 09:53:11 +0100520 return model_data;
521}
522
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200523static void
524free_with_user_data(void *data, void *user_data)
Jan Kundrát35972df2018-09-06 19:00:01 +0200525{
526 free(data);
527 (void)user_data;
528}
529
Michal Vaskoc088f982021-10-05 12:23:07 +0200530/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200531 * @brief Retrieve YANG module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200532 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200533 * @param[in] mod_name Module name.
534 * @param[in] mod_rev Module revision.
Michal Vasko5e32c402022-01-12 14:05:09 +0100535 * @param[in] user_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200536 * @param[out] format Module format.
537 * @param[out] module_data Module content.
Michal Vasko5e32c402022-01-12 14:05:09 +0100538 * @param[out] free_module_data Callback for freeing @p module_data.
539 * @return LY_ERR value.
540 */
541static LY_ERR
Michal Vasko5ca5d972022-09-14 13:51:31 +0200542retrieve_module_data(const char *mod_name, const char *mod_rev, void *user_data, LYS_INFORMAT *format,
Michal Vasko5e32c402022-01-12 14:05:09 +0100543 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
544{
545 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
546 char *model_data = NULL;
547
Michal Vasko5e32c402022-01-12 14:05:09 +0100548 /* 1. try to get data locally */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200549 model_data = retrieve_module_data_localfile(mod_name, mod_rev, clb_data, format);
Michal Vasko5e32c402022-01-12 14:05:09 +0100550
551 /* 2. try to use <get-schema> */
552 if (!model_data && clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200553 model_data = retrieve_module_data_getschema(mod_name, mod_rev, clb_data, format);
Michal Vasko5e32c402022-01-12 14:05:09 +0100554 }
555
556 /* 3. try to use user callback */
557 if (!model_data && clb_data->user_clb) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200558 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 +0100559 clb_data->user_clb(mod_name, mod_rev, NULL, NULL, clb_data->user_data, format, (const char **)&model_data,
560 free_module_data);
561 }
562
563 *free_module_data = free_with_user_data;
564 *module_data = model_data;
565 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
566}
567
568/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200569 * @brief Retrieve YANG import module content.
Michal Vasko5e32c402022-01-12 14:05:09 +0100570 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200571 * @param[in] mod_name Module name.
572 * @param[in] mod_rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200573 * @param[in] submod_name Optional submodule name.
574 * @param[in] sub_rev Submodule revision.
575 * @param[in] user_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200576 * @param[out] format Module format.
577 * @param[out] module_data Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200578 * @param[out] free_module_data Callback for freeing @p module_data.
579 * @return LY_ERR value.
580 */
Michal Vasko77367452021-02-16 16:32:18 +0100581static LY_ERR
Michal Vasko5ca5d972022-09-14 13:51:31 +0200582retrieve_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 +0100583 void *user_data, LYS_INFORMAT *format, const char **module_data,
584 void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200585{
586 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
Michal Vasko5e32c402022-01-12 14:05:09 +0100587 uint32_t u, v, match = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200588 const char *name = NULL, *rev = NULL;
589 char *model_data = NULL;
590
Michal Vasko5ca5d972022-09-14 13:51:31 +0200591 /* get and check the final name and revision of the module to be retrieved */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200592 if (!mod_rev || !mod_rev[0]) {
593 /* newest revision requested - get the newest revision from the list of available modules on server */
594 match = 0;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200595 for (u = 0; clb_data->modules[u].name; ++u) {
596 if (strcmp(mod_name, clb_data->modules[u].name)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200597 continue;
598 }
Michal Vasko5ca5d972022-09-14 13:51:31 +0200599 if (!match || (strcmp(mod_rev, clb_data->modules[u].revision) > 0)) {
600 mod_rev = clb_data->modules[u].revision;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200601 }
602 match = u + 1;
603 }
604 if (!match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200605 /* valid situation if we are retrieving YANG 1.1 module and have only capabilities for now
Michal Vasko1d2665c2021-02-12 09:28:44 +0100606 * (when loading ietf-datastore for ietf-yang-library) */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200607 VRB(clb_data->session, "Unable to identify revision of the import module \"%s\" from "
Michal Vasko05532772021-06-03 12:12:38 +0200608 "the available server side information.", mod_name);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200609 }
610 }
611 if (submod_name) {
612 name = submod_name;
613 if (sub_rev) {
614 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200615 } else if (match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200616 if (!clb_data->modules[match - 1].submodules) {
Michal Vasko05532772021-06-03 12:12:38 +0200617 VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", "
Michal Vasko5ca5d972022-09-14 13:51:31 +0200618 "in import module \"%s\", from the available server side information.", submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200619 } else {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200620 for (v = 0; clb_data->modules[match - 1].submodules[v].name; ++v) {
621 if (!strcmp(submod_name, clb_data->modules[match - 1].submodules[v].name)) {
622 rev = sub_rev = clb_data->modules[match - 1].submodules[v].revision;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200623 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200624 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200625 if (!rev) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200626 ERR(clb_data->session, "Requested submodule \"%s\" is not found in import module \"%s\" on server side.",
Michal Vasko05532772021-06-03 12:12:38 +0200627 submod_name, mod_name);
Michal Vasko77367452021-02-16 16:32:18 +0100628 return LY_ENOTFOUND;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200629 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200630 }
631 }
632 } else {
633 name = mod_name;
634 rev = mod_rev;
635 }
636
Radek Krejci65ef6d52018-08-16 16:35:02 +0200637 if (match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200638 /* we have enough information to avoid communication with server and try to get the module locally */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200639
640 /* 1. try to get data locally */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200641 model_data = retrieve_module_data_localfile(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200642
643 /* 2. try to use <get-schema> */
644 if (!model_data && clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200645 model_data = retrieve_module_data_getschema(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200646 }
647 } else {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200648 /* we are unsure which revision of the module we should load, so first try to get
Radek Krejci65ef6d52018-08-16 16:35:02 +0200649 * the newest revision from the server via get-schema and only if the server does not
650 * implement get-schema, try to load the newest revision locally. This is imperfect
651 * solution, but there are situation when a client does not know what revision is
652 * actually implemented by the server. */
653
654 /* 1. try to use <get-schema> */
655 if (clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200656 model_data = retrieve_module_data_getschema(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200657 }
658
659 /* 2. try to get data locally */
660 if (!model_data) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200661 model_data = retrieve_module_data_localfile(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200662 }
663 }
664
665 /* 3. try to use user callback */
666 if (!model_data && clb_data->user_clb) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200667 VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", name, rev ? rev : "<latest>");
Michal Vasko77367452021-02-16 16:32:18 +0100668 clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format,
669 (const char **)&model_data, free_module_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200670 }
671
Jan Kundrát35972df2018-09-06 19:00:01 +0200672 *free_module_data = free_with_user_data;
Michal Vasko77367452021-02-16 16:32:18 +0100673 *module_data = model_data;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200674 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200675}
676
Michal Vaskoc088f982021-10-05 12:23:07 +0200677/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200678 * @brief Load a YANG module into context.
Michal Vaskoc088f982021-10-05 12:23:07 +0200679 *
680 * @param[in] session NC session.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200681 * @param[in] name Module name.
682 * @param[in] revision Module revision.
683 * @param[in] features Enabled module features.
684 * @param[in] modules Server module info built from capabilities.
685 * @param[in] user_clb User callback for retrieving module data.
Michal Vaskoc088f982021-10-05 12:23:07 +0200686 * @param[in] user_data User data for @p user_clb.
687 * @param[in] has_get_schema Whether the server supports get-schema.
688 * @param[out] mod Loaded module.
689 * @return 0 on success.
690 * @return -1 on error.
691 */
Michal Vaskoceae0152018-02-14 16:03:59 +0100692static int
Michal Vasko5e32c402022-01-12 14:05:09 +0100693nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, const char **features,
Michal Vasko5ca5d972022-09-14 13:51:31 +0200694 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 +0100695{
696 int ret = 0;
Michal Vasko58791da2024-02-26 13:52:59 +0100697 const struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200698 const char *module_data = NULL;
Michal Vasko5e32c402022-01-12 14:05:09 +0100699 struct ly_in *in;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200700 LYS_INFORMAT format;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200701
702 void (*free_module_data)(void *, void *) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200703 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100704
Michal Vasko5e32c402022-01-12 14:05:09 +0100705 /* try to use a module from the context */
Michal Vaskoc3e402f2022-09-14 13:34:55 +0200706 *mod = ly_ctx_get_module_implemented(session->ctx, name);
707 if (!*mod) {
708 if (revision) {
709 *mod = ly_ctx_get_module(session->ctx, name, revision);
710 } else {
Michal Vasko5e32c402022-01-12 14:05:09 +0100711 *mod = ly_ctx_get_module_latest(session->ctx, name);
712 }
Michal Vaskoc3e402f2022-09-14 13:34:55 +0200713 } else if (revision && (!(*mod)->revision || strcmp((*mod)->revision, revision))) {
714 WRN(session, "Server implements module \"%s\" in revision \"%s\" but revision \"%s\" is already implemented"
715 " and will be used instead.", name, revision, (*mod)->revision ? (*mod)->revision : "<none>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200716 }
Michal Vasko5e32c402022-01-12 14:05:09 +0100717
Michal Vaskoceae0152018-02-14 16:03:59 +0100718 if (*mod) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100719 /* make the present module implemented and/or enable all its features */
720 if (lys_set_implemented(*mod, features)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200721 ERR(session, "Failed to implement module \"%s\".", (*mod)->name);
Michal Vasko5e32c402022-01-12 14:05:09 +0100722 ret = -1;
Michal Vaskoceae0152018-02-14 16:03:59 +0100723 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200724 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100725 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200726 clb_data.has_get_schema = has_get_schema;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200727 clb_data.modules = modules;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200728 clb_data.session = session;
729 clb_data.user_clb = user_clb;
730 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100731
732 /* clear all the errors and just collect them for now */
733 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200734 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100735
Radek Krejci65ef6d52018-08-16 16:35:02 +0200736 /* get module data */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200737 retrieve_module_data(name, revision, &clb_data, &format, &module_data, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100738
Radek Krejci65ef6d52018-08-16 16:35:02 +0200739 if (module_data) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100740 /* set import callback */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200741 ly_ctx_set_module_imp_clb(session->ctx, retrieve_module_data_imp, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100742
Michal Vasko5ca5d972022-09-14 13:51:31 +0200743 /* parse the module */
Michal Vasko5e32c402022-01-12 14:05:09 +0100744 ly_in_new_memory(module_data, &in);
745 lys_parse(session->ctx, in, format, features, mod);
746 ly_in_free(in, 0);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200747 if (*free_module_data) {
Michal Vasko77367452021-02-16 16:32:18 +0100748 (*free_module_data)((char *)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100749 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100750
Radek Krejci65ef6d52018-08-16 16:35:02 +0200751 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
752 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100753
Michal Vaskodbf7c272018-02-19 09:07:59 +0100754 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200755 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100756 if (!(*mod)) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100757 for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) {
Michal Vasko77367452021-02-16 16:32:18 +0100758 ly_err_print(session->ctx, eitem);
Michal Vaskoceae0152018-02-14 16:03:59 +0100759 }
760 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200761 } else {
762 /* print only warnings */
Michal Vasko5e32c402022-01-12 14:05:09 +0100763 for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200764 if (eitem->level == LY_LLWRN) {
Michal Vasko77367452021-02-16 16:32:18 +0100765 ly_err_print(session->ctx, eitem);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200766 }
767 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100768 }
769
Michal Vaskodbf7c272018-02-19 09:07:59 +0100770 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100771 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100772 }
773
774 return ret;
775}
776
Radek Krejci65ef6d52018-08-16 16:35:02 +0200777static void
Michal Vasko5ca5d972022-09-14 13:51:31 +0200778free_module_info(struct module_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200779{
Michal Vasko77367452021-02-16 16:32:18 +0100780 uint32_t u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200781
Radek Krejci65ef6d52018-08-16 16:35:02 +0200782 if (!list) {
783 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200784 }
785
Radek Krejci65ef6d52018-08-16 16:35:02 +0200786 for (u = 0; list[u].name; ++u) {
787 free(list[u].name);
788 free(list[u].revision);
Michal Vasko77367452021-02-16 16:32:18 +0100789 if (list[u].features) {
790 for (v = 0; list[u].features[v]; ++v) {
791 free(list[u].features[v]);
792 }
793 free(list[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200794 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200795 if (list[u].submodules) {
796 for (v = 0; list[u].submodules[v].name; ++v) {
797 free(list[u].submodules[v].name);
798 free(list[u].submodules[v].revision);
799 }
800 free(list[u].submodules);
801 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200802 }
803 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200804}
805
Michal Vaskoc088f982021-10-05 12:23:07 +0200806/**
Michal Vasko78939072022-12-12 07:43:18 +0100807 * @brief Retrieve yang-library and schema-mounts operational data from the server.
Michal Vaskoc088f982021-10-05 12:23:07 +0200808 *
809 * @param[in] session NC session.
810 * @param[in] has_get_data Whether get-data RPC is available or only get.
Michal Vasko78939072022-12-12 07:43:18 +0100811 * @param[in] filter Filter to use.
812 * @param[out] oper_data Received data.
Michal Vaskoc088f982021-10-05 12:23:07 +0200813 * @return 0 on success.
814 * @return -1 on error.
815 */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200816static int
Michal Vasko78939072022-12-12 07:43:18 +0100817get_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 +0200818{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200819 struct nc_rpc *rpc = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100820 struct lyd_node *op = NULL, *envp = NULL;
821 struct lyd_node_any *data;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200822 NC_MSG_TYPE msg;
823 uint64_t msgid;
Michal Vasko303263d2021-10-05 12:18:21 +0200824 int ret = 0;
825 const char *rpc_name;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200826
Michal Vasko78939072022-12-12 07:43:18 +0100827 /* get data from the server */
Michal Vasko303263d2021-10-05 12:18:21 +0200828 if (has_get_data) {
Michal Vasko78939072022-12-12 07:43:18 +0100829 rpc_name = "<get-data>";
830 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 +0200831 } else {
Michal Vasko78939072022-12-12 07:43:18 +0100832 rpc_name = "<get>";
833 rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200834 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200835 if (!rpc) {
836 goto cleanup;
837 }
838
839 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
840 usleep(1000);
841 }
842 if (msg == NC_MSG_ERROR) {
Michal Vasko78939072022-12-12 07:43:18 +0100843 WRN(session, "Failed to send %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200844 goto cleanup;
845 }
846
847 do {
Michal Vasko77367452021-02-16 16:32:18 +0100848 lyd_free_tree(envp);
849 lyd_free_tree(op);
850
851 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200852 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200853 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko78939072022-12-12 07:43:18 +0100854 WRN(session, "Timeout for receiving reply to a %s RPC expired.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200855 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100856 } else if (msg == NC_MSG_ERROR) {
Michal Vasko78939072022-12-12 07:43:18 +0100857 WRN(session, "Failed to receive a reply to %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200858 goto cleanup;
Jan Kundrát1d73a5a2022-03-30 16:54:49 +0200859 } else if (!op || !lyd_child(op) || !lyd_child(op)->schema || strcmp(lyd_child(op)->schema->name, "data")) {
Michal Vasko78939072022-12-12 07:43:18 +0100860 WRN(session, "Unexpected reply without data to a %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200861 goto cleanup;
862 }
863
Michal Vasko77367452021-02-16 16:32:18 +0100864 data = (struct lyd_node_any *)lyd_child(op);
865 if (data->value_type != LYD_ANYDATA_DATATREE) {
Michal Vasko78939072022-12-12 07:43:18 +0100866 WRN(session, "Unexpected data in reply to a %s RPC.", rpc_name);
Michal Vasko40528752021-06-21 15:41:51 +0200867 goto cleanup;
868 } else if (!data->value.tree) {
Michal Vasko78939072022-12-12 07:43:18 +0100869 WRN(session, "No data in reply to a %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200870 goto cleanup;
871 }
872
Michal Vasko78939072022-12-12 07:43:18 +0100873 *oper_data = data->value.tree;
874 data->value.tree = NULL;
875
876cleanup:
877 nc_rpc_free(rpc);
878 lyd_free_tree(envp);
879 lyd_free_tree(op);
880
881 if (session->status != NC_STATUS_RUNNING) {
882 /* something bad happened, discard the session */
883 ERR(session, "Invalid session, discarding.");
884 ret = -1;
885 }
886
887 return ret;
888}
889
890/**
891 * @brief Build server module info from ietf-yang-library data.
892 *
893 * @param[in] session NC session.
894 * @param[in] get_data_sup Whether get-data RPC is available or only get.
895 * @param[in] xpath_sup Whether XPath filter is supported or only subtree filter.
896 * @param[out] result Server modules.
897 * @return 0 on success.
898 * @return -1 on error.
899 */
900static int
901build_module_info_yl(struct nc_session *session, int get_data_sup, int xpath_sup, struct module_info **result)
902{
903 struct ly_set *modules = NULL;
904 uint32_t u, v, submodules_count, feature_count;
905 struct lyd_node *iter, *child, *oper_data = NULL;
906 struct lys_module *mod;
907 int ret = 0;
908
909 /* get yang-library operational data */
910 if (xpath_sup) {
911 if (get_oper_data(session, get_data_sup, "/ietf-yang-library:*", &oper_data)) {
912 goto cleanup;
913 }
914 } else {
915 if (get_oper_data(session, get_data_sup,
916 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", &oper_data)) {
917 goto cleanup;
918 }
919 }
920 if (!oper_data) {
921 goto cleanup;
922 }
923
924 if (lyd_find_xpath(oper_data, "/ietf-yang-library:modules-state/module", &modules)) {
925 WRN(NULL, "No yang-library module information found.");
Radek Krejci2d088832018-08-21 13:40:14 +0200926 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200927 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200928
Michal Vasko77367452021-02-16 16:32:18 +0100929 (*result) = calloc(modules->count + 1, sizeof **result);
roman3a95bb22023-10-26 11:07:17 +0200930 NC_CHECK_ERRMEM_GOTO(!(*result), ret = -1, cleanup);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200931
Michal Vasko77367452021-02-16 16:32:18 +0100932 for (u = 0; u < modules->count; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200933 submodules_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100934 feature_count = 0;
935 mod = ((struct lyd_node *)modules->dnodes[u])->schema->module;
936 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
Michal Vasko5d9d5972021-06-09 14:17:01 +0200937 if (!iter->schema || (iter->schema->module != mod)) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200938 /* ignore node from other schemas (augments) */
939 continue;
940 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200941 if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200942 /* ignore empty nodes */
943 continue;
944 }
945 if (!strcmp(iter->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200946 (*result)[u].name = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200947 } else if (!strcmp(iter->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200948 (*result)[u].revision = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200949 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200950 (*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200951 } else if (!strcmp(iter->schema->name, "feature")) {
Michal Vasko77367452021-02-16 16:32:18 +0100952 (*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features);
roman3a95bb22023-10-26 11:07:17 +0200953 NC_CHECK_ERRMEM_GOTO(!(*result)[u].features, free_module_info(*result); *result = NULL; ret = -1, cleanup);
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200954 (*result)[u].features[feature_count] = strdup(lyd_get_value(iter));
Michal Vasko77367452021-02-16 16:32:18 +0100955 (*result)[u].features[feature_count + 1] = NULL;
956 ++feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200957 } else if (!strcmp(iter->schema->name, "submodule")) {
958 submodules_count++;
959 }
960 }
961
962 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200963 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
roman3a95bb22023-10-26 11:07:17 +0200964 NC_CHECK_ERRMEM_GOTO(!(*result)[u].submodules, free_module_info(*result); *result = NULL; ret = -1, cleanup);
965 v = 0;
966 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
967 mod = modules->dnodes[u]->schema->module;
968 if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) {
969 LY_LIST_FOR(lyd_child(iter), child) {
970 if (mod != child->schema->module) {
971 continue;
972 } else if (!strcmp(child->schema->name, "name")) {
973 (*result)[u].submodules[v].name = strdup(lyd_get_value(child));
974 } else if (!strcmp(child->schema->name, "revision")) {
975 (*result)[u].submodules[v].revision = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200976 }
977 }
978 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200979 }
980 }
981 }
982
Radek Krejcifd5b6682017-06-13 15:52:53 +0200983cleanup:
Michal Vasko78939072022-12-12 07:43:18 +0100984 lyd_free_siblings(oper_data);
Michal Vasko77367452021-02-16 16:32:18 +0100985 ly_set_free(modules, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200986 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200987}
988
Michal Vaskoc088f982021-10-05 12:23:07 +0200989/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200990 * @brief Build server module info from received capabilities.
Michal Vaskoc088f982021-10-05 12:23:07 +0200991 *
992 * @param[in] cpblts Server capabilities.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200993 * @param[out] result Server modules.
Michal Vaskoc088f982021-10-05 12:23:07 +0200994 * @return 0 on success.
995 * @return -1 on error.
996 */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200997static int
Michal Vasko5ca5d972022-09-14 13:51:31 +0200998build_module_info_cpblts(char **cpblts, struct module_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200999{
Michal Vasko77367452021-02-16 16:32:18 +01001000 uint32_t u, v, feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001001 char *module_cpblt, *ptr, *ptr2;
1002
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001003 for (u = 0; cpblts[u]; ++u) {}
Radek Krejci235d8cb2018-08-17 14:04:32 +02001004 (*result) = calloc(u + 1, sizeof **result);
roman3a95bb22023-10-26 11:07:17 +02001005 NC_CHECK_ERRMEM_RET(!(*result), -1);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001006
1007 for (u = v = 0; cpblts[u]; ++u) {
1008 module_cpblt = strstr(cpblts[u], "module=");
1009 /* this capability requires a module */
1010 if (!module_cpblt) {
1011 continue;
1012 }
1013
1014 /* get module's name */
1015 ptr = (char *)module_cpblt + 7;
1016 ptr2 = strchr(ptr, '&');
1017 if (!ptr2) {
1018 ptr2 = ptr + strlen(ptr);
1019 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001020 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001021
1022 /* get module's revision */
1023 ptr = strstr(module_cpblt, "revision=");
1024 if (ptr) {
1025 ptr += 9;
1026 ptr2 = strchr(ptr, '&');
1027 if (!ptr2) {
1028 ptr2 = ptr + strlen(ptr);
1029 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001030 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001031 }
1032
1033 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001034 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001035
1036 /* get module's features */
1037 ptr = strstr(module_cpblt, "features=");
1038 if (ptr) {
1039 ptr += 9;
Michal Vasko77367452021-02-16 16:32:18 +01001040 feature_count = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001041 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
1042 if (*ptr == ',') {
Michal Vasko77367452021-02-16 16:32:18 +01001043 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
1044 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
1045 (*result)[v].features[feature_count + 1] = NULL;
1046 ++feature_count;
1047
Radek Krejci65ef6d52018-08-16 16:35:02 +02001048 ptr2 = ptr + 1;
1049 }
1050 }
1051 /* the last one */
Michal Vasko77367452021-02-16 16:32:18 +01001052 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
1053 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
1054 (*result)[v].features[feature_count + 1] = NULL;
1055 ++feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001056 }
1057 ++v;
1058 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001059
Michal Vasko303263d2021-10-05 12:18:21 +02001060 return 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001061}
1062
Michal Vaskoc088f982021-10-05 12:23:07 +02001063/**
Michal Vasko5ca5d972022-09-14 13:51:31 +02001064 * @brief Fill client context based on server modules info.
Michal Vaskoc088f982021-10-05 12:23:07 +02001065 *
1066 * @param[in] session NC session with the context to modify.
Michal Vasko5ca5d972022-09-14 13:51:31 +02001067 * @param[in] modules Server modules info.
1068 * @param[in] user_clb User callback for retrieving specific modules.
Michal Vaskoc088f982021-10-05 12:23:07 +02001069 * @param[in] user_data User data for @p user_clb.
1070 * @param[in] has_get_schema Whether server supports get-schema RPC.
1071 * @return 0 on success.
1072 * @return -1 on error.
1073 */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001074static int
Michal Vasko5ca5d972022-09-14 13:51:31 +02001075nc_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 +01001076 int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001077{
Michal Vasko303263d2021-10-05 12:18:21 +02001078 int ret = -1;
Michal Vasko77367452021-02-16 16:32:18 +01001079 struct lys_module *mod;
1080 uint32_t u;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001081
1082 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +02001083 /* skip import-only modules */
1084 if (!modules[u].implemented) {
1085 continue;
1086 }
1087
Radek Krejci65ef6d52018-08-16 16:35:02 +02001088 /* we can continue even if it fails */
Michal Vasko5e32c402022-01-12 14:05:09 +01001089 nc_ctx_load_module(session, modules[u].name, modules[u].revision, (const char **)modules[u].features, modules,
1090 user_clb, user_data, has_get_schema, &mod);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001091
1092 if (!mod) {
1093 if (session->status != NC_STATUS_RUNNING) {
1094 /* something bad heppened, discard the session */
Michal Vasko05532772021-06-03 12:12:38 +02001095 ERR(session, "Invalid session, discarding.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001096 goto cleanup;
1097 }
1098
Michal Vasko5ca5d972022-09-14 13:51:31 +02001099 /* all loading ways failed, the module will be ignored in the received data */
1100 WRN(session, "Failed to load module \"%s@%s\".", modules[u].name, modules[u].revision ?
Michal Vasko05532772021-06-03 12:12:38 +02001101 modules[u].revision : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001102 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001103 }
Michal Vasko60f66602017-10-17 13:52:18 +02001104 }
1105
Michal Vasko77367452021-02-16 16:32:18 +01001106 /* success */
Michal Vasko303263d2021-10-05 12:18:21 +02001107 ret = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001108
1109cleanup:
Radek Krejcifd5b6682017-06-13 15:52:53 +02001110 return ret;
1111}
1112
Michal Vaskoc088f982021-10-05 12:23:07 +02001113/**
Michal Vasko5ca5d972022-09-14 13:51:31 +02001114 * @brief Fill client context with ietf-netconf module.
Michal Vaskoc088f982021-10-05 12:23:07 +02001115 *
1116 * @param[in] session NC session with the context to modify.
Michal Vasko5ca5d972022-09-14 13:51:31 +02001117 * @param[in] modules Server module info.
1118 * @param[in] user_clb User callback for retrieving specific modules.
Michal Vaskoc088f982021-10-05 12:23:07 +02001119 * @param[in] user_data User data for @p user_clb.
1120 * @param[in] has_get_schema Whether server supports get-schema RPC.
1121 * @return 0 on success.
1122 * @return -1 on error.
1123 */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001124static int
Michal Vasko5ca5d972022-09-14 13:51:31 +02001125nc_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 +01001126 void *user_data, int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001127{
Michal Vasko77367452021-02-16 16:32:18 +01001128 uint32_t u;
Michal Vasko5e32c402022-01-12 14:05:09 +01001129 const char **features = NULL;
1130 struct ly_in *in;
Michal Vasko77367452021-02-16 16:32:18 +01001131 struct lys_module *ietfnc;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001132
Michal Vasko5e32c402022-01-12 14:05:09 +01001133 /* find supported features (capabilities) in ietf-netconf */
1134 for (u = 0; modules[u].name; ++u) {
1135 if (!strcmp(modules[u].name, "ietf-netconf")) {
1136 assert(modules[u].implemented);
1137 features = (const char **)modules[u].features;
1138 break;
1139 }
1140 }
1141 if (!modules[u].name) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001142 ERR(session, "Base NETCONF module not supported by the server.");
Michal Vasko5e32c402022-01-12 14:05:09 +01001143 return -1;
1144 }
1145
Michal Vasko77367452021-02-16 16:32:18 +01001146 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vasko5e32c402022-01-12 14:05:09 +01001147 if (ietfnc) {
1148 /* make sure to enable all the features if already loaded */
1149 lys_set_implemented(ietfnc, features);
1150 } else {
1151 /* load the module */
1152 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 +02001153 if (!ietfnc) {
Michal Vasko5e32c402022-01-12 14:05:09 +01001154 ly_in_new_memory(ietf_netconf_2013_09_29_yang, &in);
1155 lys_parse(session->ctx, in, LYS_IN_YANG, features, &ietfnc);
1156 ly_in_free(in, 0);
Michal Vasko8a4e1462020-05-07 11:32:31 +02001157 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001158 }
1159 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001160 ERR(session, "Loading base NETCONF module failed.");
Michal Vasko303263d2021-10-05 12:18:21 +02001161 return -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001162 }
1163
Radek Krejci65ef6d52018-08-16 16:35:02 +02001164 return 0;
1165}
1166
Michal Vasko78939072022-12-12 07:43:18 +01001167/**
1168 * @brief Set client session context to support schema-mount if possible.
1169 *
1170 * @param[in] session NC session with the context to modify.
1171 * @param[in] get_data_sup Whether get-data RPC is available or only get.
1172 * @param[in] xpath_sup Whether XPath filter is supported or only subtree filter.
1173 * @return 0 on success.
1174 * @return -1 on error.
1175 */
1176static int
1177nc_ctx_schema_mount(struct nc_session *session, int get_data_sup, int xpath_sup)
1178{
1179 int rc = 0;
1180 struct lyd_node *oper_data = NULL;
1181
1182 if (session->flags & NC_SESSION_SHAREDCTX) {
1183 /* context is already fully set up */
1184 goto cleanup;
1185 }
1186
1187 /* get yang-library and schema-mounts operational data */
1188 if (xpath_sup) {
1189 if ((rc = get_oper_data(session, get_data_sup, "/ietf-yang-library:* | /ietf-yang-schema-mount:*", &oper_data))) {
1190 goto cleanup;
1191 }
1192 } else {
1193 if ((rc = get_oper_data(session, get_data_sup,
1194 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>"
1195 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\"/>", &oper_data))) {
1196 goto cleanup;
1197 }
1198 }
1199
1200 if (!oper_data || lyd_find_path(oper_data, "/ietf-yang-schema-mount:schema-mounts", 0, NULL)) {
1201 /* no schema-mounts operational data */
1202 goto cleanup;
1203 }
1204
1205 /* validate the data for the parent reference prefixes to be resolved */
1206 if (lyd_validate_all(&oper_data, NULL, LYD_VALIDATE_PRESENT, NULL)) {
Michal Vasko58791da2024-02-26 13:52:59 +01001207 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 +01001208 rc = -1;
1209 goto cleanup;
1210 }
1211
1212 /* store the data in the session */
1213 session->opts.client.ext_data = oper_data;
1214 oper_data = NULL;
1215
1216cleanup:
1217 lyd_free_siblings(oper_data);
1218 return rc;
1219}
1220
Michal Vasko086311b2016-01-08 09:53:11 +01001221int
1222nc_ctx_check_and_fill(struct nc_session *session)
1223{
Michal Vaskod6c6f852022-12-14 15:37:21 +01001224 int i, get_schema_support = 0, yanglib_support = 0, xpath_support = 0, nmda_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +01001225 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001226 void *old_data = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001227 struct lys_module *mod = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +02001228 char *revision;
Michal Vasko5ca5d972022-09-14 13:51:31 +02001229 struct module_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001230
Michal Vasko2e6defd2016-10-07 15:48:15 +02001231 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +01001232
Michal Vaskoa272e092023-07-10 14:34:03 +02001233 if (client_opts.auto_context_fill_disabled) {
1234 VRB(session, "Context of the new session is left only with the default YANG modules.");
1235 return 0;
1236 }
1237
Radek Krejci65ef6d52018-08-16 16:35:02 +02001238 /* 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 +02001239 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001240
Radek Krejci65ef6d52018-08-16 16:35:02 +02001241 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
Michal Vasko5ca5d972022-09-14 13:51:31 +02001242 * modules only to those present on the server side */
Michal Vasko77367452021-02-16 16:32:18 +01001243 ly_ctx_set_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001244
1245 /* our callback is set later with appropriate data */
1246 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
1247
1248 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001249 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001250 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001251 get_schema_support = 1 + i;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001252 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001253 yanglib_support = 1 + i;
Michal Vasko78939072022-12-12 07:43:18 +01001254 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:xpath:1.0", 44)) {
1255 xpath_support = 1 + i;
Michal Vaskod6c6f852022-12-14 15:37:21 +01001256 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-nmda", 45)) {
1257 nmda_support = 1 + i;
Michal Vasko086311b2016-01-08 09:53:11 +01001258 }
1259 }
Michal Vaskod6c6f852022-12-14 15:37:21 +01001260 VRB(session, "Capability for <get-schema> support%s found.", get_schema_support ? "" : " not");
1261 VRB(session, "Capability for yang-library support%s found.", yanglib_support ? "" : " not");
1262 VRB(session, "Capability for XPath filter support%s found.", xpath_support ? "" : " not");
1263 VRB(session, "Capability for NMDA RPCs support%s found.", nmda_support ? "" : " not");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001264
Michal Vasko5ca5d972022-09-14 13:51:31 +02001265 /* get information about server's modules from capabilities list until we will have yang-library */
1266 if (build_module_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
1267 ERR(session, "Unable to get server module information from the <hello>'s capabilities.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001268 goto cleanup;
1269 }
1270
Michal Vasko086311b2016-01-08 09:53:11 +01001271 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Michal Vasko77367452021-02-16 16:32:18 +01001272 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 +02001273 WRN(session, "Loading NETCONF monitoring module failed, cannot use <get-schema>.");
Michal Vasko8a4e1462020-05-07 11:32:31 +02001274 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001275 }
1276
1277 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001278 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001279 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001280 }
1281
Radek Krejci65ef6d52018-08-16 16:35:02 +02001282 /* get correct version of ietf-yang-library into context */
1283 if (yanglib_support) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001284 /* use get-schema to get server's ietf-yang-library */
Radek Krejcib1d250e2018-04-12 13:54:18 +02001285 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1286 if (!revision) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001287 WRN(session, "Loading NETCONF ietf-yang-library module failed, missing revision in NETCONF <hello> message.");
Michal Vasko05532772021-06-03 12:12:38 +02001288 WRN(session, "Unable to automatically use <get-schema>.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001289 yanglib_support = 0;
1290 } else {
1291 revision = strndup(&revision[9], 10);
Michal Vasko5e32c402022-01-12 14:05:09 +01001292 if (nc_ctx_load_module(session, "ietf-yang-library", revision, NULL, server_modules, old_clb, old_data,
Michal Vasko0d877f92020-04-02 13:52:43 +02001293 get_schema_support, &mod)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001294 WRN(session, "Loading NETCONF ietf-yang-library module failed, unable to use it to learn all "
Michal Vasko05532772021-06-03 12:12:38 +02001295 "the supported modules.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001296 yanglib_support = 0;
1297 }
Michal Vasko0d877f92020-04-02 13:52:43 +02001298 if (strcmp(revision, "2019-01-04") >= 0) {
1299 /* we also need ietf-datastores to be implemented */
Michal Vasko5e32c402022-01-12 14:05:09 +01001300 if (nc_ctx_load_module(session, "ietf-datastores", NULL, 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-datastores module failed, unable to use yang-library "
Michal Vasko05532772021-06-03 12:12:38 +02001303 "to learn all the supported modules.");
Michal Vasko0d877f92020-04-02 13:52:43 +02001304 yanglib_support = 0;
1305 }
1306 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001307 free(revision);
1308 }
1309 }
1310
Michal Vaskod6c6f852022-12-14 15:37:21 +01001311 /* ietf-netconf-nmda is needed to issue get-data */
1312 if (nmda_support && nc_ctx_load_module(session, "ietf-netconf-nmda", NULL, NULL, server_modules, old_clb, old_data,
1313 get_schema_support, &mod)) {
1314 WRN(session, "Loading NMDA module failed, unable to use <get-data>.");
1315 nmda_support = 0;
1316 }
1317
Michal Vasko5ca5d972022-09-14 13:51:31 +02001318 /* prepare structured information about server's modules */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001319 if (yanglib_support) {
Michal Vaskod6c6f852022-12-14 15:37:21 +01001320 if (build_module_info_yl(session, nmda_support, xpath_support, &sm)) {
Radek Krejcif0633792018-08-20 15:12:09 +02001321 goto cleanup;
1322 } else if (!sm) {
Michal Vasko05532772021-06-03 12:12:38 +02001323 VRB(session, "Trying to use capabilities instead of ietf-yang-library data.");
Radek Krejcif0633792018-08-20 15:12:09 +02001324 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001325 /* prefer yang-library information, currently we have it from capabilities used for getting correct
Michal Vasko5ca5d972022-09-14 13:51:31 +02001326 * yang-library module */
1327 free_module_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001328 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001329 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001330 }
Michal Vaskoef578332016-01-25 13:20:09 +01001331
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001332 /* compile all modules at once to avoid invalid errors or warnings */
1333 ly_ctx_set_options(session->ctx, LY_CTX_EXPLICIT_COMPILE);
1334
1335 /* fill the context */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001336 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1337 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001338 }
1339
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001340 /* compile it */
1341 if (ly_ctx_compile(session->ctx)) {
1342 goto cleanup;
1343 }
1344
Michal Vasko59762c32024-04-12 12:13:12 +02001345 /* set support for schema-mount, if possible (requires ietf-yang-library support) */
1346 if (yanglib_support && nc_ctx_schema_mount(session, nmda_support, xpath_support)) {
Michal Vasko78939072022-12-12 07:43:18 +01001347 goto cleanup;
1348 }
1349
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001350 /* success */
Radek Krejcifd5b6682017-06-13 15:52:53 +02001351 ret = 0;
1352
Michal Vaskoeee99412016-11-21 10:19:43 +01001353 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001354 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 +02001355 "be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +01001356 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001357
1358cleanup:
Michal Vasko5ca5d972022-09-14 13:51:31 +02001359 free_module_info(server_modules);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001360
Radek Krejcifd5b6682017-06-13 15:52:53 +02001361 /* set user callback back */
1362 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vasko77367452021-02-16 16:32:18 +01001363 ly_ctx_unset_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001364 ly_ctx_unset_options(session->ctx, LY_CTX_EXPLICIT_COMPILE);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001365
Michal Vaskoef578332016-01-25 13:20:09 +01001366 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001367}
1368
1369API struct nc_session *
1370nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1371{
Michal Vaskod083db62016-01-19 10:31:29 +01001372 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001373
Michal Vasko45e53ae2016-04-07 11:46:03 +02001374 if (fdin < 0) {
roman40672412023-05-04 11:10:22 +02001375 ERRARG(NULL, "fdin");
Michal Vasko45e53ae2016-04-07 11:46:03 +02001376 return NULL;
1377 } else if (fdout < 0) {
roman40672412023-05-04 11:10:22 +02001378 ERRARG(NULL, "fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001379 return NULL;
1380 }
1381
1382 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001383 session = nc_new_session(NC_CLIENT, 0);
roman3a95bb22023-10-26 11:07:17 +02001384 NC_CHECK_ERRMEM_RET(!session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001385 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001386
1387 /* transport specific data */
1388 session->ti_type = NC_TI_FD;
1389 session->ti.fd.in = fdin;
1390 session->ti.fd.out = fdout;
1391
Michal Vasko78939072022-12-12 07:43:18 +01001392 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Robin Jarry4e38e292019-10-15 17:14:14 +02001393 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001394 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001395 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001396
1397 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001398 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001399 goto fail;
1400 }
1401 session->status = NC_STATUS_RUNNING;
1402
Michal Vaskoef578332016-01-25 13:20:09 +01001403 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001404 goto fail;
1405 }
1406
1407 return session;
1408
1409fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001410 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001411 return NULL;
1412}
1413
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001414API struct nc_session *
1415nc_connect_unix(const char *address, struct ly_ctx *ctx)
1416{
1417 struct nc_session *session = NULL;
1418 struct sockaddr_un sun;
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001419 struct passwd *pw, pw_buf;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001420 char *username;
1421 int sock = -1;
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001422 char *buf = NULL;
1423 size_t buf_size = 0;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001424
roman40672412023-05-04 11:10:22 +02001425 NC_CHECK_ARG_RET(NULL, address, NULL);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001426
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001427 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1428 if (sock < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001429 ERR(NULL, "Failed to create socket (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001430 goto fail;
1431 }
1432
1433 memset(&sun, 0, sizeof(sun));
1434 sun.sun_family = AF_UNIX;
1435 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1436
1437 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001438 ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001439 goto fail;
1440 }
1441
1442 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001443 ERR(NULL, "fcntl failed (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001444 goto fail;
1445 }
1446
1447 /* prepare session structure */
1448 session = nc_new_session(NC_CLIENT, 0);
roman124a4362023-10-26 15:36:22 +02001449 NC_CHECK_ERRMEM_GOTO(!session, , fail);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001450 session->status = NC_STATUS_STARTING;
1451
1452 /* transport specific data */
1453 session->ti_type = NC_TI_UNIX;
1454 session->ti.unixsock.sock = sock;
1455 sock = -1; /* do not close sock in fail label anymore */
1456
Michal Vasko78939072022-12-12 07:43:18 +01001457 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Robin Jarry4e38e292019-10-15 17:14:14 +02001458 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001459 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001460 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001461
Michal Vasko93224072021-11-09 12:14:28 +01001462 session->path = strdup(address);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001463
romanf6e32012023-04-24 15:51:26 +02001464 pw = nc_getpw(geteuid(), NULL, &pw_buf, &buf, &buf_size);
Michal Vaskoccd2dd02021-10-11 09:13:01 +02001465 if (!pw) {
1466 ERR(NULL, "Failed to find username for UID %u.", (unsigned int)geteuid());
1467 goto fail;
1468 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001469 username = strdup(pw->pw_name);
Michal Vaskoccd2dd02021-10-11 09:13:01 +02001470 free(buf);
roman124a4362023-10-26 15:36:22 +02001471 NC_CHECK_ERRMEM_GOTO(!username, , fail);
Michal Vasko93224072021-11-09 12:14:28 +01001472 session->username = username;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001473
1474 /* NETCONF handshake */
1475 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1476 goto fail;
1477 }
1478 session->status = NC_STATUS_RUNNING;
1479
1480 if (nc_ctx_check_and_fill(session) == -1) {
1481 goto fail;
1482 }
1483
1484 return session;
1485
1486fail:
1487 nc_session_free(session, NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001488 if (sock >= 0) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001489 close(sock);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001490 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001491 return NULL;
1492}
1493
Michal Vasko056f53c2022-10-21 13:38:15 +02001494/**
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001495 * @brief Convert socket IP address to string.
1496 *
1497 * @param[in] saddr Sockaddr to convert.
1498 * @param[out] str_ip String IP address.
Michal Vaskoc07d9762023-03-27 10:32:18 +02001499 * @param[out] port Optional port.
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001500 * @return 0 on success.
1501 * @return -1 on error.
1502 */
1503static int
Michal Vaskoc07d9762023-03-27 10:32:18 +02001504nc_saddr2str(const struct sockaddr *saddr, char **str_ip, uint16_t *port)
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001505{
1506 void *addr;
1507 socklen_t str_len;
1508
1509 assert((saddr->sa_family == AF_INET) || (saddr->sa_family == AF_INET6));
1510
1511 str_len = (saddr->sa_family == AF_INET) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
1512 *str_ip = malloc(str_len);
roman3a95bb22023-10-26 11:07:17 +02001513 NC_CHECK_ERRMEM_RET(!(*str_ip), -1);
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001514
1515 if (saddr->sa_family == AF_INET) {
Michal Vaskoc0f85ca2023-03-27 10:19:00 +02001516 addr = &((struct sockaddr_in *)saddr)->sin_addr;
Michal Vaskoc07d9762023-03-27 10:32:18 +02001517 if (port) {
1518 *port = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1519 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001520 } else {
Michal Vaskoc0f85ca2023-03-27 10:19:00 +02001521 addr = &((struct sockaddr_in6 *)saddr)->sin6_addr;
Michal Vaskoc07d9762023-03-27 10:32:18 +02001522 if (port) {
1523 *port = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1524 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001525 }
1526 if (!inet_ntop(saddr->sa_family, addr, *str_ip, str_len)) {
1527 ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno));
1528 free(*str_ip);
1529 return -1;
1530 }
1531
1532 return 0;
1533}
1534
1535/**
Michal Vasko056f53c2022-10-21 13:38:15 +02001536 * @brief Try to connect a socket, optionally a pending one from a previous attempt.
1537 *
1538 * @param[in] timeout_ms Timeout in ms to wait for the connection to be fully established, -1 to block.
1539 * @param[in,out] sock_pending Optional previously created socked that was not fully connected yet. If provided and
1540 * connected, is set to -1.
1541 * @param[in] res Addrinfo resource to use when creating a new socket.
1542 * @param[in] ka Keepalives to set.
1543 * @return Connected socket or -1 on error.
Frank Rimpler9f838b02018-07-25 06:44:03 +00001544 */
1545static int
Michal Vasko056f53c2022-10-21 13:38:15 +02001546sock_connect(int timeout_ms, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001547{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001548 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001549 int sock = -1;
Michal Vaskob9336672023-10-11 09:27:30 +02001550 struct pollfd fds = {0};
Frank Rimpler9f838b02018-07-25 06:44:03 +00001551 socklen_t len = sizeof(int);
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001552 uint16_t port;
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001553 char *str;
Michal Vasko086311b2016-01-08 09:53:11 +01001554
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001555 if (sock_pending && (*sock_pending != -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001556 VRB(NULL, "Trying to connect the pending socket %d.", *sock_pending);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001557 sock = *sock_pending;
1558 } else {
1559 assert(res);
Michal Vaskoc07d9762023-03-27 10:32:18 +02001560 if (nc_saddr2str(res->ai_addr, &str, &port)) {
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001561 return -1;
1562 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001563 VRB(NULL, "Trying to connect via %s to %s:%u.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", str, port);
1564 free(str);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001565
1566 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001567 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1568 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001569 ERR(NULL, "Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001570 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001571 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001572 /* make the socket non-blocking */
1573 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001574 ERR(NULL, "fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001575 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001576 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001577 /* non-blocking connect! */
1578 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1579 if (errno != EINPROGRESS) {
1580 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001581 ERR(NULL, "connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001582 goto cleanup;
1583 }
1584 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001585 }
Michal Vasko7d965dc2018-03-12 14:39:23 +01001586
Michal Vaskob9336672023-10-11 09:27:30 +02001587 fds.fd = sock;
1588 fds.events = POLLOUT;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001589
Michal Vaskob9336672023-10-11 09:27:30 +02001590 /* wait until we can write data to the socket */
1591 ret = poll(&fds, 1, timeout_ms);
1592 if (ret == -1) {
1593 /* error */
1594 ERR(NULL, "poll() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001595 goto cleanup;
Michal Vaskob9336672023-10-11 09:27:30 +02001596 } else if (ret == 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001597 /* there was a timeout */
Michal Vasko056f53c2022-10-21 13:38:15 +02001598 VRB(NULL, "Timed out after %d ms (%s).", timeout_ms, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001599 if (sock_pending) {
1600 /* no sock-close, we'll try it again */
1601 *sock_pending = sock;
1602 } else {
1603 close(sock);
1604 }
1605 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001606 }
Radek Krejci782041a2018-08-20 10:09:45 +02001607
1608 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001609 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001610 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001611 ERR(NULL, "getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001612 goto cleanup;
1613 }
Michal Vaskoe27ab4e2020-07-27 11:10:58 +02001614 if (error) {
Radek Krejci782041a2018-08-20 10:09:45 +02001615 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001616 VRB(NULL, "getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001617 errno = error;
1618 goto cleanup;
1619 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001620
roman56acd552024-04-18 16:00:37 +02001621 /* configure keepalives */
1622 if (nc_sock_configure_ka(sock, ka->enabled)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001623 goto cleanup;
1624 }
roman56acd552024-04-18 16:00:37 +02001625 if (ka->enabled) {
1626 if (nc_sock_configure_ka_idle_time(sock, ka->idle_time)) {
1627 goto cleanup;
1628 }
1629 if (nc_sock_configure_ka_max_probes(sock, ka->max_probes)) {
1630 goto cleanup;
1631 }
1632 if (nc_sock_configure_ka_probe_interval(sock, ka->probe_interval)) {
1633 goto cleanup;
1634 }
1635 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001636
Michal Vasko056f53c2022-10-21 13:38:15 +02001637 /* connected */
1638 if (sock_pending) {
1639 *sock_pending = -1;
1640 }
Michal Vasko086311b2016-01-08 09:53:11 +01001641 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001642
Frank Rimpler9f838b02018-07-25 06:44:03 +00001643cleanup:
1644 if (sock_pending) {
1645 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001646 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001647 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001648 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001649}
1650
Frank Rimpler9f838b02018-07-25 06:44:03 +00001651int
Michal Vasko056f53c2022-10-21 13:38:15 +02001652nc_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 +00001653{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001654 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001655 int sock = sock_pending ? *sock_pending : -1;
Michal Vasko0be85692021-03-02 08:04:57 +01001656 struct addrinfo hints, *res_list = NULL, *res;
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001657 char port_s[6]; /* length of string representation of short int */
1658 struct sockaddr_storage saddr;
1659 socklen_t addr_len = sizeof saddr;
1660
1661 *ip_host = NULL;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001662
Michal Vasko056f53c2022-10-21 13:38:15 +02001663 DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout_ms, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001664
1665 /* no pending socket */
1666 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001667 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001668 snprintf(port_s, 6, "%u", port);
1669 memset(&hints, 0, sizeof hints);
1670 hints.ai_family = AF_UNSPEC;
1671 hints.ai_socktype = SOCK_STREAM;
1672 hints.ai_protocol = IPPROTO_TCP;
1673 i = getaddrinfo(host, port_s, &hints, &res_list);
1674 if (i != 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001675 ERR(NULL, "Unable to translate the host address (%s).", gai_strerror(i));
Michal Vaskocb846632019-12-13 15:12:45 +01001676 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001677 }
1678
1679 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko056f53c2022-10-21 13:38:15 +02001680 sock = sock_connect(timeout_ms, sock_pending, res, ka);
Michal Vasko2af7c382020-07-27 14:21:35 +02001681 if (sock == -1) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001682 if (!sock_pending || (*sock_pending == -1)) {
Michal Vasko2af7c382020-07-27 14:21:35 +02001683 /* try the next resource */
1684 continue;
1685 } else {
1686 /* timeout, keep pending socket */
Michal Vasko0be85692021-03-02 08:04:57 +01001687 break;
Michal Vasko2af7c382020-07-27 14:21:35 +02001688 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001689 }
Michal Vasko05532772021-06-03 12:12:38 +02001690 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 +01001691
1692 opt = 1;
1693 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001694 ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001695 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001696 }
1697
Michal Vaskoc07d9762023-03-27 10:32:18 +02001698 if (nc_saddr2str(res->ai_addr, ip_host, NULL)) {
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001699 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001700 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001701 break;
1702 }
1703 freeaddrinfo(res_list);
1704
1705 } else {
1706 /* try to get a connection with the pending socket */
1707 assert(sock_pending);
Michal Vasko056f53c2022-10-21 13:38:15 +02001708 sock = sock_connect(timeout_ms, sock_pending, NULL, ka);
Tie.Liao9bca73d2023-03-23 17:25:00 +01001709
1710 if (sock > 0) {
Barbaros Tokaoglu96da5912023-06-19 18:57:05 +03001711 if (getpeername(sock, (struct sockaddr *)&saddr, &addr_len)) {
1712 ERR(NULL, "getpeername failed (%s).", strerror(errno));
Tie.Liao9bca73d2023-03-23 17:25:00 +01001713 goto error;
1714 }
1715
Michal Vaskoc07d9762023-03-27 10:32:18 +02001716 if (nc_saddr2str((struct sockaddr *)&saddr, ip_host, NULL)) {
Tie.Liao9bca73d2023-03-23 17:25:00 +01001717 goto error;
1718 }
Tie.Liao9bca73d2023-03-23 17:25:00 +01001719 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001720 }
1721
1722 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001723
1724error:
Michal Vasko0be85692021-03-02 08:04:57 +01001725 if (res_list) {
1726 freeaddrinfo(res_list);
1727 }
Michal Vaskocb846632019-12-13 15:12:45 +01001728 if (sock != -1) {
1729 close(sock);
1730 }
1731 if (sock_pending) {
1732 *sock_pending = -1;
1733 }
1734 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001735}
1736
roman2eab4742023-06-06 10:00:26 +02001737#ifdef NC_ENABLED_SSH_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001738
Michal Vasko3031aae2016-01-27 16:07:18 +01001739int
Michal Vasko9d4cca52022-09-07 11:19:57 +02001740nc_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 +01001741{
1742 int sock;
1743
roman40672412023-05-04 11:10:22 +02001744 NC_CHECK_ARG_RET(NULL, address, port, -1);
Michal Vasko3031aae2016-01-27 16:07:18 +01001745
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001746 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001747 if (sock == -1) {
1748 return -1;
1749 }
1750
1751 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001752 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1753 if (!client_opts.ch_binds) {
1754 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001755 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001756 return -1;
1757 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001758
Michal Vasko9d4cca52022-09-07 11:19:57 +02001759 client_opts.ch_binds_aux = nc_realloc(client_opts.ch_binds_aux, client_opts.ch_bind_count * sizeof *client_opts.ch_binds_aux);
1760 if (!client_opts.ch_binds_aux) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001761 ERRMEM;
1762 close(sock);
1763 return -1;
1764 }
Michal Vasko9d4cca52022-09-07 11:19:57 +02001765 client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].ti = ti;
1766 client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].hostname = hostname ? strdup(hostname) : NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001767
Michal Vasko3031aae2016-01-27 16:07:18 +01001768 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
1769 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1770 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001771 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001772
1773 return 0;
1774}
1775
1776int
1777nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1778{
1779 uint32_t i;
1780 int ret = -1;
1781
1782 if (!address && !port && !ti) {
1783 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1784 close(client_opts.ch_binds[i].sock);
Michal Vasko9d4cca52022-09-07 11:19:57 +02001785 free(client_opts.ch_binds[i].address);
1786
1787 free(client_opts.ch_binds_aux[i].hostname);
Michal Vasko3031aae2016-01-27 16:07:18 +01001788
1789 ret = 0;
1790 }
Michal Vasko9d4cca52022-09-07 11:19:57 +02001791 client_opts.ch_bind_count = 0;
1792
Michal Vasko3031aae2016-01-27 16:07:18 +01001793 free(client_opts.ch_binds);
1794 client_opts.ch_binds = NULL;
Michal Vasko9d4cca52022-09-07 11:19:57 +02001795
1796 free(client_opts.ch_binds_aux);
1797 client_opts.ch_binds_aux = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001798 } else {
1799 for (i = 0; i < client_opts.ch_bind_count; ++i) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001800 if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) &&
1801 (!port || (client_opts.ch_binds[i].port == port)) &&
Michal Vasko9d4cca52022-09-07 11:19:57 +02001802 (!ti || (client_opts.ch_binds_aux[i].ti == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001803 close(client_opts.ch_binds[i].sock);
Michal Vasko9d4cca52022-09-07 11:19:57 +02001804 free(client_opts.ch_binds[i].address);
Michal Vasko3031aae2016-01-27 16:07:18 +01001805
1806 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001807 if (!client_opts.ch_bind_count) {
1808 free(client_opts.ch_binds);
1809 client_opts.ch_binds = NULL;
Michal Vasko9d4cca52022-09-07 11:19:57 +02001810
1811 free(client_opts.ch_binds_aux);
1812 client_opts.ch_binds_aux = NULL;
Michal Vasko66c762a2016-10-13 10:34:14 +02001813 } else if (i < client_opts.ch_bind_count) {
Michal Vasko9d4cca52022-09-07 11:19:57 +02001814 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count],
1815 sizeof *client_opts.ch_binds);
1816
1817 memcpy(&client_opts.ch_binds_aux[i], &client_opts.ch_binds_aux[client_opts.ch_bind_count],
1818 sizeof *client_opts.ch_binds_aux);
Michal Vasko66c762a2016-10-13 10:34:14 +02001819 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001820
1821 ret = 0;
1822 }
1823 }
1824 }
1825
1826 return ret;
1827}
1828
1829API int
1830nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1831{
1832 int sock;
1833 char *host = NULL;
1834 uint16_t port, idx;
1835
roman40672412023-05-04 11:10:22 +02001836 NC_CHECK_ARG_RET(NULL, session, -1);
1837
Michal Vasko45e53ae2016-04-07 11:46:03 +02001838 if (!client_opts.ch_binds) {
romand82caf12024-03-05 14:21:39 +01001839 ERR(NULL, "Call-Home binds not set.");
Michal Vasko45e53ae2016-04-07 11:46:03 +02001840 return -1;
Michal Vasko3031aae2016-01-27 16:07:18 +01001841 }
1842
Michal Vasko5a2c5b92024-02-02 09:11:41 +01001843 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, &client_opts.ch_bind_lock, timeout,
1844 &host, &port, &idx);
Michal Vasko50456e82016-02-02 12:16:08 +01001845 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001846 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001847 return sock;
1848 }
1849
Michal Vasko9d4cca52022-09-07 11:19:57 +02001850 if (client_opts.ch_binds_aux[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001851 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
roman2eab4742023-06-06 10:00:26 +02001852 } else if (client_opts.ch_binds_aux[idx].ti == NC_TI_OPENSSL) {
Michal Vasko9d4cca52022-09-07 11:19:57 +02001853 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT,
1854 client_opts.ch_binds_aux[idx].hostname);
roman2eab4742023-06-06 10:00:26 +02001855 } else {
Michal Vaskofee717c2016-02-01 13:25:43 +01001856 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001857 *session = NULL;
1858 }
1859
1860 free(host);
1861
1862 if (!(*session)) {
1863 return -1;
1864 }
1865
1866 return 1;
1867}
1868
roman2eab4742023-06-06 10:00:26 +02001869#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001870
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001871API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001872nc_session_get_cpblts(const struct nc_session *session)
1873{
roman40672412023-05-04 11:10:22 +02001874 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vaskobdfb5242016-05-24 09:11:01 +02001875
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001876 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001877}
1878
1879API const char *
1880nc_session_cpblt(const struct nc_session *session, const char *capab)
1881{
1882 int i, len;
1883
roman40672412023-05-04 11:10:22 +02001884 NC_CHECK_ARG_RET(session, session, capab, NULL);
Michal Vaskobdfb5242016-05-24 09:11:01 +02001885
1886 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001887 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1888 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1889 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001890 }
1891 }
1892
1893 return NULL;
1894}
1895
Michal Vasko9cd26a82016-05-31 08:58:48 +02001896API int
1897nc_session_ntf_thread_running(const struct nc_session *session)
1898{
roman40672412023-05-04 11:10:22 +02001899 NC_CHECK_ARG_RET(session, session, 0);
1900
1901 if (session->side != NC_CLIENT) {
1902 ERRARG(NULL, "session");
Michal Vasko9cd26a82016-05-31 08:58:48 +02001903 return 0;
1904 }
1905
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02001906 return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running);
Michal Vasko9cd26a82016-05-31 08:58:48 +02001907}
1908
roman9075eab2023-09-14 10:07:26 +02001909API int
1910nc_client_init(void)
1911{
1912 int r;
1913
1914 if ((r = pthread_mutex_init(&client_opts.ch_bind_lock, NULL))) {
1915 ERR(NULL, "%s: failed to init bind lock(%s).", __func__, strerror(r));
1916 return -1;
1917 }
1918
1919 return 0;
1920}
1921
Michal Vaskob7558c52016-02-26 15:04:19 +01001922API void
1923nc_client_destroy(void)
1924{
roman9075eab2023-09-14 10:07:26 +02001925 pthread_mutex_destroy(&client_opts.ch_bind_lock);
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001926 nc_client_set_schema_searchpath(NULL);
roman2eab4742023-06-06 10:00:26 +02001927#ifdef NC_ENABLED_SSH_TLS
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001928 nc_client_ch_del_bind(NULL, 0, 0);
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001929 nc_client_ssh_destroy_opts();
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001930 nc_client_tls_destroy_opts();
roman2eab4742023-06-06 10:00:26 +02001931#endif /* NC_ENABLED_SSH_TLS */
Michal Vaskob7558c52016-02-26 15:04:19 +01001932}
1933
Michal Vasko77367452021-02-16 16:32:18 +01001934static NC_MSG_TYPE
1935recv_reply_check_msgid(struct nc_session *session, const struct lyd_node *envp, uint64_t msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001936{
Michal Vasko77367452021-02-16 16:32:18 +01001937 char *ptr;
1938 struct lyd_attr *attr;
1939 uint64_t cur_msgid;
1940
1941 assert(envp && !envp->schema);
1942
1943 /* find the message-id attribute */
1944 LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) {
1945 if (!strcmp(attr->name.name, "message-id")) {
1946 break;
1947 }
1948 }
1949
1950 if (!attr) {
Michal Vasko05532772021-06-03 12:12:38 +02001951 ERR(session, "Received a <rpc-reply> without a message-id.");
Michal Vasko77367452021-02-16 16:32:18 +01001952 return NC_MSG_REPLY_ERR_MSGID;
1953 }
1954
1955 cur_msgid = strtoul(attr->value, &ptr, 10);
1956 if (cur_msgid != msgid) {
Michal Vasko05532772021-06-03 12:12:38 +02001957 ERR(session, "Received a <rpc-reply> with an unexpected message-id %" PRIu64 " (expected %" PRIu64 ").",
1958 cur_msgid, msgid);
Michal Vasko77367452021-02-16 16:32:18 +01001959 return NC_MSG_REPLY_ERR_MSGID;
1960 }
1961
1962 return NC_MSG_REPLY;
1963}
1964
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001965/**
1966 * @brief Used to roughly estimate the type of the message, does not actually parse or verify it.
1967 *
1968 * @param[in] session NETCONF session used to send error messages.
1969 * @param[in] msg Message to check for type.
1970 * @return NC_MSG_REPLY If format roughly matches a rpc-reply;
1971 * @return NC_MSG_NOTIF If format roughly matches a notification;
1972 * @return NC_MSG_ERROR If format is malformed or unrecognized.
1973 */
1974static NC_MSG_TYPE
1975get_msg_type(struct nc_session *session, struct ly_in *msg)
1976{
1977 const char *str, *end;
1978
1979 str = ly_in_memory(msg, NULL);
1980
1981 while (*str) {
1982 /* Skip whitespaces */
1983 while (isspace(*str)) {
1984 str++;
1985 }
1986
1987 if (*str == '<') {
1988 str++;
1989 if (!strncmp(str, "!--", 3)) {
1990 /* Skip comments */
1991 end = "-->";
1992 str = strstr(str, end);
1993 } else if (!strncmp(str, "?xml", 4)) {
1994 /* Skip xml declaration */
1995 end = "?>";
1996 str = strstr(str, end);
1997 } else if (!strncmp(str, "rpc-reply", 9)) {
1998 return NC_MSG_REPLY;
1999 } else if (!strncmp(str, "notification", 12)) {
2000 return NC_MSG_NOTIF;
2001 } else {
2002 ERR(session, "Unknown xml element '%.10s'.", str);
2003 return NC_MSG_ERROR;
2004 }
2005 if (!str) {
2006 /* No matching ending tag found */
2007 ERR(session, "No matching ending tag '%s' found in xml message.", end);
2008 return NC_MSG_ERROR;
2009 }
2010 str += strlen(end);
2011 } else {
2012 /* Not a valid xml */
2013 ERR(session, "Unexpected character '%c' in xml message.", *str);
2014 return NC_MSG_ERROR;
2015 }
2016 }
2017
2018 /* Unexpected end of message */
2019 ERR(session, "Unexpected end of xml message.");
2020 return NC_MSG_ERROR;
2021}
2022
2023/**
2024 * @brief Function to receive either replies or notifications.
2025 *
2026 * @param[in] session NETCONF session from which this function receives messages.
2027 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite.
2028 * @param[in] expected Type of the message the caller desired.
2029 * @param[out] message If receiving a message succeeded this is the message, NULL otherwise.
2030 * @return NC_MSG_REPLY If a rpc-reply was received;
2031 * @return NC_MSG_NOTIF If a notification was received;
Michal Vaskofdba4a32022-01-05 12:13:53 +01002032 * @return NC_MSG_ERROR If any error occurred;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002033 * @return NC_MSG_WOULDBLOCK If the timeout was reached.
2034 */
2035static NC_MSG_TYPE
2036recv_msg(struct nc_session *session, int timeout, NC_MSG_TYPE expected, struct ly_in **message)
2037{
2038 struct nc_msg_cont **cont_ptr;
2039 struct ly_in *msg = NULL;
2040 struct nc_msg_cont *cont, *prev;
2041 NC_MSG_TYPE ret = NC_MSG_ERROR;
2042 int r;
2043
2044 *message = NULL;
2045
2046 /* MSGS LOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02002047 r = nc_session_client_msgs_lock(session, &timeout, __func__);
2048 if (!r) {
2049 ret = NC_MSG_WOULDBLOCK;
2050 goto cleanup;
2051 } else if (r == -1) {
2052 ret = NC_MSG_ERROR;
2053 goto cleanup;
2054 }
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002055
2056 /* Find the expected message in the buffer */
2057 prev = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02002058 for (cont = session->opts.client.msgs; cont && (cont->type != expected); cont = cont->next) {
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002059 prev = cont;
2060 }
2061
2062 if (cont) {
2063 /* Remove found message from buffer */
2064 if (prev) {
2065 prev->next = cont->next;
2066 } else {
2067 session->opts.client.msgs = cont->next;
2068 }
2069
2070 /* Use the buffer message */
2071 ret = cont->type;
2072 msg = cont->msg;
2073 free(cont);
Michal Vasko01130bd2021-08-26 11:47:38 +02002074 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002075 }
2076
2077 /* Read a message from the wire */
2078 r = nc_read_msg_poll_io(session, timeout, &msg);
2079 if (!r) {
2080 ret = NC_MSG_WOULDBLOCK;
Michal Vasko01130bd2021-08-26 11:47:38 +02002081 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002082 } else if (r == -1) {
2083 ret = NC_MSG_ERROR;
Michal Vasko01130bd2021-08-26 11:47:38 +02002084 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002085 }
2086
2087 /* Basic check to determine message type */
2088 ret = get_msg_type(session, msg);
2089 if (ret == NC_MSG_ERROR) {
Michal Vasko01130bd2021-08-26 11:47:38 +02002090 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002091 }
2092
2093 /* If received a message of different type store it in the buffer */
2094 if (ret != expected) {
2095 cont_ptr = &session->opts.client.msgs;
2096 while (*cont_ptr) {
2097 cont_ptr = &((*cont_ptr)->next);
2098 }
2099 *cont_ptr = malloc(sizeof **cont_ptr);
roman3a95bb22023-10-26 11:07:17 +02002100 NC_CHECK_ERRMEM_GOTO(!*cont_ptr, ret = NC_MSG_ERROR, cleanup_unlock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002101 (*cont_ptr)->msg = msg;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002102 msg = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02002103 (*cont_ptr)->type = ret;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002104 (*cont_ptr)->next = NULL;
2105 }
2106
Michal Vasko01130bd2021-08-26 11:47:38 +02002107cleanup_unlock:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002108 /* MSGS UNLOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02002109 nc_session_client_msgs_unlock(session, __func__);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002110
Michal Vasko01130bd2021-08-26 11:47:38 +02002111cleanup:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002112 if (ret == expected) {
2113 *message = msg;
2114 } else {
2115 ly_in_free(msg, 1);
2116 }
2117 return ret;
2118}
2119
Michal Vasko77367452021-02-16 16:32:18 +01002120static NC_MSG_TYPE
2121recv_reply(struct nc_session *session, int timeout, struct lyd_node *op, uint64_t msgid, struct lyd_node **envp)
2122{
Michal Vasko77367452021-02-16 16:32:18 +01002123 LY_ERR lyrc;
2124 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002125 NC_MSG_TYPE ret = NC_MSG_ERROR;
Michal Vaskob25c56f2024-03-27 15:45:56 +01002126 uint32_t temp_lo = LY_LOSTORE, *prev_lo;
Michal Vasko77367452021-02-16 16:32:18 +01002127
2128 assert(op && (op->schema->nodetype & (LYS_RPC | LYS_ACTION)));
2129
2130 *envp = NULL;
2131
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002132 /* Receive messages until a rpc-reply is found or a timeout or error reached */
2133 ret = recv_msg(session, timeout, NC_MSG_REPLY, &msg);
2134 if (ret != NC_MSG_REPLY) {
Michal Vasko77367452021-02-16 16:32:18 +01002135 goto cleanup;
2136 }
2137
2138 /* parse */
Michal Vaskob25c56f2024-03-27 15:45:56 +01002139 prev_lo = ly_temp_log_options(&temp_lo);
Michal Vasko77367452021-02-16 16:32:18 +01002140 lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL);
Michal Vaskob25c56f2024-03-27 15:45:56 +01002141 ly_temp_log_options(prev_lo);
2142
2143 if (*envp) {
2144 /* if the envelopes were parsed, check the message-id, even on error */
Michal Vasko77367452021-02-16 16:32:18 +01002145 ret = recv_reply_check_msgid(session, *envp, msgid);
2146 goto cleanup;
Michal Vaskob25c56f2024-03-27 15:45:56 +01002147 }
2148
2149 if (lyrc) {
2150 /* parsing error */
Michal Vasko58791da2024-02-26 13:52:59 +01002151 ERR(session, "Received an invalid message (%s).", ly_err_last(LYD_CTX(op))->msg);
Michal Vasko9a108052022-04-01 12:06:54 +02002152 lyd_free_tree(*envp);
2153 *envp = NULL;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002154 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002155 goto cleanup;
2156 }
2157
Michal Vasko77367452021-02-16 16:32:18 +01002158cleanup:
2159 ly_in_free(msg, 1);
2160 return ret;
2161}
2162
2163static int
2164recv_reply_dup_rpc(struct nc_session *session, struct nc_rpc *rpc, struct lyd_node **op)
2165{
Michal Vasko143aa142021-10-01 15:31:48 +02002166 LY_ERR lyrc = LY_SUCCESS;
Michal Vasko77367452021-02-16 16:32:18 +01002167 struct nc_rpc_act_generic *rpc_gen;
2168 struct ly_in *in;
2169 struct lyd_node *tree, *op2;
2170 const struct lys_module *mod;
Michal Vasko305faca2021-03-25 09:16:02 +01002171 const char *module_name = NULL, *rpc_name = NULL, *module_check = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002172
2173 switch (rpc->type) {
2174 case NC_RPC_ACT_GENERIC:
2175 rpc_gen = (struct nc_rpc_act_generic *)rpc;
2176 if (rpc_gen->has_data) {
2177 tree = rpc_gen->content.data;
2178
2179 /* find the operation node */
2180 lyrc = LY_EINVAL;
2181 LYD_TREE_DFS_BEGIN(tree, op2) {
2182 if (op2->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
2183 lyrc = lyd_dup_single(op2, NULL, 0, op);
2184 break;
2185 }
2186 LYD_TREE_DFS_END(tree, op2);
2187 }
2188 } else {
2189 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2190 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op2);
2191 ly_in_free(in, 0);
2192 if (lyrc) {
Michal Vasko9a108052022-04-01 12:06:54 +02002193 lyd_free_tree(tree);
Michal Vasko77367452021-02-16 16:32:18 +01002194 return -1;
2195 }
2196
2197 /* we want just the operation node */
2198 lyrc = lyd_dup_single(op2, NULL, 0, op);
2199
2200 lyd_free_tree(tree);
2201 }
2202 break;
2203 case NC_RPC_GETCONFIG:
2204 module_name = "ietf-netconf";
2205 rpc_name = "get-config";
2206 break;
2207 case NC_RPC_EDIT:
2208 module_name = "ietf-netconf";
2209 rpc_name = "edit-config";
2210 break;
2211 case NC_RPC_COPY:
2212 module_name = "ietf-netconf";
2213 rpc_name = "copy-config";
2214 break;
2215 case NC_RPC_DELETE:
2216 module_name = "ietf-netconf";
2217 rpc_name = "delete-config";
2218 break;
2219 case NC_RPC_LOCK:
2220 module_name = "ietf-netconf";
2221 rpc_name = "lock";
2222 break;
2223 case NC_RPC_UNLOCK:
2224 module_name = "ietf-netconf";
2225 rpc_name = "unlock";
2226 break;
2227 case NC_RPC_GET:
2228 module_name = "ietf-netconf";
2229 rpc_name = "get";
2230 break;
2231 case NC_RPC_KILL:
2232 module_name = "ietf-netconf";
2233 rpc_name = "kill-session";
2234 break;
2235 case NC_RPC_COMMIT:
2236 module_name = "ietf-netconf";
2237 rpc_name = "commit";
2238 break;
2239 case NC_RPC_DISCARD:
2240 module_name = "ietf-netconf";
2241 rpc_name = "discard-changes";
2242 break;
2243 case NC_RPC_CANCEL:
2244 module_name = "ietf-netconf";
2245 rpc_name = "cancel-commit";
2246 break;
2247 case NC_RPC_VALIDATE:
2248 module_name = "ietf-netconf";
2249 rpc_name = "validate";
2250 break;
2251 case NC_RPC_GETSCHEMA:
2252 module_name = "ietf-netconf-monitoring";
2253 rpc_name = "get-schema";
2254 break;
2255 case NC_RPC_SUBSCRIBE:
2256 module_name = "notifications";
Michal Vaskoeed893d2021-05-25 17:11:08 +02002257 rpc_name = "create-subscription";
Michal Vasko77367452021-02-16 16:32:18 +01002258 break;
2259 case NC_RPC_GETDATA:
2260 module_name = "ietf-netconf-nmda";
2261 rpc_name = "get-data";
2262 break;
2263 case NC_RPC_EDITDATA:
2264 module_name = "ietf-netconf-nmda";
2265 rpc_name = "edit-data";
2266 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002267 case NC_RPC_ESTABLISHSUB:
2268 module_name = "ietf-subscribed-notifications";
2269 rpc_name = "establish-subscription";
2270 break;
2271 case NC_RPC_MODIFYSUB:
2272 module_name = "ietf-subscribed-notifications";
2273 rpc_name = "modify-subscription";
2274 break;
2275 case NC_RPC_DELETESUB:
2276 module_name = "ietf-subscribed-notifications";
2277 rpc_name = "delete-subscription";
2278 break;
2279 case NC_RPC_KILLSUB:
2280 module_name = "ietf-subscribed-notifications";
2281 rpc_name = "kill-subscription";
2282 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002283 case NC_RPC_ESTABLISHPUSH:
2284 module_name = "ietf-subscribed-notifications";
2285 rpc_name = "establish-subscription";
2286 module_check = "ietf-yang-push";
2287 break;
2288 case NC_RPC_MODIFYPUSH:
2289 module_name = "ietf-subscribed-notifications";
2290 rpc_name = "modify-subscription";
2291 module_check = "ietf-yang-push";
2292 break;
2293 case NC_RPC_RESYNCSUB:
2294 module_name = "ietf-yang-push";
2295 rpc_name = "resync-subscription";
2296 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002297 case NC_RPC_UNKNOWN:
Michal Vasko77367452021-02-16 16:32:18 +01002298 lyrc = LY_EINT;
2299 break;
2300 }
2301
2302 if (module_name && rpc_name) {
2303 mod = ly_ctx_get_module_implemented(session->ctx, module_name);
2304 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002305 ERR(session, "Missing \"%s\" module in the context.", module_name);
Michal Vasko77367452021-02-16 16:32:18 +01002306 return -1;
2307 }
2308
2309 /* create the operation node */
2310 lyrc = lyd_new_inner(NULL, mod, rpc_name, 0, op);
2311 }
Michal Vasko305faca2021-03-25 09:16:02 +01002312 if (module_check) {
2313 if (!ly_ctx_get_module_implemented(session->ctx, module_check)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002314 ERR(session, "Missing \"%s\" module in the context.", module_check);
Michal Vasko305faca2021-03-25 09:16:02 +01002315 return -1;
2316 }
2317 }
Michal Vasko77367452021-02-16 16:32:18 +01002318
2319 if (lyrc) {
2320 return -1;
2321 }
2322 return 0;
2323}
2324
2325API NC_MSG_TYPE
2326nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct lyd_node **envp,
2327 struct lyd_node **op)
2328{
2329 NC_MSG_TYPE ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002330
roman40672412023-05-04 11:10:22 +02002331 NC_CHECK_ARG_RET(session, session, rpc, envp, op, NC_MSG_ERROR);
2332
2333 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002334 ERR(session, "Invalid session to receive RPC replies.");
Michal Vasko086311b2016-01-08 09:53:11 +01002335 return NC_MSG_ERROR;
2336 }
Michal Vasko77367452021-02-16 16:32:18 +01002337
2338 /* get a duplicate of the RPC node to append reply to */
2339 if (recv_reply_dup_rpc(session, rpc, op)) {
2340 return NC_MSG_ERROR;
Michal Vaskoeee99412016-11-21 10:19:43 +01002341 }
Michal Vasko086311b2016-01-08 09:53:11 +01002342
Michal Vasko77367452021-02-16 16:32:18 +01002343 /* receive a reply */
2344 ret = recv_reply(session, timeout, *op, msgid, envp);
Michal Vasko086311b2016-01-08 09:53:11 +01002345
Michal Vasko77367452021-02-16 16:32:18 +01002346 /* do not return the RPC copy on error or if the reply includes no data */
2347 if (((ret != NC_MSG_REPLY) && (ret != NC_MSG_REPLY_ERR_MSGID)) || !lyd_child(*op)) {
2348 lyd_free_tree(*op);
2349 *op = NULL;
2350 }
2351 return ret;
2352}
2353
2354static NC_MSG_TYPE
2355recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
2356{
Michal Vasko77367452021-02-16 16:32:18 +01002357 LY_ERR lyrc;
2358 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002359 NC_MSG_TYPE ret = NC_MSG_ERROR;
2360
2361 *op = NULL;
2362 *envp = NULL;
2363
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002364 /* Receive messages until a notification is found or a timeout or error reached */
2365 ret = recv_msg(session, timeout, NC_MSG_NOTIF, &msg);
2366 if (ret != NC_MSG_NOTIF) {
Michal Vasko77367452021-02-16 16:32:18 +01002367 goto cleanup;
2368 }
2369
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002370 /* Parse */
Michal Vasko77367452021-02-16 16:32:18 +01002371 lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op);
2372 if (!lyrc) {
Michal Vasko77367452021-02-16 16:32:18 +01002373 goto cleanup;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002374 } else {
Michal Vasko58791da2024-02-26 13:52:59 +01002375 ERR(session, "Received an invalid message (%s).", ly_err_last(session->ctx)->msg);
Michal Vasko9a108052022-04-01 12:06:54 +02002376 lyd_free_tree(*envp);
2377 *envp = NULL;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002378 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002379 goto cleanup;
2380 }
2381
Michal Vasko77367452021-02-16 16:32:18 +01002382cleanup:
2383 ly_in_free(msg, 1);
2384 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002385}
2386
2387API NC_MSG_TYPE
Michal Vasko77367452021-02-16 16:32:18 +01002388nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
Michal Vasko086311b2016-01-08 09:53:11 +01002389{
roman40672412023-05-04 11:10:22 +02002390 NC_CHECK_ARG_RET(session, session, envp, op, NC_MSG_ERROR);
2391
2392 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002393 ERR(session, "Invalid session to receive Notifications.");
Michal Vasko086311b2016-01-08 09:53:11 +01002394 return NC_MSG_ERROR;
2395 }
2396
Michal Vasko77367452021-02-16 16:32:18 +01002397 /* receive a notification */
2398 return recv_notif(session, timeout, envp, op);
Michal Vasko086311b2016-01-08 09:53:11 +01002399}
2400
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002401static void *
2402nc_recv_notif_thread(void *arg)
2403{
2404 struct nc_ntf_thread_arg *ntarg;
2405 struct nc_session *session;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002406 nc_notif_dispatch_clb notif_clb;
2407 void *user_data;
Michal Vasko743ff9f2022-10-20 10:03:13 +02002408
Michal Vaskoffb35e92022-10-20 09:07:25 +02002409 void (*free_data)(void *);
Michal Vasko77367452021-02-16 16:32:18 +01002410 struct lyd_node *envp, *op;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002411 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002412
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002413 /* detach ourselves */
Michal Vasko131120a2018-05-29 15:44:02 +02002414 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002415
2416 ntarg = (struct nc_ntf_thread_arg *)arg;
2417 session = ntarg->session;
2418 notif_clb = ntarg->notif_clb;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002419 user_data = ntarg->user_data;
2420 free_data = ntarg->free_data;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002421 free(ntarg);
2422
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002423 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
Michal Vasko77367452021-02-16 16:32:18 +01002424 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002425 if (msgtype == NC_MSG_NOTIF) {
Michal Vaskoffb35e92022-10-20 09:07:25 +02002426 notif_clb(session, envp, op, user_data);
Michal Vasko77367452021-02-16 16:32:18 +01002427 if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) {
2428 lyd_free_tree(envp);
Michal Vasko0c0239c2023-02-01 14:31:06 +01002429 lyd_free_all(op);
Michal Vaskof0537d82016-01-29 14:42:38 +01002430 break;
2431 }
Michal Vasko77367452021-02-16 16:32:18 +01002432 lyd_free_tree(envp);
Michal Vasko0c0239c2023-02-01 14:31:06 +01002433 lyd_free_all(op);
Michal Vaskoce326052018-01-04 10:32:03 +01002434 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002435 /* quit this thread once the session is broken */
2436 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002437 }
2438
2439 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2440 }
2441
Michal Vasko05532772021-06-03 12:12:38 +02002442 VRB(session, "Notification thread exit.");
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002443 ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count);
Michal Vaskoffb35e92022-10-20 09:07:25 +02002444 if (free_data) {
2445 free_data(user_data);
2446 }
2447
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002448 return NULL;
2449}
2450
2451API int
Michal Vaskoffb35e92022-10-20 09:07:25 +02002452nc_recv_notif_dispatch(struct nc_session *session, nc_notif_dispatch_clb notif_clb)
2453{
2454 return nc_recv_notif_dispatch_data(session, notif_clb, NULL, NULL);
2455}
2456
2457API int
2458nc_recv_notif_dispatch_data(struct nc_session *session, nc_notif_dispatch_clb notif_clb, void *user_data,
2459 void (*free_data)(void *))
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002460{
2461 struct nc_ntf_thread_arg *ntarg;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002462 pthread_t tid;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002463 int ret;
2464
roman40672412023-05-04 11:10:22 +02002465 NC_CHECK_ARG_RET(session, session, notif_clb, -1);
2466
2467 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002468 ERR(session, "Invalid session to receive Notifications.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002469 return -1;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002470 }
2471
2472 ntarg = malloc(sizeof *ntarg);
roman3a95bb22023-10-26 11:07:17 +02002473 NC_CHECK_ERRMEM_RET(!ntarg, -1);
2474
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002475 ntarg->session = session;
2476 ntarg->notif_clb = notif_clb;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002477 ntarg->user_data = user_data;
2478 ntarg->free_data = free_data;
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002479 ATOMIC_INC_RELAXED(session->opts.client.ntf_thread_count);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002480
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002481 /* just so that nc_recv_notif_thread() does not immediately exit */
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002482 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 1);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002483
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002484 ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002485 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02002486 ERR(session, "Failed to create a new thread (%s).", strerror(errno));
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002487 free(ntarg);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002488 if (ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count) == 1) {
2489 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
2490 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002491 return -1;
2492 }
2493
2494 return 0;
2495}
2496
Michal Vasko77367452021-02-16 16:32:18 +01002497static const char *
2498nc_wd2str(NC_WD_MODE wd)
2499{
2500 switch (wd) {
2501 case NC_WD_ALL:
2502 return "report-all";
2503 case NC_WD_ALL_TAG:
2504 return "report-all-tagged";
2505 case NC_WD_TRIM:
2506 return "trim";
2507 case NC_WD_EXPLICIT:
2508 return "explicit";
2509 default:
2510 break;
2511 }
2512
2513 return NULL;
2514}
2515
Michal Vasko086311b2016-01-08 09:53:11 +01002516API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002517nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002518{
2519 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002520 int dofree = 1;
Michal Vasko77367452021-02-16 16:32:18 +01002521 struct ly_in *in;
Michal Vasko90e8e692016-07-13 12:27:57 +02002522 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002523 struct nc_rpc_getconfig *rpc_gc;
2524 struct nc_rpc_edit *rpc_e;
2525 struct nc_rpc_copy *rpc_cp;
2526 struct nc_rpc_delete *rpc_del;
2527 struct nc_rpc_lock *rpc_lock;
2528 struct nc_rpc_get *rpc_g;
2529 struct nc_rpc_kill *rpc_k;
2530 struct nc_rpc_commit *rpc_com;
2531 struct nc_rpc_cancel *rpc_can;
2532 struct nc_rpc_validate *rpc_val;
2533 struct nc_rpc_getschema *rpc_gs;
2534 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002535 struct nc_rpc_getdata *rpc_getd;
2536 struct nc_rpc_editdata *rpc_editd;
Michal Vasko96f247a2021-03-15 13:32:10 +01002537 struct nc_rpc_establishsub *rpc_estsub;
2538 struct nc_rpc_modifysub *rpc_modsub;
2539 struct nc_rpc_deletesub *rpc_delsub;
2540 struct nc_rpc_killsub *rpc_killsub;
Michal Vasko305faca2021-03-25 09:16:02 +01002541 struct nc_rpc_establishpush *rpc_estpush;
2542 struct nc_rpc_modifypush *rpc_modpush;
2543 struct nc_rpc_resyncsub *rpc_resyncsub;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002544 struct lyd_node *data = NULL, *node, *cont;
Michal Vasko305faca2021-03-25 09:16:02 +01002545 const struct lys_module *mod = NULL, *mod2 = NULL, *ietfncwd;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002546 LY_ERR lyrc = 0;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002547 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002548 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002549 uint64_t cur_msgid;
2550
roman40672412023-05-04 11:10:22 +02002551 NC_CHECK_ARG_RET(session, session, rpc, msgid, NC_MSG_ERROR);
2552
2553 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002554 ERR(session, "Invalid session to send RPCs.");
Michal Vasko086311b2016-01-08 09:53:11 +01002555 return NC_MSG_ERROR;
2556 }
2557
Michal Vaskoc1171a42019-11-05 12:06:46 +01002558 switch (rpc->type) {
2559 case NC_RPC_ACT_GENERIC:
2560 /* checked when parsing */
2561 break;
2562 case NC_RPC_GETCONFIG:
2563 case NC_RPC_EDIT:
2564 case NC_RPC_COPY:
2565 case NC_RPC_DELETE:
2566 case NC_RPC_LOCK:
2567 case NC_RPC_UNLOCK:
2568 case NC_RPC_GET:
2569 case NC_RPC_KILL:
2570 case NC_RPC_COMMIT:
2571 case NC_RPC_DISCARD:
2572 case NC_RPC_CANCEL:
2573 case NC_RPC_VALIDATE:
Michal Vasko77367452021-02-16 16:32:18 +01002574 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002575 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002576 ERR(session, "Missing \"ietf-netconf\" module in the context.");
Michal Vasko086311b2016-01-08 09:53:11 +01002577 return NC_MSG_ERROR;
2578 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002579 break;
2580 case NC_RPC_GETSCHEMA:
Michal Vasko77367452021-02-16 16:32:18 +01002581 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002582 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002583 ERR(session, "Missing \"ietf-netconf-monitoring\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002584 return NC_MSG_ERROR;
2585 }
2586 break;
2587 case NC_RPC_SUBSCRIBE:
Michal Vasko77367452021-02-16 16:32:18 +01002588 mod = ly_ctx_get_module_implemented(session->ctx, "notifications");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002589 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002590 ERR(session, "Missing \"notifications\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002591 return NC_MSG_ERROR;
2592 }
2593 break;
2594 case NC_RPC_GETDATA:
2595 case NC_RPC_EDITDATA:
Michal Vasko77367452021-02-16 16:32:18 +01002596 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-nmda");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002597 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002598 ERR(session, "Missing \"ietf-netconf-nmda\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002599 return NC_MSG_ERROR;
2600 }
2601 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002602 case NC_RPC_ESTABLISHSUB:
2603 case NC_RPC_MODIFYSUB:
2604 case NC_RPC_DELETESUB:
2605 case NC_RPC_KILLSUB:
2606 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2607 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002608 ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context.");
Michal Vasko96f247a2021-03-15 13:32:10 +01002609 return NC_MSG_ERROR;
2610 }
2611 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002612 case NC_RPC_ESTABLISHPUSH:
2613 case NC_RPC_MODIFYPUSH:
2614 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2615 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002616 ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002617 return NC_MSG_ERROR;
2618 }
2619 mod2 = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2620 if (!mod2) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002621 ERR(session, "Missing \"ietf-yang-push\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002622 return NC_MSG_ERROR;
2623 }
2624 break;
2625 case NC_RPC_RESYNCSUB:
2626 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2627 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002628 ERR(session, "Missing \"ietf-yang-push\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002629 return NC_MSG_ERROR;
2630 }
2631 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002632 case NC_RPC_UNKNOWN:
2633 ERRINT;
2634 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002635 }
2636
Michal Vaskoab9deb62021-05-27 11:37:00 +02002637#define CHECK_LYRC_BREAK(func_call) if ((lyrc = func_call)) break;
2638
Michal Vasko086311b2016-01-08 09:53:11 +01002639 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002640 case NC_RPC_ACT_GENERIC:
2641 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002642
2643 if (rpc_gen->has_data) {
2644 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002645 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002646 } else {
Michal Vasko77367452021-02-16 16:32:18 +01002647 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2648 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &data, NULL);
2649 ly_in_free(in, 0);
2650 if (lyrc) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002651 break;
Michal Vaskoeec410f2017-11-24 09:14:55 +01002652 }
Michal Vasko086311b2016-01-08 09:53:11 +01002653 }
2654 break;
2655
2656 case NC_RPC_GETCONFIG:
2657 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2658
Michal Vaskoab9deb62021-05-27 11:37:00 +02002659 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-config", 0, &data));
2660 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
2661 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_gc->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002662 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002663 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002664 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_gc->filter, LYD_ANYDATA_XML, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002665 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002666 } else {
Michal Vasko58791da2024-02-26 13:52:59 +01002667 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002668 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2669 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_gc->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002670 }
2671 }
2672
2673 if (rpc_gc->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002674 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002675 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002676 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002677 lyrc = LY_ENOTFOUND;
2678 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002679 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002680 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 +01002681 }
2682 break;
2683
2684 case NC_RPC_EDIT:
2685 rpc_e = (struct nc_rpc_edit *)rpc;
2686
Michal Vaskoab9deb62021-05-27 11:37:00 +02002687 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-config", 0, &data));
2688 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2689 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_e->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002690
2691 if (rpc_e->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002692 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 +01002693 }
Michal Vasko086311b2016-01-08 09:53:11 +01002694 if (rpc_e->test_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002695 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 +01002696 }
Michal Vasko086311b2016-01-08 09:53:11 +01002697 if (rpc_e->error_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002698 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 +01002699 }
Michal Vasko7793bc62016-09-16 11:58:41 +02002700 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002701 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 +01002702 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002703 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_e->edit_cont, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002704 }
2705 break;
2706
2707 case NC_RPC_COPY:
2708 rpc_cp = (struct nc_rpc_copy *)rpc;
2709
Michal Vaskoab9deb62021-05-27 11:37:00 +02002710 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "copy-config", 0, &data));
2711 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002712 if (rpc_cp->url_trg) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002713 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_trg, 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(cont, mod, ncds2str[rpc_cp->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002716 }
2717
Michal Vaskoab9deb62021-05-27 11:37:00 +02002718 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002719 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002720 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002721 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 +01002722 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002723 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002724 }
2725 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002726 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002727 }
2728
2729 if (rpc_cp->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002730 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002731 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002732 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002733 lyrc = LY_ENOTFOUND;
2734 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002735 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002736 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 +01002737 }
2738 break;
2739
2740 case NC_RPC_DELETE:
2741 rpc_del = (struct nc_rpc_delete *)rpc;
2742
Michal Vaskoab9deb62021-05-27 11:37:00 +02002743 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-config", 0, &data));
2744 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002745 if (rpc_del->url) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002746 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_del->url, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002747 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002748 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_del->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002749 }
2750 break;
2751
2752 case NC_RPC_LOCK:
2753 rpc_lock = (struct nc_rpc_lock *)rpc;
2754
Michal Vaskoab9deb62021-05-27 11:37:00 +02002755 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "lock", 0, &data));
2756 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2757 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002758 break;
2759
2760 case NC_RPC_UNLOCK:
2761 rpc_lock = (struct nc_rpc_lock *)rpc;
2762
Michal Vaskoab9deb62021-05-27 11:37:00 +02002763 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "unlock", 0, &data));
2764 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2765 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002766 break;
2767
2768 case NC_RPC_GET:
2769 rpc_g = (struct nc_rpc_get *)rpc;
2770
Michal Vaskoab9deb62021-05-27 11:37:00 +02002771 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002772 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002773 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002774 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_g->filter, LYD_ANYDATA_XML, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002775 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002776 } else {
Michal Vasko58791da2024-02-26 13:52:59 +01002777 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002778 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2779 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_g->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002780 }
2781 }
2782
2783 if (rpc_g->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002784 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002785 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002786 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002787 lyrc = LY_ENOTFOUND;
2788 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002789 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002790 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 +01002791 }
2792 break;
2793
2794 case NC_RPC_KILL:
2795 rpc_k = (struct nc_rpc_kill *)rpc;
2796
Michal Vaskoab9deb62021-05-27 11:37:00 +02002797 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-session", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002798 sprintf(str, "%u", rpc_k->sid);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002799 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "session-id", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002800 break;
2801
2802 case NC_RPC_COMMIT:
2803 rpc_com = (struct nc_rpc_commit *)rpc;
2804
Michal Vaskoab9deb62021-05-27 11:37:00 +02002805 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002806 if (rpc_com->confirmed) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002807 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirmed", NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002808 }
2809
2810 if (rpc_com->confirm_timeout) {
2811 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002812 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirm-timeout", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002813 }
Michal Vasko086311b2016-01-08 09:53:11 +01002814 if (rpc_com->persist) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002815 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist", rpc_com->persist, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002816 }
Michal Vasko086311b2016-01-08 09:53:11 +01002817 if (rpc_com->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002818 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_com->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002819 }
2820 break;
2821
2822 case NC_RPC_DISCARD:
Michal Vaskoab9deb62021-05-27 11:37:00 +02002823 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "discard-changes", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002824 break;
2825
2826 case NC_RPC_CANCEL:
2827 rpc_can = (struct nc_rpc_cancel *)rpc;
2828
Michal Vaskoab9deb62021-05-27 11:37:00 +02002829 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "cancel-commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002830 if (rpc_can->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002831 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_can->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002832 }
2833 break;
2834
2835 case NC_RPC_VALIDATE:
2836 rpc_val = (struct nc_rpc_validate *)rpc;
2837
Michal Vaskoab9deb62021-05-27 11:37:00 +02002838 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "validate", 0, &data));
2839 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002840 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002841 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002842 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 +01002843 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002844 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_val->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002845 }
2846 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002847 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_val->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002848 }
2849 break;
2850
2851 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002852 rpc_gs = (struct nc_rpc_getschema *)rpc;
2853
Michal Vaskoab9deb62021-05-27 11:37:00 +02002854 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-schema", 0, &data));
2855 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "identifier", rpc_gs->identifier, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002856 if (rpc_gs->version) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002857 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "version", rpc_gs->version, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002858 }
2859 if (rpc_gs->format) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002860 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "format", rpc_gs->format, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002861 }
2862 break;
2863
2864 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002865 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2866
Michal Vaskoab9deb62021-05-27 11:37:00 +02002867 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "create-subscription", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002868 if (rpc_sub->stream) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002869 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_sub->stream, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002870 }
2871
2872 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002873 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002874 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_sub->filter, LYD_ANYDATA_XML, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002875 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002876 } else {
Michal Vasko58791da2024-02-26 13:52:59 +01002877 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node));
Michal Vaskoab9deb62021-05-27 11:37:00 +02002878 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2879 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_sub->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002880 }
2881 }
Michal Vasko086311b2016-01-08 09:53:11 +01002882 if (rpc_sub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002883 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "startTime", rpc_sub->start, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002884 }
Michal Vasko086311b2016-01-08 09:53:11 +01002885 if (rpc_sub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002886 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stopTime", rpc_sub->stop, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002887 }
2888 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002889
2890 case NC_RPC_GETDATA:
2891 rpc_getd = (struct nc_rpc_getdata *)rpc;
2892
Michal Vaskoab9deb62021-05-27 11:37:00 +02002893 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-data", 0, &data));
2894 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_getd->datastore, 0, NULL));
2895
Michal Vaskoc1171a42019-11-05 12:06:46 +01002896 if (rpc_getd->filter) {
2897 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002898 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 +01002899 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002900 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "xpath-filter", rpc_getd->filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002901 }
2902 }
2903 if (rpc_getd->config_filter) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002904 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "config-filter", rpc_getd->config_filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002905 }
2906 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002907 CHECK_LYRC_BREAK(lyd_new_term(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" :
2908 "origin-filter", rpc_getd->origin_filter[i], 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002909 }
2910 if (rpc_getd->max_depth) {
2911 sprintf(str, "%u", rpc_getd->max_depth);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002912 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "max-depth", str, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002913 }
2914 if (rpc_getd->with_origin) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002915 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-origin", NULL, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002916 }
2917
2918 if (rpc_getd->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002919 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
Michal Vaskoab9deb62021-05-27 11:37:00 +02002920 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 +01002921 }
2922 break;
2923
2924 case NC_RPC_EDITDATA:
2925 rpc_editd = (struct nc_rpc_editdata *)rpc;
2926
Michal Vaskoab9deb62021-05-27 11:37:00 +02002927 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-data", 0, &data));
2928 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_editd->datastore, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002929
2930 if (rpc_editd->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002931 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op], 0,
2932 NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002933 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002934 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002935 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 +01002936 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002937 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_editd->edit_cont, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002938 }
2939 break;
2940
Michal Vasko96f247a2021-03-15 13:32:10 +01002941 case NC_RPC_ESTABLISHSUB:
2942 rpc_estsub = (struct nc_rpc_establishsub *)rpc;
2943
Michal Vaskoab9deb62021-05-27 11:37:00 +02002944 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002945
2946 if (rpc_estsub->filter) {
2947 if (!rpc_estsub->filter[0] || (rpc_estsub->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002948 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_estsub->filter, LYD_ANYDATA_XML,
Michal Vaskoab9deb62021-05-27 11:37:00 +02002949 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002950 } else if (rpc_estsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002951 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002952 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002953 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002954 }
2955 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002956 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_estsub->stream, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002957
2958 if (rpc_estsub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002959 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "replay-start-time", rpc_estsub->start, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002960 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002961 if (rpc_estsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002962 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002963 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002964 if (rpc_estsub->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002965 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estsub->encoding, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002966 }
2967 break;
2968
2969 case NC_RPC_MODIFYSUB:
2970 rpc_modsub = (struct nc_rpc_modifysub *)rpc;
2971
Michal Vaskoab9deb62021-05-27 11:37:00 +02002972 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002973
2974 sprintf(str, "%u", rpc_modsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002975 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002976
2977 if (rpc_modsub->filter) {
2978 if (!rpc_modsub->filter[0] || (rpc_modsub->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01002979 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_modsub->filter, LYD_ANYDATA_XML,
Michal Vaskoab9deb62021-05-27 11:37:00 +02002980 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002981 } else if (rpc_modsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002982 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002983 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002984 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002985 }
2986 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002987 if (rpc_modsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002988 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002989 }
2990 break;
2991
2992 case NC_RPC_DELETESUB:
2993 rpc_delsub = (struct nc_rpc_deletesub *)rpc;
2994
Michal Vaskoab9deb62021-05-27 11:37:00 +02002995 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002996
2997 sprintf(str, "%u", rpc_delsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002998 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002999 break;
3000
3001 case NC_RPC_KILLSUB:
3002 rpc_killsub = (struct nc_rpc_killsub *)rpc;
3003
Michal Vaskoab9deb62021-05-27 11:37:00 +02003004 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01003005
3006 sprintf(str, "%u", rpc_killsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003007 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01003008 break;
3009
Michal Vasko305faca2021-03-25 09:16:02 +01003010 case NC_RPC_ESTABLISHPUSH:
3011 rpc_estpush = (struct nc_rpc_establishpush *)rpc;
3012
Michal Vaskoab9deb62021-05-27 11:37:00 +02003013 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
3014 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_estpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003015
3016 if (rpc_estpush->filter) {
3017 if (!rpc_estpush->filter[0] || (rpc_estpush->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01003018 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_estpush->filter,
Michal Vaskoab9deb62021-05-27 11:37:00 +02003019 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003020 } else if (rpc_estpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003021 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003022 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003023 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003024 }
3025 }
3026
3027 if (rpc_estpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003028 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003029 }
Michal Vasko305faca2021-03-25 09:16:02 +01003030 if (rpc_estpush->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003031 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estpush->encoding, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003032 }
3033
3034 if (rpc_estpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003035 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003036 sprintf(str, "%" PRIu32, rpc_estpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003037 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003038 if (rpc_estpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003039 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_estpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003040 }
3041 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003042 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003043 if (rpc_estpush->dampening_period) {
3044 sprintf(str, "%" PRIu32, rpc_estpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003045 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003046 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02003047 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "sync-on-start", rpc_estpush->sync_on_start ? "true" : "false", 0,
3048 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003049 if (rpc_estpush->excluded_change) {
3050 for (i = 0; rpc_estpush->excluded_change[i]; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003051 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "excluded-change", rpc_estpush->excluded_change[i], 0,
3052 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003053 }
3054 }
3055 }
3056 break;
3057
3058 case NC_RPC_MODIFYPUSH:
3059 rpc_modpush = (struct nc_rpc_modifypush *)rpc;
3060
Michal Vaskoab9deb62021-05-27 11:37:00 +02003061 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01003062
3063 sprintf(str, "%u", rpc_modpush->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003064 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
3065 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_modpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003066
3067 if (rpc_modpush->filter) {
3068 if (!rpc_modpush->filter[0] || (rpc_modpush->filter[0] == '<')) {
Michal Vasko58791da2024-02-26 13:52:59 +01003069 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_modpush->filter,
Michal Vaskoab9deb62021-05-27 11:37:00 +02003070 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003071 } else if (rpc_modpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003072 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003073 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003074 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003075 }
3076 }
Michal Vasko305faca2021-03-25 09:16:02 +01003077 if (rpc_modpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003078 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003079 }
3080
3081 if (rpc_modpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003082 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003083 sprintf(str, "%" PRIu32, rpc_modpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003084 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003085 if (rpc_modpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003086 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_modpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003087 }
3088 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003089 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003090 if (rpc_modpush->dampening_period) {
3091 sprintf(str, "%" PRIu32, rpc_modpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003092 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003093 }
3094 }
3095 break;
3096
3097 case NC_RPC_RESYNCSUB:
3098 rpc_resyncsub = (struct nc_rpc_resyncsub *)rpc;
3099
Michal Vaskoab9deb62021-05-27 11:37:00 +02003100 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "resync-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01003101 sprintf(str, "%u", rpc_resyncsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003102 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003103 break;
3104
Michal Vasko96f247a2021-03-15 13:32:10 +01003105 case NC_RPC_UNKNOWN:
Michal Vasko7f1c78b2016-01-19 09:52:14 +01003106 ERRINT;
3107 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01003108 }
3109
Michal Vaskoab9deb62021-05-27 11:37:00 +02003110#undef CHECK_LYRC_BREAK
3111
3112 if (lyrc) {
Michal Vasko05532772021-06-03 12:12:38 +02003113 ERR(session, "Failed to create RPC, perhaps a required feature is disabled.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02003114 lyd_free_tree(data);
Michal Vasko131120a2018-05-29 15:44:02 +02003115 return NC_MSG_ERROR;
3116 }
3117
Michal Vasko131120a2018-05-29 15:44:02 +02003118 /* send RPC, store its message ID */
3119 r = nc_send_msg_io(session, timeout, data);
3120 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01003121
Radek Krejcib4b19062018-02-07 16:33:06 +01003122 if (dofree) {
Michal Vasko77367452021-02-16 16:32:18 +01003123 lyd_free_tree(data);
Radek Krejcib4b19062018-02-07 16:33:06 +01003124 }
Michal Vasko086311b2016-01-08 09:53:11 +01003125
Michal Vasko131120a2018-05-29 15:44:02 +02003126 if (r == NC_MSG_RPC) {
3127 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01003128 }
Michal Vasko131120a2018-05-29 15:44:02 +02003129 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01003130}
Michal Vaskode2946c2017-01-12 12:19:26 +01003131
3132API void
3133nc_client_session_set_not_strict(struct nc_session *session)
3134{
3135 if (session->side != NC_CLIENT) {
roman40672412023-05-04 11:10:22 +02003136 ERRARG(NULL, "session");
Michal Vaskode2946c2017-01-12 12:19:26 +01003137 return;
3138 }
3139
3140 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
3141}