blob: 305456028b117d8a68f02841874f8a798389eae5 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
Radek Krejcifd5b6682017-06-13 15:52:53 +020015#define _GNU_SOURCE
Michal Vaskobbf52c82020-04-07 13:08:50 +020016
17#ifdef __linux__
18# include <sys/syscall.h>
19#endif
20
Michal Vaskob83a3fa2021-05-26 09:53:42 +020021#include <arpa/inet.h>
Michal Vasko086311b2016-01-08 09:53:11 +010022#include <assert.h>
tadeas-vintrlik54f142a2021-08-23 10:36:18 +020023#include <ctype.h>
Michal Vasko086311b2016-01-08 09:53:11 +010024#include <errno.h>
25#include <fcntl.h>
26#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010027#include <netinet/in.h>
Michal Vasko83ad17e2019-01-30 10:11:37 +010028#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020029#include <poll.h>
Michal Vasko086311b2016-01-08 09:53:11 +010030#include <pthread.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020031#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010032#include <stdlib.h>
33#include <string.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020034#include <sys/select.h>
Michal Vasko086311b2016-01-08 09:53:11 +010035#include <sys/socket.h>
36#include <sys/stat.h>
37#include <sys/types.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020038#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010039#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010040
41#include <libyang/libyang.h>
42
Michal Vasko9e8ac262020-04-07 13:06:45 +020043#include "compat.h"
Michal Vasko086311b2016-01-08 09:53:11 +010044#include "libnetconf.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010045#include "messages_client.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020046#include "session_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010047
Michal Vasko8a4e1462020-05-07 11:32:31 +020048#include "../modules/ietf_netconf@2013-09-29_yang.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020049#include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h"
Michal Vasko8a4e1462020-05-07 11:32:31 +020050
Michal Vasko80ef5d22016-01-18 09:21:02 +010051static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
52
Radek Krejci62aa0642017-05-25 16:33:49 +020053#ifdef NC_ENABLED_SSH
54int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
55char *sshauth_password(const char *username, const char *hostname, void *priv);
56char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020057char *sshauth_privkey_passphrase(const char *privkey_path, void *priv);
Radek Krejci62aa0642017-05-25 16:33:49 +020058#endif /* NC_ENABLED_SSH */
59
60static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
61static pthread_key_t nc_client_context_key;
62#ifdef __linux__
63static struct nc_client_context context_main = {
Michal Vaskoe49a15f2019-05-27 14:18:36 +020064 .opts.ka = {
65 .enabled = 1,
66 .idle_time = 1,
67 .max_probes = 10,
68 .probe_interval = 5
69 },
Radek Krejci62aa0642017-05-25 16:33:49 +020070#ifdef NC_ENABLED_SSH
71 .ssh_opts = {
72 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
73 .auth_hostkey_check = sshauth_hostkey_check,
74 .auth_password = sshauth_password,
75 .auth_interactive = sshauth_interactive,
76 .auth_privkey_passphrase = sshauth_privkey_passphrase
77 },
78 .ssh_ch_opts = {
79 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
80 .auth_hostkey_check = sshauth_hostkey_check,
81 .auth_password = sshauth_password,
82 .auth_interactive = sshauth_interactive,
83 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020084 },
Radek Krejci62aa0642017-05-25 16:33:49 +020085#endif /* NC_ENABLED_SSH */
86 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020087 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020088};
89#endif
90
91static void
92nc_client_context_free(void *ptr)
93{
94 struct nc_client_context *c = (struct nc_client_context *)ptr;
95
Radek Krejci5cebc6b2017-05-26 13:24:38 +020096 if (--(c->refcount)) {
97 /* still used */
98 return;
99 }
100
Radek Krejci62aa0642017-05-25 16:33:49 +0200101#ifdef __linux__
102 /* in __linux__ we use static memory in the main thread,
103 * so this check is for programs terminating the main()
104 * function by pthread_exit() :)
105 */
106 if (c != &context_main)
107#endif
108 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200109 /* for the main thread the same is done in nc_client_destroy() */
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400110 free(c->opts.schema_searchpath);
111
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200112#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400113 int i;
114 for (i = 0; i < c->opts.ch_bind_count; ++i) {
115 close(c->opts.ch_binds[i].sock);
116 free((char *)c->opts.ch_binds[i].address);
117 }
118 free(c->opts.ch_binds);
119 c->opts.ch_binds = NULL;
120 c->opts.ch_bind_count = 0;
Radek Krejci62aa0642017-05-25 16:33:49 +0200121#endif
122#ifdef NC_ENABLED_SSH
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400123 _nc_client_ssh_destroy_opts(&c->ssh_opts);
124 _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200125#endif
126#ifdef NC_ENABLED_TLS
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400127 _nc_client_tls_destroy_opts(&c->tls_opts);
128 _nc_client_tls_destroy_opts(&c->tls_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200129#endif
130 free(c);
131 }
132}
133
134static void
135nc_client_context_createkey(void)
136{
137 int r;
138
139 /* initiate */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200140 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {}
Radek Krejci62aa0642017-05-25 16:33:49 +0200141 pthread_setspecific(nc_client_context_key, NULL);
142}
143
144struct nc_client_context *
145nc_client_context_location(void)
146{
147 struct nc_client_context *e;
148
149 pthread_once(&nc_client_context_once, nc_client_context_createkey);
150 e = pthread_getspecific(nc_client_context_key);
151 if (!e) {
152 /* prepare ly_err storage */
153#ifdef __linux__
154 if (getpid() == syscall(SYS_gettid)) {
155 /* main thread - use global variable instead of thread-specific variable. */
156 e = &context_main;
157 } else
158#endif /* __linux__ */
159 {
160 e = calloc(1, sizeof *e);
161 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200163#ifdef NC_ENABLED_SSH
164 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
165 e->ssh_opts.auth_pref[0].value = 3;
166 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
167 e->ssh_opts.auth_pref[1].value = 2;
168 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
169 e->ssh_opts.auth_pref[2].value = 1;
170 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
171 e->ssh_opts.auth_password = sshauth_password;
172 e->ssh_opts.auth_interactive = sshauth_interactive;
173 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
174
175 /* callhome settings are the same except the inverted auth methods preferences */
176 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
177 e->ssh_ch_opts.auth_pref[0].value = 1;
178 e->ssh_ch_opts.auth_pref[1].value = 2;
179 e->ssh_ch_opts.auth_pref[2].value = 3;
180#endif /* NC_ENABLED_SSH */
181 }
182 pthread_setspecific(nc_client_context_key, e);
183 }
184
185 return e;
186}
187
188#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100189
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200190API void *
191nc_client_get_thread_context(void)
192{
193 return nc_client_context_location();
194}
195
196API void
197nc_client_set_thread_context(void *context)
198{
199 struct nc_client_context *old, *new;
200
201 if (!context) {
202 ERRARG(context);
203 return;
204 }
205
206 new = (struct nc_client_context *)context;
207 old = nc_client_context_location();
208 if (old == new) {
209 /* nothing to change */
210 return;
211 }
212
213 /* replace old by new, increase reference counter in the newly set context */
214 nc_client_context_free(old);
215 new->refcount++;
216 pthread_setspecific(nc_client_context_key, new);
217}
218
Radek Krejcifd5b6682017-06-13 15:52:53 +0200219int
220nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
221{
222 /* assign context (dicionary needed for handshake) */
223 if (!ctx) {
Michal Vasko77367452021-02-16 16:32:18 +0100224 if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200225 return EXIT_FAILURE;
226 }
227
228 /* user path must be first, the first path is used to store schemas retreived via get-schema */
229 if (client_opts.schema_searchpath) {
230 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200231 } else if (!access(NC_YANG_DIR, F_OK)) {
232 ly_ctx_set_searchdir(ctx, NC_YANG_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200233 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200234
235 /* set callback for getting schemas, if provided */
236 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
237 } else {
238 session->flags |= NC_SESSION_SHAREDCTX;
239 }
240
241 session->ctx = ctx;
242
243 return EXIT_SUCCESS;
244}
245
Michal Vasko086311b2016-01-08 09:53:11 +0100246API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100247nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100248{
Michal Vasko3031aae2016-01-27 16:07:18 +0100249 if (client_opts.schema_searchpath) {
250 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100251 }
Michal Vasko086311b2016-01-08 09:53:11 +0100252
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100253 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100254 client_opts.schema_searchpath = strdup(path);
255 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100256 ERRMEM;
257 return 1;
258 }
259 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100260 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100261 }
262
263 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100264}
265
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100266API const char *
267nc_client_get_schema_searchpath(void)
268{
269 return client_opts.schema_searchpath;
270}
271
Radek Krejcifd5b6682017-06-13 15:52:53 +0200272API int
273nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100274{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200275 client_opts.schema_clb = clb;
276 if (clb) {
277 client_opts.schema_clb_data = user_data;
278 } else {
279 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100280 }
281
282 return 0;
283}
284
Radek Krejcifd5b6682017-06-13 15:52:53 +0200285API ly_module_imp_clb
286nc_client_get_schema_callback(void **user_data)
287{
288 if (user_data) {
289 (*user_data) = client_opts.schema_clb_data;
290 }
291 return client_opts.schema_clb;
292}
293
Radek Krejci65ef6d52018-08-16 16:35:02 +0200294struct schema_info {
295 char *name;
296 char *revision;
297 struct {
298 char *name;
299 char *revision;
300 } *submodules;
Michal Vasko77367452021-02-16 16:32:18 +0100301 char **features;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200302 int implemented;
303};
304
305struct clb_data_s {
306 void *user_data;
307 ly_module_imp_clb user_clb;
308 struct schema_info *schemas;
309 struct nc_session *session;
310 int has_get_schema;
311};
312
313static char *
314retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200315 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100316{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200317 char *localfile = NULL;
318 FILE *f;
319 long length, l;
320 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100321
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200322 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200323 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
324 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200325 return NULL;
326 }
327 if (localfile) {
Michal Vasko05532772021-06-03 12:12:38 +0200328 VRB(clb_data->session, "Reading schema from localfile \"%s\".", localfile);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200329 f = fopen(localfile, "r");
330 if (!f) {
Michal Vasko05532772021-06-03 12:12:38 +0200331 ERR(clb_data->session, "Unable to open \"%s\" file to get schema (%s).", localfile, strerror(errno));
Radek Krejci65ef6d52018-08-16 16:35:02 +0200332 free(localfile);
333 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100334 }
Michal Vasko086311b2016-01-08 09:53:11 +0100335
Radek Krejci65ef6d52018-08-16 16:35:02 +0200336 fseek(f, 0, SEEK_END);
337 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200338 if (length < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200339 ERR(clb_data->session, "Unable to get size of schema file \"%s\".", localfile);
Radek Krejci3f499a62018-08-17 10:16:57 +0200340 free(localfile);
341 fclose(f);
342 return NULL;
343 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200344 fseek(f, 0, SEEK_SET);
345
346 model_data = malloc(length + 1);
347 if (!model_data) {
348 ERRMEM;
349 } else if ((l = fread(model_data, 1, length, f)) != length) {
Michal Vasko05532772021-06-03 12:12:38 +0200350 ERR(clb_data->session, "Reading schema from \"%s\" failed (%d bytes read, but %d expected).", localfile, l,
351 length);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200352 free(model_data);
353 model_data = NULL;
354 } else {
355 /* terminating NULL byte */
356 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100357 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200358 fclose(f);
359 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100360 }
361
Radek Krejci65ef6d52018-08-16 16:35:02 +0200362 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100363}
364
365static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200366retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200367 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100368{
Michal Vasko086311b2016-01-08 09:53:11 +0100369 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100370 struct lyd_node *envp = NULL, *op = NULL;
371 struct lyd_node_any *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100372 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100373 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200374 char *localfile = NULL;
375 FILE *f;
376 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100377
Michal Vasko05532772021-06-03 12:12:38 +0200378 VRB(clb_data->session, "Reading schema from server via get-schema.");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200379 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100380
Radek Krejci65ef6d52018-08-16 16:35:02 +0200381 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100382 usleep(1000);
383 }
384 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200385 ERR(clb_data->session, "Failed to send the <get-schema> RPC.");
Michal Vasko086311b2016-01-08 09:53:11 +0100386 nc_rpc_free(rpc);
387 return NULL;
388 }
389
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200390 do {
Michal Vasko77367452021-02-16 16:32:18 +0100391 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200392 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100393 nc_rpc_free(rpc);
394 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko05532772021-06-03 12:12:38 +0200395 ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired.");
Michal Vasko77367452021-02-16 16:32:18 +0100396 goto cleanup;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200397 } else if ((msg == NC_MSG_ERROR) || !op) {
Michal Vasko05532772021-06-03 12:12:38 +0200398 ERR(clb_data->session, "Failed to receive a reply to <get-schema>.");
Michal Vasko77367452021-02-16 16:32:18 +0100399 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100400 }
401
Michal Vasko77367452021-02-16 16:32:18 +0100402 if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) {
Michal Vasko05532772021-06-03 12:12:38 +0200403 ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC.");
Michal Vasko77367452021-02-16 16:32:18 +0100404 goto cleanup;
Michal Vaskob2583f12016-05-12 11:40:23 +0200405 }
Michal Vasko77367452021-02-16 16:32:18 +0100406 get_schema_data = (struct lyd_node_any *)lyd_child(op);
Radek Krejci539efb62016-08-24 15:05:16 +0200407 switch (get_schema_data->value_type) {
Radek Krejci539efb62016-08-24 15:05:16 +0200408 case LYD_ANYDATA_STRING:
Michal Vasko77367452021-02-16 16:32:18 +0100409 case LYD_ANYDATA_XML:
Michal Vaskob2583f12016-05-12 11:40:23 +0200410 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Michal Vasko77367452021-02-16 16:32:18 +0100413 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Radek Krejci539efb62016-08-24 15:05:16 +0200414 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200415 case LYD_ANYDATA_JSON:
Michal Vaskod838d292018-08-15 11:39:05 +0200416 case LYD_ANYDATA_LYB:
Radek Krejci03438ec2016-09-21 14:12:26 +0200417 ERRINT;
Michal Vasko77367452021-02-16 16:32:18 +0100418 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200419 }
Michal Vasko086311b2016-01-08 09:53:11 +0100420
Radek Krejci488b0702018-08-29 14:47:15 +0200421 if (model_data && !model_data[0]) {
422 /* empty data */
423 free(model_data);
424 model_data = NULL;
425 }
426
Radek Krejcifd5b6682017-06-13 15:52:53 +0200427 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200428 if (model_data) {
429 *format = LYS_IN_YANG;
430 if (client_opts.schema_searchpath) {
Michal Vasko77367452021-02-16 16:32:18 +0100431 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name, rev ? "@" : "",
432 rev ? rev : "") == -1) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200433 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100434 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200435 f = fopen(localfile, "w");
436 if (!f) {
Michal Vasko05532772021-06-03 12:12:38 +0200437 WRN(clb_data->session, "Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200438 localfile, strerror(errno));
Radek Krejci65ef6d52018-08-16 16:35:02 +0200439 } else {
440 fputs(model_data, f);
441 fclose(f);
442 }
443 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100444 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200445 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200446 }
447
Michal Vasko77367452021-02-16 16:32:18 +0100448cleanup:
449 lyd_free_tree(envp);
450 lyd_free_tree(op);
Michal Vasko086311b2016-01-08 09:53:11 +0100451 return model_data;
452}
453
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200454static void
455free_with_user_data(void *data, void *user_data)
Jan Kundrát35972df2018-09-06 19:00:01 +0200456{
457 free(data);
458 (void)user_data;
459}
460
Michal Vasko77367452021-02-16 16:32:18 +0100461static LY_ERR
Radek Krejci65ef6d52018-08-16 16:35:02 +0200462retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
Michal Vasko77367452021-02-16 16:32:18 +0100463 void *user_data, LYS_INFORMAT *format, const char **module_data,
464 void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200465{
466 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
467 unsigned int u, v, match = 1;
468 const char *name = NULL, *rev = NULL;
469 char *model_data = NULL;
470
471 /* get and check the final name and revision of the schema to be retrieved */
472 if (!mod_rev || !mod_rev[0]) {
473 /* newest revision requested - get the newest revision from the list of available modules on server */
474 match = 0;
475 for (u = 0; clb_data->schemas[u].name; ++u) {
476 if (strcmp(mod_name, clb_data->schemas[u].name)) {
477 continue;
478 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200479 if (!match || (strcmp(mod_rev, clb_data->schemas[u].revision) > 0)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200480 mod_rev = clb_data->schemas[u].revision;
481 }
482 match = u + 1;
483 }
484 if (!match) {
Michal Vasko1d2665c2021-02-12 09:28:44 +0100485 /* valid situation if we are retrieving YANG 1.1 schema and have only capabilities for now
486 * (when loading ietf-datastore for ietf-yang-library) */
Michal Vasko05532772021-06-03 12:12:38 +0200487 VRB(clb_data->session, "Unable to identify revision of the schema \"%s\" from "
488 "the available server side information.", mod_name);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200489 }
490 }
491 if (submod_name) {
492 name = submod_name;
493 if (sub_rev) {
494 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200495 } else if (match) {
496 if (!clb_data->schemas[match - 1].submodules) {
Michal Vasko05532772021-06-03 12:12:38 +0200497 VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", "
498 "in schema \"%s\", from the available server side information.", submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200499 } else {
500 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
501 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
502 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
503 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200504 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200505 if (!rev) {
Michal Vasko05532772021-06-03 12:12:38 +0200506 ERR(clb_data->session, "Requested submodule \"%s\" is not known for schema \"%s\" on server side.",
507 submod_name, mod_name);
Michal Vasko77367452021-02-16 16:32:18 +0100508 return LY_ENOTFOUND;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200509 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200510 }
511 }
512 } else {
513 name = mod_name;
514 rev = mod_rev;
515 }
516
Michal Vasko05532772021-06-03 12:12:38 +0200517 VRB(clb_data->session, "Retrieving data for schema \"%s\", revision \"%s\".", name, rev ? rev : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200518
519 if (match) {
520 /* we have enough information to avoid communication with server and try to get
521 * the schema locally */
522
523 /* 1. try to get data locally */
524 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
525
526 /* 2. try to use <get-schema> */
527 if (!model_data && clb_data->has_get_schema) {
528 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
529 }
530 } else {
531 /* we are unsure which revision of the schema we should load, so first try to get
532 * the newest revision from the server via get-schema and only if the server does not
533 * implement get-schema, try to load the newest revision locally. This is imperfect
534 * solution, but there are situation when a client does not know what revision is
535 * actually implemented by the server. */
536
537 /* 1. try to use <get-schema> */
538 if (clb_data->has_get_schema) {
539 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
540 }
541
542 /* 2. try to get data locally */
543 if (!model_data) {
544 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
545 }
546 }
547
548 /* 3. try to use user callback */
549 if (!model_data && clb_data->user_clb) {
Michal Vasko05532772021-06-03 12:12:38 +0200550 VRB(clb_data->session, "Reading schema via user callback.");
Michal Vasko77367452021-02-16 16:32:18 +0100551 clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format,
552 (const char **)&model_data, free_module_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200553 }
554
Jan Kundrát35972df2018-09-06 19:00:01 +0200555 *free_module_data = free_with_user_data;
Michal Vasko77367452021-02-16 16:32:18 +0100556 *module_data = model_data;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200557 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200558}
559
Michal Vaskoceae0152018-02-14 16:03:59 +0100560static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200561nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Michal Vasko77367452021-02-16 16:32:18 +0100562 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100563{
564 int ret = 0;
565 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200566 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200567 LYS_INFORMAT format;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200568
569 void (*free_module_data)(void *, void *) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200570 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100571
Radek Krejci65ef6d52018-08-16 16:35:02 +0200572 *mod = NULL;
573 if (revision) {
Michal Vasko77367452021-02-16 16:32:18 +0100574 *mod = ly_ctx_get_module(session->ctx, name, revision);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200575 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100576 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200577 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100578 /* make the present module implemented */
Michal Vasko77367452021-02-16 16:32:18 +0100579 if (lys_set_implemented(*mod, NULL)) {
Michal Vasko05532772021-06-03 12:12:38 +0200580 ERR(session, "Failed to implement model \"%s\".", (*mod)->name);
Michal Vaskoceae0152018-02-14 16:03:59 +0100581 ret = -1;
582 }
583 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200584 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100585 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200586 clb_data.has_get_schema = has_get_schema;
587 clb_data.schemas = schemas;
588 clb_data.session = session;
589 clb_data.user_clb = user_clb;
590 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100591
592 /* clear all the errors and just collect them for now */
593 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200594 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100595
Radek Krejci65ef6d52018-08-16 16:35:02 +0200596 /* get module data */
Michal Vasko77367452021-02-16 16:32:18 +0100597 retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &module_data, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100598
Radek Krejci65ef6d52018-08-16 16:35:02 +0200599 if (module_data) {
600 /* parse the schema */
601 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100602
Michal Vasko029c5372021-07-09 10:54:21 +0200603 lys_parse_mem(session->ctx, module_data, format, mod);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200604 if (*free_module_data) {
Michal Vasko77367452021-02-16 16:32:18 +0100605 (*free_module_data)((char *)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100606 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100607
Radek Krejci65ef6d52018-08-16 16:35:02 +0200608 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
609 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100610
Michal Vaskodbf7c272018-02-19 09:07:59 +0100611 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200612 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100613 if (!(*mod)) {
614 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
Michal Vasko77367452021-02-16 16:32:18 +0100615 ly_err_print(session->ctx, eitem);
Michal Vaskoceae0152018-02-14 16:03:59 +0100616 }
617 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200618 } else {
619 /* print only warnings */
620 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
621 if (eitem->level == LY_LLWRN) {
Michal Vasko77367452021-02-16 16:32:18 +0100622 ly_err_print(session->ctx, eitem);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200623 }
624 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100625 }
626
Michal Vaskodbf7c272018-02-19 09:07:59 +0100627 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100628 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100629 }
630
631 return ret;
632}
633
Radek Krejci65ef6d52018-08-16 16:35:02 +0200634static void
635free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200636{
Michal Vasko77367452021-02-16 16:32:18 +0100637 uint32_t u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200638
Radek Krejci65ef6d52018-08-16 16:35:02 +0200639 if (!list) {
640 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200641 }
642
Radek Krejci65ef6d52018-08-16 16:35:02 +0200643 for (u = 0; list[u].name; ++u) {
644 free(list[u].name);
645 free(list[u].revision);
Michal Vasko77367452021-02-16 16:32:18 +0100646 if (list[u].features) {
647 for (v = 0; list[u].features[v]; ++v) {
648 free(list[u].features[v]);
649 }
650 free(list[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200651 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200652 if (list[u].submodules) {
653 for (v = 0; list[u].submodules[v].name; ++v) {
654 free(list[u].submodules[v].name);
655 free(list[u].submodules[v].revision);
656 }
657 free(list[u].submodules);
658 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200659 }
660 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200661}
662
Radek Krejci235d8cb2018-08-17 14:04:32 +0200663static int
664build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200665{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200666 struct nc_rpc *rpc = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100667 struct lyd_node *op = NULL, *envp = NULL;
668 struct lyd_node_any *data;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200669 NC_MSG_TYPE msg;
670 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200671 struct ly_set *modules = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100672 uint32_t u, v, submodules_count, feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200673 struct lyd_node *iter, *child;
674 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200675 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200676
677 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200678 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
Michal Vasko77367452021-02-16 16:32:18 +0100679 rpc = nc_rpc_getdata("ietf-datastores:operational", "/ietf-yang-library:*", "false", NULL, 0, 0, 0, 0, 0,
680 NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200681 } else {
Michal Vasko77367452021-02-16 16:32:18 +0100682 rpc = nc_rpc_getdata("ietf-datastores:operational",
683 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", "false", NULL, 0, 0, 0, 0,
684 0, NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200685 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200686 if (!rpc) {
687 goto cleanup;
688 }
689
690 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
691 usleep(1000);
692 }
693 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200694 WRN(session, "Failed to send request for yang-library data.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200695 goto cleanup;
696 }
697
698 do {
Michal Vasko77367452021-02-16 16:32:18 +0100699 lyd_free_tree(envp);
700 lyd_free_tree(op);
701
702 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200703 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200704 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko40528752021-06-21 15:41:51 +0200705 WRN(session, "Timeout for receiving reply to a <get-data> yang-library data expired.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200706 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100707 } else if (msg == NC_MSG_ERROR) {
Michal Vasko40528752021-06-21 15:41:51 +0200708 WRN(session, "Failed to receive a reply to <get-data> of yang-library data.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200709 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100710 } else if (!op || !lyd_child(op) || strcmp(lyd_child(op)->schema->name, "data")) {
Michal Vasko40528752021-06-21 15:41:51 +0200711 WRN(session, "Unexpected reply without data to a yang-library <get-data> RPC.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200712 goto cleanup;
713 }
714
Michal Vasko77367452021-02-16 16:32:18 +0100715 data = (struct lyd_node_any *)lyd_child(op);
716 if (data->value_type != LYD_ANYDATA_DATATREE) {
Michal Vasko40528752021-06-21 15:41:51 +0200717 WRN(session, "Unexpected data in reply to a yang-library <get-data> RPC.");
718 goto cleanup;
719 } else if (!data->value.tree) {
720 WRN(session, "No data in reply to a yang-library <get-data> RPC.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200721 goto cleanup;
722 }
723
Michal Vasko77367452021-02-16 16:32:18 +0100724 if (lyd_find_xpath(data->value.tree, "/ietf-yang-library:modules-state/module", &modules)) {
Michal Vasko40528752021-06-21 15:41:51 +0200725 WRN(session, "No module information in reply to a yang-library <get-data> RPC.");
Radek Krejci2d088832018-08-21 13:40:14 +0200726 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200727 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200728
Michal Vasko77367452021-02-16 16:32:18 +0100729 (*result) = calloc(modules->count + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200730 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200731 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200732 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200733 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200734 }
735
Michal Vasko77367452021-02-16 16:32:18 +0100736 for (u = 0; u < modules->count; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200737 submodules_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100738 feature_count = 0;
739 mod = ((struct lyd_node *)modules->dnodes[u])->schema->module;
740 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
Michal Vasko5d9d5972021-06-09 14:17:01 +0200741 if (!iter->schema || (iter->schema->module != mod)) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200742 /* ignore node from other schemas (augments) */
743 continue;
744 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200745 if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200746 /* ignore empty nodes */
747 continue;
748 }
749 if (!strcmp(iter->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200750 (*result)[u].name = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200751 } else if (!strcmp(iter->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200752 (*result)[u].revision = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200753 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200754 (*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200755 } else if (!strcmp(iter->schema->name, "feature")) {
Michal Vasko77367452021-02-16 16:32:18 +0100756 (*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features);
757 if (!(*result)[u].features) {
758 ERRMEM;
759 free_schema_info(*result);
760 *result = NULL;
761 ret = EXIT_FAILURE;
762 goto cleanup;
763 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200764 (*result)[u].features[feature_count] = strdup(lyd_get_value(iter));
Michal Vasko77367452021-02-16 16:32:18 +0100765 (*result)[u].features[feature_count + 1] = NULL;
766 ++feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200767 } else if (!strcmp(iter->schema->name, "submodule")) {
768 submodules_count++;
769 }
770 }
771
772 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200773 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
774 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200775 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200776 free_schema_info(*result);
777 *result = NULL;
778 ret = EXIT_FAILURE;
779 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200780 } else {
781 v = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100782 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
783 mod = modules->dnodes[u]->schema->module;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200784 if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) {
Michal Vasko77367452021-02-16 16:32:18 +0100785 LY_LIST_FOR(lyd_child(iter), child) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200786 if (mod != child->schema->module) {
787 continue;
788 } else if (!strcmp(child->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200789 (*result)[u].submodules[v].name = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200790 } else if (!strcmp(child->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200791 (*result)[u].submodules[v].revision = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200792 }
793 }
794 }
795 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200796 }
797 }
798 }
799
Radek Krejcifd5b6682017-06-13 15:52:53 +0200800cleanup:
801 nc_rpc_free(rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100802 lyd_free_tree(envp);
803 lyd_free_tree(op);
804 ly_set_free(modules, NULL);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200805
Radek Krejci235d8cb2018-08-17 14:04:32 +0200806 if (session->status != NC_STATUS_RUNNING) {
Michal Vasko05532772021-06-03 12:12:38 +0200807 /* something bad happened, discard the session */
808 ERR(session, "Invalid session, discarding.");
Radek Krejci235d8cb2018-08-17 14:04:32 +0200809 ret = EXIT_FAILURE;
810 }
811
812 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200813}
814
Radek Krejci235d8cb2018-08-17 14:04:32 +0200815static int
816build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200817{
Michal Vasko77367452021-02-16 16:32:18 +0100818 uint32_t u, v, feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200819 char *module_cpblt, *ptr, *ptr2;
820
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200821 for (u = 0; cpblts[u]; ++u) {}
Radek Krejci235d8cb2018-08-17 14:04:32 +0200822 (*result) = calloc(u + 1, sizeof **result);
823 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200824 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200825 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200826 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200827
828 for (u = v = 0; cpblts[u]; ++u) {
829 module_cpblt = strstr(cpblts[u], "module=");
830 /* this capability requires a module */
831 if (!module_cpblt) {
832 continue;
833 }
834
835 /* get module's name */
836 ptr = (char *)module_cpblt + 7;
837 ptr2 = strchr(ptr, '&');
838 if (!ptr2) {
839 ptr2 = ptr + strlen(ptr);
840 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200841 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200842
843 /* get module's revision */
844 ptr = strstr(module_cpblt, "revision=");
845 if (ptr) {
846 ptr += 9;
847 ptr2 = strchr(ptr, '&');
848 if (!ptr2) {
849 ptr2 = ptr + strlen(ptr);
850 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200851 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200852 }
853
854 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200855 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200856
857 /* get module's features */
858 ptr = strstr(module_cpblt, "features=");
859 if (ptr) {
860 ptr += 9;
Michal Vasko77367452021-02-16 16:32:18 +0100861 feature_count = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200862 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
863 if (*ptr == ',') {
Michal Vasko77367452021-02-16 16:32:18 +0100864 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
865 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
866 (*result)[v].features[feature_count + 1] = NULL;
867 ++feature_count;
868
Radek Krejci65ef6d52018-08-16 16:35:02 +0200869 ptr2 = ptr + 1;
870 }
871 }
872 /* the last one */
Michal Vasko77367452021-02-16 16:32:18 +0100873 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
874 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
875 (*result)[v].features[feature_count + 1] = NULL;
876 ++feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200877 }
878 ++v;
879 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200880
881 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200882}
883
884static int
Michal Vasko77367452021-02-16 16:32:18 +0100885nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data,
886 int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200887{
888 int ret = EXIT_FAILURE;
Michal Vasko77367452021-02-16 16:32:18 +0100889 struct lys_module *mod;
890 uint32_t u;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200891
892 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200893 /* skip import-only modules */
894 if (!modules[u].implemented) {
895 continue;
896 }
897
Radek Krejci65ef6d52018-08-16 16:35:02 +0200898 /* we can continue even if it fails */
899 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
900
901 if (!mod) {
902 if (session->status != NC_STATUS_RUNNING) {
903 /* something bad heppened, discard the session */
Michal Vasko05532772021-06-03 12:12:38 +0200904 ERR(session, "Invalid session, discarding.");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200905 goto cleanup;
906 }
907
908 /* all loading ways failed, the schema will be ignored in the received data */
Michal Vasko05532772021-06-03 12:12:38 +0200909 WRN(session, "Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ?
910 modules[u].revision : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200911 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
912 } else {
Michal Vasko77367452021-02-16 16:32:18 +0100913 /* set the features */
914 lys_set_implemented(mod, (const char **)modules[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200915 }
Michal Vasko60f66602017-10-17 13:52:18 +0200916 }
917
Michal Vasko77367452021-02-16 16:32:18 +0100918 /* success */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200919 ret = EXIT_SUCCESS;
920
921cleanup:
Radek Krejcifd5b6682017-06-13 15:52:53 +0200922 return ret;
923}
924
Radek Krejci65ef6d52018-08-16 16:35:02 +0200925static int
Michal Vasko77367452021-02-16 16:32:18 +0100926nc_ctx_fill_ietf_netconf(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb,
927 void *user_data, int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200928{
Michal Vasko77367452021-02-16 16:32:18 +0100929 uint32_t u;
930 struct lys_module *ietfnc;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200931
Michal Vasko77367452021-02-16 16:32:18 +0100932 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200933 if (!ietfnc) {
934 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200935 if (!ietfnc) {
Michal Vasko029c5372021-07-09 10:54:21 +0200936 lys_parse_mem(session->ctx, ietf_netconf_2013_09_29_yang, LYS_IN_YANG, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200937 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200938 }
939 if (!ietfnc) {
Michal Vasko05532772021-06-03 12:12:38 +0200940 ERR(session, "Loading base NETCONF schema failed.");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200941 return 1;
942 }
943
944 /* set supported capabilities from ietf-netconf */
945 for (u = 0; modules[u].name; ++u) {
946 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
947 continue;
948 }
949
Michal Vasko77367452021-02-16 16:32:18 +0100950 lys_set_implemented(ietfnc, (const char **)modules[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200951 }
952
953 return 0;
954}
955
Michal Vasko086311b2016-01-08 09:53:11 +0100956int
957nc_ctx_check_and_fill(struct nc_session *session)
958{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200959 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100960 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100961 void *old_data = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100962 struct lys_module *mod = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200963 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200964 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100965
Michal Vasko2e6defd2016-10-07 15:48:15 +0200966 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100967
Radek Krejci65ef6d52018-08-16 16:35:02 +0200968 /* 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 +0200969 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200970
Radek Krejci65ef6d52018-08-16 16:35:02 +0200971 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
972 * schemas only to those present on the server side */
Michal Vasko77367452021-02-16 16:32:18 +0100973 ly_ctx_set_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200974
975 /* our callback is set later with appropriate data */
976 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
977
978 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200979 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200980 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200981 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200982 if (yanglib_support) {
983 break;
984 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +0100985 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200986 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200987 if (get_schema_support) {
988 break;
989 }
Michal Vasko086311b2016-01-08 09:53:11 +0100990 }
991 }
Michal Vasko31dd4c52020-04-17 10:29:44 +0200992 if (get_schema_support) {
Michal Vasko05532772021-06-03 12:12:38 +0200993 VRB(session, "Capability for <get-schema> support found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +0200994 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200995 VRB(session, "Capability for <get-schema> support not found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +0200996 }
997 if (yanglib_support) {
Michal Vasko05532772021-06-03 12:12:38 +0200998 VRB(session, "Capability for yang-library support found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +0200999 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001000 VRB(session, "Capability for yang-library support not found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +02001001 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001002
1003 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001004 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
Michal Vasko05532772021-06-03 12:12:38 +02001005 ERR(session, "Unable to get server's schema information from the <hello>'s capabilities.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001006 goto cleanup;
1007 }
1008
Michal Vasko086311b2016-01-08 09:53:11 +01001009 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Michal Vasko77367452021-02-16 16:32:18 +01001010 if (get_schema_support && lys_parse_mem(session->ctx, ietf_netconf_monitoring_2010_10_04_yang, LYS_IN_YANG, NULL)) {
Michal Vasko05532772021-06-03 12:12:38 +02001011 WRN(session, "Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Michal Vasko8a4e1462020-05-07 11:32:31 +02001012 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001013 }
1014
1015 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001016 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001017 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001018 }
1019
Radek Krejci65ef6d52018-08-16 16:35:02 +02001020 /* get correct version of ietf-yang-library into context */
1021 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001022 /* use get schema to get server's ietf-yang-library */
1023 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1024 if (!revision) {
Michal Vasko05532772021-06-03 12:12:38 +02001025 WRN(session, "Loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.");
1026 WRN(session, "Unable to automatically use <get-schema>.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001027 yanglib_support = 0;
1028 } else {
1029 revision = strndup(&revision[9], 10);
Michal Vasko0d877f92020-04-02 13:52:43 +02001030 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data,
1031 get_schema_support, &mod)) {
Michal Vasko05532772021-06-03 12:12:38 +02001032 WRN(session, "Loading NETCONF ietf-yang-library schema failed, unable to use it to learn all "
1033 "the supported modules.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001034 yanglib_support = 0;
1035 }
Michal Vasko0d877f92020-04-02 13:52:43 +02001036 if (strcmp(revision, "2019-01-04") >= 0) {
1037 /* we also need ietf-datastores to be implemented */
1038 if (nc_ctx_load_module(session, "ietf-datastores", NULL, server_modules, old_clb, old_data,
1039 get_schema_support, &mod)) {
Michal Vasko05532772021-06-03 12:12:38 +02001040 WRN(session, "Loading NETCONF ietf-datastores schema failed, unable to use yang-library "
1041 "to learn all the supported modules.");
Michal Vasko0d877f92020-04-02 13:52:43 +02001042 yanglib_support = 0;
1043 }
1044 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001045 free(revision);
Michal Vasko77367452021-02-16 16:32:18 +01001046
1047 /* ietf-netconf-nmda is needed to issue get-data */
1048 if (nc_ctx_load_module(session, "ietf-netconf-nmda", NULL, server_modules, old_clb, old_data,
1049 get_schema_support, &mod)) {
Michal Vasko05532772021-06-03 12:12:38 +02001050 WRN(session, "Loading NETCONF ietf-netconf-nmda schema failed, unable to use get-data to retrieve "
1051 "yang-library data.");
Michal Vasko77367452021-02-16 16:32:18 +01001052 yanglib_support = 0;
1053 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001054 }
1055 }
1056
Radek Krejci65ef6d52018-08-16 16:35:02 +02001057 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001058 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001059 if (build_schema_info_yl(session, &sm)) {
1060 goto cleanup;
1061 } else if (!sm) {
Michal Vasko05532772021-06-03 12:12:38 +02001062 VRB(session, "Trying to use capabilities instead of ietf-yang-library data.");
Radek Krejcif0633792018-08-20 15:12:09 +02001063 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001064 /* prefer yang-library information, currently we have it from capabilities used for getting correct
1065 * yang-library schema */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001066 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001067 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001068 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001069 }
Michal Vaskoef578332016-01-25 13:20:09 +01001070
Radek Krejci65ef6d52018-08-16 16:35:02 +02001071 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1072 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001073 }
1074
Radek Krejcifd5b6682017-06-13 15:52:53 +02001075 /* succsess */
1076 ret = 0;
1077
Michal Vaskoeee99412016-11-21 10:19:43 +01001078 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Michal Vasko05532772021-06-03 12:12:38 +02001079 WRN(session, "Some models failed to be loaded, any data from these models (and any other unknown) will "
1080 "be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +01001081 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001082
1083cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001084 free_schema_info(server_modules);
1085
Radek Krejcifd5b6682017-06-13 15:52:53 +02001086 /* set user callback back */
1087 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vasko77367452021-02-16 16:32:18 +01001088 ly_ctx_unset_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001089
Michal Vaskoef578332016-01-25 13:20:09 +01001090 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001091}
1092
1093API struct nc_session *
1094nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1095{
Michal Vaskod083db62016-01-19 10:31:29 +01001096 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001097
Michal Vasko45e53ae2016-04-07 11:46:03 +02001098 if (fdin < 0) {
1099 ERRARG("fdin");
1100 return NULL;
1101 } else if (fdout < 0) {
1102 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001103 return NULL;
1104 }
1105
1106 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001107 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001108 if (!session) {
1109 ERRMEM;
1110 return NULL;
1111 }
1112 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001113
1114 /* transport specific data */
1115 session->ti_type = NC_TI_FD;
1116 session->ti.fd.in = fdin;
1117 session->ti.fd.out = fdout;
1118
Robin Jarry4e38e292019-10-15 17:14:14 +02001119 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1120 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001121 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001122 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001123
1124 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001125 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001126 goto fail;
1127 }
1128 session->status = NC_STATUS_RUNNING;
1129
Michal Vaskoef578332016-01-25 13:20:09 +01001130 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001131 goto fail;
1132 }
1133
1134 return session;
1135
1136fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001137 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001138 return NULL;
1139}
1140
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001141API struct nc_session *
1142nc_connect_unix(const char *address, struct ly_ctx *ctx)
1143{
1144 struct nc_session *session = NULL;
1145 struct sockaddr_un sun;
1146 const struct passwd *pw;
1147 char *username;
1148 int sock = -1;
1149
1150 if (address == NULL) {
1151 ERRARG("address");
1152 return NULL;
1153 }
1154
1155 pw = getpwuid(geteuid());
1156 if (pw == NULL) {
Michal Vasko05532772021-06-03 12:12:38 +02001157 ERR(NULL, "Failed to find username for euid=%u.\n", geteuid());
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001158 goto fail;
1159 }
1160
1161 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1162 if (sock < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001163 ERR(NULL, "Failed to create socket (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001164 goto fail;
1165 }
1166
1167 memset(&sun, 0, sizeof(sun));
1168 sun.sun_family = AF_UNIX;
1169 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1170
1171 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001172 ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001173 goto fail;
1174 }
1175
1176 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001177 ERR(NULL, "fcntl failed (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001178 goto fail;
1179 }
1180
1181 /* prepare session structure */
1182 session = nc_new_session(NC_CLIENT, 0);
1183 if (!session) {
1184 ERRMEM;
1185 goto fail;
1186 }
1187 session->status = NC_STATUS_STARTING;
1188
1189 /* transport specific data */
1190 session->ti_type = NC_TI_UNIX;
1191 session->ti.unixsock.sock = sock;
1192 sock = -1; /* do not close sock in fail label anymore */
1193
Robin Jarry4e38e292019-10-15 17:14:14 +02001194 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1195 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001196 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001197 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001198
Michal Vasko77367452021-02-16 16:32:18 +01001199 lydict_insert(ctx, address, 0, &session->path);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001200
1201 username = strdup(pw->pw_name);
1202 if (username == NULL) {
1203 ERRMEM;
1204 goto fail;
1205 }
Michal Vasko77367452021-02-16 16:32:18 +01001206 lydict_insert_zc(ctx, username, &session->username);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001207
1208 /* NETCONF handshake */
1209 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1210 goto fail;
1211 }
1212 session->status = NC_STATUS_RUNNING;
1213
1214 if (nc_ctx_check_and_fill(session) == -1) {
1215 goto fail;
1216 }
1217
1218 return session;
1219
1220fail:
1221 nc_session_free(session, NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001222 if (sock >= 0) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001223 close(sock);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001224 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001225 return NULL;
1226}
1227
Frank Rimpler9f838b02018-07-25 06:44:03 +00001228/*
1229 Helper for a non-blocking connect (which is required because of the locking
1230 concept for e.g. call home settings). For more details see nc_sock_connect().
1231 */
1232static int
Michal Vasko5e8d0192019-06-24 19:19:49 +02001233_non_blocking_connect(int timeout, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001234{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001235 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001236 int sock = -1;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001237 fd_set wset;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001238 struct timeval ts;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001239 socklen_t len = sizeof(int);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001240 struct in_addr *addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001241 uint16_t port;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001242 char str[INET6_ADDRSTRLEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001243
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001244 if (sock_pending && (*sock_pending != -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001245 VRB(NULL, "Trying to connect the pending socket %d.", *sock_pending);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001246 sock = *sock_pending;
1247 } else {
1248 assert(res);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001249 if (res->ai_family == AF_INET6) {
1250 addr = (struct in_addr *) &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001251 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001252 } else {
1253 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001254 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001255 }
1256 if (!inet_ntop(res->ai_family, addr, str, res->ai_addrlen)) {
Michal Vasko05532772021-06-03 12:12:38 +02001257 WRN(NULL, "inet_ntop() failed (%s).", strerror(errno));
Michal Vasko5e8d0192019-06-24 19:19:49 +02001258 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001259 VRB(NULL, "Trying to connect via %s to %s:%u.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", str, port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001260 }
1261
1262 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001263 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1264 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001265 ERR(NULL, "Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001266 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001267 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001268 /* make the socket non-blocking */
1269 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001270 ERR(NULL, "fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001271 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001272 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001273 /* non-blocking connect! */
1274 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1275 if (errno != EINPROGRESS) {
1276 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001277 ERR(NULL, "connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001278 goto cleanup;
1279 }
1280 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001281 }
1282 ts.tv_sec = timeout;
1283 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001284
Frank Rimpler9f838b02018-07-25 06:44:03 +00001285 FD_ZERO(&wset);
1286 FD_SET(sock, &wset);
1287
1288 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001289 ERR(NULL, "select() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001290 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001291 }
1292
Michal Vasko5e8d0192019-06-24 19:19:49 +02001293 if (ret == 0) {
1294 /* there was a timeout */
Michal Vasko05532772021-06-03 12:12:38 +02001295 VRB(NULL, "Timed out after %ds (%s).", timeout, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001296 if (sock_pending) {
1297 /* no sock-close, we'll try it again */
1298 *sock_pending = sock;
1299 } else {
1300 close(sock);
1301 }
1302 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001303 }
Radek Krejci782041a2018-08-20 10:09:45 +02001304
1305 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001306 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001307 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001308 ERR(NULL, "getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001309 goto cleanup;
1310 }
Michal Vaskoe27ab4e2020-07-27 11:10:58 +02001311 if (error) {
Radek Krejci782041a2018-08-20 10:09:45 +02001312 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001313 VRB(NULL, "getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001314 errno = error;
1315 goto cleanup;
1316 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001317
1318 /* enable keep-alive */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001319 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001320 goto cleanup;
1321 }
1322
Michal Vasko086311b2016-01-08 09:53:11 +01001323 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001324
Frank Rimpler9f838b02018-07-25 06:44:03 +00001325cleanup:
1326 if (sock_pending) {
1327 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001328 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001329 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001330 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001331}
1332
Frank Rimpler9f838b02018-07-25 06:44:03 +00001333/* A given timeout value limits the time how long the function blocks. If it has to block
1334 only for some seconds, a socket connection might not yet have been fully established.
1335 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1336 value will be -1. In such a case a subsequent invokation is required, by providing the
1337 stored sock_pending, again.
1338 In general, if this function returns -1, when a timeout has been given, this function
1339 has to be invoked, until it returns a valid socket.
1340 */
1341int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001342nc_sock_connect(const char *host, uint16_t port, int timeout, struct nc_keepalives *ka, int *sock_pending, char **ip_host)
Frank Rimpler9f838b02018-07-25 06:44:03 +00001343{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001344 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001345 int sock = sock_pending ? *sock_pending : -1;
Michal Vasko0be85692021-03-02 08:04:57 +01001346 struct addrinfo hints, *res_list = NULL, *res;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001347 char *buf, port_s[6]; /* length of string representation of short int */
Michal Vasko66032bc2019-01-22 15:03:12 +01001348 void *addr;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001349
Michal Vasko05532772021-06-03 12:12:38 +02001350 DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001351
1352 /* no pending socket */
1353 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001354 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001355 snprintf(port_s, 6, "%u", port);
1356 memset(&hints, 0, sizeof hints);
1357 hints.ai_family = AF_UNSPEC;
1358 hints.ai_socktype = SOCK_STREAM;
1359 hints.ai_protocol = IPPROTO_TCP;
1360 i = getaddrinfo(host, port_s, &hints, &res_list);
1361 if (i != 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001362 ERR(NULL, "Unable to translate the host address (%s).", gai_strerror(i));
Michal Vaskocb846632019-12-13 15:12:45 +01001363 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001364 }
1365
1366 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001367 sock = _non_blocking_connect(timeout, sock_pending, res, ka);
Michal Vasko2af7c382020-07-27 14:21:35 +02001368 if (sock == -1) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001369 if (!sock_pending || (*sock_pending == -1)) {
Michal Vasko2af7c382020-07-27 14:21:35 +02001370 /* try the next resource */
1371 continue;
1372 } else {
1373 /* timeout, keep pending socket */
Michal Vasko0be85692021-03-02 08:04:57 +01001374 break;
Michal Vasko2af7c382020-07-27 14:21:35 +02001375 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001376 }
Michal Vasko05532772021-06-03 12:12:38 +02001377 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 +01001378
1379 opt = 1;
1380 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001381 ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001382 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001383 }
1384
Michal Vasko66032bc2019-01-22 15:03:12 +01001385 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1386 buf = malloc(INET6_ADDRSTRLEN);
1387 if (!buf) {
1388 ERRMEM;
Michal Vaskocb846632019-12-13 15:12:45 +01001389 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001390 }
1391 if (res->ai_family == AF_INET) {
1392 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1393 } else {
1394 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1395 }
1396 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
Michal Vasko05532772021-06-03 12:12:38 +02001397 ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno));
Michal Vasko66032bc2019-01-22 15:03:12 +01001398 free(buf);
Michal Vaskocb846632019-12-13 15:12:45 +01001399 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001400 }
1401
1402 *ip_host = buf;
1403 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001404 break;
1405 }
1406 freeaddrinfo(res_list);
1407
1408 } else {
1409 /* try to get a connection with the pending socket */
1410 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001411 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001412 }
1413
1414 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001415
1416error:
Michal Vasko0be85692021-03-02 08:04:57 +01001417 if (res_list) {
1418 freeaddrinfo(res_list);
1419 }
Michal Vaskocb846632019-12-13 15:12:45 +01001420 if (sock != -1) {
1421 close(sock);
1422 }
1423 if (sock_pending) {
1424 *sock_pending = -1;
1425 }
1426 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001427}
1428
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001429#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001430
Michal Vasko3031aae2016-01-27 16:07:18 +01001431int
1432nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1433{
1434 int sock;
1435
Michal Vasko45e53ae2016-04-07 11:46:03 +02001436 if (!address) {
1437 ERRARG("address");
1438 return -1;
1439 } else if (!port) {
1440 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001441 return -1;
1442 }
1443
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001444 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001445 if (sock == -1) {
1446 return -1;
1447 }
1448
1449 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001450 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1451 if (!client_opts.ch_binds) {
1452 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001453 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001454 return -1;
1455 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001456
Michal Vasko2e6defd2016-10-07 15:48:15 +02001457 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1458 if (!client_opts.ch_bind_ti) {
1459 ERRMEM;
1460 close(sock);
1461 return -1;
1462 }
1463 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1464
Michal Vasko3031aae2016-01-27 16:07:18 +01001465 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001466 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1467 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001468 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001469 return -1;
1470 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001471 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1472 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001473 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001474
1475 return 0;
1476}
1477
1478int
1479nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1480{
1481 uint32_t i;
1482 int ret = -1;
1483
1484 if (!address && !port && !ti) {
1485 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1486 close(client_opts.ch_binds[i].sock);
1487 free((char *)client_opts.ch_binds[i].address);
1488
1489 ret = 0;
1490 }
1491 free(client_opts.ch_binds);
1492 client_opts.ch_binds = NULL;
1493 client_opts.ch_bind_count = 0;
1494 } else {
1495 for (i = 0; i < client_opts.ch_bind_count; ++i) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001496 if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) &&
1497 (!port || (client_opts.ch_binds[i].port == port)) &&
1498 (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001499 close(client_opts.ch_binds[i].sock);
1500 free((char *)client_opts.ch_binds[i].address);
1501
1502 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001503 if (!client_opts.ch_bind_count) {
1504 free(client_opts.ch_binds);
1505 client_opts.ch_binds = NULL;
1506 } else if (i < client_opts.ch_bind_count) {
1507 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1508 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1509 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001510
1511 ret = 0;
1512 }
1513 }
1514 }
1515
1516 return ret;
1517}
1518
1519API int
1520nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1521{
1522 int sock;
1523 char *host = NULL;
1524 uint16_t port, idx;
1525
Michal Vasko45e53ae2016-04-07 11:46:03 +02001526 if (!client_opts.ch_binds) {
1527 ERRINIT;
1528 return -1;
1529 } else if (!session) {
1530 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001531 return -1;
1532 }
1533
1534 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1535
Michal Vasko50456e82016-02-02 12:16:08 +01001536 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001537 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001538 return sock;
1539 }
1540
Radek Krejci53691be2016-02-22 13:58:37 +01001541#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001542 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001543 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001544 } else
1545#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001546#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001547 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001548 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001549 } else
1550#endif
1551 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001552 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001553 *session = NULL;
1554 }
1555
1556 free(host);
1557
1558 if (!(*session)) {
1559 return -1;
1560 }
1561
1562 return 1;
1563}
1564
Radek Krejci53691be2016-02-22 13:58:37 +01001565#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001566
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001567API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001568nc_session_get_cpblts(const struct nc_session *session)
1569{
1570 if (!session) {
1571 ERRARG("session");
1572 return NULL;
1573 }
1574
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001575 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001576}
1577
1578API const char *
1579nc_session_cpblt(const struct nc_session *session, const char *capab)
1580{
1581 int i, len;
1582
1583 if (!session) {
1584 ERRARG("session");
1585 return NULL;
1586 } else if (!capab) {
1587 ERRARG("capab");
1588 return NULL;
1589 }
1590
1591 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001592 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1593 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1594 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001595 }
1596 }
1597
1598 return NULL;
1599}
1600
Michal Vasko9cd26a82016-05-31 08:58:48 +02001601API int
1602nc_session_ntf_thread_running(const struct nc_session *session)
1603{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001604 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001605 ERRARG("session");
1606 return 0;
1607 }
1608
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001609 return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread) ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001610}
1611
Michal Vaskob7558c52016-02-26 15:04:19 +01001612API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001613nc_client_init(void)
1614{
1615 nc_init();
1616}
1617
1618API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001619nc_client_destroy(void)
1620{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001621 nc_client_set_schema_searchpath(NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001622#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001623 nc_client_ch_del_bind(NULL, 0, 0);
1624#endif
1625#ifdef NC_ENABLED_SSH
1626 nc_client_ssh_destroy_opts();
1627#endif
1628#ifdef NC_ENABLED_TLS
1629 nc_client_tls_destroy_opts();
1630#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001631 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001632}
1633
Michal Vasko77367452021-02-16 16:32:18 +01001634static NC_MSG_TYPE
1635recv_reply_check_msgid(struct nc_session *session, const struct lyd_node *envp, uint64_t msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001636{
Michal Vasko77367452021-02-16 16:32:18 +01001637 char *ptr;
1638 struct lyd_attr *attr;
1639 uint64_t cur_msgid;
1640
1641 assert(envp && !envp->schema);
1642
1643 /* find the message-id attribute */
1644 LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) {
1645 if (!strcmp(attr->name.name, "message-id")) {
1646 break;
1647 }
1648 }
1649
1650 if (!attr) {
Michal Vasko05532772021-06-03 12:12:38 +02001651 ERR(session, "Received a <rpc-reply> without a message-id.");
Michal Vasko77367452021-02-16 16:32:18 +01001652 return NC_MSG_REPLY_ERR_MSGID;
1653 }
1654
1655 cur_msgid = strtoul(attr->value, &ptr, 10);
1656 if (cur_msgid != msgid) {
Michal Vasko05532772021-06-03 12:12:38 +02001657 ERR(session, "Received a <rpc-reply> with an unexpected message-id %" PRIu64 " (expected %" PRIu64 ").",
1658 cur_msgid, msgid);
Michal Vasko77367452021-02-16 16:32:18 +01001659 return NC_MSG_REPLY_ERR_MSGID;
1660 }
1661
1662 return NC_MSG_REPLY;
1663}
1664
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001665/**
1666 * @brief Used to roughly estimate the type of the message, does not actually parse or verify it.
1667 *
1668 * @param[in] session NETCONF session used to send error messages.
1669 * @param[in] msg Message to check for type.
1670 * @return NC_MSG_REPLY If format roughly matches a rpc-reply;
1671 * @return NC_MSG_NOTIF If format roughly matches a notification;
1672 * @return NC_MSG_ERROR If format is malformed or unrecognized.
1673 */
1674static NC_MSG_TYPE
1675get_msg_type(struct nc_session *session, struct ly_in *msg)
1676{
1677 const char *str, *end;
1678
1679 str = ly_in_memory(msg, NULL);
1680
1681 while (*str) {
1682 /* Skip whitespaces */
1683 while (isspace(*str)) {
1684 str++;
1685 }
1686
1687 if (*str == '<') {
1688 str++;
1689 if (!strncmp(str, "!--", 3)) {
1690 /* Skip comments */
1691 end = "-->";
1692 str = strstr(str, end);
1693 } else if (!strncmp(str, "?xml", 4)) {
1694 /* Skip xml declaration */
1695 end = "?>";
1696 str = strstr(str, end);
1697 } else if (!strncmp(str, "rpc-reply", 9)) {
1698 return NC_MSG_REPLY;
1699 } else if (!strncmp(str, "notification", 12)) {
1700 return NC_MSG_NOTIF;
1701 } else {
1702 ERR(session, "Unknown xml element '%.10s'.", str);
1703 return NC_MSG_ERROR;
1704 }
1705 if (!str) {
1706 /* No matching ending tag found */
1707 ERR(session, "No matching ending tag '%s' found in xml message.", end);
1708 return NC_MSG_ERROR;
1709 }
1710 str += strlen(end);
1711 } else {
1712 /* Not a valid xml */
1713 ERR(session, "Unexpected character '%c' in xml message.", *str);
1714 return NC_MSG_ERROR;
1715 }
1716 }
1717
1718 /* Unexpected end of message */
1719 ERR(session, "Unexpected end of xml message.");
1720 return NC_MSG_ERROR;
1721}
1722
1723/**
1724 * @brief Function to receive either replies or notifications.
1725 *
1726 * @param[in] session NETCONF session from which this function receives messages.
1727 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite.
1728 * @param[in] expected Type of the message the caller desired.
1729 * @param[out] message If receiving a message succeeded this is the message, NULL otherwise.
1730 * @return NC_MSG_REPLY If a rpc-reply was received;
1731 * @return NC_MSG_NOTIF If a notification was received;
1732 * @return NC_MSG_ERROR If any error occured;
1733 * @return NC_MSG_WOULDBLOCK If the timeout was reached.
1734 */
1735static NC_MSG_TYPE
1736recv_msg(struct nc_session *session, int timeout, NC_MSG_TYPE expected, struct ly_in **message)
1737{
1738 struct nc_msg_cont **cont_ptr;
1739 struct ly_in *msg = NULL;
1740 struct nc_msg_cont *cont, *prev;
1741 NC_MSG_TYPE ret = NC_MSG_ERROR;
1742 int r;
1743
1744 *message = NULL;
1745
1746 /* MSGS LOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02001747 r = nc_session_client_msgs_lock(session, &timeout, __func__);
1748 if (!r) {
1749 ret = NC_MSG_WOULDBLOCK;
1750 goto cleanup;
1751 } else if (r == -1) {
1752 ret = NC_MSG_ERROR;
1753 goto cleanup;
1754 }
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001755
1756 /* Find the expected message in the buffer */
1757 prev = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02001758 for (cont = session->opts.client.msgs; cont && (cont->type != expected); cont = cont->next) {
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001759 prev = cont;
1760 }
1761
1762 if (cont) {
1763 /* Remove found message from buffer */
1764 if (prev) {
1765 prev->next = cont->next;
1766 } else {
1767 session->opts.client.msgs = cont->next;
1768 }
1769
1770 /* Use the buffer message */
1771 ret = cont->type;
1772 msg = cont->msg;
1773 free(cont);
Michal Vasko01130bd2021-08-26 11:47:38 +02001774 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001775 }
1776
1777 /* Read a message from the wire */
1778 r = nc_read_msg_poll_io(session, timeout, &msg);
1779 if (!r) {
1780 ret = NC_MSG_WOULDBLOCK;
Michal Vasko01130bd2021-08-26 11:47:38 +02001781 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001782 } else if (r == -1) {
1783 ret = NC_MSG_ERROR;
Michal Vasko01130bd2021-08-26 11:47:38 +02001784 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001785 }
1786
1787 /* Basic check to determine message type */
1788 ret = get_msg_type(session, msg);
1789 if (ret == NC_MSG_ERROR) {
Michal Vasko01130bd2021-08-26 11:47:38 +02001790 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001791 }
1792
1793 /* If received a message of different type store it in the buffer */
1794 if (ret != expected) {
1795 cont_ptr = &session->opts.client.msgs;
1796 while (*cont_ptr) {
1797 cont_ptr = &((*cont_ptr)->next);
1798 }
1799 *cont_ptr = malloc(sizeof **cont_ptr);
1800 if (!*cont_ptr) {
1801 ERRMEM;
1802 ret = NC_MSG_ERROR;
Michal Vasko01130bd2021-08-26 11:47:38 +02001803 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001804 }
1805 (*cont_ptr)->msg = msg;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001806 msg = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02001807 (*cont_ptr)->type = ret;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001808 (*cont_ptr)->next = NULL;
1809 }
1810
Michal Vasko01130bd2021-08-26 11:47:38 +02001811cleanup_unlock:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001812 /* MSGS UNLOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02001813 nc_session_client_msgs_unlock(session, __func__);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001814
Michal Vasko01130bd2021-08-26 11:47:38 +02001815cleanup:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001816 if (ret == expected) {
1817 *message = msg;
1818 } else {
1819 ly_in_free(msg, 1);
1820 }
1821 return ret;
1822}
1823
Michal Vasko77367452021-02-16 16:32:18 +01001824static NC_MSG_TYPE
1825recv_reply(struct nc_session *session, int timeout, struct lyd_node *op, uint64_t msgid, struct lyd_node **envp)
1826{
Michal Vasko77367452021-02-16 16:32:18 +01001827 LY_ERR lyrc;
1828 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001829 NC_MSG_TYPE ret = NC_MSG_ERROR;
1830
1831 assert(op && (op->schema->nodetype & (LYS_RPC | LYS_ACTION)));
1832
1833 *envp = NULL;
1834
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001835 /* Receive messages until a rpc-reply is found or a timeout or error reached */
1836 ret = recv_msg(session, timeout, NC_MSG_REPLY, &msg);
1837 if (ret != NC_MSG_REPLY) {
Michal Vasko77367452021-02-16 16:32:18 +01001838 goto cleanup;
1839 }
1840
1841 /* parse */
1842 lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL);
1843 if (!lyrc) {
1844 ret = recv_reply_check_msgid(session, *envp, msgid);
1845 goto cleanup;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001846 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001847 ERR(session, "Received an invalid message (%s).", ly_errmsg(LYD_CTX(op)));
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001848 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01001849 goto cleanup;
1850 }
1851
Michal Vasko77367452021-02-16 16:32:18 +01001852cleanup:
1853 ly_in_free(msg, 1);
1854 return ret;
1855}
1856
1857static int
1858recv_reply_dup_rpc(struct nc_session *session, struct nc_rpc *rpc, struct lyd_node **op)
1859{
1860 LY_ERR lyrc;
1861 struct nc_rpc_act_generic *rpc_gen;
1862 struct ly_in *in;
1863 struct lyd_node *tree, *op2;
1864 const struct lys_module *mod;
Michal Vasko305faca2021-03-25 09:16:02 +01001865 const char *module_name = NULL, *rpc_name = NULL, *module_check = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001866
1867 switch (rpc->type) {
1868 case NC_RPC_ACT_GENERIC:
1869 rpc_gen = (struct nc_rpc_act_generic *)rpc;
1870 if (rpc_gen->has_data) {
1871 tree = rpc_gen->content.data;
1872
1873 /* find the operation node */
1874 lyrc = LY_EINVAL;
1875 LYD_TREE_DFS_BEGIN(tree, op2) {
1876 if (op2->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1877 lyrc = lyd_dup_single(op2, NULL, 0, op);
1878 break;
1879 }
1880 LYD_TREE_DFS_END(tree, op2);
1881 }
1882 } else {
1883 ly_in_new_memory(rpc_gen->content.xml_str, &in);
1884 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op2);
1885 ly_in_free(in, 0);
1886 if (lyrc) {
1887 return -1;
1888 }
1889
1890 /* we want just the operation node */
1891 lyrc = lyd_dup_single(op2, NULL, 0, op);
1892
1893 lyd_free_tree(tree);
1894 }
1895 break;
1896 case NC_RPC_GETCONFIG:
1897 module_name = "ietf-netconf";
1898 rpc_name = "get-config";
1899 break;
1900 case NC_RPC_EDIT:
1901 module_name = "ietf-netconf";
1902 rpc_name = "edit-config";
1903 break;
1904 case NC_RPC_COPY:
1905 module_name = "ietf-netconf";
1906 rpc_name = "copy-config";
1907 break;
1908 case NC_RPC_DELETE:
1909 module_name = "ietf-netconf";
1910 rpc_name = "delete-config";
1911 break;
1912 case NC_RPC_LOCK:
1913 module_name = "ietf-netconf";
1914 rpc_name = "lock";
1915 break;
1916 case NC_RPC_UNLOCK:
1917 module_name = "ietf-netconf";
1918 rpc_name = "unlock";
1919 break;
1920 case NC_RPC_GET:
1921 module_name = "ietf-netconf";
1922 rpc_name = "get";
1923 break;
1924 case NC_RPC_KILL:
1925 module_name = "ietf-netconf";
1926 rpc_name = "kill-session";
1927 break;
1928 case NC_RPC_COMMIT:
1929 module_name = "ietf-netconf";
1930 rpc_name = "commit";
1931 break;
1932 case NC_RPC_DISCARD:
1933 module_name = "ietf-netconf";
1934 rpc_name = "discard-changes";
1935 break;
1936 case NC_RPC_CANCEL:
1937 module_name = "ietf-netconf";
1938 rpc_name = "cancel-commit";
1939 break;
1940 case NC_RPC_VALIDATE:
1941 module_name = "ietf-netconf";
1942 rpc_name = "validate";
1943 break;
1944 case NC_RPC_GETSCHEMA:
1945 module_name = "ietf-netconf-monitoring";
1946 rpc_name = "get-schema";
1947 break;
1948 case NC_RPC_SUBSCRIBE:
1949 module_name = "notifications";
Michal Vaskoeed893d2021-05-25 17:11:08 +02001950 rpc_name = "create-subscription";
Michal Vasko77367452021-02-16 16:32:18 +01001951 break;
1952 case NC_RPC_GETDATA:
1953 module_name = "ietf-netconf-nmda";
1954 rpc_name = "get-data";
1955 break;
1956 case NC_RPC_EDITDATA:
1957 module_name = "ietf-netconf-nmda";
1958 rpc_name = "edit-data";
1959 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01001960 case NC_RPC_ESTABLISHSUB:
1961 module_name = "ietf-subscribed-notifications";
1962 rpc_name = "establish-subscription";
1963 break;
1964 case NC_RPC_MODIFYSUB:
1965 module_name = "ietf-subscribed-notifications";
1966 rpc_name = "modify-subscription";
1967 break;
1968 case NC_RPC_DELETESUB:
1969 module_name = "ietf-subscribed-notifications";
1970 rpc_name = "delete-subscription";
1971 break;
1972 case NC_RPC_KILLSUB:
1973 module_name = "ietf-subscribed-notifications";
1974 rpc_name = "kill-subscription";
1975 break;
Michal Vasko305faca2021-03-25 09:16:02 +01001976 case NC_RPC_ESTABLISHPUSH:
1977 module_name = "ietf-subscribed-notifications";
1978 rpc_name = "establish-subscription";
1979 module_check = "ietf-yang-push";
1980 break;
1981 case NC_RPC_MODIFYPUSH:
1982 module_name = "ietf-subscribed-notifications";
1983 rpc_name = "modify-subscription";
1984 module_check = "ietf-yang-push";
1985 break;
1986 case NC_RPC_RESYNCSUB:
1987 module_name = "ietf-yang-push";
1988 rpc_name = "resync-subscription";
1989 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01001990 case NC_RPC_UNKNOWN:
Michal Vasko77367452021-02-16 16:32:18 +01001991 lyrc = LY_EINT;
1992 break;
1993 }
1994
1995 if (module_name && rpc_name) {
1996 mod = ly_ctx_get_module_implemented(session->ctx, module_name);
1997 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02001998 ERR(session, "Missing \"%s\" schema in the context.", module_name);
Michal Vasko77367452021-02-16 16:32:18 +01001999 return -1;
2000 }
2001
2002 /* create the operation node */
2003 lyrc = lyd_new_inner(NULL, mod, rpc_name, 0, op);
2004 }
Michal Vasko305faca2021-03-25 09:16:02 +01002005 if (module_check) {
2006 if (!ly_ctx_get_module_implemented(session->ctx, module_check)) {
Michal Vasko05532772021-06-03 12:12:38 +02002007 ERR(session, "Missing \"%s\" schema in the context.", module_check);
Michal Vasko305faca2021-03-25 09:16:02 +01002008 return -1;
2009 }
2010 }
Michal Vasko77367452021-02-16 16:32:18 +01002011
2012 if (lyrc) {
2013 return -1;
2014 }
2015 return 0;
2016}
2017
2018API NC_MSG_TYPE
2019nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct lyd_node **envp,
2020 struct lyd_node **op)
2021{
2022 NC_MSG_TYPE ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002023
Michal Vasko45e53ae2016-04-07 11:46:03 +02002024 if (!session) {
2025 ERRARG("session");
2026 return NC_MSG_ERROR;
2027 } else if (!rpc) {
2028 ERRARG("rpc");
2029 return NC_MSG_ERROR;
Michal Vasko5a2c7162020-01-30 11:24:17 +01002030 } else if (!msgid) {
2031 ERRARG("msgid");
2032 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002033 } else if (!envp) {
2034 ERRARG("envp");
Michal Vasko45e53ae2016-04-07 11:46:03 +02002035 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002036 } else if (!op) {
2037 ERRARG("op");
Michal Vasko086311b2016-01-08 09:53:11 +01002038 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002039 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002040 ERR(session, "Invalid session to receive RPC replies.");
Michal Vasko086311b2016-01-08 09:53:11 +01002041 return NC_MSG_ERROR;
2042 }
Michal Vasko77367452021-02-16 16:32:18 +01002043
2044 /* get a duplicate of the RPC node to append reply to */
2045 if (recv_reply_dup_rpc(session, rpc, op)) {
2046 return NC_MSG_ERROR;
Michal Vaskoeee99412016-11-21 10:19:43 +01002047 }
Michal Vasko086311b2016-01-08 09:53:11 +01002048
Michal Vasko77367452021-02-16 16:32:18 +01002049 /* receive a reply */
2050 ret = recv_reply(session, timeout, *op, msgid, envp);
Michal Vasko086311b2016-01-08 09:53:11 +01002051
Michal Vasko77367452021-02-16 16:32:18 +01002052 /* do not return the RPC copy on error or if the reply includes no data */
2053 if (((ret != NC_MSG_REPLY) && (ret != NC_MSG_REPLY_ERR_MSGID)) || !lyd_child(*op)) {
2054 lyd_free_tree(*op);
2055 *op = NULL;
2056 }
2057 return ret;
2058}
2059
2060static NC_MSG_TYPE
2061recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
2062{
Michal Vasko77367452021-02-16 16:32:18 +01002063 LY_ERR lyrc;
2064 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002065 NC_MSG_TYPE ret = NC_MSG_ERROR;
2066
2067 *op = NULL;
2068 *envp = NULL;
2069
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002070 /* Receive messages until a notification is found or a timeout or error reached */
2071 ret = recv_msg(session, timeout, NC_MSG_NOTIF, &msg);
2072 if (ret != NC_MSG_NOTIF) {
Michal Vasko77367452021-02-16 16:32:18 +01002073 goto cleanup;
2074 }
2075
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002076 /* Parse */
Michal Vasko77367452021-02-16 16:32:18 +01002077 lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op);
2078 if (!lyrc) {
Michal Vasko77367452021-02-16 16:32:18 +01002079 goto cleanup;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002080 } else {
Michal Vasko05532772021-06-03 12:12:38 +02002081 ERR(session, "Received an invalid message (%s).", ly_errmsg(session->ctx));
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002082 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002083 goto cleanup;
2084 }
2085
Michal Vasko77367452021-02-16 16:32:18 +01002086cleanup:
2087 ly_in_free(msg, 1);
2088 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002089}
2090
2091API NC_MSG_TYPE
Michal Vasko77367452021-02-16 16:32:18 +01002092nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
Michal Vasko086311b2016-01-08 09:53:11 +01002093{
Michal Vasko45e53ae2016-04-07 11:46:03 +02002094 if (!session) {
2095 ERRARG("session");
2096 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002097 } else if (!envp) {
2098 ERRARG("envp");
2099 return NC_MSG_ERROR;
2100 } else if (!op) {
2101 ERRARG("op");
Michal Vasko086311b2016-01-08 09:53:11 +01002102 return NC_MSG_ERROR;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002103 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002104 ERR(session, "Invalid session to receive Notifications.");
Michal Vasko086311b2016-01-08 09:53:11 +01002105 return NC_MSG_ERROR;
2106 }
2107
Michal Vasko77367452021-02-16 16:32:18 +01002108 /* receive a notification */
2109 return recv_notif(session, timeout, envp, op);
Michal Vasko086311b2016-01-08 09:53:11 +01002110}
2111
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002112static void *
2113nc_recv_notif_thread(void *arg)
2114{
2115 struct nc_ntf_thread_arg *ntarg;
2116 struct nc_session *session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002117
Michal Vasko77367452021-02-16 16:32:18 +01002118 void (*notif_clb)(struct nc_session *session, const struct lyd_node *envp, const struct lyd_node *op);
2119 struct lyd_node *envp, *op;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002120 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002121
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002122 /* detach ourselves */
Michal Vasko131120a2018-05-29 15:44:02 +02002123 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002124
2125 ntarg = (struct nc_ntf_thread_arg *)arg;
2126 session = ntarg->session;
2127 notif_clb = ntarg->notif_clb;
2128 free(ntarg);
2129
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002130 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread) == 1) {
Michal Vasko77367452021-02-16 16:32:18 +01002131 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002132 if (msgtype == NC_MSG_NOTIF) {
Michal Vasko77367452021-02-16 16:32:18 +01002133 notif_clb(session, envp, op);
2134 if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) {
2135 lyd_free_tree(envp);
2136 lyd_free_tree(op);
Michal Vaskof0537d82016-01-29 14:42:38 +01002137 break;
2138 }
Michal Vasko77367452021-02-16 16:32:18 +01002139 lyd_free_tree(envp);
2140 lyd_free_tree(op);
Michal Vaskoce326052018-01-04 10:32:03 +01002141 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002142 /* quit this thread once the session is broken */
2143 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002144 }
2145
2146 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2147 }
2148
Michal Vasko05532772021-06-03 12:12:38 +02002149 VRB(session, "Notification thread exit.");
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002150 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 0);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002151 return NULL;
2152}
2153
2154API int
Michal Vasko77367452021-02-16 16:32:18 +01002155nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session,
2156 const struct lyd_node *envp, const struct lyd_node *op))
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002157{
2158 struct nc_ntf_thread_arg *ntarg;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002159 pthread_t tid;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002160 int ret;
2161
Michal Vasko45e53ae2016-04-07 11:46:03 +02002162 if (!session) {
2163 ERRARG("session");
2164 return -1;
2165 } else if (!notif_clb) {
2166 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002167 return -1;
2168 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002169 ERR(session, "Invalid session to receive Notifications.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002170 return -1;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002171 } else if (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
Michal Vasko05532772021-06-03 12:12:38 +02002172 ERR(session, "Separate notification thread is already running.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002173 return -1;
2174 }
2175
2176 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002177 if (!ntarg) {
2178 ERRMEM;
2179 return -1;
2180 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002181 ntarg->session = session;
2182 ntarg->notif_clb = notif_clb;
2183
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002184 /* just so that nc_recv_notif_thread() does not immediately exit */
2185 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 1);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002186
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002187 ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002188 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02002189 ERR(session, "Failed to create a new thread (%s).", strerror(errno));
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002190 free(ntarg);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002191 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 0);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002192 return -1;
2193 }
2194
2195 return 0;
2196}
2197
Michal Vasko77367452021-02-16 16:32:18 +01002198static const char *
2199nc_wd2str(NC_WD_MODE wd)
2200{
2201 switch (wd) {
2202 case NC_WD_ALL:
2203 return "report-all";
2204 case NC_WD_ALL_TAG:
2205 return "report-all-tagged";
2206 case NC_WD_TRIM:
2207 return "trim";
2208 case NC_WD_EXPLICIT:
2209 return "explicit";
2210 default:
2211 break;
2212 }
2213
2214 return NULL;
2215}
2216
Michal Vasko086311b2016-01-08 09:53:11 +01002217API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002218nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002219{
2220 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002221 int dofree = 1;
Michal Vasko77367452021-02-16 16:32:18 +01002222 struct ly_in *in;
Michal Vasko90e8e692016-07-13 12:27:57 +02002223 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002224 struct nc_rpc_getconfig *rpc_gc;
2225 struct nc_rpc_edit *rpc_e;
2226 struct nc_rpc_copy *rpc_cp;
2227 struct nc_rpc_delete *rpc_del;
2228 struct nc_rpc_lock *rpc_lock;
2229 struct nc_rpc_get *rpc_g;
2230 struct nc_rpc_kill *rpc_k;
2231 struct nc_rpc_commit *rpc_com;
2232 struct nc_rpc_cancel *rpc_can;
2233 struct nc_rpc_validate *rpc_val;
2234 struct nc_rpc_getschema *rpc_gs;
2235 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002236 struct nc_rpc_getdata *rpc_getd;
2237 struct nc_rpc_editdata *rpc_editd;
Michal Vasko96f247a2021-03-15 13:32:10 +01002238 struct nc_rpc_establishsub *rpc_estsub;
2239 struct nc_rpc_modifysub *rpc_modsub;
2240 struct nc_rpc_deletesub *rpc_delsub;
2241 struct nc_rpc_killsub *rpc_killsub;
Michal Vasko305faca2021-03-25 09:16:02 +01002242 struct nc_rpc_establishpush *rpc_estpush;
2243 struct nc_rpc_modifypush *rpc_modpush;
2244 struct nc_rpc_resyncsub *rpc_resyncsub;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002245 struct lyd_node *data = NULL, *node, *cont;
Michal Vasko305faca2021-03-25 09:16:02 +01002246 const struct lys_module *mod = NULL, *mod2 = NULL, *ietfncwd;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002247 LY_ERR lyrc = 0;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002248 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002249 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002250 uint64_t cur_msgid;
2251
Michal Vasko45e53ae2016-04-07 11:46:03 +02002252 if (!session) {
2253 ERRARG("session");
2254 return NC_MSG_ERROR;
2255 } else if (!rpc) {
2256 ERRARG("rpc");
2257 return NC_MSG_ERROR;
2258 } else if (!msgid) {
2259 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002260 return NC_MSG_ERROR;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002261 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002262 ERR(session, "Invalid session to send RPCs.");
Michal Vasko086311b2016-01-08 09:53:11 +01002263 return NC_MSG_ERROR;
2264 }
2265
Michal Vaskoc1171a42019-11-05 12:06:46 +01002266 switch (rpc->type) {
2267 case NC_RPC_ACT_GENERIC:
2268 /* checked when parsing */
2269 break;
2270 case NC_RPC_GETCONFIG:
2271 case NC_RPC_EDIT:
2272 case NC_RPC_COPY:
2273 case NC_RPC_DELETE:
2274 case NC_RPC_LOCK:
2275 case NC_RPC_UNLOCK:
2276 case NC_RPC_GET:
2277 case NC_RPC_KILL:
2278 case NC_RPC_COMMIT:
2279 case NC_RPC_DISCARD:
2280 case NC_RPC_CANCEL:
2281 case NC_RPC_VALIDATE:
Michal Vasko77367452021-02-16 16:32:18 +01002282 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002283 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002284 ERR(session, "Missing \"ietf-netconf\" schema in the context.");
Michal Vasko086311b2016-01-08 09:53:11 +01002285 return NC_MSG_ERROR;
2286 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002287 break;
2288 case NC_RPC_GETSCHEMA:
Michal Vasko77367452021-02-16 16:32:18 +01002289 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002290 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002291 ERR(session, "Missing \"ietf-netconf-monitoring\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002292 return NC_MSG_ERROR;
2293 }
2294 break;
2295 case NC_RPC_SUBSCRIBE:
Michal Vasko77367452021-02-16 16:32:18 +01002296 mod = ly_ctx_get_module_implemented(session->ctx, "notifications");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002297 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002298 ERR(session, "Missing \"notifications\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002299 return NC_MSG_ERROR;
2300 }
2301 break;
2302 case NC_RPC_GETDATA:
2303 case NC_RPC_EDITDATA:
Michal Vasko77367452021-02-16 16:32:18 +01002304 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-nmda");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002305 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002306 ERR(session, "Missing \"ietf-netconf-nmda\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002307 return NC_MSG_ERROR;
2308 }
2309 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002310 case NC_RPC_ESTABLISHSUB:
2311 case NC_RPC_MODIFYSUB:
2312 case NC_RPC_DELETESUB:
2313 case NC_RPC_KILLSUB:
2314 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2315 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002316 ERR(session, "Missing \"ietf-subscribed-notifications\" schema in the context.");
Michal Vasko96f247a2021-03-15 13:32:10 +01002317 return NC_MSG_ERROR;
2318 }
2319 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002320 case NC_RPC_ESTABLISHPUSH:
2321 case NC_RPC_MODIFYPUSH:
2322 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2323 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002324 ERR(session, "Missing \"ietf-subscribed-notifications\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002325 return NC_MSG_ERROR;
2326 }
2327 mod2 = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2328 if (!mod2) {
Michal Vasko05532772021-06-03 12:12:38 +02002329 ERR(session, "Missing \"ietf-yang-push\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002330 return NC_MSG_ERROR;
2331 }
2332 break;
2333 case NC_RPC_RESYNCSUB:
2334 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2335 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002336 ERR(session, "Missing \"ietf-yang-push\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002337 return NC_MSG_ERROR;
2338 }
2339 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002340 case NC_RPC_UNKNOWN:
2341 ERRINT;
2342 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002343 }
2344
Michal Vaskoab9deb62021-05-27 11:37:00 +02002345#define CHECK_LYRC_BREAK(func_call) if ((lyrc = func_call)) break;
2346
Michal Vasko086311b2016-01-08 09:53:11 +01002347 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002348 case NC_RPC_ACT_GENERIC:
2349 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002350
2351 if (rpc_gen->has_data) {
2352 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002353 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002354 } else {
Michal Vasko77367452021-02-16 16:32:18 +01002355 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2356 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &data, NULL);
2357 ly_in_free(in, 0);
2358 if (lyrc) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002359 break;
Michal Vaskoeec410f2017-11-24 09:14:55 +01002360 }
Michal Vasko086311b2016-01-08 09:53:11 +01002361 }
2362 break;
2363
2364 case NC_RPC_GETCONFIG:
2365 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2366
Michal Vaskoab9deb62021-05-27 11:37:00 +02002367 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-config", 0, &data));
2368 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
2369 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_gc->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002370 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002371 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002372 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_gc->filter, 0, LYD_ANYDATA_XML, 0, &node));
2373 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002374 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002375 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2376 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2377 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_gc->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002378 }
2379 }
2380
2381 if (rpc_gc->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002382 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002383 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002384 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002385 lyrc = LY_ENOTFOUND;
2386 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002387 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002388 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 +01002389 }
2390 break;
2391
2392 case NC_RPC_EDIT:
2393 rpc_e = (struct nc_rpc_edit *)rpc;
2394
Michal Vaskoab9deb62021-05-27 11:37:00 +02002395 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-config", 0, &data));
2396 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2397 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_e->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002398
2399 if (rpc_e->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002400 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 +01002401 }
Michal Vasko086311b2016-01-08 09:53:11 +01002402 if (rpc_e->test_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002403 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 +01002404 }
Michal Vasko086311b2016-01-08 09:53:11 +01002405 if (rpc_e->error_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002406 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 +01002407 }
Michal Vasko7793bc62016-09-16 11:58:41 +02002408 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002409 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_e->edit_cont, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002410 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002411 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_e->edit_cont, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002412 }
2413 break;
2414
2415 case NC_RPC_COPY:
2416 rpc_cp = (struct nc_rpc_copy *)rpc;
2417
Michal Vaskoab9deb62021-05-27 11:37:00 +02002418 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "copy-config", 0, &data));
2419 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002420 if (rpc_cp->url_trg) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002421 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_trg, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002422 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002423 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002424 }
2425
Michal Vaskoab9deb62021-05-27 11:37:00 +02002426 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002427 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002428 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002429 CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_cp->url_config_src, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002430 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002431 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002432 }
2433 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002434 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002435 }
2436
2437 if (rpc_cp->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002438 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002439 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002440 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002441 lyrc = LY_ENOTFOUND;
2442 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002443 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002444 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 +01002445 }
2446 break;
2447
2448 case NC_RPC_DELETE:
2449 rpc_del = (struct nc_rpc_delete *)rpc;
2450
Michal Vaskoab9deb62021-05-27 11:37:00 +02002451 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-config", 0, &data));
2452 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002453 if (rpc_del->url) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002454 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_del->url, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002455 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002456 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_del->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002457 }
2458 break;
2459
2460 case NC_RPC_LOCK:
2461 rpc_lock = (struct nc_rpc_lock *)rpc;
2462
Michal Vaskoab9deb62021-05-27 11:37:00 +02002463 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "lock", 0, &data));
2464 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2465 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002466 break;
2467
2468 case NC_RPC_UNLOCK:
2469 rpc_lock = (struct nc_rpc_lock *)rpc;
2470
Michal Vaskoab9deb62021-05-27 11:37:00 +02002471 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "unlock", 0, &data));
2472 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2473 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002474 break;
2475
2476 case NC_RPC_GET:
2477 rpc_g = (struct nc_rpc_get *)rpc;
2478
Michal Vaskoab9deb62021-05-27 11:37:00 +02002479 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002480 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002481 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002482 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_g->filter, 0, LYD_ANYDATA_XML, 0, &node));
2483 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002484 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002485 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2486 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2487 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_g->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002488 }
2489 }
2490
2491 if (rpc_g->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002492 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002493 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002494 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002495 lyrc = LY_ENOTFOUND;
2496 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002497 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002498 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 +01002499 }
2500 break;
2501
2502 case NC_RPC_KILL:
2503 rpc_k = (struct nc_rpc_kill *)rpc;
2504
Michal Vaskoab9deb62021-05-27 11:37:00 +02002505 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-session", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002506 sprintf(str, "%u", rpc_k->sid);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002507 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "session-id", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002508 break;
2509
2510 case NC_RPC_COMMIT:
2511 rpc_com = (struct nc_rpc_commit *)rpc;
2512
Michal Vaskoab9deb62021-05-27 11:37:00 +02002513 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002514 if (rpc_com->confirmed) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002515 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirmed", NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002516 }
2517
2518 if (rpc_com->confirm_timeout) {
2519 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002520 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirm-timeout", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002521 }
Michal Vasko086311b2016-01-08 09:53:11 +01002522 if (rpc_com->persist) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002523 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist", rpc_com->persist, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002524 }
Michal Vasko086311b2016-01-08 09:53:11 +01002525 if (rpc_com->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002526 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_com->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002527 }
2528 break;
2529
2530 case NC_RPC_DISCARD:
Michal Vaskoab9deb62021-05-27 11:37:00 +02002531 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "discard-changes", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002532 break;
2533
2534 case NC_RPC_CANCEL:
2535 rpc_can = (struct nc_rpc_cancel *)rpc;
2536
Michal Vaskoab9deb62021-05-27 11:37:00 +02002537 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "cancel-commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002538 if (rpc_can->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002539 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_can->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002540 }
2541 break;
2542
2543 case NC_RPC_VALIDATE:
2544 rpc_val = (struct nc_rpc_validate *)rpc;
2545
Michal Vaskoab9deb62021-05-27 11:37:00 +02002546 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "validate", 0, &data));
2547 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002548 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002549 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002550 CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_val->url_config_src, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002551 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002552 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_val->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002553 }
2554 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002555 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_val->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002556 }
2557 break;
2558
2559 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002560 rpc_gs = (struct nc_rpc_getschema *)rpc;
2561
Michal Vaskoab9deb62021-05-27 11:37:00 +02002562 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-schema", 0, &data));
2563 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "identifier", rpc_gs->identifier, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002564 if (rpc_gs->version) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002565 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "version", rpc_gs->version, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002566 }
2567 if (rpc_gs->format) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002568 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "format", rpc_gs->format, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002569 }
2570 break;
2571
2572 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002573 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2574
Michal Vaskoab9deb62021-05-27 11:37:00 +02002575 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "create-subscription", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002576 if (rpc_sub->stream) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002577 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_sub->stream, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002578 }
2579
2580 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002581 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002582 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_sub->filter, 0, LYD_ANYDATA_XML, 0, &node));
2583 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002584 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002585 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2586 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2587 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_sub->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002588 }
2589 }
Michal Vasko086311b2016-01-08 09:53:11 +01002590 if (rpc_sub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002591 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "startTime", rpc_sub->start, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002592 }
Michal Vasko086311b2016-01-08 09:53:11 +01002593 if (rpc_sub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002594 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stopTime", rpc_sub->stop, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002595 }
2596 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002597
2598 case NC_RPC_GETDATA:
2599 rpc_getd = (struct nc_rpc_getdata *)rpc;
2600
Michal Vaskoab9deb62021-05-27 11:37:00 +02002601 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-data", 0, &data));
2602 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_getd->datastore, 0, NULL));
2603
Michal Vaskoc1171a42019-11-05 12:06:46 +01002604 if (rpc_getd->filter) {
2605 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002606 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "subtree-filter", rpc_getd->filter, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002607 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002608 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "xpath-filter", rpc_getd->filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002609 }
2610 }
2611 if (rpc_getd->config_filter) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002612 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "config-filter", rpc_getd->config_filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002613 }
2614 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002615 CHECK_LYRC_BREAK(lyd_new_term(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" :
2616 "origin-filter", rpc_getd->origin_filter[i], 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002617 }
2618 if (rpc_getd->max_depth) {
2619 sprintf(str, "%u", rpc_getd->max_depth);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002620 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "max-depth", str, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002621 }
2622 if (rpc_getd->with_origin) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002623 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-origin", NULL, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002624 }
2625
2626 if (rpc_getd->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002627 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
Michal Vaskoab9deb62021-05-27 11:37:00 +02002628 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 +01002629 }
2630 break;
2631
2632 case NC_RPC_EDITDATA:
2633 rpc_editd = (struct nc_rpc_editdata *)rpc;
2634
Michal Vaskoab9deb62021-05-27 11:37:00 +02002635 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-data", 0, &data));
2636 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_editd->datastore, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002637
2638 if (rpc_editd->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002639 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op], 0,
2640 NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002641 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002642 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002643 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_editd->edit_cont, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002644 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002645 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_editd->edit_cont, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002646 }
2647 break;
2648
Michal Vasko96f247a2021-03-15 13:32:10 +01002649 case NC_RPC_ESTABLISHSUB:
2650 rpc_estsub = (struct nc_rpc_establishsub *)rpc;
2651
Michal Vaskoab9deb62021-05-27 11:37:00 +02002652 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002653
2654 if (rpc_estsub->filter) {
2655 if (!rpc_estsub->filter[0] || (rpc_estsub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002656 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_estsub->filter, 0, LYD_ANYDATA_XML,
2657 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002658 } else if (rpc_estsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002659 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002660 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002661 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002662 }
2663 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002664 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_estsub->stream, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002665
2666 if (rpc_estsub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002667 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "replay-start-time", rpc_estsub->start, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002668 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002669 if (rpc_estsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002670 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002671 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002672 if (rpc_estsub->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002673 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estsub->encoding, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002674 }
2675 break;
2676
2677 case NC_RPC_MODIFYSUB:
2678 rpc_modsub = (struct nc_rpc_modifysub *)rpc;
2679
Michal Vaskoab9deb62021-05-27 11:37:00 +02002680 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002681
2682 sprintf(str, "%u", rpc_modsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002683 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002684
2685 if (rpc_modsub->filter) {
2686 if (!rpc_modsub->filter[0] || (rpc_modsub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002687 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_modsub->filter, 0, LYD_ANYDATA_XML,
2688 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002689 } else if (rpc_modsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002690 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002691 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002692 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002693 }
2694 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002695 if (rpc_modsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002696 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002697 }
2698 break;
2699
2700 case NC_RPC_DELETESUB:
2701 rpc_delsub = (struct nc_rpc_deletesub *)rpc;
2702
Michal Vaskoab9deb62021-05-27 11:37:00 +02002703 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002704
2705 sprintf(str, "%u", rpc_delsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002706 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002707 break;
2708
2709 case NC_RPC_KILLSUB:
2710 rpc_killsub = (struct nc_rpc_killsub *)rpc;
2711
Michal Vaskoab9deb62021-05-27 11:37:00 +02002712 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002713
2714 sprintf(str, "%u", rpc_killsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002715 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002716 break;
2717
Michal Vasko305faca2021-03-25 09:16:02 +01002718 case NC_RPC_ESTABLISHPUSH:
2719 rpc_estpush = (struct nc_rpc_establishpush *)rpc;
2720
Michal Vaskoab9deb62021-05-27 11:37:00 +02002721 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
2722 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_estpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002723
2724 if (rpc_estpush->filter) {
2725 if (!rpc_estpush->filter[0] || (rpc_estpush->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002726 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_estpush->filter, 0,
2727 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002728 } else if (rpc_estpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002729 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002730 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002731 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002732 }
2733 }
2734
2735 if (rpc_estpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002736 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002737 }
Michal Vasko305faca2021-03-25 09:16:02 +01002738 if (rpc_estpush->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002739 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estpush->encoding, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002740 }
2741
2742 if (rpc_estpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002743 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002744 sprintf(str, "%" PRIu32, rpc_estpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002745 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002746 if (rpc_estpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002747 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_estpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002748 }
2749 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002750 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002751 if (rpc_estpush->dampening_period) {
2752 sprintf(str, "%" PRIu32, rpc_estpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002753 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002754 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002755 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "sync-on-start", rpc_estpush->sync_on_start ? "true" : "false", 0,
2756 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002757 if (rpc_estpush->excluded_change) {
2758 for (i = 0; rpc_estpush->excluded_change[i]; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002759 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "excluded-change", rpc_estpush->excluded_change[i], 0,
2760 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002761 }
2762 }
2763 }
2764 break;
2765
2766 case NC_RPC_MODIFYPUSH:
2767 rpc_modpush = (struct nc_rpc_modifypush *)rpc;
2768
Michal Vaskoab9deb62021-05-27 11:37:00 +02002769 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01002770
2771 sprintf(str, "%u", rpc_modpush->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002772 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
2773 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_modpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002774
2775 if (rpc_modpush->filter) {
2776 if (!rpc_modpush->filter[0] || (rpc_modpush->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002777 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_modpush->filter, 0,
2778 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002779 } else if (rpc_modpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002780 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002781 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002782 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002783 }
2784 }
Michal Vasko305faca2021-03-25 09:16:02 +01002785 if (rpc_modpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002786 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002787 }
2788
2789 if (rpc_modpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002790 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002791 sprintf(str, "%" PRIu32, rpc_modpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002792 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002793 if (rpc_modpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002794 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_modpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002795 }
2796 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002797 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002798 if (rpc_modpush->dampening_period) {
2799 sprintf(str, "%" PRIu32, rpc_modpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002800 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002801 }
2802 }
2803 break;
2804
2805 case NC_RPC_RESYNCSUB:
2806 rpc_resyncsub = (struct nc_rpc_resyncsub *)rpc;
2807
Michal Vaskoab9deb62021-05-27 11:37:00 +02002808 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "resync-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01002809 sprintf(str, "%u", rpc_resyncsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002810 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002811 break;
2812
Michal Vasko96f247a2021-03-15 13:32:10 +01002813 case NC_RPC_UNKNOWN:
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002814 ERRINT;
2815 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002816 }
2817
Michal Vaskoab9deb62021-05-27 11:37:00 +02002818#undef CHECK_LYRC_BREAK
2819
2820 if (lyrc) {
Michal Vasko05532772021-06-03 12:12:38 +02002821 ERR(session, "Failed to create RPC, perhaps a required feature is disabled.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002822 lyd_free_tree(data);
Michal Vasko131120a2018-05-29 15:44:02 +02002823 return NC_MSG_ERROR;
2824 }
2825
Michal Vasko131120a2018-05-29 15:44:02 +02002826 /* send RPC, store its message ID */
2827 r = nc_send_msg_io(session, timeout, data);
2828 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002829
Radek Krejcib4b19062018-02-07 16:33:06 +01002830 if (dofree) {
Michal Vasko77367452021-02-16 16:32:18 +01002831 lyd_free_tree(data);
Radek Krejcib4b19062018-02-07 16:33:06 +01002832 }
Michal Vasko086311b2016-01-08 09:53:11 +01002833
Michal Vasko131120a2018-05-29 15:44:02 +02002834 if (r == NC_MSG_RPC) {
2835 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002836 }
Michal Vasko131120a2018-05-29 15:44:02 +02002837 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002838}
Michal Vaskode2946c2017-01-12 12:19:26 +01002839
2840API void
2841nc_client_session_set_not_strict(struct nc_session *session)
2842{
2843 if (session->side != NC_CLIENT) {
2844 ERRARG("session");
2845 return;
2846 }
2847
2848 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2849}