blob: a8ecb29562face79f85d4948c1b868e39b418f7b [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>
23#include <errno.h>
24#include <fcntl.h>
25#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010026#include <netinet/in.h>
Michal Vasko83ad17e2019-01-30 10:11:37 +010027#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020028#include <poll.h>
Michal Vasko086311b2016-01-08 09:53:11 +010029#include <pthread.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010031#include <stdlib.h>
32#include <string.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020033#include <sys/select.h>
Michal Vasko086311b2016-01-08 09:53:11 +010034#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020037#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010038#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010039
40#include <libyang/libyang.h>
41
Michal Vasko9e8ac262020-04-07 13:06:45 +020042#include "compat.h"
Michal Vasko086311b2016-01-08 09:53:11 +010043#include "libnetconf.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010044#include "messages_client.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020045#include "session_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010046
Michal Vasko8a4e1462020-05-07 11:32:31 +020047#include "../modules/ietf_netconf@2013-09-29_yang.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020048#include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h"
Michal Vasko8a4e1462020-05-07 11:32:31 +020049
Michal Vasko80ef5d22016-01-18 09:21:02 +010050static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
51
Radek Krejci62aa0642017-05-25 16:33:49 +020052#ifdef NC_ENABLED_SSH
53int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
54char *sshauth_password(const char *username, const char *hostname, void *priv);
55char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020056char *sshauth_privkey_passphrase(const char *privkey_path, void *priv);
Radek Krejci62aa0642017-05-25 16:33:49 +020057#endif /* NC_ENABLED_SSH */
58
59static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
60static pthread_key_t nc_client_context_key;
61#ifdef __linux__
62static struct nc_client_context context_main = {
Michal Vaskoe49a15f2019-05-27 14:18:36 +020063 .opts.ka = {
64 .enabled = 1,
65 .idle_time = 1,
66 .max_probes = 10,
67 .probe_interval = 5
68 },
Radek Krejci62aa0642017-05-25 16:33:49 +020069#ifdef NC_ENABLED_SSH
70 .ssh_opts = {
71 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
72 .auth_hostkey_check = sshauth_hostkey_check,
73 .auth_password = sshauth_password,
74 .auth_interactive = sshauth_interactive,
75 .auth_privkey_passphrase = sshauth_privkey_passphrase
76 },
77 .ssh_ch_opts = {
78 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
79 .auth_hostkey_check = sshauth_hostkey_check,
80 .auth_password = sshauth_password,
81 .auth_interactive = sshauth_interactive,
82 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020083 },
Radek Krejci62aa0642017-05-25 16:33:49 +020084#endif /* NC_ENABLED_SSH */
85 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020086 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020087};
88#endif
89
90static void
91nc_client_context_free(void *ptr)
92{
93 struct nc_client_context *c = (struct nc_client_context *)ptr;
94
Radek Krejci5cebc6b2017-05-26 13:24:38 +020095 if (--(c->refcount)) {
96 /* still used */
97 return;
98 }
99
Radek Krejci62aa0642017-05-25 16:33:49 +0200100#ifdef __linux__
101 /* in __linux__ we use static memory in the main thread,
102 * so this check is for programs terminating the main()
103 * function by pthread_exit() :)
104 */
105 if (c != &context_main)
106#endif
107 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200108 /* for the main thread the same is done in nc_client_destroy() */
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400109 free(c->opts.schema_searchpath);
110
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200111#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400112 int i;
113 for (i = 0; i < c->opts.ch_bind_count; ++i) {
114 close(c->opts.ch_binds[i].sock);
115 free((char *)c->opts.ch_binds[i].address);
116 }
117 free(c->opts.ch_binds);
118 c->opts.ch_binds = NULL;
119 c->opts.ch_bind_count = 0;
Radek Krejci62aa0642017-05-25 16:33:49 +0200120#endif
121#ifdef NC_ENABLED_SSH
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400122 _nc_client_ssh_destroy_opts(&c->ssh_opts);
123 _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200124#endif
125#ifdef NC_ENABLED_TLS
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400126 _nc_client_tls_destroy_opts(&c->tls_opts);
127 _nc_client_tls_destroy_opts(&c->tls_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200128#endif
129 free(c);
130 }
131}
132
133static void
134nc_client_context_createkey(void)
135{
136 int r;
137
138 /* initiate */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200139 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {}
Radek Krejci62aa0642017-05-25 16:33:49 +0200140 pthread_setspecific(nc_client_context_key, NULL);
141}
142
143struct nc_client_context *
144nc_client_context_location(void)
145{
146 struct nc_client_context *e;
147
148 pthread_once(&nc_client_context_once, nc_client_context_createkey);
149 e = pthread_getspecific(nc_client_context_key);
150 if (!e) {
151 /* prepare ly_err storage */
152#ifdef __linux__
153 if (getpid() == syscall(SYS_gettid)) {
154 /* main thread - use global variable instead of thread-specific variable. */
155 e = &context_main;
156 } else
157#endif /* __linux__ */
158 {
159 e = calloc(1, sizeof *e);
160 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200161 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200162#ifdef NC_ENABLED_SSH
163 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
164 e->ssh_opts.auth_pref[0].value = 3;
165 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
166 e->ssh_opts.auth_pref[1].value = 2;
167 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
168 e->ssh_opts.auth_pref[2].value = 1;
169 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
170 e->ssh_opts.auth_password = sshauth_password;
171 e->ssh_opts.auth_interactive = sshauth_interactive;
172 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
173
174 /* callhome settings are the same except the inverted auth methods preferences */
175 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
176 e->ssh_ch_opts.auth_pref[0].value = 1;
177 e->ssh_ch_opts.auth_pref[1].value = 2;
178 e->ssh_ch_opts.auth_pref[2].value = 3;
179#endif /* NC_ENABLED_SSH */
180 }
181 pthread_setspecific(nc_client_context_key, e);
182 }
183
184 return e;
185}
186
187#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100188
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200189API void *
190nc_client_get_thread_context(void)
191{
192 return nc_client_context_location();
193}
194
195API void
196nc_client_set_thread_context(void *context)
197{
198 struct nc_client_context *old, *new;
199
200 if (!context) {
201 ERRARG(context);
202 return;
203 }
204
205 new = (struct nc_client_context *)context;
206 old = nc_client_context_location();
207 if (old == new) {
208 /* nothing to change */
209 return;
210 }
211
212 /* replace old by new, increase reference counter in the newly set context */
213 nc_client_context_free(old);
214 new->refcount++;
215 pthread_setspecific(nc_client_context_key, new);
216}
217
Radek Krejcifd5b6682017-06-13 15:52:53 +0200218int
219nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
220{
221 /* assign context (dicionary needed for handshake) */
222 if (!ctx) {
Michal Vasko77367452021-02-16 16:32:18 +0100223 if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200224 return EXIT_FAILURE;
225 }
226
227 /* user path must be first, the first path is used to store schemas retreived via get-schema */
228 if (client_opts.schema_searchpath) {
229 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200230 } else if (!access(NC_YANG_DIR, F_OK)) {
231 ly_ctx_set_searchdir(ctx, NC_YANG_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200232 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200233
234 /* set callback for getting schemas, if provided */
235 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
236 } else {
237 session->flags |= NC_SESSION_SHAREDCTX;
238 }
239
240 session->ctx = ctx;
241
242 return EXIT_SUCCESS;
243}
244
Michal Vasko086311b2016-01-08 09:53:11 +0100245API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100246nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100247{
Michal Vasko3031aae2016-01-27 16:07:18 +0100248 if (client_opts.schema_searchpath) {
249 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100250 }
Michal Vasko086311b2016-01-08 09:53:11 +0100251
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100252 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100253 client_opts.schema_searchpath = strdup(path);
254 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100255 ERRMEM;
256 return 1;
257 }
258 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100259 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100260 }
261
262 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100263}
264
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100265API const char *
266nc_client_get_schema_searchpath(void)
267{
268 return client_opts.schema_searchpath;
269}
270
Radek Krejcifd5b6682017-06-13 15:52:53 +0200271API int
272nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100273{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200274 client_opts.schema_clb = clb;
275 if (clb) {
276 client_opts.schema_clb_data = user_data;
277 } else {
278 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100279 }
280
281 return 0;
282}
283
Radek Krejcifd5b6682017-06-13 15:52:53 +0200284API ly_module_imp_clb
285nc_client_get_schema_callback(void **user_data)
286{
287 if (user_data) {
288 (*user_data) = client_opts.schema_clb_data;
289 }
290 return client_opts.schema_clb;
291}
292
Radek Krejci65ef6d52018-08-16 16:35:02 +0200293struct schema_info {
294 char *name;
295 char *revision;
296 struct {
297 char *name;
298 char *revision;
299 } *submodules;
Michal Vasko77367452021-02-16 16:32:18 +0100300 char **features;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200301 int implemented;
302};
303
304struct clb_data_s {
305 void *user_data;
306 ly_module_imp_clb user_clb;
307 struct schema_info *schemas;
308 struct nc_session *session;
309 int has_get_schema;
310};
311
312static char *
313retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200314 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100315{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200316 char *localfile = NULL;
317 FILE *f;
318 long length, l;
319 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100320
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200321 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200322 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
323 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200324 return NULL;
325 }
326 if (localfile) {
Michal Vasko05532772021-06-03 12:12:38 +0200327 VRB(clb_data->session, "Reading schema from localfile \"%s\".", localfile);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200328 f = fopen(localfile, "r");
329 if (!f) {
Michal Vasko05532772021-06-03 12:12:38 +0200330 ERR(clb_data->session, "Unable to open \"%s\" file to get schema (%s).", localfile, strerror(errno));
Radek Krejci65ef6d52018-08-16 16:35:02 +0200331 free(localfile);
332 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100333 }
Michal Vasko086311b2016-01-08 09:53:11 +0100334
Radek Krejci65ef6d52018-08-16 16:35:02 +0200335 fseek(f, 0, SEEK_END);
336 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200337 if (length < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200338 ERR(clb_data->session, "Unable to get size of schema file \"%s\".", localfile);
Radek Krejci3f499a62018-08-17 10:16:57 +0200339 free(localfile);
340 fclose(f);
341 return NULL;
342 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200343 fseek(f, 0, SEEK_SET);
344
345 model_data = malloc(length + 1);
346 if (!model_data) {
347 ERRMEM;
348 } else if ((l = fread(model_data, 1, length, f)) != length) {
Michal Vasko05532772021-06-03 12:12:38 +0200349 ERR(clb_data->session, "Reading schema from \"%s\" failed (%d bytes read, but %d expected).", localfile, l,
350 length);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200351 free(model_data);
352 model_data = NULL;
353 } else {
354 /* terminating NULL byte */
355 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100356 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200357 fclose(f);
358 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100359 }
360
Radek Krejci65ef6d52018-08-16 16:35:02 +0200361 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100362}
363
364static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200365retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200366 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100367{
Michal Vasko086311b2016-01-08 09:53:11 +0100368 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100369 struct lyd_node *envp = NULL, *op = NULL;
370 struct lyd_node_any *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100371 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100372 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200373 char *localfile = NULL;
374 FILE *f;
375 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100376
Michal Vasko05532772021-06-03 12:12:38 +0200377 VRB(clb_data->session, "Reading schema from server via get-schema.");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200378 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100379
Radek Krejci65ef6d52018-08-16 16:35:02 +0200380 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100381 usleep(1000);
382 }
383 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200384 ERR(clb_data->session, "Failed to send the <get-schema> RPC.");
Michal Vasko086311b2016-01-08 09:53:11 +0100385 nc_rpc_free(rpc);
386 return NULL;
387 }
388
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200389 do {
Michal Vasko77367452021-02-16 16:32:18 +0100390 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200391 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100392 nc_rpc_free(rpc);
393 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko05532772021-06-03 12:12:38 +0200394 ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired.");
Michal Vasko77367452021-02-16 16:32:18 +0100395 goto cleanup;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200396 } else if ((msg == NC_MSG_ERROR) || !op) {
Michal Vasko05532772021-06-03 12:12:38 +0200397 ERR(clb_data->session, "Failed to receive a reply to <get-schema>.");
Michal Vasko77367452021-02-16 16:32:18 +0100398 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100399 }
400
Michal Vasko77367452021-02-16 16:32:18 +0100401 if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) {
Michal Vasko05532772021-06-03 12:12:38 +0200402 ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC.");
Michal Vasko77367452021-02-16 16:32:18 +0100403 goto cleanup;
Michal Vaskob2583f12016-05-12 11:40:23 +0200404 }
Michal Vasko77367452021-02-16 16:32:18 +0100405 get_schema_data = (struct lyd_node_any *)lyd_child(op);
Radek Krejci539efb62016-08-24 15:05:16 +0200406 switch (get_schema_data->value_type) {
Radek Krejci539efb62016-08-24 15:05:16 +0200407 case LYD_ANYDATA_STRING:
Michal Vasko77367452021-02-16 16:32:18 +0100408 case LYD_ANYDATA_XML:
Michal Vaskob2583f12016-05-12 11:40:23 +0200409 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200410 break;
411 case LYD_ANYDATA_DATATREE:
Michal Vasko77367452021-02-16 16:32:18 +0100412 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Radek Krejci539efb62016-08-24 15:05:16 +0200413 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200414 case LYD_ANYDATA_JSON:
Michal Vaskod838d292018-08-15 11:39:05 +0200415 case LYD_ANYDATA_LYB:
Radek Krejci03438ec2016-09-21 14:12:26 +0200416 ERRINT;
Michal Vasko77367452021-02-16 16:32:18 +0100417 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200418 }
Michal Vasko086311b2016-01-08 09:53:11 +0100419
Radek Krejci488b0702018-08-29 14:47:15 +0200420 if (model_data && !model_data[0]) {
421 /* empty data */
422 free(model_data);
423 model_data = NULL;
424 }
425
Radek Krejcifd5b6682017-06-13 15:52:53 +0200426 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200427 if (model_data) {
428 *format = LYS_IN_YANG;
429 if (client_opts.schema_searchpath) {
Michal Vasko77367452021-02-16 16:32:18 +0100430 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name, rev ? "@" : "",
431 rev ? rev : "") == -1) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200432 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100433 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200434 f = fopen(localfile, "w");
435 if (!f) {
Michal Vasko05532772021-06-03 12:12:38 +0200436 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 +0200437 localfile, strerror(errno));
Radek Krejci65ef6d52018-08-16 16:35:02 +0200438 } else {
439 fputs(model_data, f);
440 fclose(f);
441 }
442 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100443 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200444 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200445 }
446
Michal Vasko77367452021-02-16 16:32:18 +0100447cleanup:
448 lyd_free_tree(envp);
449 lyd_free_tree(op);
Michal Vasko086311b2016-01-08 09:53:11 +0100450 return model_data;
451}
452
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200453static void
454free_with_user_data(void *data, void *user_data)
Jan Kundrát35972df2018-09-06 19:00:01 +0200455{
456 free(data);
457 (void)user_data;
458}
459
Michal Vasko77367452021-02-16 16:32:18 +0100460static LY_ERR
Radek Krejci65ef6d52018-08-16 16:35:02 +0200461retrieve_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 +0100462 void *user_data, LYS_INFORMAT *format, const char **module_data,
463 void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200464{
465 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
466 unsigned int u, v, match = 1;
467 const char *name = NULL, *rev = NULL;
468 char *model_data = NULL;
469
470 /* get and check the final name and revision of the schema to be retrieved */
471 if (!mod_rev || !mod_rev[0]) {
472 /* newest revision requested - get the newest revision from the list of available modules on server */
473 match = 0;
474 for (u = 0; clb_data->schemas[u].name; ++u) {
475 if (strcmp(mod_name, clb_data->schemas[u].name)) {
476 continue;
477 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200478 if (!match || (strcmp(mod_rev, clb_data->schemas[u].revision) > 0)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200479 mod_rev = clb_data->schemas[u].revision;
480 }
481 match = u + 1;
482 }
483 if (!match) {
Michal Vasko1d2665c2021-02-12 09:28:44 +0100484 /* valid situation if we are retrieving YANG 1.1 schema and have only capabilities for now
485 * (when loading ietf-datastore for ietf-yang-library) */
Michal Vasko05532772021-06-03 12:12:38 +0200486 VRB(clb_data->session, "Unable to identify revision of the schema \"%s\" from "
487 "the available server side information.", mod_name);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200488 }
489 }
490 if (submod_name) {
491 name = submod_name;
492 if (sub_rev) {
493 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200494 } else if (match) {
495 if (!clb_data->schemas[match - 1].submodules) {
Michal Vasko05532772021-06-03 12:12:38 +0200496 VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", "
497 "in schema \"%s\", from the available server side information.", submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200498 } else {
499 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
500 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
501 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
502 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200503 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200504 if (!rev) {
Michal Vasko05532772021-06-03 12:12:38 +0200505 ERR(clb_data->session, "Requested submodule \"%s\" is not known for schema \"%s\" on server side.",
506 submod_name, mod_name);
Michal Vasko77367452021-02-16 16:32:18 +0100507 return LY_ENOTFOUND;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200508 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200509 }
510 }
511 } else {
512 name = mod_name;
513 rev = mod_rev;
514 }
515
Michal Vasko05532772021-06-03 12:12:38 +0200516 VRB(clb_data->session, "Retrieving data for schema \"%s\", revision \"%s\".", name, rev ? rev : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200517
518 if (match) {
519 /* we have enough information to avoid communication with server and try to get
520 * the schema locally */
521
522 /* 1. try to get data locally */
523 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
524
525 /* 2. try to use <get-schema> */
526 if (!model_data && clb_data->has_get_schema) {
527 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
528 }
529 } else {
530 /* we are unsure which revision of the schema we should load, so first try to get
531 * the newest revision from the server via get-schema and only if the server does not
532 * implement get-schema, try to load the newest revision locally. This is imperfect
533 * solution, but there are situation when a client does not know what revision is
534 * actually implemented by the server. */
535
536 /* 1. try to use <get-schema> */
537 if (clb_data->has_get_schema) {
538 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
539 }
540
541 /* 2. try to get data locally */
542 if (!model_data) {
543 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
544 }
545 }
546
547 /* 3. try to use user callback */
548 if (!model_data && clb_data->user_clb) {
Michal Vasko05532772021-06-03 12:12:38 +0200549 VRB(clb_data->session, "Reading schema via user callback.");
Michal Vasko77367452021-02-16 16:32:18 +0100550 clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format,
551 (const char **)&model_data, free_module_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200552 }
553
Jan Kundrát35972df2018-09-06 19:00:01 +0200554 *free_module_data = free_with_user_data;
Michal Vasko77367452021-02-16 16:32:18 +0100555 *module_data = model_data;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200556 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200557}
558
Michal Vaskoceae0152018-02-14 16:03:59 +0100559static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200560nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Michal Vasko77367452021-02-16 16:32:18 +0100561 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100562{
563 int ret = 0;
564 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200565 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200566 LYS_INFORMAT format;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200567
568 void (*free_module_data)(void *, void *) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200569 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100570
Radek Krejci65ef6d52018-08-16 16:35:02 +0200571 *mod = NULL;
572 if (revision) {
Michal Vasko77367452021-02-16 16:32:18 +0100573 *mod = ly_ctx_get_module(session->ctx, name, revision);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200574 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100575 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200576 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100577 /* make the present module implemented */
Michal Vasko77367452021-02-16 16:32:18 +0100578 if (lys_set_implemented(*mod, NULL)) {
Michal Vasko05532772021-06-03 12:12:38 +0200579 ERR(session, "Failed to implement model \"%s\".", (*mod)->name);
Michal Vaskoceae0152018-02-14 16:03:59 +0100580 ret = -1;
581 }
582 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200583 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100584 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200585 clb_data.has_get_schema = has_get_schema;
586 clb_data.schemas = schemas;
587 clb_data.session = session;
588 clb_data.user_clb = user_clb;
589 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100590
591 /* clear all the errors and just collect them for now */
592 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200593 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100594
Radek Krejci65ef6d52018-08-16 16:35:02 +0200595 /* get module data */
Michal Vasko77367452021-02-16 16:32:18 +0100596 retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &module_data, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100597
Radek Krejci65ef6d52018-08-16 16:35:02 +0200598 if (module_data) {
599 /* parse the schema */
600 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100601
Michal Vasko029c5372021-07-09 10:54:21 +0200602 lys_parse_mem(session->ctx, module_data, format, mod);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200603 if (*free_module_data) {
Michal Vasko77367452021-02-16 16:32:18 +0100604 (*free_module_data)((char *)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100605 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100606
Radek Krejci65ef6d52018-08-16 16:35:02 +0200607 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
608 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100609
Michal Vaskodbf7c272018-02-19 09:07:59 +0100610 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200611 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100612 if (!(*mod)) {
613 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
Michal Vasko77367452021-02-16 16:32:18 +0100614 ly_err_print(session->ctx, eitem);
Michal Vaskoceae0152018-02-14 16:03:59 +0100615 }
616 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200617 } else {
618 /* print only warnings */
619 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
620 if (eitem->level == LY_LLWRN) {
Michal Vasko77367452021-02-16 16:32:18 +0100621 ly_err_print(session->ctx, eitem);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200622 }
623 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100624 }
625
Michal Vaskodbf7c272018-02-19 09:07:59 +0100626 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100627 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100628 }
629
630 return ret;
631}
632
Radek Krejci65ef6d52018-08-16 16:35:02 +0200633static void
634free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200635{
Michal Vasko77367452021-02-16 16:32:18 +0100636 uint32_t u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200637
Radek Krejci65ef6d52018-08-16 16:35:02 +0200638 if (!list) {
639 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200640 }
641
Radek Krejci65ef6d52018-08-16 16:35:02 +0200642 for (u = 0; list[u].name; ++u) {
643 free(list[u].name);
644 free(list[u].revision);
Michal Vasko77367452021-02-16 16:32:18 +0100645 if (list[u].features) {
646 for (v = 0; list[u].features[v]; ++v) {
647 free(list[u].features[v]);
648 }
649 free(list[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200650 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200651 if (list[u].submodules) {
652 for (v = 0; list[u].submodules[v].name; ++v) {
653 free(list[u].submodules[v].name);
654 free(list[u].submodules[v].revision);
655 }
656 free(list[u].submodules);
657 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200658 }
659 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200660}
661
Radek Krejci235d8cb2018-08-17 14:04:32 +0200662static int
663build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200664{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200665 struct nc_rpc *rpc = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100666 struct lyd_node *op = NULL, *envp = NULL;
667 struct lyd_node_any *data;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200668 NC_MSG_TYPE msg;
669 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200670 struct ly_set *modules = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100671 uint32_t u, v, submodules_count, feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200672 struct lyd_node *iter, *child;
673 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200674 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200675
676 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200677 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
Michal Vasko77367452021-02-16 16:32:18 +0100678 rpc = nc_rpc_getdata("ietf-datastores:operational", "/ietf-yang-library:*", "false", NULL, 0, 0, 0, 0, 0,
679 NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200680 } else {
Michal Vasko77367452021-02-16 16:32:18 +0100681 rpc = nc_rpc_getdata("ietf-datastores:operational",
682 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", "false", NULL, 0, 0, 0, 0,
683 0, NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200684 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200685 if (!rpc) {
686 goto cleanup;
687 }
688
689 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
690 usleep(1000);
691 }
692 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200693 WRN(session, "Failed to send request for yang-library data.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200694 goto cleanup;
695 }
696
697 do {
Michal Vasko77367452021-02-16 16:32:18 +0100698 lyd_free_tree(envp);
699 lyd_free_tree(op);
700
701 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200702 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200703 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko40528752021-06-21 15:41:51 +0200704 WRN(session, "Timeout for receiving reply to a <get-data> yang-library data expired.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200705 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100706 } else if (msg == NC_MSG_ERROR) {
Michal Vasko40528752021-06-21 15:41:51 +0200707 WRN(session, "Failed to receive a reply to <get-data> of yang-library data.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200708 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100709 } else if (!op || !lyd_child(op) || strcmp(lyd_child(op)->schema->name, "data")) {
Michal Vasko40528752021-06-21 15:41:51 +0200710 WRN(session, "Unexpected reply without data to a yang-library <get-data> RPC.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200711 goto cleanup;
712 }
713
Michal Vasko77367452021-02-16 16:32:18 +0100714 data = (struct lyd_node_any *)lyd_child(op);
715 if (data->value_type != LYD_ANYDATA_DATATREE) {
Michal Vasko40528752021-06-21 15:41:51 +0200716 WRN(session, "Unexpected data in reply to a yang-library <get-data> RPC.");
717 goto cleanup;
718 } else if (!data->value.tree) {
719 WRN(session, "No data in reply to a yang-library <get-data> RPC.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200720 goto cleanup;
721 }
722
Michal Vasko77367452021-02-16 16:32:18 +0100723 if (lyd_find_xpath(data->value.tree, "/ietf-yang-library:modules-state/module", &modules)) {
Michal Vasko40528752021-06-21 15:41:51 +0200724 WRN(session, "No module information in reply to a yang-library <get-data> RPC.");
Radek Krejci2d088832018-08-21 13:40:14 +0200725 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200726 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200727
Michal Vasko77367452021-02-16 16:32:18 +0100728 (*result) = calloc(modules->count + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200729 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200730 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200731 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200732 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200733 }
734
Michal Vasko77367452021-02-16 16:32:18 +0100735 for (u = 0; u < modules->count; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200736 submodules_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100737 feature_count = 0;
738 mod = ((struct lyd_node *)modules->dnodes[u])->schema->module;
739 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
Michal Vasko5d9d5972021-06-09 14:17:01 +0200740 if (!iter->schema || (iter->schema->module != mod)) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200741 /* ignore node from other schemas (augments) */
742 continue;
743 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200744 if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200745 /* ignore empty nodes */
746 continue;
747 }
748 if (!strcmp(iter->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200749 (*result)[u].name = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200750 } else if (!strcmp(iter->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200751 (*result)[u].revision = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200752 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200753 (*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200754 } else if (!strcmp(iter->schema->name, "feature")) {
Michal Vasko77367452021-02-16 16:32:18 +0100755 (*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features);
756 if (!(*result)[u].features) {
757 ERRMEM;
758 free_schema_info(*result);
759 *result = NULL;
760 ret = EXIT_FAILURE;
761 goto cleanup;
762 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200763 (*result)[u].features[feature_count] = strdup(lyd_get_value(iter));
Michal Vasko77367452021-02-16 16:32:18 +0100764 (*result)[u].features[feature_count + 1] = NULL;
765 ++feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200766 } else if (!strcmp(iter->schema->name, "submodule")) {
767 submodules_count++;
768 }
769 }
770
771 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200772 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
773 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200774 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200775 free_schema_info(*result);
776 *result = NULL;
777 ret = EXIT_FAILURE;
778 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200779 } else {
780 v = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100781 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
782 mod = modules->dnodes[u]->schema->module;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200783 if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) {
Michal Vasko77367452021-02-16 16:32:18 +0100784 LY_LIST_FOR(lyd_child(iter), child) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200785 if (mod != child->schema->module) {
786 continue;
787 } else if (!strcmp(child->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200788 (*result)[u].submodules[v].name = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200789 } else if (!strcmp(child->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200790 (*result)[u].submodules[v].revision = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200791 }
792 }
793 }
794 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200795 }
796 }
797 }
798
Radek Krejcifd5b6682017-06-13 15:52:53 +0200799cleanup:
800 nc_rpc_free(rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100801 lyd_free_tree(envp);
802 lyd_free_tree(op);
803 ly_set_free(modules, NULL);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200804
Radek Krejci235d8cb2018-08-17 14:04:32 +0200805 if (session->status != NC_STATUS_RUNNING) {
Michal Vasko05532772021-06-03 12:12:38 +0200806 /* something bad happened, discard the session */
807 ERR(session, "Invalid session, discarding.");
Radek Krejci235d8cb2018-08-17 14:04:32 +0200808 ret = EXIT_FAILURE;
809 }
810
811 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200812}
813
Radek Krejci235d8cb2018-08-17 14:04:32 +0200814static int
815build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200816{
Michal Vasko77367452021-02-16 16:32:18 +0100817 uint32_t u, v, feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200818 char *module_cpblt, *ptr, *ptr2;
819
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200820 for (u = 0; cpblts[u]; ++u) {}
Radek Krejci235d8cb2018-08-17 14:04:32 +0200821 (*result) = calloc(u + 1, sizeof **result);
822 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200823 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200824 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200825 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200826
827 for (u = v = 0; cpblts[u]; ++u) {
828 module_cpblt = strstr(cpblts[u], "module=");
829 /* this capability requires a module */
830 if (!module_cpblt) {
831 continue;
832 }
833
834 /* get module's name */
835 ptr = (char *)module_cpblt + 7;
836 ptr2 = strchr(ptr, '&');
837 if (!ptr2) {
838 ptr2 = ptr + strlen(ptr);
839 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200840 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200841
842 /* get module's revision */
843 ptr = strstr(module_cpblt, "revision=");
844 if (ptr) {
845 ptr += 9;
846 ptr2 = strchr(ptr, '&');
847 if (!ptr2) {
848 ptr2 = ptr + strlen(ptr);
849 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200850 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200851 }
852
853 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200854 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200855
856 /* get module's features */
857 ptr = strstr(module_cpblt, "features=");
858 if (ptr) {
859 ptr += 9;
Michal Vasko77367452021-02-16 16:32:18 +0100860 feature_count = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200861 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
862 if (*ptr == ',') {
Michal Vasko77367452021-02-16 16:32:18 +0100863 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
864 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
865 (*result)[v].features[feature_count + 1] = NULL;
866 ++feature_count;
867
Radek Krejci65ef6d52018-08-16 16:35:02 +0200868 ptr2 = ptr + 1;
869 }
870 }
871 /* the last one */
Michal Vasko77367452021-02-16 16:32:18 +0100872 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
873 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
874 (*result)[v].features[feature_count + 1] = NULL;
875 ++feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200876 }
877 ++v;
878 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200879
880 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200881}
882
883static int
Michal Vasko77367452021-02-16 16:32:18 +0100884nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data,
885 int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200886{
887 int ret = EXIT_FAILURE;
Michal Vasko77367452021-02-16 16:32:18 +0100888 struct lys_module *mod;
889 uint32_t u;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200890
891 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200892 /* skip import-only modules */
893 if (!modules[u].implemented) {
894 continue;
895 }
896
Radek Krejci65ef6d52018-08-16 16:35:02 +0200897 /* we can continue even if it fails */
898 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
899
900 if (!mod) {
901 if (session->status != NC_STATUS_RUNNING) {
902 /* something bad heppened, discard the session */
Michal Vasko05532772021-06-03 12:12:38 +0200903 ERR(session, "Invalid session, discarding.");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200904 goto cleanup;
905 }
906
907 /* all loading ways failed, the schema will be ignored in the received data */
Michal Vasko05532772021-06-03 12:12:38 +0200908 WRN(session, "Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ?
909 modules[u].revision : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200910 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
911 } else {
Michal Vasko77367452021-02-16 16:32:18 +0100912 /* set the features */
913 lys_set_implemented(mod, (const char **)modules[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200914 }
Michal Vasko60f66602017-10-17 13:52:18 +0200915 }
916
Michal Vasko77367452021-02-16 16:32:18 +0100917 /* success */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200918 ret = EXIT_SUCCESS;
919
920cleanup:
Radek Krejcifd5b6682017-06-13 15:52:53 +0200921 return ret;
922}
923
Radek Krejci65ef6d52018-08-16 16:35:02 +0200924static int
Michal Vasko77367452021-02-16 16:32:18 +0100925nc_ctx_fill_ietf_netconf(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb,
926 void *user_data, int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200927{
Michal Vasko77367452021-02-16 16:32:18 +0100928 uint32_t u;
929 struct lys_module *ietfnc;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200930
Michal Vasko77367452021-02-16 16:32:18 +0100931 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200932 if (!ietfnc) {
933 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200934 if (!ietfnc) {
Michal Vasko029c5372021-07-09 10:54:21 +0200935 lys_parse_mem(session->ctx, ietf_netconf_2013_09_29_yang, LYS_IN_YANG, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200936 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200937 }
938 if (!ietfnc) {
Michal Vasko05532772021-06-03 12:12:38 +0200939 ERR(session, "Loading base NETCONF schema failed.");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200940 return 1;
941 }
942
943 /* set supported capabilities from ietf-netconf */
944 for (u = 0; modules[u].name; ++u) {
945 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
946 continue;
947 }
948
Michal Vasko77367452021-02-16 16:32:18 +0100949 lys_set_implemented(ietfnc, (const char **)modules[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200950 }
951
952 return 0;
953}
954
Michal Vasko086311b2016-01-08 09:53:11 +0100955int
956nc_ctx_check_and_fill(struct nc_session *session)
957{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200958 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100959 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100960 void *old_data = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100961 struct lys_module *mod = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200962 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200963 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100964
Michal Vasko2e6defd2016-10-07 15:48:15 +0200965 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100966
Radek Krejci65ef6d52018-08-16 16:35:02 +0200967 /* 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 +0200968 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200969
Radek Krejci65ef6d52018-08-16 16:35:02 +0200970 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
971 * schemas only to those present on the server side */
Michal Vasko77367452021-02-16 16:32:18 +0100972 ly_ctx_set_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200973
974 /* our callback is set later with appropriate data */
975 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
976
977 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200978 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200979 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200980 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200981 if (yanglib_support) {
982 break;
983 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +0100984 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200985 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200986 if (get_schema_support) {
987 break;
988 }
Michal Vasko086311b2016-01-08 09:53:11 +0100989 }
990 }
Michal Vasko31dd4c52020-04-17 10:29:44 +0200991 if (get_schema_support) {
Michal Vasko05532772021-06-03 12:12:38 +0200992 VRB(session, "Capability for <get-schema> support found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +0200993 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200994 VRB(session, "Capability for <get-schema> support not found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +0200995 }
996 if (yanglib_support) {
Michal Vasko05532772021-06-03 12:12:38 +0200997 VRB(session, "Capability for yang-library support found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +0200998 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200999 VRB(session, "Capability for yang-library support not found.");
Michal Vasko31dd4c52020-04-17 10:29:44 +02001000 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001001
1002 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001003 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
Michal Vasko05532772021-06-03 12:12:38 +02001004 ERR(session, "Unable to get server's schema information from the <hello>'s capabilities.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001005 goto cleanup;
1006 }
1007
Michal Vasko086311b2016-01-08 09:53:11 +01001008 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Michal Vasko77367452021-02-16 16:32:18 +01001009 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 +02001010 WRN(session, "Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Michal Vasko8a4e1462020-05-07 11:32:31 +02001011 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001012 }
1013
1014 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001015 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001016 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001017 }
1018
Radek Krejci65ef6d52018-08-16 16:35:02 +02001019 /* get correct version of ietf-yang-library into context */
1020 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001021 /* use get schema to get server's ietf-yang-library */
1022 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1023 if (!revision) {
Michal Vasko05532772021-06-03 12:12:38 +02001024 WRN(session, "Loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.");
1025 WRN(session, "Unable to automatically use <get-schema>.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001026 yanglib_support = 0;
1027 } else {
1028 revision = strndup(&revision[9], 10);
Michal Vasko0d877f92020-04-02 13:52:43 +02001029 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data,
1030 get_schema_support, &mod)) {
Michal Vasko05532772021-06-03 12:12:38 +02001031 WRN(session, "Loading NETCONF ietf-yang-library schema failed, unable to use it to learn all "
1032 "the supported modules.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001033 yanglib_support = 0;
1034 }
Michal Vasko0d877f92020-04-02 13:52:43 +02001035 if (strcmp(revision, "2019-01-04") >= 0) {
1036 /* we also need ietf-datastores to be implemented */
1037 if (nc_ctx_load_module(session, "ietf-datastores", NULL, server_modules, old_clb, old_data,
1038 get_schema_support, &mod)) {
Michal Vasko05532772021-06-03 12:12:38 +02001039 WRN(session, "Loading NETCONF ietf-datastores schema failed, unable to use yang-library "
1040 "to learn all the supported modules.");
Michal Vasko0d877f92020-04-02 13:52:43 +02001041 yanglib_support = 0;
1042 }
1043 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001044 free(revision);
Michal Vasko77367452021-02-16 16:32:18 +01001045
1046 /* ietf-netconf-nmda is needed to issue get-data */
1047 if (nc_ctx_load_module(session, "ietf-netconf-nmda", NULL, server_modules, old_clb, old_data,
1048 get_schema_support, &mod)) {
Michal Vasko05532772021-06-03 12:12:38 +02001049 WRN(session, "Loading NETCONF ietf-netconf-nmda schema failed, unable to use get-data to retrieve "
1050 "yang-library data.");
Michal Vasko77367452021-02-16 16:32:18 +01001051 yanglib_support = 0;
1052 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001053 }
1054 }
1055
Radek Krejci65ef6d52018-08-16 16:35:02 +02001056 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001057 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001058 if (build_schema_info_yl(session, &sm)) {
1059 goto cleanup;
1060 } else if (!sm) {
Michal Vasko05532772021-06-03 12:12:38 +02001061 VRB(session, "Trying to use capabilities instead of ietf-yang-library data.");
Radek Krejcif0633792018-08-20 15:12:09 +02001062 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001063 /* prefer yang-library information, currently we have it from capabilities used for getting correct
1064 * yang-library schema */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001065 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001066 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001067 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001068 }
Michal Vaskoef578332016-01-25 13:20:09 +01001069
Radek Krejci65ef6d52018-08-16 16:35:02 +02001070 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1071 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001072 }
1073
Radek Krejcifd5b6682017-06-13 15:52:53 +02001074 /* succsess */
1075 ret = 0;
1076
Michal Vaskoeee99412016-11-21 10:19:43 +01001077 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Michal Vasko05532772021-06-03 12:12:38 +02001078 WRN(session, "Some models failed to be loaded, any data from these models (and any other unknown) will "
1079 "be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +01001080 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001081
1082cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001083 free_schema_info(server_modules);
1084
Radek Krejcifd5b6682017-06-13 15:52:53 +02001085 /* set user callback back */
1086 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vasko77367452021-02-16 16:32:18 +01001087 ly_ctx_unset_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001088
Michal Vaskoef578332016-01-25 13:20:09 +01001089 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001090}
1091
1092API struct nc_session *
1093nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1094{
Michal Vaskod083db62016-01-19 10:31:29 +01001095 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001096
Michal Vasko45e53ae2016-04-07 11:46:03 +02001097 if (fdin < 0) {
1098 ERRARG("fdin");
1099 return NULL;
1100 } else if (fdout < 0) {
1101 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001102 return NULL;
1103 }
1104
1105 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001106 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001107 if (!session) {
1108 ERRMEM;
1109 return NULL;
1110 }
1111 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001112
1113 /* transport specific data */
1114 session->ti_type = NC_TI_FD;
1115 session->ti.fd.in = fdin;
1116 session->ti.fd.out = fdout;
1117
Robin Jarry4e38e292019-10-15 17:14:14 +02001118 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1119 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001120 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001121 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001122
1123 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001124 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001125 goto fail;
1126 }
1127 session->status = NC_STATUS_RUNNING;
1128
Michal Vaskoef578332016-01-25 13:20:09 +01001129 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001130 goto fail;
1131 }
1132
1133 return session;
1134
1135fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001136 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001137 return NULL;
1138}
1139
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001140API struct nc_session *
1141nc_connect_unix(const char *address, struct ly_ctx *ctx)
1142{
1143 struct nc_session *session = NULL;
1144 struct sockaddr_un sun;
1145 const struct passwd *pw;
1146 char *username;
1147 int sock = -1;
1148
1149 if (address == NULL) {
1150 ERRARG("address");
1151 return NULL;
1152 }
1153
1154 pw = getpwuid(geteuid());
1155 if (pw == NULL) {
Michal Vasko05532772021-06-03 12:12:38 +02001156 ERR(NULL, "Failed to find username for euid=%u.\n", geteuid());
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001157 goto fail;
1158 }
1159
1160 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1161 if (sock < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001162 ERR(NULL, "Failed to create socket (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001163 goto fail;
1164 }
1165
1166 memset(&sun, 0, sizeof(sun));
1167 sun.sun_family = AF_UNIX;
1168 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1169
1170 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001171 ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001172 goto fail;
1173 }
1174
1175 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001176 ERR(NULL, "fcntl failed (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001177 goto fail;
1178 }
1179
1180 /* prepare session structure */
1181 session = nc_new_session(NC_CLIENT, 0);
1182 if (!session) {
1183 ERRMEM;
1184 goto fail;
1185 }
1186 session->status = NC_STATUS_STARTING;
1187
1188 /* transport specific data */
1189 session->ti_type = NC_TI_UNIX;
1190 session->ti.unixsock.sock = sock;
1191 sock = -1; /* do not close sock in fail label anymore */
1192
Robin Jarry4e38e292019-10-15 17:14:14 +02001193 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1194 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001195 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001196 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001197
Michal Vasko77367452021-02-16 16:32:18 +01001198 lydict_insert(ctx, address, 0, &session->path);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001199
1200 username = strdup(pw->pw_name);
1201 if (username == NULL) {
1202 ERRMEM;
1203 goto fail;
1204 }
Michal Vasko77367452021-02-16 16:32:18 +01001205 lydict_insert_zc(ctx, username, &session->username);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001206
1207 /* NETCONF handshake */
1208 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1209 goto fail;
1210 }
1211 session->status = NC_STATUS_RUNNING;
1212
1213 if (nc_ctx_check_and_fill(session) == -1) {
1214 goto fail;
1215 }
1216
1217 return session;
1218
1219fail:
1220 nc_session_free(session, NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001221 if (sock >= 0) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001222 close(sock);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001223 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001224 return NULL;
1225}
1226
Frank Rimpler9f838b02018-07-25 06:44:03 +00001227/*
1228 Helper for a non-blocking connect (which is required because of the locking
1229 concept for e.g. call home settings). For more details see nc_sock_connect().
1230 */
1231static int
Michal Vasko5e8d0192019-06-24 19:19:49 +02001232_non_blocking_connect(int timeout, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001233{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001234 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001235 int sock = -1;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001236 fd_set wset;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001237 struct timeval ts;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001238 socklen_t len = sizeof(int);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001239 struct in_addr *addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001240 uint16_t port;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001241 char str[INET6_ADDRSTRLEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001242
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001243 if (sock_pending && (*sock_pending != -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001244 VRB(NULL, "Trying to connect the pending socket %d.", *sock_pending);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001245 sock = *sock_pending;
1246 } else {
1247 assert(res);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001248 if (res->ai_family == AF_INET6) {
1249 addr = (struct in_addr *) &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001250 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001251 } else {
1252 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001253 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001254 }
1255 if (!inet_ntop(res->ai_family, addr, str, res->ai_addrlen)) {
Michal Vasko05532772021-06-03 12:12:38 +02001256 WRN(NULL, "inet_ntop() failed (%s).", strerror(errno));
Michal Vasko5e8d0192019-06-24 19:19:49 +02001257 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001258 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 +02001259 }
1260
1261 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001262 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1263 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001264 ERR(NULL, "Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001265 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001266 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001267 /* make the socket non-blocking */
1268 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001269 ERR(NULL, "fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001270 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001271 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001272 /* non-blocking connect! */
1273 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1274 if (errno != EINPROGRESS) {
1275 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001276 ERR(NULL, "connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001277 goto cleanup;
1278 }
1279 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001280 }
1281 ts.tv_sec = timeout;
1282 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001283
Frank Rimpler9f838b02018-07-25 06:44:03 +00001284 FD_ZERO(&wset);
1285 FD_SET(sock, &wset);
1286
1287 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001288 ERR(NULL, "select() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001289 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001290 }
1291
Michal Vasko5e8d0192019-06-24 19:19:49 +02001292 if (ret == 0) {
1293 /* there was a timeout */
Michal Vasko05532772021-06-03 12:12:38 +02001294 VRB(NULL, "Timed out after %ds (%s).", timeout, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001295 if (sock_pending) {
1296 /* no sock-close, we'll try it again */
1297 *sock_pending = sock;
1298 } else {
1299 close(sock);
1300 }
1301 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001302 }
Radek Krejci782041a2018-08-20 10:09:45 +02001303
1304 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001305 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001306 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001307 ERR(NULL, "getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001308 goto cleanup;
1309 }
Michal Vaskoe27ab4e2020-07-27 11:10:58 +02001310 if (error) {
Radek Krejci782041a2018-08-20 10:09:45 +02001311 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001312 VRB(NULL, "getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001313 errno = error;
1314 goto cleanup;
1315 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001316
1317 /* enable keep-alive */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001318 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001319 goto cleanup;
1320 }
1321
Michal Vasko086311b2016-01-08 09:53:11 +01001322 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001323
Frank Rimpler9f838b02018-07-25 06:44:03 +00001324cleanup:
1325 if (sock_pending) {
1326 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001327 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001328 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001329 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001330}
1331
Frank Rimpler9f838b02018-07-25 06:44:03 +00001332/* A given timeout value limits the time how long the function blocks. If it has to block
1333 only for some seconds, a socket connection might not yet have been fully established.
1334 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1335 value will be -1. In such a case a subsequent invokation is required, by providing the
1336 stored sock_pending, again.
1337 In general, if this function returns -1, when a timeout has been given, this function
1338 has to be invoked, until it returns a valid socket.
1339 */
1340int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001341nc_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 +00001342{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001343 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001344 int sock = sock_pending ? *sock_pending : -1;
Michal Vasko0be85692021-03-02 08:04:57 +01001345 struct addrinfo hints, *res_list = NULL, *res;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001346 char *buf, port_s[6]; /* length of string representation of short int */
Michal Vasko66032bc2019-01-22 15:03:12 +01001347 void *addr;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001348
Michal Vasko05532772021-06-03 12:12:38 +02001349 DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001350
1351 /* no pending socket */
1352 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001353 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001354 snprintf(port_s, 6, "%u", port);
1355 memset(&hints, 0, sizeof hints);
1356 hints.ai_family = AF_UNSPEC;
1357 hints.ai_socktype = SOCK_STREAM;
1358 hints.ai_protocol = IPPROTO_TCP;
1359 i = getaddrinfo(host, port_s, &hints, &res_list);
1360 if (i != 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001361 ERR(NULL, "Unable to translate the host address (%s).", gai_strerror(i));
Michal Vaskocb846632019-12-13 15:12:45 +01001362 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001363 }
1364
1365 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001366 sock = _non_blocking_connect(timeout, sock_pending, res, ka);
Michal Vasko2af7c382020-07-27 14:21:35 +02001367 if (sock == -1) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001368 if (!sock_pending || (*sock_pending == -1)) {
Michal Vasko2af7c382020-07-27 14:21:35 +02001369 /* try the next resource */
1370 continue;
1371 } else {
1372 /* timeout, keep pending socket */
Michal Vasko0be85692021-03-02 08:04:57 +01001373 break;
Michal Vasko2af7c382020-07-27 14:21:35 +02001374 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001375 }
Michal Vasko05532772021-06-03 12:12:38 +02001376 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 +01001377
1378 opt = 1;
1379 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001380 ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001381 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001382 }
1383
Michal Vasko66032bc2019-01-22 15:03:12 +01001384 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1385 buf = malloc(INET6_ADDRSTRLEN);
1386 if (!buf) {
1387 ERRMEM;
Michal Vaskocb846632019-12-13 15:12:45 +01001388 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001389 }
1390 if (res->ai_family == AF_INET) {
1391 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1392 } else {
1393 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1394 }
1395 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
Michal Vasko05532772021-06-03 12:12:38 +02001396 ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno));
Michal Vasko66032bc2019-01-22 15:03:12 +01001397 free(buf);
Michal Vaskocb846632019-12-13 15:12:45 +01001398 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001399 }
1400
1401 *ip_host = buf;
1402 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001403 break;
1404 }
1405 freeaddrinfo(res_list);
1406
1407 } else {
1408 /* try to get a connection with the pending socket */
1409 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001410 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001411 }
1412
1413 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001414
1415error:
Michal Vasko0be85692021-03-02 08:04:57 +01001416 if (res_list) {
1417 freeaddrinfo(res_list);
1418 }
Michal Vaskocb846632019-12-13 15:12:45 +01001419 if (sock != -1) {
1420 close(sock);
1421 }
1422 if (sock_pending) {
1423 *sock_pending = -1;
1424 }
1425 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001426}
1427
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001428#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001429
Michal Vasko3031aae2016-01-27 16:07:18 +01001430int
1431nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1432{
1433 int sock;
1434
Michal Vasko45e53ae2016-04-07 11:46:03 +02001435 if (!address) {
1436 ERRARG("address");
1437 return -1;
1438 } else if (!port) {
1439 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001440 return -1;
1441 }
1442
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001443 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001444 if (sock == -1) {
1445 return -1;
1446 }
1447
1448 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001449 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1450 if (!client_opts.ch_binds) {
1451 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001452 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001453 return -1;
1454 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001455
Michal Vasko2e6defd2016-10-07 15:48:15 +02001456 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1457 if (!client_opts.ch_bind_ti) {
1458 ERRMEM;
1459 close(sock);
1460 return -1;
1461 }
1462 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1463
Michal Vasko3031aae2016-01-27 16:07:18 +01001464 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001465 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1466 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001467 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001468 return -1;
1469 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001470 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1471 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001472 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001473
1474 return 0;
1475}
1476
1477int
1478nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1479{
1480 uint32_t i;
1481 int ret = -1;
1482
1483 if (!address && !port && !ti) {
1484 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1485 close(client_opts.ch_binds[i].sock);
1486 free((char *)client_opts.ch_binds[i].address);
1487
1488 ret = 0;
1489 }
1490 free(client_opts.ch_binds);
1491 client_opts.ch_binds = NULL;
1492 client_opts.ch_bind_count = 0;
1493 } else {
1494 for (i = 0; i < client_opts.ch_bind_count; ++i) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001495 if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) &&
1496 (!port || (client_opts.ch_binds[i].port == port)) &&
1497 (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001498 close(client_opts.ch_binds[i].sock);
1499 free((char *)client_opts.ch_binds[i].address);
1500
1501 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001502 if (!client_opts.ch_bind_count) {
1503 free(client_opts.ch_binds);
1504 client_opts.ch_binds = NULL;
1505 } else if (i < client_opts.ch_bind_count) {
1506 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1507 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1508 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001509
1510 ret = 0;
1511 }
1512 }
1513 }
1514
1515 return ret;
1516}
1517
1518API int
1519nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1520{
1521 int sock;
1522 char *host = NULL;
1523 uint16_t port, idx;
1524
Michal Vasko45e53ae2016-04-07 11:46:03 +02001525 if (!client_opts.ch_binds) {
1526 ERRINIT;
1527 return -1;
1528 } else if (!session) {
1529 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001530 return -1;
1531 }
1532
1533 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1534
Michal Vasko50456e82016-02-02 12:16:08 +01001535 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001536 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001537 return sock;
1538 }
1539
Radek Krejci53691be2016-02-22 13:58:37 +01001540#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001541 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001542 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001543 } else
1544#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001545#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001546 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001547 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001548 } else
1549#endif
1550 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001551 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001552 *session = NULL;
1553 }
1554
1555 free(host);
1556
1557 if (!(*session)) {
1558 return -1;
1559 }
1560
1561 return 1;
1562}
1563
Radek Krejci53691be2016-02-22 13:58:37 +01001564#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001565
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001566API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001567nc_session_get_cpblts(const struct nc_session *session)
1568{
1569 if (!session) {
1570 ERRARG("session");
1571 return NULL;
1572 }
1573
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001574 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001575}
1576
1577API const char *
1578nc_session_cpblt(const struct nc_session *session, const char *capab)
1579{
1580 int i, len;
1581
1582 if (!session) {
1583 ERRARG("session");
1584 return NULL;
1585 } else if (!capab) {
1586 ERRARG("capab");
1587 return NULL;
1588 }
1589
1590 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001591 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1592 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1593 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001594 }
1595 }
1596
1597 return NULL;
1598}
1599
Michal Vasko9cd26a82016-05-31 08:58:48 +02001600API int
1601nc_session_ntf_thread_running(const struct nc_session *session)
1602{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001603 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001604 ERRARG("session");
1605 return 0;
1606 }
1607
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001608 return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread) ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001609}
1610
Michal Vaskob7558c52016-02-26 15:04:19 +01001611API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001612nc_client_init(void)
1613{
1614 nc_init();
1615}
1616
1617API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001618nc_client_destroy(void)
1619{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001620 nc_client_set_schema_searchpath(NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001621#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001622 nc_client_ch_del_bind(NULL, 0, 0);
1623#endif
1624#ifdef NC_ENABLED_SSH
1625 nc_client_ssh_destroy_opts();
1626#endif
1627#ifdef NC_ENABLED_TLS
1628 nc_client_tls_destroy_opts();
1629#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001630 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001631}
1632
Michal Vasko77367452021-02-16 16:32:18 +01001633static NC_MSG_TYPE
1634recv_reply_check_msgid(struct nc_session *session, const struct lyd_node *envp, uint64_t msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001635{
Michal Vasko77367452021-02-16 16:32:18 +01001636 char *ptr;
1637 struct lyd_attr *attr;
1638 uint64_t cur_msgid;
1639
1640 assert(envp && !envp->schema);
1641
1642 /* find the message-id attribute */
1643 LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) {
1644 if (!strcmp(attr->name.name, "message-id")) {
1645 break;
1646 }
1647 }
1648
1649 if (!attr) {
Michal Vasko05532772021-06-03 12:12:38 +02001650 ERR(session, "Received a <rpc-reply> without a message-id.");
Michal Vasko77367452021-02-16 16:32:18 +01001651 return NC_MSG_REPLY_ERR_MSGID;
1652 }
1653
1654 cur_msgid = strtoul(attr->value, &ptr, 10);
1655 if (cur_msgid != msgid) {
Michal Vasko05532772021-06-03 12:12:38 +02001656 ERR(session, "Received a <rpc-reply> with an unexpected message-id %" PRIu64 " (expected %" PRIu64 ").",
1657 cur_msgid, msgid);
Michal Vasko77367452021-02-16 16:32:18 +01001658 return NC_MSG_REPLY_ERR_MSGID;
1659 }
1660
1661 return NC_MSG_REPLY;
1662}
1663
1664static NC_MSG_TYPE
1665recv_reply(struct nc_session *session, int timeout, struct lyd_node *op, uint64_t msgid, struct lyd_node **envp)
1666{
1667 int r;
1668 LY_ERR lyrc;
1669 struct ly_in *msg = NULL;
1670 struct nc_msg_cont *cont, **cont_ptr;
1671 NC_MSG_TYPE ret = NC_MSG_ERROR;
1672
1673 assert(op && (op->schema->nodetype & (LYS_RPC | LYS_ACTION)));
1674
1675 *envp = NULL;
1676
1677 /* try to get rpc-reply from the session's queue */
1678 while (session->opts.client.replies) {
1679 cont = session->opts.client.replies;
1680 session->opts.client.replies = cont->next;
1681
1682 msg = cont->msg;
1683 free(cont);
1684
1685 /* parse */
1686 lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL);
1687 if (!lyrc) {
1688 ret = recv_reply_check_msgid(session, *envp, msgid);
1689 goto cleanup;
1690 } else if (lyrc != LY_ENOT) {
1691 lyd_free_tree(*envp);
1692 *envp = NULL;
Michal Vasko05532772021-06-03 12:12:38 +02001693 ERR(session, "Received an invalid message (%s).", ly_errmsg(LYD_CTX(op)));
Michal Vasko77367452021-02-16 16:32:18 +01001694 goto cleanup;
1695 } else {
1696 /* it was not a notification so it is nothing known */
Michal Vasko05532772021-06-03 12:12:38 +02001697 ERR(session, "Received an unexpected message.");
Michal Vasko77367452021-02-16 16:32:18 +01001698 }
1699
1700 /* try the next message */
1701 ly_in_free(msg, 1);
1702 msg = NULL;
1703 }
1704
1705 /* read message from wire */
1706 r = nc_read_msg_poll_io(session, timeout, &msg);
1707 if (!r) {
1708 ret = NC_MSG_WOULDBLOCK;
1709 goto cleanup;
1710 } else if (r == -1) {
1711 goto cleanup;
1712 }
1713
1714 /* parse */
1715 lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL);
1716 if (!lyrc) {
1717 ret = recv_reply_check_msgid(session, *envp, msgid);
1718 goto cleanup;
1719 } else if (lyrc != LY_ENOT) {
1720 lyd_free_tree(*envp);
1721 *envp = NULL;
Michal Vasko05532772021-06-03 12:12:38 +02001722 ERR(session, "Received an invalid message (%s).", ly_errmsg(LYD_CTX(op)));
Michal Vasko77367452021-02-16 16:32:18 +01001723 goto cleanup;
1724 }
1725
Michal Vaskoc5ea6382021-03-15 16:24:08 +01001726 /* assume a notification, reset and store it */
1727 ly_in_reset(msg);
Michal Vasko77367452021-02-16 16:32:18 +01001728 cont_ptr = &session->opts.client.notifs;
1729 while (*cont_ptr) {
1730 cont_ptr = &((*cont_ptr)->next);
1731 }
1732 *cont_ptr = malloc(sizeof **cont_ptr);
1733 if (!*cont_ptr) {
1734 ERRMEM;
1735 goto cleanup;
1736 }
1737 (*cont_ptr)->msg = msg;
Michal Vaskoc5ea6382021-03-15 16:24:08 +01001738 msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001739 (*cont_ptr)->next = NULL;
1740
1741 ret = NC_MSG_NOTIF;
1742
1743cleanup:
1744 ly_in_free(msg, 1);
1745 return ret;
1746}
1747
1748static int
1749recv_reply_dup_rpc(struct nc_session *session, struct nc_rpc *rpc, struct lyd_node **op)
1750{
1751 LY_ERR lyrc;
1752 struct nc_rpc_act_generic *rpc_gen;
1753 struct ly_in *in;
1754 struct lyd_node *tree, *op2;
1755 const struct lys_module *mod;
Michal Vasko305faca2021-03-25 09:16:02 +01001756 const char *module_name = NULL, *rpc_name = NULL, *module_check = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001757
1758 switch (rpc->type) {
1759 case NC_RPC_ACT_GENERIC:
1760 rpc_gen = (struct nc_rpc_act_generic *)rpc;
1761 if (rpc_gen->has_data) {
1762 tree = rpc_gen->content.data;
1763
1764 /* find the operation node */
1765 lyrc = LY_EINVAL;
1766 LYD_TREE_DFS_BEGIN(tree, op2) {
1767 if (op2->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1768 lyrc = lyd_dup_single(op2, NULL, 0, op);
1769 break;
1770 }
1771 LYD_TREE_DFS_END(tree, op2);
1772 }
1773 } else {
1774 ly_in_new_memory(rpc_gen->content.xml_str, &in);
1775 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op2);
1776 ly_in_free(in, 0);
1777 if (lyrc) {
1778 return -1;
1779 }
1780
1781 /* we want just the operation node */
1782 lyrc = lyd_dup_single(op2, NULL, 0, op);
1783
1784 lyd_free_tree(tree);
1785 }
1786 break;
1787 case NC_RPC_GETCONFIG:
1788 module_name = "ietf-netconf";
1789 rpc_name = "get-config";
1790 break;
1791 case NC_RPC_EDIT:
1792 module_name = "ietf-netconf";
1793 rpc_name = "edit-config";
1794 break;
1795 case NC_RPC_COPY:
1796 module_name = "ietf-netconf";
1797 rpc_name = "copy-config";
1798 break;
1799 case NC_RPC_DELETE:
1800 module_name = "ietf-netconf";
1801 rpc_name = "delete-config";
1802 break;
1803 case NC_RPC_LOCK:
1804 module_name = "ietf-netconf";
1805 rpc_name = "lock";
1806 break;
1807 case NC_RPC_UNLOCK:
1808 module_name = "ietf-netconf";
1809 rpc_name = "unlock";
1810 break;
1811 case NC_RPC_GET:
1812 module_name = "ietf-netconf";
1813 rpc_name = "get";
1814 break;
1815 case NC_RPC_KILL:
1816 module_name = "ietf-netconf";
1817 rpc_name = "kill-session";
1818 break;
1819 case NC_RPC_COMMIT:
1820 module_name = "ietf-netconf";
1821 rpc_name = "commit";
1822 break;
1823 case NC_RPC_DISCARD:
1824 module_name = "ietf-netconf";
1825 rpc_name = "discard-changes";
1826 break;
1827 case NC_RPC_CANCEL:
1828 module_name = "ietf-netconf";
1829 rpc_name = "cancel-commit";
1830 break;
1831 case NC_RPC_VALIDATE:
1832 module_name = "ietf-netconf";
1833 rpc_name = "validate";
1834 break;
1835 case NC_RPC_GETSCHEMA:
1836 module_name = "ietf-netconf-monitoring";
1837 rpc_name = "get-schema";
1838 break;
1839 case NC_RPC_SUBSCRIBE:
1840 module_name = "notifications";
Michal Vaskoeed893d2021-05-25 17:11:08 +02001841 rpc_name = "create-subscription";
Michal Vasko77367452021-02-16 16:32:18 +01001842 break;
1843 case NC_RPC_GETDATA:
1844 module_name = "ietf-netconf-nmda";
1845 rpc_name = "get-data";
1846 break;
1847 case NC_RPC_EDITDATA:
1848 module_name = "ietf-netconf-nmda";
1849 rpc_name = "edit-data";
1850 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01001851 case NC_RPC_ESTABLISHSUB:
1852 module_name = "ietf-subscribed-notifications";
1853 rpc_name = "establish-subscription";
1854 break;
1855 case NC_RPC_MODIFYSUB:
1856 module_name = "ietf-subscribed-notifications";
1857 rpc_name = "modify-subscription";
1858 break;
1859 case NC_RPC_DELETESUB:
1860 module_name = "ietf-subscribed-notifications";
1861 rpc_name = "delete-subscription";
1862 break;
1863 case NC_RPC_KILLSUB:
1864 module_name = "ietf-subscribed-notifications";
1865 rpc_name = "kill-subscription";
1866 break;
Michal Vasko305faca2021-03-25 09:16:02 +01001867 case NC_RPC_ESTABLISHPUSH:
1868 module_name = "ietf-subscribed-notifications";
1869 rpc_name = "establish-subscription";
1870 module_check = "ietf-yang-push";
1871 break;
1872 case NC_RPC_MODIFYPUSH:
1873 module_name = "ietf-subscribed-notifications";
1874 rpc_name = "modify-subscription";
1875 module_check = "ietf-yang-push";
1876 break;
1877 case NC_RPC_RESYNCSUB:
1878 module_name = "ietf-yang-push";
1879 rpc_name = "resync-subscription";
1880 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01001881 case NC_RPC_UNKNOWN:
Michal Vasko77367452021-02-16 16:32:18 +01001882 lyrc = LY_EINT;
1883 break;
1884 }
1885
1886 if (module_name && rpc_name) {
1887 mod = ly_ctx_get_module_implemented(session->ctx, module_name);
1888 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02001889 ERR(session, "Missing \"%s\" schema in the context.", module_name);
Michal Vasko77367452021-02-16 16:32:18 +01001890 return -1;
1891 }
1892
1893 /* create the operation node */
1894 lyrc = lyd_new_inner(NULL, mod, rpc_name, 0, op);
1895 }
Michal Vasko305faca2021-03-25 09:16:02 +01001896 if (module_check) {
1897 if (!ly_ctx_get_module_implemented(session->ctx, module_check)) {
Michal Vasko05532772021-06-03 12:12:38 +02001898 ERR(session, "Missing \"%s\" schema in the context.", module_check);
Michal Vasko305faca2021-03-25 09:16:02 +01001899 return -1;
1900 }
1901 }
Michal Vasko77367452021-02-16 16:32:18 +01001902
1903 if (lyrc) {
1904 return -1;
1905 }
1906 return 0;
1907}
1908
1909API NC_MSG_TYPE
1910nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct lyd_node **envp,
1911 struct lyd_node **op)
1912{
1913 NC_MSG_TYPE ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001914
Michal Vasko45e53ae2016-04-07 11:46:03 +02001915 if (!session) {
1916 ERRARG("session");
1917 return NC_MSG_ERROR;
1918 } else if (!rpc) {
1919 ERRARG("rpc");
1920 return NC_MSG_ERROR;
Michal Vasko5a2c7162020-01-30 11:24:17 +01001921 } else if (!msgid) {
1922 ERRARG("msgid");
1923 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01001924 } else if (!envp) {
1925 ERRARG("envp");
Michal Vasko45e53ae2016-04-07 11:46:03 +02001926 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01001927 } else if (!op) {
1928 ERRARG("op");
Michal Vasko086311b2016-01-08 09:53:11 +01001929 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001930 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02001931 ERR(session, "Invalid session to receive RPC replies.");
Michal Vasko086311b2016-01-08 09:53:11 +01001932 return NC_MSG_ERROR;
1933 }
Michal Vasko77367452021-02-16 16:32:18 +01001934
1935 /* get a duplicate of the RPC node to append reply to */
1936 if (recv_reply_dup_rpc(session, rpc, op)) {
1937 return NC_MSG_ERROR;
Michal Vaskoeee99412016-11-21 10:19:43 +01001938 }
Michal Vasko086311b2016-01-08 09:53:11 +01001939
Michal Vasko77367452021-02-16 16:32:18 +01001940 /* receive a reply */
1941 ret = recv_reply(session, timeout, *op, msgid, envp);
Michal Vasko086311b2016-01-08 09:53:11 +01001942
Michal Vasko77367452021-02-16 16:32:18 +01001943 /* do not return the RPC copy on error or if the reply includes no data */
1944 if (((ret != NC_MSG_REPLY) && (ret != NC_MSG_REPLY_ERR_MSGID)) || !lyd_child(*op)) {
1945 lyd_free_tree(*op);
1946 *op = NULL;
1947 }
1948 return ret;
1949}
1950
1951static NC_MSG_TYPE
1952recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
1953{
1954 int r;
1955 LY_ERR lyrc;
1956 struct ly_in *msg = NULL;
1957 struct nc_msg_cont *cont, **cont_ptr;
1958 NC_MSG_TYPE ret = NC_MSG_ERROR;
1959
1960 *op = NULL;
1961 *envp = NULL;
1962
1963 /* try to get notification from the session's queue */
1964 while (session->opts.client.notifs) {
1965 cont = session->opts.client.notifs;
1966 session->opts.client.notifs = cont->next;
1967
1968 msg = cont->msg;
1969 free(cont);
1970
1971 /* parse */
1972 lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op);
1973 if (!lyrc) {
1974 ret = NC_MSG_NOTIF;
1975 goto cleanup;
1976 } else if (lyrc != LY_ENOT) {
1977 lyd_free_tree(*envp);
1978 *envp = NULL;
Michal Vasko05532772021-06-03 12:12:38 +02001979 ERR(session, "Received an invalid message (%s).", ly_errmsg(session->ctx));
Michal Vasko77367452021-02-16 16:32:18 +01001980 goto cleanup;
1981 } else {
1982 /* it was not a rpc-reply so it is nothing known */
Michal Vasko05532772021-06-03 12:12:38 +02001983 ERR(session, "Received an unexpected message.");
Michal Vasko086311b2016-01-08 09:53:11 +01001984 }
Michal Vasko086311b2016-01-08 09:53:11 +01001985
Michal Vasko77367452021-02-16 16:32:18 +01001986 /* try the next message */
1987 ly_in_free(msg, 1);
1988 msg = NULL;
1989 }
1990
1991 /* read message from wire */
1992 r = nc_read_msg_poll_io(session, timeout, &msg);
1993 if (!r) {
1994 ret = NC_MSG_WOULDBLOCK;
1995 goto cleanup;
1996 } else if (r == -1) {
1997 goto cleanup;
1998 }
1999
2000 /* parse */
2001 lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op);
2002 if (!lyrc) {
2003 ret = NC_MSG_NOTIF;
2004 goto cleanup;
2005 } else if (lyrc != LY_ENOT) {
2006 lyd_free_tree(*envp);
2007 *envp = NULL;
Michal Vasko05532772021-06-03 12:12:38 +02002008 ERR(session, "Received an invalid message (%s).", ly_errmsg(session->ctx));
Michal Vasko77367452021-02-16 16:32:18 +01002009 goto cleanup;
2010 }
2011
Michal Vaskoc5ea6382021-03-15 16:24:08 +01002012 /* assume a rpc-reply, reset and store it */
2013 ly_in_reset(msg);
Michal Vasko77367452021-02-16 16:32:18 +01002014 cont_ptr = &session->opts.client.replies;
2015 while (*cont_ptr) {
2016 cont_ptr = &((*cont_ptr)->next);
2017 }
2018 *cont_ptr = malloc(sizeof **cont_ptr);
2019 if (!*cont_ptr) {
2020 ERRMEM;
2021 goto cleanup;
2022 }
2023 (*cont_ptr)->msg = msg;
Michal Vaskoc5ea6382021-03-15 16:24:08 +01002024 msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002025 (*cont_ptr)->next = NULL;
2026
2027 ret = NC_MSG_REPLY;
2028
2029cleanup:
2030 ly_in_free(msg, 1);
2031 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002032}
2033
2034API NC_MSG_TYPE
Michal Vasko77367452021-02-16 16:32:18 +01002035nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
Michal Vasko086311b2016-01-08 09:53:11 +01002036{
Michal Vasko45e53ae2016-04-07 11:46:03 +02002037 if (!session) {
2038 ERRARG("session");
2039 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002040 } else if (!envp) {
2041 ERRARG("envp");
2042 return NC_MSG_ERROR;
2043 } else if (!op) {
2044 ERRARG("op");
Michal Vasko086311b2016-01-08 09:53:11 +01002045 return NC_MSG_ERROR;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002046 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002047 ERR(session, "Invalid session to receive Notifications.");
Michal Vasko086311b2016-01-08 09:53:11 +01002048 return NC_MSG_ERROR;
2049 }
2050
Michal Vasko77367452021-02-16 16:32:18 +01002051 /* receive a notification */
2052 return recv_notif(session, timeout, envp, op);
Michal Vasko086311b2016-01-08 09:53:11 +01002053}
2054
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002055static void *
2056nc_recv_notif_thread(void *arg)
2057{
2058 struct nc_ntf_thread_arg *ntarg;
2059 struct nc_session *session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002060
Michal Vasko77367452021-02-16 16:32:18 +01002061 void (*notif_clb)(struct nc_session *session, const struct lyd_node *envp, const struct lyd_node *op);
2062 struct lyd_node *envp, *op;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002063 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002064
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002065 /* detach ourselves */
Michal Vasko131120a2018-05-29 15:44:02 +02002066 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002067
2068 ntarg = (struct nc_ntf_thread_arg *)arg;
2069 session = ntarg->session;
2070 notif_clb = ntarg->notif_clb;
2071 free(ntarg);
2072
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002073 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread) == 1) {
Michal Vasko77367452021-02-16 16:32:18 +01002074 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002075 if (msgtype == NC_MSG_NOTIF) {
Michal Vasko77367452021-02-16 16:32:18 +01002076 notif_clb(session, envp, op);
2077 if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) {
2078 lyd_free_tree(envp);
2079 lyd_free_tree(op);
Michal Vaskof0537d82016-01-29 14:42:38 +01002080 break;
2081 }
Michal Vasko77367452021-02-16 16:32:18 +01002082 lyd_free_tree(envp);
2083 lyd_free_tree(op);
Michal Vaskoce326052018-01-04 10:32:03 +01002084 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002085 /* quit this thread once the session is broken */
2086 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002087 }
2088
2089 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2090 }
2091
Michal Vasko05532772021-06-03 12:12:38 +02002092 VRB(session, "Notification thread exit.");
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002093 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 0);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002094 return NULL;
2095}
2096
2097API int
Michal Vasko77367452021-02-16 16:32:18 +01002098nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session,
2099 const struct lyd_node *envp, const struct lyd_node *op))
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002100{
2101 struct nc_ntf_thread_arg *ntarg;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002102 pthread_t tid;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002103 int ret;
2104
Michal Vasko45e53ae2016-04-07 11:46:03 +02002105 if (!session) {
2106 ERRARG("session");
2107 return -1;
2108 } else if (!notif_clb) {
2109 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002110 return -1;
2111 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002112 ERR(session, "Invalid session to receive Notifications.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002113 return -1;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002114 } else if (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
Michal Vasko05532772021-06-03 12:12:38 +02002115 ERR(session, "Separate notification thread is already running.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002116 return -1;
2117 }
2118
2119 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002120 if (!ntarg) {
2121 ERRMEM;
2122 return -1;
2123 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002124 ntarg->session = session;
2125 ntarg->notif_clb = notif_clb;
2126
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002127 /* just so that nc_recv_notif_thread() does not immediately exit */
2128 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 1);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002129
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002130 ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002131 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02002132 ERR(session, "Failed to create a new thread (%s).", strerror(errno));
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002133 free(ntarg);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002134 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 0);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002135 return -1;
2136 }
2137
2138 return 0;
2139}
2140
Michal Vasko77367452021-02-16 16:32:18 +01002141static const char *
2142nc_wd2str(NC_WD_MODE wd)
2143{
2144 switch (wd) {
2145 case NC_WD_ALL:
2146 return "report-all";
2147 case NC_WD_ALL_TAG:
2148 return "report-all-tagged";
2149 case NC_WD_TRIM:
2150 return "trim";
2151 case NC_WD_EXPLICIT:
2152 return "explicit";
2153 default:
2154 break;
2155 }
2156
2157 return NULL;
2158}
2159
Michal Vasko086311b2016-01-08 09:53:11 +01002160API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002161nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002162{
2163 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002164 int dofree = 1;
Michal Vasko77367452021-02-16 16:32:18 +01002165 struct ly_in *in;
Michal Vasko90e8e692016-07-13 12:27:57 +02002166 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002167 struct nc_rpc_getconfig *rpc_gc;
2168 struct nc_rpc_edit *rpc_e;
2169 struct nc_rpc_copy *rpc_cp;
2170 struct nc_rpc_delete *rpc_del;
2171 struct nc_rpc_lock *rpc_lock;
2172 struct nc_rpc_get *rpc_g;
2173 struct nc_rpc_kill *rpc_k;
2174 struct nc_rpc_commit *rpc_com;
2175 struct nc_rpc_cancel *rpc_can;
2176 struct nc_rpc_validate *rpc_val;
2177 struct nc_rpc_getschema *rpc_gs;
2178 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002179 struct nc_rpc_getdata *rpc_getd;
2180 struct nc_rpc_editdata *rpc_editd;
Michal Vasko96f247a2021-03-15 13:32:10 +01002181 struct nc_rpc_establishsub *rpc_estsub;
2182 struct nc_rpc_modifysub *rpc_modsub;
2183 struct nc_rpc_deletesub *rpc_delsub;
2184 struct nc_rpc_killsub *rpc_killsub;
Michal Vasko305faca2021-03-25 09:16:02 +01002185 struct nc_rpc_establishpush *rpc_estpush;
2186 struct nc_rpc_modifypush *rpc_modpush;
2187 struct nc_rpc_resyncsub *rpc_resyncsub;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002188 struct lyd_node *data = NULL, *node, *cont;
Michal Vasko305faca2021-03-25 09:16:02 +01002189 const struct lys_module *mod = NULL, *mod2 = NULL, *ietfncwd;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002190 LY_ERR lyrc = 0;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002191 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002192 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002193 uint64_t cur_msgid;
2194
Michal Vasko45e53ae2016-04-07 11:46:03 +02002195 if (!session) {
2196 ERRARG("session");
2197 return NC_MSG_ERROR;
2198 } else if (!rpc) {
2199 ERRARG("rpc");
2200 return NC_MSG_ERROR;
2201 } else if (!msgid) {
2202 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002203 return NC_MSG_ERROR;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002204 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002205 ERR(session, "Invalid session to send RPCs.");
Michal Vasko086311b2016-01-08 09:53:11 +01002206 return NC_MSG_ERROR;
2207 }
2208
Michal Vaskoc1171a42019-11-05 12:06:46 +01002209 switch (rpc->type) {
2210 case NC_RPC_ACT_GENERIC:
2211 /* checked when parsing */
2212 break;
2213 case NC_RPC_GETCONFIG:
2214 case NC_RPC_EDIT:
2215 case NC_RPC_COPY:
2216 case NC_RPC_DELETE:
2217 case NC_RPC_LOCK:
2218 case NC_RPC_UNLOCK:
2219 case NC_RPC_GET:
2220 case NC_RPC_KILL:
2221 case NC_RPC_COMMIT:
2222 case NC_RPC_DISCARD:
2223 case NC_RPC_CANCEL:
2224 case NC_RPC_VALIDATE:
Michal Vasko77367452021-02-16 16:32:18 +01002225 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002226 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002227 ERR(session, "Missing \"ietf-netconf\" schema in the context.");
Michal Vasko086311b2016-01-08 09:53:11 +01002228 return NC_MSG_ERROR;
2229 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002230 break;
2231 case NC_RPC_GETSCHEMA:
Michal Vasko77367452021-02-16 16:32:18 +01002232 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002233 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002234 ERR(session, "Missing \"ietf-netconf-monitoring\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002235 return NC_MSG_ERROR;
2236 }
2237 break;
2238 case NC_RPC_SUBSCRIBE:
Michal Vasko77367452021-02-16 16:32:18 +01002239 mod = ly_ctx_get_module_implemented(session->ctx, "notifications");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002240 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002241 ERR(session, "Missing \"notifications\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002242 return NC_MSG_ERROR;
2243 }
2244 break;
2245 case NC_RPC_GETDATA:
2246 case NC_RPC_EDITDATA:
Michal Vasko77367452021-02-16 16:32:18 +01002247 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-nmda");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002248 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002249 ERR(session, "Missing \"ietf-netconf-nmda\" schema in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002250 return NC_MSG_ERROR;
2251 }
2252 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002253 case NC_RPC_ESTABLISHSUB:
2254 case NC_RPC_MODIFYSUB:
2255 case NC_RPC_DELETESUB:
2256 case NC_RPC_KILLSUB:
2257 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2258 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002259 ERR(session, "Missing \"ietf-subscribed-notifications\" schema in the context.");
Michal Vasko96f247a2021-03-15 13:32:10 +01002260 return NC_MSG_ERROR;
2261 }
2262 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002263 case NC_RPC_ESTABLISHPUSH:
2264 case NC_RPC_MODIFYPUSH:
2265 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2266 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002267 ERR(session, "Missing \"ietf-subscribed-notifications\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002268 return NC_MSG_ERROR;
2269 }
2270 mod2 = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2271 if (!mod2) {
Michal Vasko05532772021-06-03 12:12:38 +02002272 ERR(session, "Missing \"ietf-yang-push\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002273 return NC_MSG_ERROR;
2274 }
2275 break;
2276 case NC_RPC_RESYNCSUB:
2277 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2278 if (!mod) {
Michal Vasko05532772021-06-03 12:12:38 +02002279 ERR(session, "Missing \"ietf-yang-push\" schema in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002280 return NC_MSG_ERROR;
2281 }
2282 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002283 case NC_RPC_UNKNOWN:
2284 ERRINT;
2285 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002286 }
2287
Michal Vaskoab9deb62021-05-27 11:37:00 +02002288#define CHECK_LYRC_BREAK(func_call) if ((lyrc = func_call)) break;
2289
Michal Vasko086311b2016-01-08 09:53:11 +01002290 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002291 case NC_RPC_ACT_GENERIC:
2292 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002293
2294 if (rpc_gen->has_data) {
2295 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002296 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002297 } else {
Michal Vasko77367452021-02-16 16:32:18 +01002298 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2299 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &data, NULL);
2300 ly_in_free(in, 0);
2301 if (lyrc) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002302 break;
Michal Vaskoeec410f2017-11-24 09:14:55 +01002303 }
Michal Vasko086311b2016-01-08 09:53:11 +01002304 }
2305 break;
2306
2307 case NC_RPC_GETCONFIG:
2308 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2309
Michal Vaskoab9deb62021-05-27 11:37:00 +02002310 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-config", 0, &data));
2311 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
2312 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_gc->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002313 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002314 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002315 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_gc->filter, 0, LYD_ANYDATA_XML, 0, &node));
2316 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002317 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002318 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2319 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2320 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_gc->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002321 }
2322 }
2323
2324 if (rpc_gc->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002325 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002326 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002327 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002328 lyrc = LY_ENOTFOUND;
2329 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002330 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002331 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 +01002332 }
2333 break;
2334
2335 case NC_RPC_EDIT:
2336 rpc_e = (struct nc_rpc_edit *)rpc;
2337
Michal Vaskoab9deb62021-05-27 11:37:00 +02002338 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-config", 0, &data));
2339 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2340 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_e->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002341
2342 if (rpc_e->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002343 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 +01002344 }
Michal Vasko086311b2016-01-08 09:53:11 +01002345 if (rpc_e->test_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002346 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 +01002347 }
Michal Vasko086311b2016-01-08 09:53:11 +01002348 if (rpc_e->error_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002349 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 +01002350 }
Michal Vasko7793bc62016-09-16 11:58:41 +02002351 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002352 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 +01002353 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002354 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_e->edit_cont, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002355 }
2356 break;
2357
2358 case NC_RPC_COPY:
2359 rpc_cp = (struct nc_rpc_copy *)rpc;
2360
Michal Vaskoab9deb62021-05-27 11:37:00 +02002361 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "copy-config", 0, &data));
2362 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002363 if (rpc_cp->url_trg) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002364 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_trg, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002365 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002366 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002367 }
2368
Michal Vaskoab9deb62021-05-27 11:37:00 +02002369 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002370 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002371 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002372 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 +01002373 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002374 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002375 }
2376 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002377 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002378 }
2379
2380 if (rpc_cp->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002381 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002382 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002383 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002384 lyrc = LY_ENOTFOUND;
2385 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002386 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002387 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 +01002388 }
2389 break;
2390
2391 case NC_RPC_DELETE:
2392 rpc_del = (struct nc_rpc_delete *)rpc;
2393
Michal Vaskoab9deb62021-05-27 11:37:00 +02002394 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-config", 0, &data));
2395 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002396 if (rpc_del->url) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002397 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_del->url, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002398 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002399 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_del->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002400 }
2401 break;
2402
2403 case NC_RPC_LOCK:
2404 rpc_lock = (struct nc_rpc_lock *)rpc;
2405
Michal Vaskoab9deb62021-05-27 11:37:00 +02002406 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "lock", 0, &data));
2407 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2408 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002409 break;
2410
2411 case NC_RPC_UNLOCK:
2412 rpc_lock = (struct nc_rpc_lock *)rpc;
2413
Michal Vaskoab9deb62021-05-27 11:37:00 +02002414 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "unlock", 0, &data));
2415 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2416 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002417 break;
2418
2419 case NC_RPC_GET:
2420 rpc_g = (struct nc_rpc_get *)rpc;
2421
Michal Vaskoab9deb62021-05-27 11:37:00 +02002422 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002423 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002424 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002425 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_g->filter, 0, LYD_ANYDATA_XML, 0, &node));
2426 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002427 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002428 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2429 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2430 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_g->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002431 }
2432 }
2433
2434 if (rpc_g->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002435 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002436 if (!ietfncwd) {
Michal Vasko05532772021-06-03 12:12:38 +02002437 ERR(session, "Missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002438 lyrc = LY_ENOTFOUND;
2439 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002440 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002441 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 +01002442 }
2443 break;
2444
2445 case NC_RPC_KILL:
2446 rpc_k = (struct nc_rpc_kill *)rpc;
2447
Michal Vaskoab9deb62021-05-27 11:37:00 +02002448 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-session", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002449 sprintf(str, "%u", rpc_k->sid);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002450 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "session-id", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002451 break;
2452
2453 case NC_RPC_COMMIT:
2454 rpc_com = (struct nc_rpc_commit *)rpc;
2455
Michal Vaskoab9deb62021-05-27 11:37:00 +02002456 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002457 if (rpc_com->confirmed) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002458 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirmed", NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002459 }
2460
2461 if (rpc_com->confirm_timeout) {
2462 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002463 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirm-timeout", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002464 }
Michal Vasko086311b2016-01-08 09:53:11 +01002465 if (rpc_com->persist) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002466 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist", rpc_com->persist, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002467 }
Michal Vasko086311b2016-01-08 09:53:11 +01002468 if (rpc_com->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002469 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_com->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002470 }
2471 break;
2472
2473 case NC_RPC_DISCARD:
Michal Vaskoab9deb62021-05-27 11:37:00 +02002474 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "discard-changes", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002475 break;
2476
2477 case NC_RPC_CANCEL:
2478 rpc_can = (struct nc_rpc_cancel *)rpc;
2479
Michal Vaskoab9deb62021-05-27 11:37:00 +02002480 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "cancel-commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002481 if (rpc_can->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002482 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_can->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002483 }
2484 break;
2485
2486 case NC_RPC_VALIDATE:
2487 rpc_val = (struct nc_rpc_validate *)rpc;
2488
Michal Vaskoab9deb62021-05-27 11:37:00 +02002489 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "validate", 0, &data));
2490 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002491 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002492 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002493 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 +01002494 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002495 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_val->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002496 }
2497 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002498 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_val->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002499 }
2500 break;
2501
2502 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002503 rpc_gs = (struct nc_rpc_getschema *)rpc;
2504
Michal Vaskoab9deb62021-05-27 11:37:00 +02002505 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-schema", 0, &data));
2506 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "identifier", rpc_gs->identifier, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002507 if (rpc_gs->version) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002508 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "version", rpc_gs->version, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002509 }
2510 if (rpc_gs->format) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002511 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "format", rpc_gs->format, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002512 }
2513 break;
2514
2515 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002516 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2517
Michal Vaskoab9deb62021-05-27 11:37:00 +02002518 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "create-subscription", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002519 if (rpc_sub->stream) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002520 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_sub->stream, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002521 }
2522
2523 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002524 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002525 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_sub->filter, 0, LYD_ANYDATA_XML, 0, &node));
2526 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002527 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002528 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2529 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2530 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_sub->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002531 }
2532 }
Michal Vasko086311b2016-01-08 09:53:11 +01002533 if (rpc_sub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002534 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "startTime", rpc_sub->start, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002535 }
Michal Vasko086311b2016-01-08 09:53:11 +01002536 if (rpc_sub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002537 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stopTime", rpc_sub->stop, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002538 }
2539 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002540
2541 case NC_RPC_GETDATA:
2542 rpc_getd = (struct nc_rpc_getdata *)rpc;
2543
Michal Vaskoab9deb62021-05-27 11:37:00 +02002544 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-data", 0, &data));
2545 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_getd->datastore, 0, NULL));
2546
Michal Vaskoc1171a42019-11-05 12:06:46 +01002547 if (rpc_getd->filter) {
2548 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002549 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 +01002550 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002551 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "xpath-filter", rpc_getd->filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002552 }
2553 }
2554 if (rpc_getd->config_filter) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002555 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "config-filter", rpc_getd->config_filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002556 }
2557 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002558 CHECK_LYRC_BREAK(lyd_new_term(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" :
2559 "origin-filter", rpc_getd->origin_filter[i], 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002560 }
2561 if (rpc_getd->max_depth) {
2562 sprintf(str, "%u", rpc_getd->max_depth);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002563 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "max-depth", str, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002564 }
2565 if (rpc_getd->with_origin) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002566 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-origin", NULL, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002567 }
2568
2569 if (rpc_getd->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002570 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
Michal Vaskoab9deb62021-05-27 11:37:00 +02002571 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 +01002572 }
2573 break;
2574
2575 case NC_RPC_EDITDATA:
2576 rpc_editd = (struct nc_rpc_editdata *)rpc;
2577
Michal Vaskoab9deb62021-05-27 11:37:00 +02002578 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-data", 0, &data));
2579 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_editd->datastore, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002580
2581 if (rpc_editd->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002582 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op], 0,
2583 NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002584 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002585 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002586 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 +01002587 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002588 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_editd->edit_cont, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002589 }
2590 break;
2591
Michal Vasko96f247a2021-03-15 13:32:10 +01002592 case NC_RPC_ESTABLISHSUB:
2593 rpc_estsub = (struct nc_rpc_establishsub *)rpc;
2594
Michal Vaskoab9deb62021-05-27 11:37:00 +02002595 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002596
2597 if (rpc_estsub->filter) {
2598 if (!rpc_estsub->filter[0] || (rpc_estsub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002599 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_estsub->filter, 0, LYD_ANYDATA_XML,
2600 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002601 } else if (rpc_estsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002602 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002603 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002604 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002605 }
2606 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002607 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_estsub->stream, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002608
2609 if (rpc_estsub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002610 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "replay-start-time", rpc_estsub->start, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002611 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002612 if (rpc_estsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002613 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002614 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002615 if (rpc_estsub->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002616 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estsub->encoding, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002617 }
2618 break;
2619
2620 case NC_RPC_MODIFYSUB:
2621 rpc_modsub = (struct nc_rpc_modifysub *)rpc;
2622
Michal Vaskoab9deb62021-05-27 11:37:00 +02002623 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002624
2625 sprintf(str, "%u", rpc_modsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002626 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002627
2628 if (rpc_modsub->filter) {
2629 if (!rpc_modsub->filter[0] || (rpc_modsub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002630 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_modsub->filter, 0, LYD_ANYDATA_XML,
2631 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002632 } else if (rpc_modsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002633 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002634 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002635 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002636 }
2637 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002638 if (rpc_modsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002639 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002640 }
2641 break;
2642
2643 case NC_RPC_DELETESUB:
2644 rpc_delsub = (struct nc_rpc_deletesub *)rpc;
2645
Michal Vaskoab9deb62021-05-27 11:37:00 +02002646 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002647
2648 sprintf(str, "%u", rpc_delsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002649 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002650 break;
2651
2652 case NC_RPC_KILLSUB:
2653 rpc_killsub = (struct nc_rpc_killsub *)rpc;
2654
Michal Vaskoab9deb62021-05-27 11:37:00 +02002655 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002656
2657 sprintf(str, "%u", rpc_killsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002658 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002659 break;
2660
Michal Vasko305faca2021-03-25 09:16:02 +01002661 case NC_RPC_ESTABLISHPUSH:
2662 rpc_estpush = (struct nc_rpc_establishpush *)rpc;
2663
Michal Vaskoab9deb62021-05-27 11:37:00 +02002664 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
2665 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_estpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002666
2667 if (rpc_estpush->filter) {
2668 if (!rpc_estpush->filter[0] || (rpc_estpush->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002669 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_estpush->filter, 0,
2670 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002671 } else if (rpc_estpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002672 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002673 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002674 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002675 }
2676 }
2677
2678 if (rpc_estpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002679 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002680 }
Michal Vasko305faca2021-03-25 09:16:02 +01002681 if (rpc_estpush->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002682 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estpush->encoding, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002683 }
2684
2685 if (rpc_estpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002686 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002687 sprintf(str, "%" PRIu32, rpc_estpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002688 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002689 if (rpc_estpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002690 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_estpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002691 }
2692 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002693 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002694 if (rpc_estpush->dampening_period) {
2695 sprintf(str, "%" PRIu32, rpc_estpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002696 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002697 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002698 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "sync-on-start", rpc_estpush->sync_on_start ? "true" : "false", 0,
2699 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002700 if (rpc_estpush->excluded_change) {
2701 for (i = 0; rpc_estpush->excluded_change[i]; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002702 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "excluded-change", rpc_estpush->excluded_change[i], 0,
2703 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002704 }
2705 }
2706 }
2707 break;
2708
2709 case NC_RPC_MODIFYPUSH:
2710 rpc_modpush = (struct nc_rpc_modifypush *)rpc;
2711
Michal Vaskoab9deb62021-05-27 11:37:00 +02002712 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01002713
2714 sprintf(str, "%u", rpc_modpush->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002715 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
2716 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_modpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002717
2718 if (rpc_modpush->filter) {
2719 if (!rpc_modpush->filter[0] || (rpc_modpush->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002720 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_modpush->filter, 0,
2721 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002722 } else if (rpc_modpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002723 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002724 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002725 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002726 }
2727 }
Michal Vasko305faca2021-03-25 09:16:02 +01002728 if (rpc_modpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002729 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002730 }
2731
2732 if (rpc_modpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002733 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002734 sprintf(str, "%" PRIu32, rpc_modpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002735 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002736 if (rpc_modpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002737 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_modpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002738 }
2739 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002740 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01002741 if (rpc_modpush->dampening_period) {
2742 sprintf(str, "%" PRIu32, rpc_modpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002743 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002744 }
2745 }
2746 break;
2747
2748 case NC_RPC_RESYNCSUB:
2749 rpc_resyncsub = (struct nc_rpc_resyncsub *)rpc;
2750
Michal Vaskoab9deb62021-05-27 11:37:00 +02002751 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "resync-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01002752 sprintf(str, "%u", rpc_resyncsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002753 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01002754 break;
2755
Michal Vasko96f247a2021-03-15 13:32:10 +01002756 case NC_RPC_UNKNOWN:
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002757 ERRINT;
2758 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002759 }
2760
Michal Vaskoab9deb62021-05-27 11:37:00 +02002761#undef CHECK_LYRC_BREAK
2762
2763 if (lyrc) {
Michal Vasko05532772021-06-03 12:12:38 +02002764 ERR(session, "Failed to create RPC, perhaps a required feature is disabled.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002765 lyd_free_tree(data);
Michal Vasko131120a2018-05-29 15:44:02 +02002766 return NC_MSG_ERROR;
2767 }
2768
Michal Vasko131120a2018-05-29 15:44:02 +02002769 /* send RPC, store its message ID */
2770 r = nc_send_msg_io(session, timeout, data);
2771 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002772
Radek Krejcib4b19062018-02-07 16:33:06 +01002773 if (dofree) {
Michal Vasko77367452021-02-16 16:32:18 +01002774 lyd_free_tree(data);
Radek Krejcib4b19062018-02-07 16:33:06 +01002775 }
Michal Vasko086311b2016-01-08 09:53:11 +01002776
Michal Vasko131120a2018-05-29 15:44:02 +02002777 if (r == NC_MSG_RPC) {
2778 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002779 }
Michal Vasko131120a2018-05-29 15:44:02 +02002780 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002781}
Michal Vaskode2946c2017-01-12 12:19:26 +01002782
2783API void
2784nc_client_session_set_not_strict(struct nc_session *session)
2785{
2786 if (session->side != NC_CLIENT) {
2787 ERRARG("session");
2788 return;
2789 }
2790
2791 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2792}