blob: 2ae88c0a4d62bf873217edb64991837809464825 [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 Vasko086311b2016-01-08 09:53:11 +010021#include <assert.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010025#include <netinet/in.h>
Michal Vasko83ad17e2019-01-30 10:11:37 +010026#include <netinet/tcp.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <pthread.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/socket.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020031#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010032#include <sys/stat.h>
33#include <sys/types.h>
Michal Vasko16799662020-04-07 13:09:11 +020034#include <sys/select.h>
Michal Vasko086311b2016-01-08 09:53:11 +010035#include <unistd.h>
36#include <arpa/inet.h>
37#include <poll.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020038#include <pwd.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 Vasko1a38c862016-01-15 15:50:07 +010044#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010045#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010046
Michal Vasko8a4e1462020-05-07 11:32:31 +020047#include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h"
48#include "../modules/ietf_netconf@2013-09-29_yang.h"
49
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);
56char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
57#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
Radek Krejci62aa0642017-05-25 16:33:49 +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 */
139 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
140 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) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200223 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200224 if (!ctx) {
225 return EXIT_FAILURE;
226 }
227
228 /* user path must be first, the first path is used to store schemas retreived via get-schema */
229 if (client_opts.schema_searchpath) {
230 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200231 } else if (!access(NC_YANG_DIR, F_OK)) {
232 ly_ctx_set_searchdir(ctx, NC_YANG_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200233 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200234
235 /* set callback for getting schemas, if provided */
236 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
237 } else {
238 session->flags |= NC_SESSION_SHAREDCTX;
239 }
240
241 session->ctx = ctx;
242
243 return EXIT_SUCCESS;
244}
245
Michal Vasko086311b2016-01-08 09:53:11 +0100246API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100247nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100248{
Michal Vasko3031aae2016-01-27 16:07:18 +0100249 if (client_opts.schema_searchpath) {
250 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100251 }
Michal Vasko086311b2016-01-08 09:53:11 +0100252
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100253 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100254 client_opts.schema_searchpath = strdup(path);
255 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100256 ERRMEM;
257 return 1;
258 }
259 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100260 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100261 }
262
263 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100264}
265
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100266API const char *
267nc_client_get_schema_searchpath(void)
268{
269 return client_opts.schema_searchpath;
270}
271
Radek Krejcifd5b6682017-06-13 15:52:53 +0200272API int
273nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100274{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200275 client_opts.schema_clb = clb;
276 if (clb) {
277 client_opts.schema_clb_data = user_data;
278 } else {
279 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100280 }
281
282 return 0;
283}
284
Radek Krejcifd5b6682017-06-13 15:52:53 +0200285API ly_module_imp_clb
286nc_client_get_schema_callback(void **user_data)
287{
288 if (user_data) {
289 (*user_data) = client_opts.schema_clb_data;
290 }
291 return client_opts.schema_clb;
292}
293
Radek Krejci65ef6d52018-08-16 16:35:02 +0200294
295struct schema_info {
296 char *name;
297 char *revision;
298 struct {
299 char *name;
300 char *revision;
301 } *submodules;
302 struct ly_set features;
303 int implemented;
304};
305
306struct clb_data_s {
307 void *user_data;
308 ly_module_imp_clb user_clb;
309 struct schema_info *schemas;
310 struct nc_session *session;
311 int has_get_schema;
312};
313
314static char *
315retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
316 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100317{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200318 char *localfile = NULL;
319 FILE *f;
320 long length, l;
321 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100322
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200323 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
324 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
325 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200326 return NULL;
327 }
328 if (localfile) {
329 VRB("Session %u: reading schema from localfile \"%s\".", clb_data->session->id, localfile);
330 f = fopen(localfile, "r");
331 if (!f) {
332 ERR("Session %u: unable to open \"%s\" file to get schema (%s).",
333 clb_data->session->id, localfile, strerror(errno));
334 free(localfile);
335 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100336 }
Michal Vasko086311b2016-01-08 09:53:11 +0100337
Radek Krejci65ef6d52018-08-16 16:35:02 +0200338 fseek(f, 0, SEEK_END);
339 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200340 if (length < 0) {
341 ERR("Session %u: unable to get size of schema file \"%s\".",
342 clb_data->session->id, localfile);
343 free(localfile);
344 fclose(f);
345 return NULL;
346 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200347 fseek(f, 0, SEEK_SET);
348
349 model_data = malloc(length + 1);
350 if (!model_data) {
351 ERRMEM;
352 } else if ((l = fread(model_data, 1, length, f)) != length) {
353 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
354 clb_data->session->id, localfile, l, length);
355 free(model_data);
356 model_data = NULL;
357 } else {
358 /* terminating NULL byte */
359 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100360 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200361 fclose(f);
362 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100363 }
364
Radek Krejci65ef6d52018-08-16 16:35:02 +0200365 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100366}
367
368static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200369retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
370 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100371{
Michal Vasko086311b2016-01-08 09:53:11 +0100372 struct nc_rpc *rpc;
373 struct nc_reply *reply;
374 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200375 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200376 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100377 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100378 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200379 char *localfile = NULL;
380 FILE *f;
381 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100382
Radek Krejci65ef6d52018-08-16 16:35:02 +0200383 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
384 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100385
Radek Krejci65ef6d52018-08-16 16:35:02 +0200386 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100387 usleep(1000);
388 }
389 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200390 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100391 nc_rpc_free(rpc);
392 return NULL;
393 }
394
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200395 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200396 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Robin Jarry52ddb952019-10-15 16:23:01 +0200397 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100398 nc_rpc_free(rpc);
399 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200400 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100401 return NULL;
Robin Jarry52ddb952019-10-15 16:23:01 +0200402 } else if (msg == NC_MSG_ERROR || reply == NULL) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200403 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100404 return NULL;
405 }
406
Michal Vasko998ba412016-09-16 12:00:07 +0200407 switch (reply->type) {
408 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200409 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200410 nc_reply_free(reply);
411 return NULL;
412 case NC_RPL_DATA:
413 /* fine */
414 break;
415 case NC_RPL_ERROR:
416 error_rpl = (struct nc_reply_error *)reply;
417 if (error_rpl->count) {
418 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200419 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200420 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200421 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200422 }
423 nc_reply_free(reply);
424 return NULL;
425 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200426 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100427 nc_reply_free(reply);
428 return NULL;
429 }
430
Michal Vasko086311b2016-01-08 09:53:11 +0100431 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko1c042702018-11-29 08:27:51 +0100432 if (!data_rpl->data || (data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
Michal Vaskob2583f12016-05-12 11:40:23 +0200433 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200434 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200435 nc_reply_free(reply);
436 return NULL;
437 }
Radek Krejci539efb62016-08-24 15:05:16 +0200438 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
439 switch (get_schema_data->value_type) {
440 case LYD_ANYDATA_CONSTSTRING:
441 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200442 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200443 break;
444 case LYD_ANYDATA_DATATREE:
445 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
446 break;
447 case LYD_ANYDATA_XML:
448 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
449 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200450 case LYD_ANYDATA_JSON:
451 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200452 case LYD_ANYDATA_SXML:
453 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200454 case LYD_ANYDATA_LYB:
455 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200456 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200457 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200458 }
Michal Vasko086311b2016-01-08 09:53:11 +0100459 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100460
Radek Krejci488b0702018-08-29 14:47:15 +0200461 if (model_data && !model_data[0]) {
462 /* empty data */
463 free(model_data);
464 model_data = NULL;
465 }
466
Radek Krejcifd5b6682017-06-13 15:52:53 +0200467 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200468 if (model_data) {
469 *format = LYS_IN_YANG;
470 if (client_opts.schema_searchpath) {
471 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
472 rev ? "@" : "", rev ? rev : "") == -1) {
473 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100474 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200475 f = fopen(localfile, "w");
476 if (!f) {
477 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
478 localfile, strerror(errno));
479 } else {
480 fputs(model_data, f);
481 fclose(f);
482 }
483 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100484 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200485 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200486 }
487
Michal Vasko086311b2016-01-08 09:53:11 +0100488 return model_data;
489}
490
Jan Kundrát35972df2018-09-06 19:00:01 +0200491static void free_with_user_data(void *data, void *user_data)
492{
493 free(data);
494 (void)user_data;
495}
496
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200497static const char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200498retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
Jan Kundrát35972df2018-09-06 19:00:01 +0200499 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200500{
501 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
502 unsigned int u, v, match = 1;
503 const char *name = NULL, *rev = NULL;
504 char *model_data = NULL;
505
506 /* get and check the final name and revision of the schema to be retrieved */
507 if (!mod_rev || !mod_rev[0]) {
508 /* newest revision requested - get the newest revision from the list of available modules on server */
509 match = 0;
510 for (u = 0; clb_data->schemas[u].name; ++u) {
511 if (strcmp(mod_name, clb_data->schemas[u].name)) {
512 continue;
513 }
514 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
515 mod_rev = clb_data->schemas[u].revision;
516 }
517 match = u + 1;
518 }
519 if (!match) {
Michal Vasko1d2665c2021-02-12 09:28:44 +0100520 /* valid situation if we are retrieving YANG 1.1 schema and have only capabilities for now
521 * (when loading ietf-datastore for ietf-yang-library) */
522 VRB("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200523 clb_data->session->id, mod_name);
524 }
525 }
526 if (submod_name) {
527 name = submod_name;
528 if (sub_rev) {
529 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200530 } else if (match) {
531 if (!clb_data->schemas[match - 1].submodules) {
Michal Vasko1d2665c2021-02-12 09:28:44 +0100532 VRB("Session %u: Unable to identify revision of the requested submodule \"%s\", in schema \"%s\", from the available server side information.",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200533 clb_data->session->id, submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200534 } else {
535 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
536 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
537 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
538 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200539 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200540 if (!rev) {
541 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
542 clb_data->session->id, submod_name, mod_name);
543 return NULL;
544 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200545 }
546 }
547 } else {
548 name = mod_name;
549 rev = mod_rev;
550 }
551
Michal Vasko1d2665c2021-02-12 09:28:44 +0100552 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev ? rev : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200553
554 if (match) {
555 /* we have enough information to avoid communication with server and try to get
556 * the schema locally */
557
558 /* 1. try to get data locally */
559 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
560
561 /* 2. try to use <get-schema> */
562 if (!model_data && clb_data->has_get_schema) {
563 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
564 }
565 } else {
566 /* we are unsure which revision of the schema we should load, so first try to get
567 * the newest revision from the server via get-schema and only if the server does not
568 * implement get-schema, try to load the newest revision locally. This is imperfect
569 * solution, but there are situation when a client does not know what revision is
570 * actually implemented by the server. */
571
572 /* 1. try to use <get-schema> */
573 if (clb_data->has_get_schema) {
574 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
575 }
576
577 /* 2. try to get data locally */
578 if (!model_data) {
579 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
580 }
581 }
582
583 /* 3. try to use user callback */
584 if (!model_data && clb_data->user_clb) {
585 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
586 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
587 }
588
Jan Kundrát35972df2018-09-06 19:00:01 +0200589 *free_module_data = free_with_user_data;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200590 return model_data;
591}
592
Michal Vaskoceae0152018-02-14 16:03:59 +0100593static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200594nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200595 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, const struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100596{
597 int ret = 0;
598 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200599 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200600 LYS_INFORMAT format;
Jan Kundrát35972df2018-09-06 19:00:01 +0200601 void (*free_module_data)(void*, void*) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200602 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100603
Radek Krejci65ef6d52018-08-16 16:35:02 +0200604 *mod = NULL;
605 if (revision) {
606 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
607 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100608 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200609 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100610 /* make the present module implemented */
611 if (lys_set_implemented(*mod)) {
612 ERR("Failed to implement model \"%s\".", (*mod)->name);
613 ret = -1;
614 }
615 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200616 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100617 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200618 clb_data.has_get_schema = has_get_schema;
619 clb_data.schemas = schemas;
620 clb_data.session = session;
621 clb_data.user_clb = user_clb;
622 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100623
624 /* clear all the errors and just collect them for now */
625 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200626 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100627
Radek Krejci65ef6d52018-08-16 16:35:02 +0200628 /* get module data */
629 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100630
Radek Krejci65ef6d52018-08-16 16:35:02 +0200631 if (module_data) {
632 /* parse the schema */
633 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100634
Radek Krejci65ef6d52018-08-16 16:35:02 +0200635 *mod = lys_parse_mem(session->ctx, module_data, format);
636 if (*free_module_data) {
Jan Kundrát35972df2018-09-06 19:00:01 +0200637 (*free_module_data)((char*)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100638 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100639
Radek Krejci65ef6d52018-08-16 16:35:02 +0200640 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
641 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100642
Michal Vaskodbf7c272018-02-19 09:07:59 +0100643 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200644 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100645 if (!(*mod)) {
646 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
647 ly_err_print(eitem);
648 }
649 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200650 } else {
651 /* print only warnings */
652 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
653 if (eitem->level == LY_LLWRN) {
654 ly_err_print(eitem);
655 }
656 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100657 }
658
Michal Vaskodbf7c272018-02-19 09:07:59 +0100659 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100660 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100661 }
662
663 return ret;
664}
665
Radek Krejci65ef6d52018-08-16 16:35:02 +0200666static void
667free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200668{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200669 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200670
Radek Krejci65ef6d52018-08-16 16:35:02 +0200671 if (!list) {
672 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200673 }
674
Radek Krejci65ef6d52018-08-16 16:35:02 +0200675 for (u = 0; list[u].name; ++u) {
676 free(list[u].name);
677 free(list[u].revision);
678 for (v = 0; v < list[u].features.number; ++v) {
679 free(list[u].features.set.g[v]);
680 }
681 free(list[u].features.set.g);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200682 if (list[u].submodules) {
683 for (v = 0; list[u].submodules[v].name; ++v) {
684 free(list[u].submodules[v].name);
685 free(list[u].submodules[v].revision);
686 }
687 free(list[u].submodules);
688 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200689 }
690 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200691}
692
Radek Krejci235d8cb2018-08-17 14:04:32 +0200693
694static int
695build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200696{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200697 struct nc_rpc *rpc = NULL;
698 struct nc_reply *reply = NULL;
699 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200700 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200701 NC_MSG_TYPE msg;
702 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200703 struct ly_set *modules = NULL;
Radek Krejci2d088832018-08-21 13:40:14 +0200704 unsigned int u, v, submodules_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200705 struct lyd_node *iter, *child;
706 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200707 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200708
709 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200710 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
711 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
712 } else {
713 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0, NC_PARAMTYPE_CONST);
714 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200715 if (!rpc) {
716 goto cleanup;
717 }
718
719 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
720 usleep(1000);
721 }
722 if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200723 WRN("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200724 session->id);
725 goto cleanup;
726 }
727
728 do {
729 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Robin Jarry52ddb952019-10-15 16:23:01 +0200730 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200731 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200732 WRN("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200733 goto cleanup;
Robin Jarry52ddb952019-10-15 16:23:01 +0200734 } else if (msg == NC_MSG_ERROR || reply == NULL) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200735 WRN("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200736 goto cleanup;
737 }
738
739 switch (reply->type) {
740 case NC_RPL_OK:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200741 WRN("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200742 goto cleanup;
743 case NC_RPL_DATA:
744 /* fine */
745 break;
746 case NC_RPL_ERROR:
747 error_rpl = (struct nc_reply_error *)reply;
748 if (error_rpl->count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200749 WRN("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200750 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
751 } else {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200752 WRN("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200753 }
754 goto cleanup;
755 case NC_RPL_NOTIF:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200756 WRN("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200757 goto cleanup;
758 }
759
Radek Krejci65ef6d52018-08-16 16:35:02 +0200760 yldata = ((struct nc_reply_data *)reply)->data;
761 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200762 WRN("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200763 goto cleanup;
764 }
765
Radek Krejci65ef6d52018-08-16 16:35:02 +0200766 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200767 if (!modules) {
Radek Krejci2d088832018-08-21 13:40:14 +0200768 WRN("Session %u: no module information in reply to a yang-library <get> RPC.", session->id);
769 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200770 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200771
Radek Krejci2d088832018-08-21 13:40:14 +0200772 (*result) = calloc(modules->number + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200773 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200774 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200775 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200776 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200777 }
778
Radek Krejci2d088832018-08-21 13:40:14 +0200779 for (u = 0; u < modules->number; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200780 submodules_count = 0;
781 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200782 LY_TREE_FOR(modules->set.d[u]->child, iter) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200783 if (iter->schema->module != mod) {
784 /* ignore node from other schemas (augments) */
785 continue;
786 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200787 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
788 /* ignore empty nodes */
789 continue;
790 }
791 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200792 (*result)[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200793 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200794 (*result)[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200795 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200796 (*result)[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200797 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200798 ly_set_add(&(*result)[u].features, (void *)strdup(((struct lyd_node_leaf_list *)iter)->value_str), LY_SET_OPT_USEASLIST);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200799 } else if (!strcmp(iter->schema->name, "submodule")) {
800 submodules_count++;
801 }
802 }
803
804 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200805 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
806 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200807 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200808 free_schema_info(*result);
809 *result = NULL;
810 ret = EXIT_FAILURE;
811 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200812 } else {
813 v = 0;
814 LY_TREE_FOR(modules->set.d[u]->child, iter) {
815 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
816 if (mod == iter->schema->module && !strcmp(iter->schema->name, "submodule")) {
817 LY_TREE_FOR(iter->child, child) {
818 if (mod != child->schema->module) {
819 continue;
820 } else if (!strcmp(child->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200821 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200822 } else if (!strcmp(child->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200823 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200824 }
825 }
826 }
827 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200828 }
829 }
830 }
831
Radek Krejcifd5b6682017-06-13 15:52:53 +0200832cleanup:
833 nc_rpc_free(rpc);
834 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200835 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200836
Radek Krejci235d8cb2018-08-17 14:04:32 +0200837 if (session->status != NC_STATUS_RUNNING) {
838 /* something bad heppened, discard the session */
839 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
840 ret = EXIT_FAILURE;
841 }
842
843 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200844}
845
Radek Krejci235d8cb2018-08-17 14:04:32 +0200846static int
847build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200848{
849 unsigned int u, v;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200850 char *module_cpblt, *ptr, *ptr2;
851
852 for (u = 0; cpblts[u]; ++u);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200853 (*result) = calloc(u + 1, sizeof **result);
854 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200855 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200856 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200857 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200858
859 for (u = v = 0; cpblts[u]; ++u) {
860 module_cpblt = strstr(cpblts[u], "module=");
861 /* this capability requires a module */
862 if (!module_cpblt) {
863 continue;
864 }
865
866 /* get module's name */
867 ptr = (char *)module_cpblt + 7;
868 ptr2 = strchr(ptr, '&');
869 if (!ptr2) {
870 ptr2 = ptr + strlen(ptr);
871 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200872 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200873
874 /* get module's revision */
875 ptr = strstr(module_cpblt, "revision=");
876 if (ptr) {
877 ptr += 9;
878 ptr2 = strchr(ptr, '&');
879 if (!ptr2) {
880 ptr2 = ptr + strlen(ptr);
881 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200882 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200883 }
884
885 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200886 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200887
888 /* get module's features */
889 ptr = strstr(module_cpblt, "features=");
890 if (ptr) {
891 ptr += 9;
892 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
893 if (*ptr == ',') {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200894 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200895 ptr2 = ptr + 1;
896 }
897 }
898 /* the last one */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200899 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200900 }
901 ++v;
902 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200903
904 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200905}
906
907static int
908nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
909{
910 int ret = EXIT_FAILURE;
911 const struct lys_module *mod;
912 unsigned int u, v;
913
914 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200915 /* skip import-only modules */
916 if (!modules[u].implemented) {
917 continue;
918 }
919
Radek Krejci65ef6d52018-08-16 16:35:02 +0200920 /* we can continue even if it fails */
921 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
922
923 if (!mod) {
924 if (session->status != NC_STATUS_RUNNING) {
925 /* something bad heppened, discard the session */
926 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
927 goto cleanup;
928 }
929
930 /* all loading ways failed, the schema will be ignored in the received data */
931 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
932 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
933 } else {
934 /* set features - first disable all to enable specified then */
935 lys_features_disable(mod, "*");
936 for (v = 0; v < modules[u].features.number; v++) {
937 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
938 }
939 }
Michal Vasko60f66602017-10-17 13:52:18 +0200940 }
941
Radek Krejci65ef6d52018-08-16 16:35:02 +0200942 /* done */
943 ret = EXIT_SUCCESS;
944
945cleanup:
946
Radek Krejcifd5b6682017-06-13 15:52:53 +0200947 return ret;
948}
949
Radek Krejci65ef6d52018-08-16 16:35:02 +0200950static int
951nc_ctx_fill_ietf_netconf(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
952{
953 unsigned int u, v;
954 const struct lys_module *ietfnc;
955
956 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
957 if (!ietfnc) {
958 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200959 if (!ietfnc) {
960 ietfnc = lys_parse_mem(session->ctx, ietf_netconf_2013_09_29_yang, LYS_YANG);
961 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200962 }
963 if (!ietfnc) {
964 ERR("Loading base NETCONF schema failed.");
965 return 1;
966 }
967
968 /* set supported capabilities from ietf-netconf */
969 for (u = 0; modules[u].name; ++u) {
970 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
971 continue;
972 }
973
974 lys_features_disable(ietfnc, "*");
975 for (v = 0; v < modules[u].features.number; v++) {
976 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
977 }
978 }
979
980 return 0;
981}
982
Michal Vasko086311b2016-01-08 09:53:11 +0100983int
984nc_ctx_check_and_fill(struct nc_session *session)
985{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200986 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100987 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100988 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200989 const struct lys_module *mod = NULL;
990 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200991 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100992
Michal Vasko2e6defd2016-10-07 15:48:15 +0200993 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100994
Radek Krejci65ef6d52018-08-16 16:35:02 +0200995 /* 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 +0200996 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200997
Radek Krejci65ef6d52018-08-16 16:35:02 +0200998 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
999 * schemas only to those present on the server side */
1000 ly_ctx_set_disable_searchdirs(session->ctx);
1001
1002 /* our callback is set later with appropriate data */
1003 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
1004
1005 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001006 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001007 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001008 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +02001009 if (yanglib_support) {
1010 break;
1011 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001012 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001013 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +02001014 if (get_schema_support) {
1015 break;
1016 }
Michal Vasko086311b2016-01-08 09:53:11 +01001017 }
1018 }
Michal Vasko31dd4c52020-04-17 10:29:44 +02001019 if (get_schema_support) {
1020 VRB("Session %u: capability for <get-schema> support found.", session->id);
1021 } else {
1022 VRB("Session %u: capability for <get-schema> support not found.", session->id);
1023 }
1024 if (yanglib_support) {
1025 VRB("Session %u: capability for yang-library support found.", session->id);
1026 } else {
1027 VRB("Session %u: capability for yang-library support not found.", session->id);
1028 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001029
1030 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001031 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
1032 ERR("Session %u: unable to get server's schema information from the <hello>'s capabilities.", session->id);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001033 goto cleanup;
1034 }
1035
Michal Vasko086311b2016-01-08 09:53:11 +01001036 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Michal Vasko8a4e1462020-05-07 11:32:31 +02001037 if (get_schema_support && !lys_parse_mem(session->ctx, ietf_netconf_monitoring_2010_10_04_yang, LYS_YANG)) {
1038 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
1039 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001040 }
1041
1042 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001043 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001044 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001045 }
1046
Radek Krejci65ef6d52018-08-16 16:35:02 +02001047 /* get correct version of ietf-yang-library into context */
1048 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001049 /* use get schema to get server's ietf-yang-library */
1050 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1051 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001052 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
1053 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001054 yanglib_support = 0;
1055 } else {
1056 revision = strndup(&revision[9], 10);
Michal Vasko0d877f92020-04-02 13:52:43 +02001057 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data,
1058 get_schema_support, &mod)) {
1059 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, unable to use it to learn all the supported modules.",
1060 session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001061 yanglib_support = 0;
1062 }
Michal Vasko0d877f92020-04-02 13:52:43 +02001063 if (strcmp(revision, "2019-01-04") >= 0) {
1064 /* we also need ietf-datastores to be implemented */
1065 if (nc_ctx_load_module(session, "ietf-datastores", NULL, server_modules, old_clb, old_data,
1066 get_schema_support, &mod)) {
1067 WRN("Session %u: loading NETCONF ietf-datastores schema failed, unable to use yang-library"
1068 " to learn all the supported modules.", session->id);
1069 yanglib_support = 0;
1070 }
1071 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001072 free(revision);
1073 }
1074 }
1075
Radek Krejci65ef6d52018-08-16 16:35:02 +02001076 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001077 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001078 if (build_schema_info_yl(session, &sm)) {
1079 goto cleanup;
1080 } else if (!sm) {
1081 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
1082 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001083 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
1084 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001085 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001086 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001087 }
Michal Vaskoef578332016-01-25 13:20:09 +01001088
Radek Krejci65ef6d52018-08-16 16:35:02 +02001089 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1090 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001091 }
1092
Radek Krejcifd5b6682017-06-13 15:52:53 +02001093 /* succsess */
1094 ret = 0;
1095
Michal Vaskoeee99412016-11-21 10:19:43 +01001096 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001097 WRN("Session %u: some models failed to be loaded, any data from these models (and any other unknown) will be ignored.", session->id);
Michal Vaskoef578332016-01-25 13:20:09 +01001098 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001099
1100cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001101 free_schema_info(server_modules);
1102
Radek Krejcifd5b6682017-06-13 15:52:53 +02001103 /* set user callback back */
1104 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001105 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001106
Michal Vaskoef578332016-01-25 13:20:09 +01001107 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001108}
1109
1110API struct nc_session *
1111nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1112{
Michal Vaskod083db62016-01-19 10:31:29 +01001113 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001114
Michal Vasko45e53ae2016-04-07 11:46:03 +02001115 if (fdin < 0) {
1116 ERRARG("fdin");
1117 return NULL;
1118 } else if (fdout < 0) {
1119 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001120 return NULL;
1121 }
1122
1123 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001124 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001125 if (!session) {
1126 ERRMEM;
1127 return NULL;
1128 }
1129 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001130
1131 /* transport specific data */
1132 session->ti_type = NC_TI_FD;
1133 session->ti.fd.in = fdin;
1134 session->ti.fd.out = fdout;
1135
Robin Jarry4e38e292019-10-15 17:14:14 +02001136 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1137 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001138 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001139 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001140
1141 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001142 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001143 goto fail;
1144 }
1145 session->status = NC_STATUS_RUNNING;
1146
Michal Vaskoef578332016-01-25 13:20:09 +01001147 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001148 goto fail;
1149 }
1150
1151 return session;
1152
1153fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001154 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001155 return NULL;
1156}
1157
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001158API struct nc_session *
1159nc_connect_unix(const char *address, struct ly_ctx *ctx)
1160{
1161 struct nc_session *session = NULL;
1162 struct sockaddr_un sun;
1163 const struct passwd *pw;
1164 char *username;
1165 int sock = -1;
1166
1167 if (address == NULL) {
1168 ERRARG("address");
1169 return NULL;
1170 }
1171
1172 pw = getpwuid(geteuid());
1173 if (pw == NULL) {
1174 ERR("Failed to find username for euid=%u.\n", geteuid());
1175 goto fail;
1176 }
1177
1178 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1179 if (sock < 0) {
1180 ERR("Failed to create socket (%s).", strerror(errno));
1181 goto fail;
1182 }
1183
1184 memset(&sun, 0, sizeof(sun));
1185 sun.sun_family = AF_UNIX;
1186 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1187
1188 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
1189 ERR("cannot connect to sock server %s (%s)",
1190 address, strerror(errno));
1191 goto fail;
1192 }
1193
1194 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
1195 ERR("Fcntl failed (%s).", strerror(errno));
1196 goto fail;
1197 }
1198
1199 /* prepare session structure */
1200 session = nc_new_session(NC_CLIENT, 0);
1201 if (!session) {
1202 ERRMEM;
1203 goto fail;
1204 }
1205 session->status = NC_STATUS_STARTING;
1206
1207 /* transport specific data */
1208 session->ti_type = NC_TI_UNIX;
1209 session->ti.unixsock.sock = sock;
1210 sock = -1; /* do not close sock in fail label anymore */
1211
Robin Jarry4e38e292019-10-15 17:14:14 +02001212 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1213 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001214 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001215 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001216
1217 session->path = lydict_insert(ctx, address, 0);
1218
1219 username = strdup(pw->pw_name);
1220 if (username == NULL) {
1221 ERRMEM;
1222 goto fail;
1223 }
1224 session->username = lydict_insert_zc(ctx, username);
1225
1226 /* NETCONF handshake */
1227 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1228 goto fail;
1229 }
1230 session->status = NC_STATUS_RUNNING;
1231
1232 if (nc_ctx_check_and_fill(session) == -1) {
1233 goto fail;
1234 }
1235
1236 return session;
1237
1238fail:
1239 nc_session_free(session, NULL);
1240 if (sock >= 0)
1241 close(sock);
1242 return NULL;
1243}
1244
Frank Rimpler9f838b02018-07-25 06:44:03 +00001245/*
1246 Helper for a non-blocking connect (which is required because of the locking
1247 concept for e.g. call home settings). For more details see nc_sock_connect().
1248 */
1249static int
Michal Vasko5e8d0192019-06-24 19:19:49 +02001250_non_blocking_connect(int timeout, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001251{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001252 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001253 int sock = -1;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001254 fd_set wset;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001255 struct timeval ts;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001256 socklen_t len = sizeof(int);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001257 struct in_addr *addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001258 uint16_t port;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001259 char str[INET6_ADDRSTRLEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001260
Frank Rimpler9f838b02018-07-25 06:44:03 +00001261 if (sock_pending && *sock_pending != -1) {
Michal Vasko4c612cd2021-02-05 08:53:42 +01001262 VRB("Trying to connect the pending socket %d.", *sock_pending );
Frank Rimpler9f838b02018-07-25 06:44:03 +00001263 sock = *sock_pending;
1264 } else {
1265 assert(res);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001266 if (res->ai_family == AF_INET6) {
1267 addr = (struct in_addr *) &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001268 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001269 } else {
1270 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001271 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001272 }
1273 if (!inet_ntop(res->ai_family, addr, str, res->ai_addrlen)) {
1274 WRN("inet_ntop() failed (%s).", strerror(errno));
1275 } else {
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001276 VRB("Trying to connect via %s to %s:%u.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", str, port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001277 }
1278
1279 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001280 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1281 if (sock == -1) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001282 ERR("Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001283 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001284 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001285 /* make the socket non-blocking */
1286 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001287 ERR("fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001288 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001289 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001290 /* non-blocking connect! */
1291 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1292 if (errno != EINPROGRESS) {
1293 /* network connection failed, try another resource */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001294 ERR("connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001295 goto cleanup;
1296 }
1297 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001298 }
1299 ts.tv_sec = timeout;
1300 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001301
Frank Rimpler9f838b02018-07-25 06:44:03 +00001302 FD_ZERO(&wset);
1303 FD_SET(sock, &wset);
1304
1305 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001306 ERR("select() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001307 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001308 }
1309
Michal Vasko5e8d0192019-06-24 19:19:49 +02001310 if (ret == 0) {
1311 /* there was a timeout */
1312 VRB("Timed out after %ds (%s).", timeout, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001313 if (sock_pending) {
1314 /* no sock-close, we'll try it again */
1315 *sock_pending = sock;
1316 } else {
1317 close(sock);
1318 }
1319 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001320 }
Radek Krejci782041a2018-08-20 10:09:45 +02001321
1322 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001323 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001324 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001325 ERR("getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001326 goto cleanup;
1327 }
Michal Vaskoe27ab4e2020-07-27 11:10:58 +02001328 if (error) {
Radek Krejci782041a2018-08-20 10:09:45 +02001329 /* network connection failed, try another resource */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001330 VRB("getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001331 errno = error;
1332 goto cleanup;
1333 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001334
1335 /* enable keep-alive */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001336 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001337 goto cleanup;
1338 }
1339
Michal Vasko086311b2016-01-08 09:53:11 +01001340 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001341
Frank Rimpler9f838b02018-07-25 06:44:03 +00001342cleanup:
1343 if (sock_pending) {
1344 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001345 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001346 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001347 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001348}
1349
Frank Rimpler9f838b02018-07-25 06:44:03 +00001350/* A given timeout value limits the time how long the function blocks. If it has to block
1351 only for some seconds, a socket connection might not yet have been fully established.
1352 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1353 value will be -1. In such a case a subsequent invokation is required, by providing the
1354 stored sock_pending, again.
1355 In general, if this function returns -1, when a timeout has been given, this function
1356 has to be invoked, until it returns a valid socket.
1357 */
1358int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001359nc_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 +00001360{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001361 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001362 int sock = sock_pending ? *sock_pending : -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001363 struct addrinfo hints, *res_list, *res;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001364 char *buf, port_s[6]; /* length of string representation of short int */
Michal Vasko66032bc2019-01-22 15:03:12 +01001365 void *addr;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001366
Michal Vasko66032bc2019-01-22 15:03:12 +01001367 DBG("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001368
1369 /* no pending socket */
1370 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001371 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001372 snprintf(port_s, 6, "%u", port);
1373 memset(&hints, 0, sizeof hints);
1374 hints.ai_family = AF_UNSPEC;
1375 hints.ai_socktype = SOCK_STREAM;
1376 hints.ai_protocol = IPPROTO_TCP;
1377 i = getaddrinfo(host, port_s, &hints, &res_list);
1378 if (i != 0) {
1379 ERR("Unable to translate the host address (%s).", gai_strerror(i));
Michal Vaskocb846632019-12-13 15:12:45 +01001380 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001381 }
1382
1383 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001384 sock = _non_blocking_connect(timeout, sock_pending, res, ka);
Michal Vasko2af7c382020-07-27 14:21:35 +02001385 if (sock == -1) {
1386 if (!sock_pending || *sock_pending == -1) {
1387 /* try the next resource */
1388 continue;
1389 } else {
1390 /* timeout, keep pending socket */
1391 return -1;
1392 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001393 }
1394 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
Michal Vasko83ad17e2019-01-30 10:11:37 +01001395
1396 opt = 1;
1397 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
1398 ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001399 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001400 }
1401
Michal Vasko66032bc2019-01-22 15:03:12 +01001402 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1403 buf = malloc(INET6_ADDRSTRLEN);
1404 if (!buf) {
1405 ERRMEM;
Michal Vaskocb846632019-12-13 15:12:45 +01001406 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001407 }
1408 if (res->ai_family == AF_INET) {
1409 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1410 } else {
1411 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1412 }
1413 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
1414 ERR("Converting host to IP address failed (%s).", strerror(errno));
1415 free(buf);
Michal Vaskocb846632019-12-13 15:12:45 +01001416 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001417 }
1418
1419 *ip_host = buf;
1420 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001421 break;
1422 }
1423 freeaddrinfo(res_list);
1424
1425 } else {
1426 /* try to get a connection with the pending socket */
1427 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001428 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001429 }
1430
1431 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001432
1433error:
1434 if (sock != -1) {
1435 close(sock);
1436 }
1437 if (sock_pending) {
1438 *sock_pending = -1;
1439 }
1440 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001441}
1442
Michal Vasko086311b2016-01-08 09:53:11 +01001443static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001444get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001445{
Michal Vasko086311b2016-01-08 09:53:11 +01001446 char *ptr;
1447 const char *str_msgid;
1448 uint64_t cur_msgid;
1449 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001450 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001451 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1452
Michal Vasko086311b2016-01-08 09:53:11 +01001453 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001454 if (!msgid && session->opts.client.notifs) {
1455 cont = session->opts.client.notifs;
1456 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001457
Michal Vasko71ba2da2016-05-04 10:53:16 +02001458 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001459 free(cont);
1460
Michal Vasko71ba2da2016-05-04 10:53:16 +02001461 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001462 }
1463
1464 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001465 if (msgid && session->opts.client.replies) {
1466 cont = session->opts.client.replies;
1467 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001468
Michal Vasko71ba2da2016-05-04 10:53:16 +02001469 xml = cont->msg;
1470 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001471
Michal Vasko71ba2da2016-05-04 10:53:16 +02001472 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001473 }
1474
Michal Vasko71ba2da2016-05-04 10:53:16 +02001475 if (!msgtype) {
1476 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001477 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001478 }
Michal Vasko086311b2016-01-08 09:53:11 +01001479
1480 /* we read rpc-reply, want a notif */
1481 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001482 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001483 while (*cont_ptr) {
1484 cont_ptr = &((*cont_ptr)->next);
1485 }
1486 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001487 if (!*cont_ptr) {
1488 ERRMEM;
1489 lyxml_free(session->ctx, xml);
1490 return NC_MSG_ERROR;
1491 }
Michal Vasko086311b2016-01-08 09:53:11 +01001492 (*cont_ptr)->msg = xml;
1493 (*cont_ptr)->next = NULL;
1494 }
1495
1496 /* we read notif, want a rpc-reply */
1497 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02001498 if (!ATOMIC_LOAD(session->opts.client.ntf_tid)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001499 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001500 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001501 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001502 }
Michal Vasko086311b2016-01-08 09:53:11 +01001503
Michal Vasko2e6defd2016-10-07 15:48:15 +02001504 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001505 while (*cont_ptr) {
1506 cont_ptr = &((*cont_ptr)->next);
1507 }
1508 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001509 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001510 ERRMEM;
1511 lyxml_free(session->ctx, xml);
1512 return NC_MSG_ERROR;
1513 }
Michal Vasko086311b2016-01-08 09:53:11 +01001514 (*cont_ptr)->msg = xml;
1515 (*cont_ptr)->next = NULL;
1516 }
1517
Michal Vasko086311b2016-01-08 09:53:11 +01001518 switch (msgtype) {
1519 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001520 if (!msgid) {
1521 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001522 }
Michal Vasko086311b2016-01-08 09:53:11 +01001523 break;
1524
1525 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001526 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001527 /* check message-id */
1528 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1529 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001530 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001531 } else {
1532 cur_msgid = strtoul(str_msgid, &ptr, 10);
1533 if (cur_msgid != msgid) {
1534 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1535 session->id, str_msgid);
1536 msgtype = NC_MSG_REPLY_ERR_MSGID;
1537 }
1538 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001539 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001540 }
Michal Vasko086311b2016-01-08 09:53:11 +01001541 break;
1542
1543 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001544 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001545 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001546 msgtype = NC_MSG_ERROR;
1547 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001548
1549 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001550 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001551 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001552 msgtype = NC_MSG_ERROR;
1553 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001554
1555 default:
1556 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1557 * NC_MSG_NONE is not returned by nc_read_msg()
1558 */
1559 break;
1560 }
1561
1562 return msgtype;
1563}
1564
1565/* cannot strictly fail, but does not need to fill any error parameter at all */
1566static void
1567parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1568{
1569 struct lyxml_elem *iter, *next, *info;
1570
1571 LY_TREE_FOR(xml->child, iter) {
1572 if (!iter->ns) {
1573 if (iter->content) {
1574 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1575 } else {
1576 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1577 }
1578 continue;
1579 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1580 if (iter->content) {
1581 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1582 iter->name, iter->content, iter->ns->value);
1583 } else {
1584 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1585 }
1586 continue;
1587 }
1588
1589 if (!strcmp(iter->name, "error-type")) {
1590 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1591 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1592 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1593 } else if (err->type) {
1594 WRN("<rpc-error> <error-type> duplicated.");
1595 } else {
1596 err->type = lydict_insert(ctx, iter->content, 0);
1597 }
1598 } else if (!strcmp(iter->name, "error-tag")) {
1599 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1600 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1601 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1602 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1603 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1604 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1605 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1606 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1607 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1608 && strcmp(iter->content, "malformed-message"))) {
1609 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1610 } else if (err->tag) {
1611 WRN("<rpc-error> <error-tag> duplicated.");
1612 } else {
1613 err->tag = lydict_insert(ctx, iter->content, 0);
1614 }
1615 } else if (!strcmp(iter->name, "error-severity")) {
1616 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1617 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1618 } else if (err->severity) {
1619 WRN("<rpc-error> <error-severity> duplicated.");
1620 } else {
1621 err->severity = lydict_insert(ctx, iter->content, 0);
1622 }
1623 } else if (!strcmp(iter->name, "error-app-tag")) {
1624 if (err->apptag) {
1625 WRN("<rpc-error> <error-app-tag> duplicated.");
1626 } else {
1627 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1628 }
1629 } else if (!strcmp(iter->name, "error-path")) {
1630 if (err->path) {
1631 WRN("<rpc-error> <error-path> duplicated.");
1632 } else {
1633 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1634 }
1635 } else if (!strcmp(iter->name, "error-message")) {
1636 if (err->message) {
1637 WRN("<rpc-error> <error-message> duplicated.");
1638 } else {
1639 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001640 if (err->message_lang) {
1641 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1642 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001643 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1644 }
1645 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1646 }
1647 } else if (!strcmp(iter->name, "error-info")) {
1648 LY_TREE_FOR_SAFE(iter->child, next, info) {
1649 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1650 if (!strcmp(info->name, "session-id")) {
1651 if (err->sid) {
1652 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1653 } else {
1654 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1655 }
Michal Vasko630485f2018-01-03 14:28:32 +01001656 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001657 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001658 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1659 if (!err->attr) {
1660 ERRMEM;
1661 return;
1662 }
Michal Vasko086311b2016-01-08 09:53:11 +01001663 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1664 } else if (!strcmp(info->name, "bad-element")) {
1665 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001666 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1667 if (!err->elem) {
1668 ERRMEM;
1669 return;
1670 }
Michal Vasko086311b2016-01-08 09:53:11 +01001671 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1672 } else if (!strcmp(info->name, "bad-namespace")) {
1673 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001674 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1675 if (!err->ns) {
1676 ERRMEM;
1677 return;
1678 }
Michal Vasko086311b2016-01-08 09:53:11 +01001679 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1680 } else {
1681 if (info->content) {
1682 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1683 info->name, info->content);
1684 } else {
1685 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1686 }
1687 }
1688 } else {
1689 lyxml_unlink(ctx, info);
1690 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001691 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1692 if (!err->other) {
1693 ERRMEM;
1694 return;
1695 }
Michal Vasko086311b2016-01-08 09:53:11 +01001696 err->other[err->other_count - 1] = info;
1697 }
1698 }
1699 } else {
1700 if (iter->content) {
1701 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1702 } else {
1703 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1704 }
1705 }
1706 }
1707}
1708
1709static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001710parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001711{
1712 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001713 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001714 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001715 struct nc_reply_data *data_rpl;
1716 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001717 struct nc_rpc_act_generic *rpc_gen;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001718 int i, data_parsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001719
Michal Vasko086311b2016-01-08 09:53:11 +01001720 /* rpc-error */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001721 if (xml->child && !strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001722 /* count and check elements */
1723 i = 0;
1724 LY_TREE_FOR(xml->child, iter) {
1725 if (strcmp(iter->name, "rpc-error")) {
1726 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1727 return NULL;
1728 } else if (!iter->ns) {
1729 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1730 return NULL;
1731 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1732 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1733 return NULL;
1734 }
1735 ++i;
1736 }
1737
1738 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001739 if (!error_rpl) {
1740 ERRMEM;
1741 return NULL;
1742 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001743 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001744 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001745 if (!error_rpl->err) {
1746 ERRMEM;
1747 free(error_rpl);
1748 return NULL;
1749 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001750 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001751 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001752 reply = (struct nc_reply *)error_rpl;
1753
1754 i = 0;
1755 LY_TREE_FOR(xml->child, iter) {
1756 parse_rpc_error(ctx, iter, error_rpl->err + i);
1757 ++i;
1758 }
1759
1760 /* ok */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001761 } else if (xml->child && !strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001762 if (xml->child->next) {
1763 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1764 return NULL;
1765 }
1766 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001767 if (!reply) {
1768 ERRMEM;
1769 return NULL;
1770 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001771 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001772
1773 /* some RPC output */
1774 } else {
1775 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001776 case NC_RPC_ACT_GENERIC:
1777 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001778
1779 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001780 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1781 if (!rpc_act) {
1782 ERR("Failed to duplicate a generic RPC/action.");
1783 return NULL;
1784 }
Michal Vasko086311b2016-01-08 09:53:11 +01001785 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001786 rpc_act = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
Michal Vaskoe1708602016-10-18 12:17:22 +02001787 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001788 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001789 return NULL;
1790 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001791 }
Michal Vasko086311b2016-01-08 09:53:11 +01001792 break;
1793
1794 case NC_RPC_GETCONFIG:
1795 case NC_RPC_GET:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001796 case NC_RPC_GETDATA:
Michal Vasko6b281ff2019-02-14 10:15:19 +01001797 /* we should definitely have received at least an empty "data" element even on empty reply, but fine */
1798 if (!xml->child || !xml->child->child) {
Michal Vasko13ed2942016-02-29 16:21:00 +01001799 /* we did not receive any data */
1800 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001801 if (!data_rpl) {
1802 ERRMEM;
1803 return NULL;
1804 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001805 data_rpl->type = NC_RPL_DATA;
1806 data_rpl->data = NULL;
1807 return (struct nc_reply *)data_rpl;
1808 }
1809
Michal Vasko086311b2016-01-08 09:53:11 +01001810 /* special treatment */
Michal Vaskof45e0c12018-11-28 08:38:23 +01001811 ly_errno = 0;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001812 data = lyd_parse_xml(ctx, &xml->child->child,
1813 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001814 | parseroptions);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001815 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001816 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1817 return NULL;
1818 }
Michal Vaskof45e0c12018-11-28 08:38:23 +01001819 data_parsed = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001820 break;
1821
1822 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001823 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001824 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001825 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001826 return NULL;
1827 }
1828 break;
1829
1830 case NC_RPC_EDIT:
1831 case NC_RPC_COPY:
1832 case NC_RPC_DELETE:
1833 case NC_RPC_LOCK:
1834 case NC_RPC_UNLOCK:
1835 case NC_RPC_KILL:
1836 case NC_RPC_COMMIT:
1837 case NC_RPC_DISCARD:
1838 case NC_RPC_CANCEL:
1839 case NC_RPC_VALIDATE:
1840 case NC_RPC_SUBSCRIBE:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001841 case NC_RPC_EDITDATA:
Michal Vasko086311b2016-01-08 09:53:11 +01001842 /* there is no output defined */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001843 ERR("Unexpected data reply (root elem \"%s\").", xml->child ? xml->child->name : NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001844 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001845 default:
1846 ERRINT;
1847 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001848 }
1849
1850 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001851 if (!data_rpl) {
1852 ERRMEM;
1853 return NULL;
1854 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001855 data_rpl->type = NC_RPL_DATA;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001856
1857 ly_errno = 0;
1858 if (!data_parsed) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001859 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001860 rpc_act, NULL);
Michal Vasko6b281ff2019-02-14 10:15:19 +01001861 if (!ly_errno && !data_rpl->data->child) {
1862 ERR("An empty data <rpc-reply>.");
1863 lyd_free_withsiblings(rpc_act);
1864 lyd_free(data_rpl->data);
1865 free(data_rpl);
1866 return NULL;
1867 }
Michal Vasko086311b2016-01-08 09:53:11 +01001868 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01001869 /* <get>, <get-config>, <get-data> */
Michal Vasko086311b2016-01-08 09:53:11 +01001870 data_rpl->data = data;
1871 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001872 lyd_free_withsiblings(rpc_act);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001873 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001874 ERR("Failed to parse <rpc-reply>.");
1875 free(data_rpl);
1876 return NULL;
1877 }
1878 reply = (struct nc_reply *)data_rpl;
1879 }
1880
1881 return reply;
1882}
1883
Radek Krejci53691be2016-02-22 13:58:37 +01001884#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001885
Michal Vasko3031aae2016-01-27 16:07:18 +01001886int
1887nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1888{
1889 int sock;
1890
Michal Vasko45e53ae2016-04-07 11:46:03 +02001891 if (!address) {
1892 ERRARG("address");
1893 return -1;
1894 } else if (!port) {
1895 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001896 return -1;
1897 }
1898
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001899 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001900 if (sock == -1) {
1901 return -1;
1902 }
1903
1904 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001905 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1906 if (!client_opts.ch_binds) {
1907 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001908 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001909 return -1;
1910 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001911
Michal Vasko2e6defd2016-10-07 15:48:15 +02001912 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1913 if (!client_opts.ch_bind_ti) {
1914 ERRMEM;
1915 close(sock);
1916 return -1;
1917 }
1918 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1919
Michal Vasko3031aae2016-01-27 16:07:18 +01001920 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001921 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1922 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001923 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001924 return -1;
1925 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001926 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1927 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001928 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001929
1930 return 0;
1931}
1932
1933int
1934nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1935{
1936 uint32_t i;
1937 int ret = -1;
1938
1939 if (!address && !port && !ti) {
1940 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1941 close(client_opts.ch_binds[i].sock);
1942 free((char *)client_opts.ch_binds[i].address);
1943
1944 ret = 0;
1945 }
1946 free(client_opts.ch_binds);
1947 client_opts.ch_binds = NULL;
1948 client_opts.ch_bind_count = 0;
1949 } else {
1950 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1951 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1952 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001953 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001954 close(client_opts.ch_binds[i].sock);
1955 free((char *)client_opts.ch_binds[i].address);
1956
1957 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001958 if (!client_opts.ch_bind_count) {
1959 free(client_opts.ch_binds);
1960 client_opts.ch_binds = NULL;
1961 } else if (i < client_opts.ch_bind_count) {
1962 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1963 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1964 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001965
1966 ret = 0;
1967 }
1968 }
1969 }
1970
1971 return ret;
1972}
1973
1974API int
1975nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1976{
1977 int sock;
1978 char *host = NULL;
1979 uint16_t port, idx;
1980
Michal Vasko45e53ae2016-04-07 11:46:03 +02001981 if (!client_opts.ch_binds) {
1982 ERRINIT;
1983 return -1;
1984 } else if (!session) {
1985 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001986 return -1;
1987 }
1988
1989 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1990
Michal Vasko50456e82016-02-02 12:16:08 +01001991 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001992 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001993 return sock;
1994 }
1995
Radek Krejci53691be2016-02-22 13:58:37 +01001996#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001997 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001998 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001999 } else
2000#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002001#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002002 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01002003 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01002004 } else
2005#endif
2006 {
Michal Vaskofee717c2016-02-01 13:25:43 +01002007 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01002008 *session = NULL;
2009 }
2010
2011 free(host);
2012
2013 if (!(*session)) {
2014 return -1;
2015 }
2016
2017 return 1;
2018}
2019
Radek Krejci53691be2016-02-22 13:58:37 +01002020#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01002021
Michal Vasko1b2ddc92017-05-24 08:59:49 +02002022API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02002023nc_session_get_cpblts(const struct nc_session *session)
2024{
2025 if (!session) {
2026 ERRARG("session");
2027 return NULL;
2028 }
2029
Michal Vasko1b2ddc92017-05-24 08:59:49 +02002030 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02002031}
2032
2033API const char *
2034nc_session_cpblt(const struct nc_session *session, const char *capab)
2035{
2036 int i, len;
2037
2038 if (!session) {
2039 ERRARG("session");
2040 return NULL;
2041 } else if (!capab) {
2042 ERRARG("capab");
2043 return NULL;
2044 }
2045
2046 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002047 for (i = 0; session->opts.client.cpblts[i]; ++i) {
2048 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
2049 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02002050 }
2051 }
2052
2053 return NULL;
2054}
2055
Michal Vasko9cd26a82016-05-31 08:58:48 +02002056API int
2057nc_session_ntf_thread_running(const struct nc_session *session)
2058{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002059 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02002060 ERRARG("session");
2061 return 0;
2062 }
2063
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002064 return ATOMIC_LOAD(session->opts.client.ntf_tid) ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02002065}
2066
Michal Vaskob7558c52016-02-26 15:04:19 +01002067API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002068nc_client_init(void)
2069{
2070 nc_init();
2071}
2072
2073API void
Michal Vaskob7558c52016-02-26 15:04:19 +01002074nc_client_destroy(void)
2075{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02002076 nc_client_set_schema_searchpath(NULL);
2077#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
2078 nc_client_ch_del_bind(NULL, 0, 0);
2079#endif
2080#ifdef NC_ENABLED_SSH
2081 nc_client_ssh_destroy_opts();
2082#endif
2083#ifdef NC_ENABLED_TLS
2084 nc_client_tls_destroy_opts();
2085#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002086 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01002087}
2088
Michal Vasko086311b2016-01-08 09:53:11 +01002089API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002090nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, int parseroptions, struct nc_reply **reply)
Michal Vasko086311b2016-01-08 09:53:11 +01002091{
Michal Vasko5a2c7162020-01-30 11:24:17 +01002092 struct lyxml_elem *xml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01002093 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2094
Michal Vasko45e53ae2016-04-07 11:46:03 +02002095 if (!session) {
2096 ERRARG("session");
2097 return NC_MSG_ERROR;
2098 } else if (!rpc) {
2099 ERRARG("rpc");
2100 return NC_MSG_ERROR;
Michal Vasko5a2c7162020-01-30 11:24:17 +01002101 } else if (!msgid) {
2102 ERRARG("msgid");
2103 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02002104 } else if (!reply) {
2105 ERRARG("reply");
2106 return NC_MSG_ERROR;
2107 } else if (parseroptions & LYD_OPT_TYPEMASK) {
2108 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01002109 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002110 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01002111 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002112 return NC_MSG_ERROR;
2113 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002114 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01002115 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
2116 parseroptions &= LYD_OPT_STRICT;
2117 }
Michal Vasko41adf392017-01-17 10:39:04 +01002118 /* no mechanism to check external dependencies is provided */
2119 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01002120 *reply = NULL;
2121
2122 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002123
Michal Vasko5a2c7162020-01-30 11:24:17 +01002124 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01002125 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko5a2c7162020-01-30 11:24:17 +01002126 lyxml_free_withsiblings(session->ctx, xml);
2127 xml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01002128 if (!(*reply)) {
2129 return NC_MSG_ERROR;
2130 }
2131 }
Michal Vasko5a2c7162020-01-30 11:24:17 +01002132 assert(!xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002133
2134 return msgtype;
2135}
2136
2137API NC_MSG_TYPE
2138nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
2139{
2140 struct lyxml_elem *xml, *ev_time;
2141 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2142
Michal Vasko45e53ae2016-04-07 11:46:03 +02002143 if (!session) {
2144 ERRARG("session");
2145 return NC_MSG_ERROR;
2146 } else if (!notif) {
2147 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01002148 return NC_MSG_ERROR;
2149 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002150 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002151 return NC_MSG_ERROR;
2152 }
2153
2154 msgtype = get_msg(session, timeout, 0, &xml);
2155
2156 if (msgtype == NC_MSG_NOTIF) {
2157 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002158 if (!*notif) {
2159 ERRMEM;
2160 lyxml_free(session->ctx, xml);
2161 return NC_MSG_ERROR;
2162 }
Michal Vasko086311b2016-01-08 09:53:11 +01002163
2164 /* eventTime */
2165 LY_TREE_FOR(xml->child, ev_time) {
2166 if (!strcmp(ev_time->name, "eventTime")) {
2167 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
2168 /* lyd_parse does not know this element */
2169 lyxml_free(session->ctx, ev_time);
2170 break;
2171 }
2172 }
2173 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01002174 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002175 goto fail;
2176 }
2177
2178 /* notification body */
Michal Vasko40208012020-12-09 19:24:23 +01002179 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT | LYD_OPT_TRUSTED
Michal Vaskoeee99412016-11-21 10:19:43 +01002180 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002181 lyxml_free(session->ctx, xml);
2182 xml = NULL;
2183 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01002184 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002185 goto fail;
2186 }
2187 }
2188
2189 return msgtype;
2190
2191fail:
2192 lydict_remove(session->ctx, (*notif)->datetime);
2193 lyd_free((*notif)->tree);
2194 free(*notif);
2195 *notif = NULL;
2196 lyxml_free(session->ctx, xml);
2197
2198 return NC_MSG_ERROR;
2199}
2200
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002201static void *
2202nc_recv_notif_thread(void *arg)
2203{
2204 struct nc_ntf_thread_arg *ntarg;
2205 struct nc_session *session;
2206 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
2207 struct nc_notif *notif;
2208 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002209 pthread_t *ntf_tid;
2210
2211 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002212
2213 ntarg = (struct nc_ntf_thread_arg *)arg;
2214 session = ntarg->session;
2215 notif_clb = ntarg->notif_clb;
2216 free(ntarg);
2217
Michal Vasko131120a2018-05-29 15:44:02 +02002218 /* remember our allocated tid, we will be freeing it */
Michal Vaskob639e9c2020-09-09 09:51:48 +02002219 ntf_tid = (pthread_t *)ATOMIC_LOAD(session->opts.client.ntf_tid);
Michal Vasko131120a2018-05-29 15:44:02 +02002220
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002221 while (ATOMIC_LOAD(session->opts.client.ntf_tid)) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002222 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002223 if (msgtype == NC_MSG_NOTIF) {
2224 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002225 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2226 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2227 nc_notif_free(notif);
2228 break;
2229 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002230 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002231 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002232 /* quit this thread once the session is broken */
2233 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002234 }
2235
2236 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2237 }
2238
Michal Vasko0651c902016-05-19 15:55:42 +02002239 VRB("Session %u: notification thread exit.", session->id);
Michal Vaskob639e9c2020-09-09 09:51:48 +02002240 ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)NULL);
Michal Vasko131120a2018-05-29 15:44:02 +02002241 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002242 return NULL;
2243}
2244
2245API int
2246nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2247{
2248 struct nc_ntf_thread_arg *ntarg;
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002249 pthread_t *tid;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002250 int ret;
2251
Michal Vasko45e53ae2016-04-07 11:46:03 +02002252 if (!session) {
2253 ERRARG("session");
2254 return -1;
2255 } else if (!notif_clb) {
2256 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002257 return -1;
2258 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2259 ERR("Session %u: invalid session to receive Notifications.", session->id);
2260 return -1;
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002261 } else if (ATOMIC_LOAD(session->opts.client.ntf_tid)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002262 ERR("Session %u: separate notification thread is already running.", session->id);
2263 return -1;
2264 }
2265
2266 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002267 if (!ntarg) {
2268 ERRMEM;
2269 return -1;
2270 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002271 ntarg->session = session;
2272 ntarg->notif_clb = notif_clb;
2273
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002274 tid = malloc(sizeof *tid);
2275 if (!tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002276 ERRMEM;
2277 free(ntarg);
2278 return -1;
2279 }
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002280 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vaskob639e9c2020-09-09 09:51:48 +02002281 ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002282
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002283 ret = pthread_create(tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002284 if (ret) {
2285 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2286 free(ntarg);
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002287 free(tid);
Michal Vaskob639e9c2020-09-09 09:51:48 +02002288 ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)NULL);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002289 return -1;
2290 }
2291
2292 return 0;
2293}
2294
Michal Vasko086311b2016-01-08 09:53:11 +01002295API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002296nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002297{
2298 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002299 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002300 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002301 struct nc_rpc_getconfig *rpc_gc;
2302 struct nc_rpc_edit *rpc_e;
2303 struct nc_rpc_copy *rpc_cp;
2304 struct nc_rpc_delete *rpc_del;
2305 struct nc_rpc_lock *rpc_lock;
2306 struct nc_rpc_get *rpc_g;
2307 struct nc_rpc_kill *rpc_k;
2308 struct nc_rpc_commit *rpc_com;
2309 struct nc_rpc_cancel *rpc_can;
2310 struct nc_rpc_validate *rpc_val;
2311 struct nc_rpc_getschema *rpc_gs;
2312 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002313 struct nc_rpc_getdata *rpc_getd;
2314 struct nc_rpc_editdata *rpc_editd;
Michal Vasko086311b2016-01-08 09:53:11 +01002315 struct lyd_node *data, *node;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002316 const struct lys_module *mod = NULL, *ietfncwd;
2317 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002318 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002319 uint64_t cur_msgid;
2320
Michal Vasko45e53ae2016-04-07 11:46:03 +02002321 if (!session) {
2322 ERRARG("session");
2323 return NC_MSG_ERROR;
2324 } else if (!rpc) {
2325 ERRARG("rpc");
2326 return NC_MSG_ERROR;
2327 } else if (!msgid) {
2328 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002329 return NC_MSG_ERROR;
2330 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002331 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002332 return NC_MSG_ERROR;
2333 }
2334
Michal Vaskoc1171a42019-11-05 12:06:46 +01002335 switch (rpc->type) {
2336 case NC_RPC_ACT_GENERIC:
2337 /* checked when parsing */
2338 break;
2339 case NC_RPC_GETCONFIG:
2340 case NC_RPC_EDIT:
2341 case NC_RPC_COPY:
2342 case NC_RPC_DELETE:
2343 case NC_RPC_LOCK:
2344 case NC_RPC_UNLOCK:
2345 case NC_RPC_GET:
2346 case NC_RPC_KILL:
2347 case NC_RPC_COMMIT:
2348 case NC_RPC_DISCARD:
2349 case NC_RPC_CANCEL:
2350 case NC_RPC_VALIDATE:
2351 mod = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
2352 if (!mod) {
Michal Vasko086cd572017-01-12 12:19:05 +01002353 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002354 return NC_MSG_ERROR;
2355 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002356 break;
2357 case NC_RPC_GETSCHEMA:
2358 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
2359 if (!mod) {
2360 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
2361 return NC_MSG_ERROR;
2362 }
2363 break;
2364 case NC_RPC_SUBSCRIBE:
2365 mod = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
2366 if (!mod) {
2367 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
2368 return NC_MSG_ERROR;
2369 }
2370 break;
2371 case NC_RPC_GETDATA:
2372 case NC_RPC_EDITDATA:
2373 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-nmda", NULL, 1);
2374 if (!mod) {
2375 ERR("Session %u: missing \"ietf-netconf-nmda\" schema in the context.", session->id);
2376 return NC_MSG_ERROR;
2377 }
2378 break;
2379 case NC_RPC_UNKNOWN:
2380 ERRINT;
2381 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002382 }
2383
2384 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002385 case NC_RPC_ACT_GENERIC:
2386 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002387
2388 if (rpc_gen->has_data) {
2389 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002390 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002391 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002392 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01002393 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002394 if (!data) {
2395 return NC_MSG_ERROR;
2396 }
Michal Vasko086311b2016-01-08 09:53:11 +01002397 }
2398 break;
2399
2400 case NC_RPC_GETCONFIG:
2401 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2402
Michal Vaskoc1171a42019-11-05 12:06:46 +01002403 data = lyd_new(NULL, mod, "get-config");
2404 node = lyd_new(data, mod, "source");
2405 node = lyd_new_leaf(node, mod, ncds2str[rpc_gc->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002406 if (!node) {
2407 lyd_free(data);
2408 return NC_MSG_ERROR;
2409 }
2410 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002411 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002412 node = lyd_new_anydata(data, mod, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002413 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002414 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002415 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002416 lyd_insert_attr(node, NULL, "type", "xpath");
2417 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002418 }
2419 if (!node) {
2420 lyd_free(data);
2421 return NC_MSG_ERROR;
2422 }
2423 }
2424
2425 if (rpc_gc->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002426 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002427 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002428 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2429 lyd_free(data);
2430 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002431 }
2432 switch (rpc_gc->wd_mode) {
2433 case NC_WD_UNKNOWN:
2434 /* cannot get here */
2435 break;
2436 case NC_WD_ALL:
2437 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2438 break;
2439 case NC_WD_ALL_TAG:
2440 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2441 break;
2442 case NC_WD_TRIM:
2443 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2444 break;
2445 case NC_WD_EXPLICIT:
2446 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2447 break;
2448 }
2449 if (!node) {
2450 lyd_free(data);
2451 return NC_MSG_ERROR;
2452 }
2453 }
2454 break;
2455
2456 case NC_RPC_EDIT:
2457 rpc_e = (struct nc_rpc_edit *)rpc;
2458
Michal Vaskoc1171a42019-11-05 12:06:46 +01002459 data = lyd_new(NULL, mod, "edit-config");
2460 node = lyd_new(data, mod, "target");
2461 node = lyd_new_leaf(node, mod, ncds2str[rpc_e->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002462 if (!node) {
2463 lyd_free(data);
2464 return NC_MSG_ERROR;
2465 }
2466
2467 if (rpc_e->default_op) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002468 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
Michal Vasko086311b2016-01-08 09:53:11 +01002469 if (!node) {
2470 lyd_free(data);
2471 return NC_MSG_ERROR;
2472 }
2473 }
2474
2475 if (rpc_e->test_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002476 node = lyd_new_leaf(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002477 if (!node) {
2478 lyd_free(data);
2479 return NC_MSG_ERROR;
2480 }
2481 }
2482
2483 if (rpc_e->error_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002484 node = lyd_new_leaf(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002485 if (!node) {
2486 lyd_free(data);
2487 return NC_MSG_ERROR;
2488 }
2489 }
2490
Michal Vasko7793bc62016-09-16 11:58:41 +02002491 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002492 node = lyd_new_anydata(data, mod, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002493 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002494 node = lyd_new_leaf(data, mod, "url", rpc_e->edit_cont);
Michal Vasko086311b2016-01-08 09:53:11 +01002495 }
2496 if (!node) {
2497 lyd_free(data);
2498 return NC_MSG_ERROR;
2499 }
2500 break;
2501
2502 case NC_RPC_COPY:
2503 rpc_cp = (struct nc_rpc_copy *)rpc;
2504
Michal Vaskoc1171a42019-11-05 12:06:46 +01002505 data = lyd_new(NULL, mod, "copy-config");
2506 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002507 if (rpc_cp->url_trg) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002508 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_trg);
Michal Vasko086311b2016-01-08 09:53:11 +01002509 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002510 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002511 }
2512 if (!node) {
2513 lyd_free(data);
2514 return NC_MSG_ERROR;
2515 }
2516
Michal Vaskoc1171a42019-11-05 12:06:46 +01002517 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002518 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002519 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002520 node = lyd_new_anydata(node, mod, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002521 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002522 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002523 }
2524 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002525 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002526 }
2527 if (!node) {
2528 lyd_free(data);
2529 return NC_MSG_ERROR;
2530 }
2531
2532 if (rpc_cp->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002533 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002534 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002535 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2536 lyd_free(data);
2537 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002538 }
2539 switch (rpc_cp->wd_mode) {
2540 case NC_WD_UNKNOWN:
2541 /* cannot get here */
2542 break;
2543 case NC_WD_ALL:
2544 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2545 break;
2546 case NC_WD_ALL_TAG:
2547 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2548 break;
2549 case NC_WD_TRIM:
2550 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2551 break;
2552 case NC_WD_EXPLICIT:
2553 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2554 break;
2555 }
2556 if (!node) {
2557 lyd_free(data);
2558 return NC_MSG_ERROR;
2559 }
2560 }
2561 break;
2562
2563 case NC_RPC_DELETE:
2564 rpc_del = (struct nc_rpc_delete *)rpc;
2565
Michal Vaskoc1171a42019-11-05 12:06:46 +01002566 data = lyd_new(NULL, mod, "delete-config");
2567 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002568 if (rpc_del->url) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002569 node = lyd_new_leaf(node, mod, "url", rpc_del->url);
Michal Vasko086311b2016-01-08 09:53:11 +01002570 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002571 node = lyd_new_leaf(node, mod, ncds2str[rpc_del->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002572 }
2573 if (!node) {
2574 lyd_free(data);
2575 return NC_MSG_ERROR;
2576 }
2577 break;
2578
2579 case NC_RPC_LOCK:
2580 rpc_lock = (struct nc_rpc_lock *)rpc;
2581
Michal Vaskoc1171a42019-11-05 12:06:46 +01002582 data = lyd_new(NULL, mod, "lock");
2583 node = lyd_new(data, mod, "target");
2584 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002585 if (!node) {
2586 lyd_free(data);
2587 return NC_MSG_ERROR;
2588 }
2589 break;
2590
2591 case NC_RPC_UNLOCK:
2592 rpc_lock = (struct nc_rpc_lock *)rpc;
2593
Michal Vaskoc1171a42019-11-05 12:06:46 +01002594 data = lyd_new(NULL, mod, "unlock");
2595 node = lyd_new(data, mod, "target");
2596 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002597 if (!node) {
2598 lyd_free(data);
2599 return NC_MSG_ERROR;
2600 }
2601 break;
2602
2603 case NC_RPC_GET:
2604 rpc_g = (struct nc_rpc_get *)rpc;
2605
Michal Vaskoc1171a42019-11-05 12:06:46 +01002606 data = lyd_new(NULL, mod, "get");
Michal Vasko086311b2016-01-08 09:53:11 +01002607 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002608 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002609 node = lyd_new_anydata(data, mod, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002610 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002611 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002612 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002613 lyd_insert_attr(node, NULL, "type", "xpath");
2614 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002615 }
2616 if (!node) {
2617 lyd_free(data);
2618 return NC_MSG_ERROR;
2619 }
2620 }
2621
2622 if (rpc_g->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002623 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002624 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002625 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2626 lyd_free(data);
2627 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002628 }
2629 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002630 case NC_WD_ALL:
2631 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2632 break;
2633 case NC_WD_ALL_TAG:
2634 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2635 break;
2636 case NC_WD_TRIM:
2637 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2638 break;
2639 case NC_WD_EXPLICIT:
2640 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2641 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002642 default:
2643 /* cannot get here */
2644 node = NULL;
2645 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002646 }
2647 if (!node) {
2648 lyd_free(data);
2649 return NC_MSG_ERROR;
2650 }
2651 }
2652 break;
2653
2654 case NC_RPC_KILL:
2655 rpc_k = (struct nc_rpc_kill *)rpc;
2656
Michal Vaskoc1171a42019-11-05 12:06:46 +01002657 data = lyd_new(NULL, mod, "kill-session");
Michal Vasko086311b2016-01-08 09:53:11 +01002658 sprintf(str, "%u", rpc_k->sid);
Michal Vasko283c3c02021-01-13 14:12:19 +01002659 node = lyd_new_leaf(data, mod, "session-id", str);
2660 if (!node) {
2661 lyd_free(data);
2662 return NC_MSG_ERROR;
2663 }
Michal Vasko086311b2016-01-08 09:53:11 +01002664 break;
2665
2666 case NC_RPC_COMMIT:
2667 rpc_com = (struct nc_rpc_commit *)rpc;
2668
Michal Vaskoc1171a42019-11-05 12:06:46 +01002669 data = lyd_new(NULL, mod, "commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002670 if (rpc_com->confirmed) {
Michal Vasko283c3c02021-01-13 14:12:19 +01002671 node = lyd_new_leaf(data, mod, "confirmed", NULL);
2672 if (!node) {
2673 lyd_free(data);
2674 return NC_MSG_ERROR;
2675 }
Michal Vasko086311b2016-01-08 09:53:11 +01002676 }
2677
2678 if (rpc_com->confirm_timeout) {
2679 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vasko283c3c02021-01-13 14:12:19 +01002680 node = lyd_new_leaf(data, mod, "confirm-timeout", str);
2681 if (!node) {
2682 lyd_free(data);
2683 return NC_MSG_ERROR;
2684 }
Michal Vasko086311b2016-01-08 09:53:11 +01002685 }
2686
2687 if (rpc_com->persist) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002688 node = lyd_new_leaf(data, mod, "persist", rpc_com->persist);
Michal Vasko086311b2016-01-08 09:53:11 +01002689 if (!node) {
2690 lyd_free(data);
2691 return NC_MSG_ERROR;
2692 }
2693 }
2694
2695 if (rpc_com->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002696 node = lyd_new_leaf(data, mod, "persist-id", rpc_com->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002697 if (!node) {
2698 lyd_free(data);
2699 return NC_MSG_ERROR;
2700 }
2701 }
2702 break;
2703
2704 case NC_RPC_DISCARD:
Michal Vaskoc1171a42019-11-05 12:06:46 +01002705 data = lyd_new(NULL, mod, "discard-changes");
Michal Vasko086311b2016-01-08 09:53:11 +01002706 break;
2707
2708 case NC_RPC_CANCEL:
2709 rpc_can = (struct nc_rpc_cancel *)rpc;
2710
Michal Vaskoc1171a42019-11-05 12:06:46 +01002711 data = lyd_new(NULL, mod, "cancel-commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002712 if (rpc_can->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002713 node = lyd_new_leaf(data, mod, "persist-id", rpc_can->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002714 if (!node) {
2715 lyd_free(data);
2716 return NC_MSG_ERROR;
2717 }
2718 }
2719 break;
2720
2721 case NC_RPC_VALIDATE:
2722 rpc_val = (struct nc_rpc_validate *)rpc;
2723
Michal Vaskoc1171a42019-11-05 12:06:46 +01002724 data = lyd_new(NULL, mod, "validate");
2725 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002726 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002727 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002728 node = lyd_new_anydata(node, mod, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002729 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002730 node = lyd_new_leaf(node, mod, "url", rpc_val->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002731 }
2732 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002733 node = lyd_new_leaf(node, mod, ncds2str[rpc_val->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002734 }
2735 if (!node) {
2736 lyd_free(data);
2737 return NC_MSG_ERROR;
2738 }
2739 break;
2740
2741 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002742 rpc_gs = (struct nc_rpc_getschema *)rpc;
2743
Michal Vaskoc1171a42019-11-05 12:06:46 +01002744 data = lyd_new(NULL, mod, "get-schema");
2745 node = lyd_new_leaf(data, mod, "identifier", rpc_gs->identifier);
Michal Vasko086311b2016-01-08 09:53:11 +01002746 if (!node) {
2747 lyd_free(data);
2748 return NC_MSG_ERROR;
2749 }
2750 if (rpc_gs->version) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002751 node = lyd_new_leaf(data, mod, "version", rpc_gs->version);
Michal Vasko086311b2016-01-08 09:53:11 +01002752 if (!node) {
2753 lyd_free(data);
2754 return NC_MSG_ERROR;
2755 }
2756 }
2757 if (rpc_gs->format) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002758 node = lyd_new_leaf(data, mod, "format", rpc_gs->format);
Michal Vasko086311b2016-01-08 09:53:11 +01002759 if (!node) {
2760 lyd_free(data);
2761 return NC_MSG_ERROR;
2762 }
2763 }
2764 break;
2765
2766 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002767 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2768
Michal Vaskoc1171a42019-11-05 12:06:46 +01002769 data = lyd_new(NULL, mod, "create-subscription");
Michal Vasko086311b2016-01-08 09:53:11 +01002770 if (rpc_sub->stream) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002771 node = lyd_new_leaf(data, mod, "stream", rpc_sub->stream);
Michal Vasko086311b2016-01-08 09:53:11 +01002772 if (!node) {
2773 lyd_free(data);
2774 return NC_MSG_ERROR;
2775 }
2776 }
2777
2778 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002779 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002780 node = lyd_new_anydata(data, mod, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002781 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002782 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002783 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002784 lyd_insert_attr(node, NULL, "type", "xpath");
2785 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002786 }
2787 if (!node) {
2788 lyd_free(data);
2789 return NC_MSG_ERROR;
2790 }
2791 }
2792
2793 if (rpc_sub->start) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002794 node = lyd_new_leaf(data, mod, "startTime", rpc_sub->start);
Michal Vasko086311b2016-01-08 09:53:11 +01002795 if (!node) {
2796 lyd_free(data);
2797 return NC_MSG_ERROR;
2798 }
2799 }
2800
2801 if (rpc_sub->stop) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002802 node = lyd_new_leaf(data, mod, "stopTime", rpc_sub->stop);
Michal Vasko086311b2016-01-08 09:53:11 +01002803 if (!node) {
2804 lyd_free(data);
2805 return NC_MSG_ERROR;
2806 }
2807 }
2808 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002809
2810 case NC_RPC_GETDATA:
2811 rpc_getd = (struct nc_rpc_getdata *)rpc;
2812
2813 data = lyd_new(NULL, mod, "get-data");
2814 node = lyd_new_leaf(data, mod, "datastore", rpc_getd->datastore);
2815 if (!node) {
2816 lyd_free(data);
2817 return NC_MSG_ERROR;
2818 }
2819 if (rpc_getd->filter) {
2820 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
2821 node = lyd_new_anydata(data, mod, "subtree-filter", rpc_getd->filter, LYD_ANYDATA_SXML);
2822 } else {
2823 node = lyd_new_leaf(data, mod, "xpath-filter", rpc_getd->filter);
2824 }
2825 if (!node) {
2826 lyd_free(data);
2827 return NC_MSG_ERROR;
2828 }
2829 }
2830 if (rpc_getd->config_filter) {
2831 node = lyd_new_leaf(data, mod, "config-filter", rpc_getd->config_filter);
2832 if (!node) {
2833 lyd_free(data);
2834 return NC_MSG_ERROR;
2835 }
2836 }
2837 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
2838 node = lyd_new_leaf(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" : "origin-filter",
2839 rpc_getd->origin_filter[i]);
2840 if (!node) {
2841 lyd_free(data);
2842 return NC_MSG_ERROR;
2843 }
2844 }
2845 if (rpc_getd->max_depth) {
2846 sprintf(str, "%u", rpc_getd->max_depth);
2847 node = lyd_new_leaf(data, mod, "max-depth", str);
2848 if (!node) {
2849 lyd_free(data);
2850 return NC_MSG_ERROR;
2851 }
2852 }
2853 if (rpc_getd->with_origin) {
2854 node = lyd_new_leaf(data, mod, "with-origin", NULL);
2855 if (!node) {
2856 lyd_free(data);
2857 return NC_MSG_ERROR;
2858 }
2859 }
2860
2861 if (rpc_getd->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002862 switch (rpc_getd->wd_mode) {
2863 case NC_WD_UNKNOWN:
2864 /* cannot get here */
2865 break;
2866 case NC_WD_ALL:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002867 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
2868 node = lyd_new_leaf(data, mod, "with-defaults", "report-all");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002869 break;
2870 case NC_WD_ALL_TAG:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002871 node = lyd_new_leaf(data, mod, "with-defaults", "report-all-tagged");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002872 break;
2873 case NC_WD_TRIM:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002874 node = lyd_new_leaf(data, mod, "with-defaults", "trim");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002875 break;
2876 case NC_WD_EXPLICIT:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002877 node = lyd_new_leaf(data, mod, "with-defaults", "explicit");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002878 break;
2879 }
2880 if (!node) {
2881 lyd_free(data);
2882 return NC_MSG_ERROR;
2883 }
2884 }
2885 break;
2886
2887 case NC_RPC_EDITDATA:
2888 rpc_editd = (struct nc_rpc_editdata *)rpc;
2889
2890 data = lyd_new(NULL, mod, "edit-data");
2891 node = lyd_new_leaf(data, mod, "datastore", rpc_editd->datastore);
2892 if (!node) {
2893 lyd_free(data);
2894 return NC_MSG_ERROR;
2895 }
2896
2897 if (rpc_editd->default_op) {
2898 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op]);
2899 if (!node) {
2900 lyd_free(data);
2901 return NC_MSG_ERROR;
2902 }
2903 }
2904
2905 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
2906 node = lyd_new_anydata(data, mod, "config", rpc_editd->edit_cont, LYD_ANYDATA_SXML);
2907 } else {
2908 node = lyd_new_leaf(data, mod, "url", rpc_editd->edit_cont);
2909 }
2910 if (!node) {
2911 lyd_free(data);
2912 return NC_MSG_ERROR;
2913 }
2914 break;
2915
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002916 default:
2917 ERRINT;
2918 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002919 }
2920
Michal Vasko131120a2018-05-29 15:44:02 +02002921 if (!data) {
2922 /* error was already printed */
2923 return NC_MSG_ERROR;
2924 }
2925
Michal Vasko41adf392017-01-17 10:39:04 +01002926 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2927 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002928 if (dofree) {
2929 lyd_free(data);
2930 }
Michal Vasko086311b2016-01-08 09:53:11 +01002931 return NC_MSG_ERROR;
2932 }
2933
Michal Vasko131120a2018-05-29 15:44:02 +02002934 /* send RPC, store its message ID */
2935 r = nc_send_msg_io(session, timeout, data);
2936 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002937
Radek Krejcib4b19062018-02-07 16:33:06 +01002938 if (dofree) {
2939 lyd_free(data);
2940 }
Michal Vasko086311b2016-01-08 09:53:11 +01002941
Michal Vasko131120a2018-05-29 15:44:02 +02002942 if (r == NC_MSG_RPC) {
2943 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002944 }
Michal Vasko131120a2018-05-29 15:44:02 +02002945 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002946}
Michal Vaskode2946c2017-01-12 12:19:26 +01002947
2948API void
2949nc_client_session_set_not_strict(struct nc_session *session)
2950{
2951 if (session->side != NC_CLIENT) {
2952 ERRARG("session");
2953 return;
2954 }
2955
2956 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2957}