blob: 941f872cf65ee82fc05a0eb34fd3e2d98ba8a1b2 [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) {
520 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
521 clb_data->session->id, mod_name);
522 }
523 }
524 if (submod_name) {
525 name = submod_name;
526 if (sub_rev) {
527 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200528 } else if (match) {
529 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200530 WRN("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 +0200531 clb_data->session->id, submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200532 } else {
533 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
534 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
535 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
536 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200537 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200538 if (!rev) {
539 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
540 clb_data->session->id, submod_name, mod_name);
541 return NULL;
542 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200543 }
544 }
545 } else {
546 name = mod_name;
547 rev = mod_rev;
548 }
549
550 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
551
552 if (match) {
553 /* we have enough information to avoid communication with server and try to get
554 * the schema locally */
555
556 /* 1. try to get data locally */
557 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
558
559 /* 2. try to use <get-schema> */
560 if (!model_data && clb_data->has_get_schema) {
561 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
562 }
563 } else {
564 /* we are unsure which revision of the schema we should load, so first try to get
565 * the newest revision from the server via get-schema and only if the server does not
566 * implement get-schema, try to load the newest revision locally. This is imperfect
567 * solution, but there are situation when a client does not know what revision is
568 * actually implemented by the server. */
569
570 /* 1. try to use <get-schema> */
571 if (clb_data->has_get_schema) {
572 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
573 }
574
575 /* 2. try to get data locally */
576 if (!model_data) {
577 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
578 }
579 }
580
581 /* 3. try to use user callback */
582 if (!model_data && clb_data->user_clb) {
583 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
584 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
585 }
586
Jan Kundrát35972df2018-09-06 19:00:01 +0200587 *free_module_data = free_with_user_data;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200588 return model_data;
589}
590
Michal Vaskoceae0152018-02-14 16:03:59 +0100591static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200592nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200593 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 +0100594{
595 int ret = 0;
596 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200597 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200598 LYS_INFORMAT format;
Jan Kundrát35972df2018-09-06 19:00:01 +0200599 void (*free_module_data)(void*, void*) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200600 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100601
Radek Krejci65ef6d52018-08-16 16:35:02 +0200602 *mod = NULL;
603 if (revision) {
604 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
605 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100606 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200607 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100608 /* make the present module implemented */
609 if (lys_set_implemented(*mod)) {
610 ERR("Failed to implement model \"%s\".", (*mod)->name);
611 ret = -1;
612 }
613 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200614 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100615 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200616 clb_data.has_get_schema = has_get_schema;
617 clb_data.schemas = schemas;
618 clb_data.session = session;
619 clb_data.user_clb = user_clb;
620 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100621
622 /* clear all the errors and just collect them for now */
623 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200624 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100625
Radek Krejci65ef6d52018-08-16 16:35:02 +0200626 /* get module data */
627 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100628
Radek Krejci65ef6d52018-08-16 16:35:02 +0200629 if (module_data) {
630 /* parse the schema */
631 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100632
Radek Krejci65ef6d52018-08-16 16:35:02 +0200633 *mod = lys_parse_mem(session->ctx, module_data, format);
634 if (*free_module_data) {
Jan Kundrát35972df2018-09-06 19:00:01 +0200635 (*free_module_data)((char*)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100636 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100637
Radek Krejci65ef6d52018-08-16 16:35:02 +0200638 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
639 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100640
Michal Vaskodbf7c272018-02-19 09:07:59 +0100641 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200642 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100643 if (!(*mod)) {
644 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
645 ly_err_print(eitem);
646 }
647 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200648 } else {
649 /* print only warnings */
650 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
651 if (eitem->level == LY_LLWRN) {
652 ly_err_print(eitem);
653 }
654 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100655 }
656
Michal Vaskodbf7c272018-02-19 09:07:59 +0100657 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100658 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100659 }
660
661 return ret;
662}
663
Radek Krejci65ef6d52018-08-16 16:35:02 +0200664static void
665free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200666{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200667 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200668
Radek Krejci65ef6d52018-08-16 16:35:02 +0200669 if (!list) {
670 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200671 }
672
Radek Krejci65ef6d52018-08-16 16:35:02 +0200673 for (u = 0; list[u].name; ++u) {
674 free(list[u].name);
675 free(list[u].revision);
676 for (v = 0; v < list[u].features.number; ++v) {
677 free(list[u].features.set.g[v]);
678 }
679 free(list[u].features.set.g);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200680 if (list[u].submodules) {
681 for (v = 0; list[u].submodules[v].name; ++v) {
682 free(list[u].submodules[v].name);
683 free(list[u].submodules[v].revision);
684 }
685 free(list[u].submodules);
686 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200687 }
688 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200689}
690
Radek Krejci235d8cb2018-08-17 14:04:32 +0200691
692static int
693build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200694{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200695 struct nc_rpc *rpc = NULL;
696 struct nc_reply *reply = NULL;
697 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200698 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200699 NC_MSG_TYPE msg;
700 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200701 struct ly_set *modules = NULL;
Radek Krejci2d088832018-08-21 13:40:14 +0200702 unsigned int u, v, submodules_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200703 struct lyd_node *iter, *child;
704 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200705 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200706
707 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200708 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
709 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
710 } else {
711 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0, NC_PARAMTYPE_CONST);
712 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200713 if (!rpc) {
714 goto cleanup;
715 }
716
717 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
718 usleep(1000);
719 }
720 if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200721 WRN("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200722 session->id);
723 goto cleanup;
724 }
725
726 do {
727 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Robin Jarry52ddb952019-10-15 16:23:01 +0200728 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200729 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200730 WRN("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200731 goto cleanup;
Robin Jarry52ddb952019-10-15 16:23:01 +0200732 } else if (msg == NC_MSG_ERROR || reply == NULL) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200733 WRN("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200734 goto cleanup;
735 }
736
737 switch (reply->type) {
738 case NC_RPL_OK:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200739 WRN("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200740 goto cleanup;
741 case NC_RPL_DATA:
742 /* fine */
743 break;
744 case NC_RPL_ERROR:
745 error_rpl = (struct nc_reply_error *)reply;
746 if (error_rpl->count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200747 WRN("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200748 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
749 } else {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200750 WRN("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200751 }
752 goto cleanup;
753 case NC_RPL_NOTIF:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200754 WRN("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200755 goto cleanup;
756 }
757
Radek Krejci65ef6d52018-08-16 16:35:02 +0200758 yldata = ((struct nc_reply_data *)reply)->data;
759 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200760 WRN("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200761 goto cleanup;
762 }
763
Radek Krejci65ef6d52018-08-16 16:35:02 +0200764 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200765 if (!modules) {
Radek Krejci2d088832018-08-21 13:40:14 +0200766 WRN("Session %u: no module information in reply to a yang-library <get> RPC.", session->id);
767 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200768 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200769
Radek Krejci2d088832018-08-21 13:40:14 +0200770 (*result) = calloc(modules->number + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200771 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200772 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200773 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200774 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200775 }
776
Radek Krejci2d088832018-08-21 13:40:14 +0200777 for (u = 0; u < modules->number; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200778 submodules_count = 0;
779 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200780 LY_TREE_FOR(modules->set.d[u]->child, iter) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200781 if (iter->schema->module != mod) {
782 /* ignore node from other schemas (augments) */
783 continue;
784 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200785 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
786 /* ignore empty nodes */
787 continue;
788 }
789 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200790 (*result)[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200791 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200792 (*result)[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200793 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200794 (*result)[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200795 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200796 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 +0200797 } else if (!strcmp(iter->schema->name, "submodule")) {
798 submodules_count++;
799 }
800 }
801
802 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200803 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
804 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200805 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200806 free_schema_info(*result);
807 *result = NULL;
808 ret = EXIT_FAILURE;
809 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200810 } else {
811 v = 0;
812 LY_TREE_FOR(modules->set.d[u]->child, iter) {
813 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
814 if (mod == iter->schema->module && !strcmp(iter->schema->name, "submodule")) {
815 LY_TREE_FOR(iter->child, child) {
816 if (mod != child->schema->module) {
817 continue;
818 } else if (!strcmp(child->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200819 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200820 } else if (!strcmp(child->schema->name, "revision")) {
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 }
823 }
824 }
825 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200826 }
827 }
828 }
829
Radek Krejcifd5b6682017-06-13 15:52:53 +0200830cleanup:
831 nc_rpc_free(rpc);
832 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200833 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200834
Radek Krejci235d8cb2018-08-17 14:04:32 +0200835 if (session->status != NC_STATUS_RUNNING) {
836 /* something bad heppened, discard the session */
837 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
838 ret = EXIT_FAILURE;
839 }
840
841 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200842}
843
Radek Krejci235d8cb2018-08-17 14:04:32 +0200844static int
845build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200846{
847 unsigned int u, v;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200848 char *module_cpblt, *ptr, *ptr2;
849
850 for (u = 0; cpblts[u]; ++u);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200851 (*result) = calloc(u + 1, sizeof **result);
852 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200853 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200854 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200855 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200856
857 for (u = v = 0; cpblts[u]; ++u) {
858 module_cpblt = strstr(cpblts[u], "module=");
859 /* this capability requires a module */
860 if (!module_cpblt) {
861 continue;
862 }
863
864 /* get module's name */
865 ptr = (char *)module_cpblt + 7;
866 ptr2 = strchr(ptr, '&');
867 if (!ptr2) {
868 ptr2 = ptr + strlen(ptr);
869 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200870 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200871
872 /* get module's revision */
873 ptr = strstr(module_cpblt, "revision=");
874 if (ptr) {
875 ptr += 9;
876 ptr2 = strchr(ptr, '&');
877 if (!ptr2) {
878 ptr2 = ptr + strlen(ptr);
879 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200880 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200881 }
882
883 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200884 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200885
886 /* get module's features */
887 ptr = strstr(module_cpblt, "features=");
888 if (ptr) {
889 ptr += 9;
890 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
891 if (*ptr == ',') {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200892 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200893 ptr2 = ptr + 1;
894 }
895 }
896 /* the last one */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200897 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200898 }
899 ++v;
900 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200901
902 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200903}
904
905static int
906nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
907{
908 int ret = EXIT_FAILURE;
909 const struct lys_module *mod;
910 unsigned int u, v;
911
912 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200913 /* skip import-only modules */
914 if (!modules[u].implemented) {
915 continue;
916 }
917
Radek Krejci65ef6d52018-08-16 16:35:02 +0200918 /* we can continue even if it fails */
919 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
920
921 if (!mod) {
922 if (session->status != NC_STATUS_RUNNING) {
923 /* something bad heppened, discard the session */
924 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
925 goto cleanup;
926 }
927
928 /* all loading ways failed, the schema will be ignored in the received data */
929 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
930 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
931 } else {
932 /* set features - first disable all to enable specified then */
933 lys_features_disable(mod, "*");
934 for (v = 0; v < modules[u].features.number; v++) {
935 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
936 }
937 }
Michal Vasko60f66602017-10-17 13:52:18 +0200938 }
939
Radek Krejci65ef6d52018-08-16 16:35:02 +0200940 /* done */
941 ret = EXIT_SUCCESS;
942
943cleanup:
944
Radek Krejcifd5b6682017-06-13 15:52:53 +0200945 return ret;
946}
947
Radek Krejci65ef6d52018-08-16 16:35:02 +0200948static int
949nc_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)
950{
951 unsigned int u, v;
952 const struct lys_module *ietfnc;
953
954 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
955 if (!ietfnc) {
956 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +0200957 if (!ietfnc) {
958 ietfnc = lys_parse_mem(session->ctx, ietf_netconf_2013_09_29_yang, LYS_YANG);
959 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200960 }
961 if (!ietfnc) {
962 ERR("Loading base NETCONF schema failed.");
963 return 1;
964 }
965
966 /* set supported capabilities from ietf-netconf */
967 for (u = 0; modules[u].name; ++u) {
968 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
969 continue;
970 }
971
972 lys_features_disable(ietfnc, "*");
973 for (v = 0; v < modules[u].features.number; v++) {
974 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
975 }
976 }
977
978 return 0;
979}
980
Michal Vasko086311b2016-01-08 09:53:11 +0100981int
982nc_ctx_check_and_fill(struct nc_session *session)
983{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200984 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100985 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100986 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200987 const struct lys_module *mod = NULL;
988 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200989 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100990
Michal Vasko2e6defd2016-10-07 15:48:15 +0200991 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100992
Radek Krejci65ef6d52018-08-16 16:35:02 +0200993 /* 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 +0200994 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200995
Radek Krejci65ef6d52018-08-16 16:35:02 +0200996 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
997 * schemas only to those present on the server side */
998 ly_ctx_set_disable_searchdirs(session->ctx);
999
1000 /* our callback is set later with appropriate data */
1001 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
1002
1003 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001004 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001005 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001006 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +02001007 if (yanglib_support) {
1008 break;
1009 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001010 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001011 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +02001012 if (get_schema_support) {
1013 break;
1014 }
Michal Vasko086311b2016-01-08 09:53:11 +01001015 }
1016 }
Michal Vasko31dd4c52020-04-17 10:29:44 +02001017 if (get_schema_support) {
1018 VRB("Session %u: capability for <get-schema> support found.", session->id);
1019 } else {
1020 VRB("Session %u: capability for <get-schema> support not found.", session->id);
1021 }
1022 if (yanglib_support) {
1023 VRB("Session %u: capability for yang-library support found.", session->id);
1024 } else {
1025 VRB("Session %u: capability for yang-library support not found.", session->id);
1026 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001027
1028 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001029 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
1030 ERR("Session %u: unable to get server's schema information from the <hello>'s capabilities.", session->id);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001031 goto cleanup;
1032 }
1033
Michal Vasko086311b2016-01-08 09:53:11 +01001034 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Michal Vasko8a4e1462020-05-07 11:32:31 +02001035 if (get_schema_support && !lys_parse_mem(session->ctx, ietf_netconf_monitoring_2010_10_04_yang, LYS_YANG)) {
1036 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
1037 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001038 }
1039
1040 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001041 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001042 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001043 }
1044
Radek Krejci65ef6d52018-08-16 16:35:02 +02001045 /* get correct version of ietf-yang-library into context */
1046 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001047 /* use get schema to get server's ietf-yang-library */
1048 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1049 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001050 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
1051 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001052 yanglib_support = 0;
1053 } else {
1054 revision = strndup(&revision[9], 10);
Michal Vasko0d877f92020-04-02 13:52:43 +02001055 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data,
1056 get_schema_support, &mod)) {
1057 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, unable to use it to learn all the supported modules.",
1058 session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001059 yanglib_support = 0;
1060 }
Michal Vasko0d877f92020-04-02 13:52:43 +02001061 if (strcmp(revision, "2019-01-04") >= 0) {
1062 /* we also need ietf-datastores to be implemented */
1063 if (nc_ctx_load_module(session, "ietf-datastores", NULL, server_modules, old_clb, old_data,
1064 get_schema_support, &mod)) {
1065 WRN("Session %u: loading NETCONF ietf-datastores schema failed, unable to use yang-library"
1066 " to learn all the supported modules.", session->id);
1067 yanglib_support = 0;
1068 }
1069 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001070 free(revision);
1071 }
1072 }
1073
Radek Krejci65ef6d52018-08-16 16:35:02 +02001074 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001075 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001076 if (build_schema_info_yl(session, &sm)) {
1077 goto cleanup;
1078 } else if (!sm) {
1079 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
1080 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001081 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
1082 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001083 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001084 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001085 }
Michal Vaskoef578332016-01-25 13:20:09 +01001086
Radek Krejci65ef6d52018-08-16 16:35:02 +02001087 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1088 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001089 }
1090
Radek Krejcifd5b6682017-06-13 15:52:53 +02001091 /* succsess */
1092 ret = 0;
1093
Michal Vaskoeee99412016-11-21 10:19:43 +01001094 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001095 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 +01001096 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001097
1098cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001099 free_schema_info(server_modules);
1100
Radek Krejcifd5b6682017-06-13 15:52:53 +02001101 /* set user callback back */
1102 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001103 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001104
Michal Vaskoef578332016-01-25 13:20:09 +01001105 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001106}
1107
1108API struct nc_session *
1109nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1110{
Michal Vaskod083db62016-01-19 10:31:29 +01001111 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001112
Michal Vasko45e53ae2016-04-07 11:46:03 +02001113 if (fdin < 0) {
1114 ERRARG("fdin");
1115 return NULL;
1116 } else if (fdout < 0) {
1117 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001118 return NULL;
1119 }
1120
1121 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001122 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001123 if (!session) {
1124 ERRMEM;
1125 return NULL;
1126 }
1127 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001128
1129 /* transport specific data */
1130 session->ti_type = NC_TI_FD;
1131 session->ti.fd.in = fdin;
1132 session->ti.fd.out = fdout;
1133
Robin Jarry4e38e292019-10-15 17:14:14 +02001134 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1135 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001136 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001137 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001138
1139 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001140 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001141 goto fail;
1142 }
1143 session->status = NC_STATUS_RUNNING;
1144
Michal Vaskoef578332016-01-25 13:20:09 +01001145 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001146 goto fail;
1147 }
1148
1149 return session;
1150
1151fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001152 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001153 return NULL;
1154}
1155
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001156API struct nc_session *
1157nc_connect_unix(const char *address, struct ly_ctx *ctx)
1158{
1159 struct nc_session *session = NULL;
1160 struct sockaddr_un sun;
1161 const struct passwd *pw;
1162 char *username;
1163 int sock = -1;
1164
1165 if (address == NULL) {
1166 ERRARG("address");
1167 return NULL;
1168 }
1169
1170 pw = getpwuid(geteuid());
1171 if (pw == NULL) {
1172 ERR("Failed to find username for euid=%u.\n", geteuid());
1173 goto fail;
1174 }
1175
1176 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1177 if (sock < 0) {
1178 ERR("Failed to create socket (%s).", strerror(errno));
1179 goto fail;
1180 }
1181
1182 memset(&sun, 0, sizeof(sun));
1183 sun.sun_family = AF_UNIX;
1184 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1185
1186 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
1187 ERR("cannot connect to sock server %s (%s)",
1188 address, strerror(errno));
1189 goto fail;
1190 }
1191
1192 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
1193 ERR("Fcntl failed (%s).", strerror(errno));
1194 goto fail;
1195 }
1196
1197 /* prepare session structure */
1198 session = nc_new_session(NC_CLIENT, 0);
1199 if (!session) {
1200 ERRMEM;
1201 goto fail;
1202 }
1203 session->status = NC_STATUS_STARTING;
1204
1205 /* transport specific data */
1206 session->ti_type = NC_TI_UNIX;
1207 session->ti.unixsock.sock = sock;
1208 sock = -1; /* do not close sock in fail label anymore */
1209
Robin Jarry4e38e292019-10-15 17:14:14 +02001210 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1211 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001212 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001213 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001214
1215 session->path = lydict_insert(ctx, address, 0);
1216
1217 username = strdup(pw->pw_name);
1218 if (username == NULL) {
1219 ERRMEM;
1220 goto fail;
1221 }
1222 session->username = lydict_insert_zc(ctx, username);
1223
1224 /* NETCONF handshake */
1225 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1226 goto fail;
1227 }
1228 session->status = NC_STATUS_RUNNING;
1229
1230 if (nc_ctx_check_and_fill(session) == -1) {
1231 goto fail;
1232 }
1233
1234 return session;
1235
1236fail:
1237 nc_session_free(session, NULL);
1238 if (sock >= 0)
1239 close(sock);
1240 return NULL;
1241}
1242
Frank Rimpler9f838b02018-07-25 06:44:03 +00001243/*
1244 Helper for a non-blocking connect (which is required because of the locking
1245 concept for e.g. call home settings). For more details see nc_sock_connect().
1246 */
1247static int
Michal Vasko5e8d0192019-06-24 19:19:49 +02001248_non_blocking_connect(int timeout, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001249{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001250 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001251 int sock = -1;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001252 fd_set wset;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001253 struct timeval ts;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001254 socklen_t len = sizeof(int);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001255 struct in_addr *addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001256 uint16_t port;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001257 char str[INET6_ADDRSTRLEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001258
Frank Rimpler9f838b02018-07-25 06:44:03 +00001259 if (sock_pending && *sock_pending != -1) {
1260 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1261 sock = *sock_pending;
1262 } else {
1263 assert(res);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001264 if (res->ai_family == AF_INET6) {
1265 addr = (struct in_addr *) &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001266 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001267 } else {
1268 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001269 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001270 }
1271 if (!inet_ntop(res->ai_family, addr, str, res->ai_addrlen)) {
1272 WRN("inet_ntop() failed (%s).", strerror(errno));
1273 } else {
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001274 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 +02001275 }
1276
1277 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001278 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1279 if (sock == -1) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001280 ERR("Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001281 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001282 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001283 /* make the socket non-blocking */
1284 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001285 ERR("fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001286 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001287 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001288 /* non-blocking connect! */
1289 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1290 if (errno != EINPROGRESS) {
1291 /* network connection failed, try another resource */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001292 ERR("connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001293 goto cleanup;
1294 }
1295 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001296 }
1297 ts.tv_sec = timeout;
1298 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001299
Frank Rimpler9f838b02018-07-25 06:44:03 +00001300 FD_ZERO(&wset);
1301 FD_SET(sock, &wset);
1302
1303 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001304 ERR("select() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001305 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001306 }
1307
Michal Vasko5e8d0192019-06-24 19:19:49 +02001308 if (ret == 0) {
1309 /* there was a timeout */
1310 VRB("Timed out after %ds (%s).", timeout, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001311 if (sock_pending) {
1312 /* no sock-close, we'll try it again */
1313 *sock_pending = sock;
1314 } else {
1315 close(sock);
1316 }
1317 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001318 }
Radek Krejci782041a2018-08-20 10:09:45 +02001319
1320 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001321 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001322 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001323 ERR("getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001324 goto cleanup;
1325 }
Michal Vaskoe27ab4e2020-07-27 11:10:58 +02001326 if (error) {
Radek Krejci782041a2018-08-20 10:09:45 +02001327 /* network connection failed, try another resource */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001328 VRB("getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001329 errno = error;
1330 goto cleanup;
1331 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001332
1333 /* enable keep-alive */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001334 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001335 goto cleanup;
1336 }
1337
Michal Vasko086311b2016-01-08 09:53:11 +01001338 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001339
Frank Rimpler9f838b02018-07-25 06:44:03 +00001340cleanup:
1341 if (sock_pending) {
1342 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001343 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001344 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001345 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001346}
1347
Frank Rimpler9f838b02018-07-25 06:44:03 +00001348/* A given timeout value limits the time how long the function blocks. If it has to block
1349 only for some seconds, a socket connection might not yet have been fully established.
1350 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1351 value will be -1. In such a case a subsequent invokation is required, by providing the
1352 stored sock_pending, again.
1353 In general, if this function returns -1, when a timeout has been given, this function
1354 has to be invoked, until it returns a valid socket.
1355 */
1356int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001357nc_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 +00001358{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001359 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001360 int sock = sock_pending ? *sock_pending : -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001361 struct addrinfo hints, *res_list, *res;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001362 char *buf, port_s[6]; /* length of string representation of short int */
Michal Vasko66032bc2019-01-22 15:03:12 +01001363 void *addr;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001364
Michal Vasko66032bc2019-01-22 15:03:12 +01001365 DBG("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001366
1367 /* no pending socket */
1368 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001369 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001370 snprintf(port_s, 6, "%u", port);
1371 memset(&hints, 0, sizeof hints);
1372 hints.ai_family = AF_UNSPEC;
1373 hints.ai_socktype = SOCK_STREAM;
1374 hints.ai_protocol = IPPROTO_TCP;
1375 i = getaddrinfo(host, port_s, &hints, &res_list);
1376 if (i != 0) {
1377 ERR("Unable to translate the host address (%s).", gai_strerror(i));
Michal Vaskocb846632019-12-13 15:12:45 +01001378 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001379 }
1380
1381 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001382 sock = _non_blocking_connect(timeout, sock_pending, res, ka);
Michal Vasko2af7c382020-07-27 14:21:35 +02001383 if (sock == -1) {
1384 if (!sock_pending || *sock_pending == -1) {
1385 /* try the next resource */
1386 continue;
1387 } else {
1388 /* timeout, keep pending socket */
1389 return -1;
1390 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001391 }
1392 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 +01001393
1394 opt = 1;
1395 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
1396 ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001397 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001398 }
1399
Michal Vasko66032bc2019-01-22 15:03:12 +01001400 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1401 buf = malloc(INET6_ADDRSTRLEN);
1402 if (!buf) {
1403 ERRMEM;
Michal Vaskocb846632019-12-13 15:12:45 +01001404 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001405 }
1406 if (res->ai_family == AF_INET) {
1407 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1408 } else {
1409 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1410 }
1411 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
1412 ERR("Converting host to IP address failed (%s).", strerror(errno));
1413 free(buf);
Michal Vaskocb846632019-12-13 15:12:45 +01001414 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001415 }
1416
1417 *ip_host = buf;
1418 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001419 break;
1420 }
1421 freeaddrinfo(res_list);
1422
1423 } else {
1424 /* try to get a connection with the pending socket */
1425 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001426 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001427 }
1428
1429 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001430
1431error:
1432 if (sock != -1) {
1433 close(sock);
1434 }
1435 if (sock_pending) {
1436 *sock_pending = -1;
1437 }
1438 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001439}
1440
Michal Vasko086311b2016-01-08 09:53:11 +01001441static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001442get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001443{
Michal Vasko086311b2016-01-08 09:53:11 +01001444 char *ptr;
1445 const char *str_msgid;
1446 uint64_t cur_msgid;
1447 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001448 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001449 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1450
Michal Vasko086311b2016-01-08 09:53:11 +01001451 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001452 if (!msgid && session->opts.client.notifs) {
1453 cont = session->opts.client.notifs;
1454 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001455
Michal Vasko71ba2da2016-05-04 10:53:16 +02001456 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001457 free(cont);
1458
Michal Vasko71ba2da2016-05-04 10:53:16 +02001459 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001460 }
1461
1462 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001463 if (msgid && session->opts.client.replies) {
1464 cont = session->opts.client.replies;
1465 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001466
Michal Vasko71ba2da2016-05-04 10:53:16 +02001467 xml = cont->msg;
1468 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001469
Michal Vasko71ba2da2016-05-04 10:53:16 +02001470 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001471 }
1472
Michal Vasko71ba2da2016-05-04 10:53:16 +02001473 if (!msgtype) {
1474 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001475 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001476 }
Michal Vasko086311b2016-01-08 09:53:11 +01001477
1478 /* we read rpc-reply, want a notif */
1479 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001480 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001481 while (*cont_ptr) {
1482 cont_ptr = &((*cont_ptr)->next);
1483 }
1484 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001485 if (!*cont_ptr) {
1486 ERRMEM;
1487 lyxml_free(session->ctx, xml);
1488 return NC_MSG_ERROR;
1489 }
Michal Vasko086311b2016-01-08 09:53:11 +01001490 (*cont_ptr)->msg = xml;
1491 (*cont_ptr)->next = NULL;
1492 }
1493
1494 /* we read notif, want a rpc-reply */
1495 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02001496 if (!ATOMIC_LOAD(session->opts.client.ntf_tid)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001497 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001498 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001499 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001500 }
Michal Vasko086311b2016-01-08 09:53:11 +01001501
Michal Vasko2e6defd2016-10-07 15:48:15 +02001502 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001503 while (*cont_ptr) {
1504 cont_ptr = &((*cont_ptr)->next);
1505 }
1506 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001507 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001508 ERRMEM;
1509 lyxml_free(session->ctx, xml);
1510 return NC_MSG_ERROR;
1511 }
Michal Vasko086311b2016-01-08 09:53:11 +01001512 (*cont_ptr)->msg = xml;
1513 (*cont_ptr)->next = NULL;
1514 }
1515
Michal Vasko086311b2016-01-08 09:53:11 +01001516 switch (msgtype) {
1517 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001518 if (!msgid) {
1519 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001520 }
Michal Vasko086311b2016-01-08 09:53:11 +01001521 break;
1522
1523 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001524 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001525 /* check message-id */
1526 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1527 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001528 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001529 } else {
1530 cur_msgid = strtoul(str_msgid, &ptr, 10);
1531 if (cur_msgid != msgid) {
1532 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1533 session->id, str_msgid);
1534 msgtype = NC_MSG_REPLY_ERR_MSGID;
1535 }
1536 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001537 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001538 }
Michal Vasko086311b2016-01-08 09:53:11 +01001539 break;
1540
1541 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001542 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001543 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001544 msgtype = NC_MSG_ERROR;
1545 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001546
1547 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001548 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001549 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001550 msgtype = NC_MSG_ERROR;
1551 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001552
1553 default:
1554 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1555 * NC_MSG_NONE is not returned by nc_read_msg()
1556 */
1557 break;
1558 }
1559
1560 return msgtype;
1561}
1562
1563/* cannot strictly fail, but does not need to fill any error parameter at all */
1564static void
1565parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1566{
1567 struct lyxml_elem *iter, *next, *info;
1568
1569 LY_TREE_FOR(xml->child, iter) {
1570 if (!iter->ns) {
1571 if (iter->content) {
1572 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1573 } else {
1574 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1575 }
1576 continue;
1577 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1578 if (iter->content) {
1579 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1580 iter->name, iter->content, iter->ns->value);
1581 } else {
1582 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1583 }
1584 continue;
1585 }
1586
1587 if (!strcmp(iter->name, "error-type")) {
1588 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1589 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1590 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1591 } else if (err->type) {
1592 WRN("<rpc-error> <error-type> duplicated.");
1593 } else {
1594 err->type = lydict_insert(ctx, iter->content, 0);
1595 }
1596 } else if (!strcmp(iter->name, "error-tag")) {
1597 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1598 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1599 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1600 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1601 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1602 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1603 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1604 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1605 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1606 && strcmp(iter->content, "malformed-message"))) {
1607 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1608 } else if (err->tag) {
1609 WRN("<rpc-error> <error-tag> duplicated.");
1610 } else {
1611 err->tag = lydict_insert(ctx, iter->content, 0);
1612 }
1613 } else if (!strcmp(iter->name, "error-severity")) {
1614 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1615 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1616 } else if (err->severity) {
1617 WRN("<rpc-error> <error-severity> duplicated.");
1618 } else {
1619 err->severity = lydict_insert(ctx, iter->content, 0);
1620 }
1621 } else if (!strcmp(iter->name, "error-app-tag")) {
1622 if (err->apptag) {
1623 WRN("<rpc-error> <error-app-tag> duplicated.");
1624 } else {
1625 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1626 }
1627 } else if (!strcmp(iter->name, "error-path")) {
1628 if (err->path) {
1629 WRN("<rpc-error> <error-path> duplicated.");
1630 } else {
1631 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1632 }
1633 } else if (!strcmp(iter->name, "error-message")) {
1634 if (err->message) {
1635 WRN("<rpc-error> <error-message> duplicated.");
1636 } else {
1637 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001638 if (err->message_lang) {
1639 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1640 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001641 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1642 }
1643 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1644 }
1645 } else if (!strcmp(iter->name, "error-info")) {
1646 LY_TREE_FOR_SAFE(iter->child, next, info) {
1647 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1648 if (!strcmp(info->name, "session-id")) {
1649 if (err->sid) {
1650 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1651 } else {
1652 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1653 }
Michal Vasko630485f2018-01-03 14:28:32 +01001654 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001655 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001656 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1657 if (!err->attr) {
1658 ERRMEM;
1659 return;
1660 }
Michal Vasko086311b2016-01-08 09:53:11 +01001661 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1662 } else if (!strcmp(info->name, "bad-element")) {
1663 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001664 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1665 if (!err->elem) {
1666 ERRMEM;
1667 return;
1668 }
Michal Vasko086311b2016-01-08 09:53:11 +01001669 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1670 } else if (!strcmp(info->name, "bad-namespace")) {
1671 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001672 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1673 if (!err->ns) {
1674 ERRMEM;
1675 return;
1676 }
Michal Vasko086311b2016-01-08 09:53:11 +01001677 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1678 } else {
1679 if (info->content) {
1680 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1681 info->name, info->content);
1682 } else {
1683 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1684 }
1685 }
1686 } else {
1687 lyxml_unlink(ctx, info);
1688 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001689 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1690 if (!err->other) {
1691 ERRMEM;
1692 return;
1693 }
Michal Vasko086311b2016-01-08 09:53:11 +01001694 err->other[err->other_count - 1] = info;
1695 }
1696 }
1697 } else {
1698 if (iter->content) {
1699 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1700 } else {
1701 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1702 }
1703 }
1704 }
1705}
1706
1707static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001708parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001709{
1710 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001711 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001712 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001713 struct nc_reply_data *data_rpl;
1714 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001715 struct nc_rpc_act_generic *rpc_gen;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001716 int i, data_parsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001717
Michal Vasko086311b2016-01-08 09:53:11 +01001718 /* rpc-error */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001719 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 +01001720 /* count and check elements */
1721 i = 0;
1722 LY_TREE_FOR(xml->child, iter) {
1723 if (strcmp(iter->name, "rpc-error")) {
1724 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1725 return NULL;
1726 } else if (!iter->ns) {
1727 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1728 return NULL;
1729 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1730 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1731 return NULL;
1732 }
1733 ++i;
1734 }
1735
1736 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001737 if (!error_rpl) {
1738 ERRMEM;
1739 return NULL;
1740 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001741 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001742 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001743 if (!error_rpl->err) {
1744 ERRMEM;
1745 free(error_rpl);
1746 return NULL;
1747 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001748 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001749 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001750 reply = (struct nc_reply *)error_rpl;
1751
1752 i = 0;
1753 LY_TREE_FOR(xml->child, iter) {
1754 parse_rpc_error(ctx, iter, error_rpl->err + i);
1755 ++i;
1756 }
1757
1758 /* ok */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001759 } 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 +01001760 if (xml->child->next) {
1761 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1762 return NULL;
1763 }
1764 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001765 if (!reply) {
1766 ERRMEM;
1767 return NULL;
1768 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001769 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001770
1771 /* some RPC output */
1772 } else {
1773 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001774 case NC_RPC_ACT_GENERIC:
1775 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001776
1777 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001778 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1779 if (!rpc_act) {
1780 ERR("Failed to duplicate a generic RPC/action.");
1781 return NULL;
1782 }
Michal Vasko086311b2016-01-08 09:53:11 +01001783 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001784 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 +02001785 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001786 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001787 return NULL;
1788 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001789 }
Michal Vasko086311b2016-01-08 09:53:11 +01001790 break;
1791
1792 case NC_RPC_GETCONFIG:
1793 case NC_RPC_GET:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001794 case NC_RPC_GETDATA:
Michal Vasko6b281ff2019-02-14 10:15:19 +01001795 /* we should definitely have received at least an empty "data" element even on empty reply, but fine */
1796 if (!xml->child || !xml->child->child) {
Michal Vasko13ed2942016-02-29 16:21:00 +01001797 /* we did not receive any data */
1798 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001799 if (!data_rpl) {
1800 ERRMEM;
1801 return NULL;
1802 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001803 data_rpl->type = NC_RPL_DATA;
1804 data_rpl->data = NULL;
1805 return (struct nc_reply *)data_rpl;
1806 }
1807
Michal Vasko086311b2016-01-08 09:53:11 +01001808 /* special treatment */
Michal Vaskof45e0c12018-11-28 08:38:23 +01001809 ly_errno = 0;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001810 data = lyd_parse_xml(ctx, &xml->child->child,
1811 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001812 | parseroptions);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001813 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001814 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1815 return NULL;
1816 }
Michal Vaskof45e0c12018-11-28 08:38:23 +01001817 data_parsed = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001818 break;
1819
1820 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001821 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001822 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001823 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001824 return NULL;
1825 }
1826 break;
1827
1828 case NC_RPC_EDIT:
1829 case NC_RPC_COPY:
1830 case NC_RPC_DELETE:
1831 case NC_RPC_LOCK:
1832 case NC_RPC_UNLOCK:
1833 case NC_RPC_KILL:
1834 case NC_RPC_COMMIT:
1835 case NC_RPC_DISCARD:
1836 case NC_RPC_CANCEL:
1837 case NC_RPC_VALIDATE:
1838 case NC_RPC_SUBSCRIBE:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001839 case NC_RPC_EDITDATA:
Michal Vasko086311b2016-01-08 09:53:11 +01001840 /* there is no output defined */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001841 ERR("Unexpected data reply (root elem \"%s\").", xml->child ? xml->child->name : NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001842 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001843 default:
1844 ERRINT;
1845 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001846 }
1847
1848 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001849 if (!data_rpl) {
1850 ERRMEM;
1851 return NULL;
1852 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001853 data_rpl->type = NC_RPL_DATA;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001854
1855 ly_errno = 0;
1856 if (!data_parsed) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001857 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001858 rpc_act, NULL);
Michal Vasko6b281ff2019-02-14 10:15:19 +01001859 if (!ly_errno && !data_rpl->data->child) {
1860 ERR("An empty data <rpc-reply>.");
1861 lyd_free_withsiblings(rpc_act);
1862 lyd_free(data_rpl->data);
1863 free(data_rpl);
1864 return NULL;
1865 }
Michal Vasko086311b2016-01-08 09:53:11 +01001866 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01001867 /* <get>, <get-config>, <get-data> */
Michal Vasko086311b2016-01-08 09:53:11 +01001868 data_rpl->data = data;
1869 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001870 lyd_free_withsiblings(rpc_act);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001871 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001872 ERR("Failed to parse <rpc-reply>.");
1873 free(data_rpl);
1874 return NULL;
1875 }
1876 reply = (struct nc_reply *)data_rpl;
1877 }
1878
1879 return reply;
1880}
1881
Radek Krejci53691be2016-02-22 13:58:37 +01001882#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001883
Michal Vasko3031aae2016-01-27 16:07:18 +01001884int
1885nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1886{
1887 int sock;
1888
Michal Vasko45e53ae2016-04-07 11:46:03 +02001889 if (!address) {
1890 ERRARG("address");
1891 return -1;
1892 } else if (!port) {
1893 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001894 return -1;
1895 }
1896
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001897 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001898 if (sock == -1) {
1899 return -1;
1900 }
1901
1902 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001903 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1904 if (!client_opts.ch_binds) {
1905 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001906 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001907 return -1;
1908 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001909
Michal Vasko2e6defd2016-10-07 15:48:15 +02001910 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1911 if (!client_opts.ch_bind_ti) {
1912 ERRMEM;
1913 close(sock);
1914 return -1;
1915 }
1916 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1917
Michal Vasko3031aae2016-01-27 16:07:18 +01001918 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001919 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1920 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001921 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001922 return -1;
1923 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001924 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1925 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001926 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001927
1928 return 0;
1929}
1930
1931int
1932nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1933{
1934 uint32_t i;
1935 int ret = -1;
1936
1937 if (!address && !port && !ti) {
1938 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1939 close(client_opts.ch_binds[i].sock);
1940 free((char *)client_opts.ch_binds[i].address);
1941
1942 ret = 0;
1943 }
1944 free(client_opts.ch_binds);
1945 client_opts.ch_binds = NULL;
1946 client_opts.ch_bind_count = 0;
1947 } else {
1948 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1949 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1950 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001951 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001952 close(client_opts.ch_binds[i].sock);
1953 free((char *)client_opts.ch_binds[i].address);
1954
1955 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001956 if (!client_opts.ch_bind_count) {
1957 free(client_opts.ch_binds);
1958 client_opts.ch_binds = NULL;
1959 } else if (i < client_opts.ch_bind_count) {
1960 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1961 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1962 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001963
1964 ret = 0;
1965 }
1966 }
1967 }
1968
1969 return ret;
1970}
1971
1972API int
1973nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1974{
1975 int sock;
1976 char *host = NULL;
1977 uint16_t port, idx;
1978
Michal Vasko45e53ae2016-04-07 11:46:03 +02001979 if (!client_opts.ch_binds) {
1980 ERRINIT;
1981 return -1;
1982 } else if (!session) {
1983 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001984 return -1;
1985 }
1986
1987 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1988
Michal Vasko50456e82016-02-02 12:16:08 +01001989 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001990 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001991 return sock;
1992 }
1993
Radek Krejci53691be2016-02-22 13:58:37 +01001994#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001995 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001996 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001997 } else
1998#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001999#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002000 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01002001 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01002002 } else
2003#endif
2004 {
Michal Vaskofee717c2016-02-01 13:25:43 +01002005 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01002006 *session = NULL;
2007 }
2008
2009 free(host);
2010
2011 if (!(*session)) {
2012 return -1;
2013 }
2014
2015 return 1;
2016}
2017
Radek Krejci53691be2016-02-22 13:58:37 +01002018#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01002019
Michal Vasko1b2ddc92017-05-24 08:59:49 +02002020API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02002021nc_session_get_cpblts(const struct nc_session *session)
2022{
2023 if (!session) {
2024 ERRARG("session");
2025 return NULL;
2026 }
2027
Michal Vasko1b2ddc92017-05-24 08:59:49 +02002028 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02002029}
2030
2031API const char *
2032nc_session_cpblt(const struct nc_session *session, const char *capab)
2033{
2034 int i, len;
2035
2036 if (!session) {
2037 ERRARG("session");
2038 return NULL;
2039 } else if (!capab) {
2040 ERRARG("capab");
2041 return NULL;
2042 }
2043
2044 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002045 for (i = 0; session->opts.client.cpblts[i]; ++i) {
2046 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
2047 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02002048 }
2049 }
2050
2051 return NULL;
2052}
2053
Michal Vasko9cd26a82016-05-31 08:58:48 +02002054API int
2055nc_session_ntf_thread_running(const struct nc_session *session)
2056{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002057 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02002058 ERRARG("session");
2059 return 0;
2060 }
2061
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002062 return ATOMIC_LOAD(session->opts.client.ntf_tid) ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02002063}
2064
Michal Vaskob7558c52016-02-26 15:04:19 +01002065API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002066nc_client_init(void)
2067{
2068 nc_init();
2069}
2070
2071API void
Michal Vaskob7558c52016-02-26 15:04:19 +01002072nc_client_destroy(void)
2073{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02002074 nc_client_set_schema_searchpath(NULL);
2075#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
2076 nc_client_ch_del_bind(NULL, 0, 0);
2077#endif
2078#ifdef NC_ENABLED_SSH
2079 nc_client_ssh_destroy_opts();
2080#endif
2081#ifdef NC_ENABLED_TLS
2082 nc_client_tls_destroy_opts();
2083#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002084 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01002085}
2086
Michal Vasko086311b2016-01-08 09:53:11 +01002087API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002088nc_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 +01002089{
Michal Vasko5a2c7162020-01-30 11:24:17 +01002090 struct lyxml_elem *xml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01002091 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2092
Michal Vasko45e53ae2016-04-07 11:46:03 +02002093 if (!session) {
2094 ERRARG("session");
2095 return NC_MSG_ERROR;
2096 } else if (!rpc) {
2097 ERRARG("rpc");
2098 return NC_MSG_ERROR;
Michal Vasko5a2c7162020-01-30 11:24:17 +01002099 } else if (!msgid) {
2100 ERRARG("msgid");
2101 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02002102 } else if (!reply) {
2103 ERRARG("reply");
2104 return NC_MSG_ERROR;
2105 } else if (parseroptions & LYD_OPT_TYPEMASK) {
2106 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01002107 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002108 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01002109 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002110 return NC_MSG_ERROR;
2111 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002112 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01002113 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
2114 parseroptions &= LYD_OPT_STRICT;
2115 }
Michal Vasko41adf392017-01-17 10:39:04 +01002116 /* no mechanism to check external dependencies is provided */
2117 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01002118 *reply = NULL;
2119
2120 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002121
Michal Vasko5a2c7162020-01-30 11:24:17 +01002122 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01002123 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko5a2c7162020-01-30 11:24:17 +01002124 lyxml_free_withsiblings(session->ctx, xml);
2125 xml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01002126 if (!(*reply)) {
2127 return NC_MSG_ERROR;
2128 }
2129 }
Michal Vasko5a2c7162020-01-30 11:24:17 +01002130 assert(!xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002131
2132 return msgtype;
2133}
2134
2135API NC_MSG_TYPE
2136nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
2137{
2138 struct lyxml_elem *xml, *ev_time;
2139 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2140
Michal Vasko45e53ae2016-04-07 11:46:03 +02002141 if (!session) {
2142 ERRARG("session");
2143 return NC_MSG_ERROR;
2144 } else if (!notif) {
2145 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01002146 return NC_MSG_ERROR;
2147 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002148 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002149 return NC_MSG_ERROR;
2150 }
2151
2152 msgtype = get_msg(session, timeout, 0, &xml);
2153
2154 if (msgtype == NC_MSG_NOTIF) {
2155 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002156 if (!*notif) {
2157 ERRMEM;
2158 lyxml_free(session->ctx, xml);
2159 return NC_MSG_ERROR;
2160 }
Michal Vasko086311b2016-01-08 09:53:11 +01002161
2162 /* eventTime */
2163 LY_TREE_FOR(xml->child, ev_time) {
2164 if (!strcmp(ev_time->name, "eventTime")) {
2165 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
2166 /* lyd_parse does not know this element */
2167 lyxml_free(session->ctx, ev_time);
2168 break;
2169 }
2170 }
2171 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01002172 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002173 goto fail;
2174 }
2175
2176 /* notification body */
Michal Vasko40208012020-12-09 19:24:23 +01002177 (*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 +01002178 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002179 lyxml_free(session->ctx, xml);
2180 xml = NULL;
2181 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01002182 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002183 goto fail;
2184 }
2185 }
2186
2187 return msgtype;
2188
2189fail:
2190 lydict_remove(session->ctx, (*notif)->datetime);
2191 lyd_free((*notif)->tree);
2192 free(*notif);
2193 *notif = NULL;
2194 lyxml_free(session->ctx, xml);
2195
2196 return NC_MSG_ERROR;
2197}
2198
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002199static void *
2200nc_recv_notif_thread(void *arg)
2201{
2202 struct nc_ntf_thread_arg *ntarg;
2203 struct nc_session *session;
2204 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
2205 struct nc_notif *notif;
2206 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002207 pthread_t *ntf_tid;
2208
2209 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002210
2211 ntarg = (struct nc_ntf_thread_arg *)arg;
2212 session = ntarg->session;
2213 notif_clb = ntarg->notif_clb;
2214 free(ntarg);
2215
Michal Vasko131120a2018-05-29 15:44:02 +02002216 /* remember our allocated tid, we will be freeing it */
Michal Vaskob639e9c2020-09-09 09:51:48 +02002217 ntf_tid = (pthread_t *)ATOMIC_LOAD(session->opts.client.ntf_tid);
Michal Vasko131120a2018-05-29 15:44:02 +02002218
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002219 while (ATOMIC_LOAD(session->opts.client.ntf_tid)) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002220 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002221 if (msgtype == NC_MSG_NOTIF) {
2222 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002223 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2224 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2225 nc_notif_free(notif);
2226 break;
2227 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002228 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002229 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002230 /* quit this thread once the session is broken */
2231 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002232 }
2233
2234 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2235 }
2236
Michal Vasko0651c902016-05-19 15:55:42 +02002237 VRB("Session %u: notification thread exit.", session->id);
Michal Vaskob639e9c2020-09-09 09:51:48 +02002238 ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)NULL);
Michal Vasko131120a2018-05-29 15:44:02 +02002239 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002240 return NULL;
2241}
2242
2243API int
2244nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2245{
2246 struct nc_ntf_thread_arg *ntarg;
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002247 pthread_t *tid;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002248 int ret;
2249
Michal Vasko45e53ae2016-04-07 11:46:03 +02002250 if (!session) {
2251 ERRARG("session");
2252 return -1;
2253 } else if (!notif_clb) {
2254 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002255 return -1;
2256 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2257 ERR("Session %u: invalid session to receive Notifications.", session->id);
2258 return -1;
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002259 } else if (ATOMIC_LOAD(session->opts.client.ntf_tid)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002260 ERR("Session %u: separate notification thread is already running.", session->id);
2261 return -1;
2262 }
2263
2264 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002265 if (!ntarg) {
2266 ERRMEM;
2267 return -1;
2268 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002269 ntarg->session = session;
2270 ntarg->notif_clb = notif_clb;
2271
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002272 tid = malloc(sizeof *tid);
2273 if (!tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002274 ERRMEM;
2275 free(ntarg);
2276 return -1;
2277 }
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002278 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vaskob639e9c2020-09-09 09:51:48 +02002279 ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002280
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002281 ret = pthread_create(tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002282 if (ret) {
2283 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2284 free(ntarg);
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02002285 free(tid);
Michal Vaskob639e9c2020-09-09 09:51:48 +02002286 ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)NULL);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002287 return -1;
2288 }
2289
2290 return 0;
2291}
2292
Michal Vasko086311b2016-01-08 09:53:11 +01002293API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002294nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002295{
2296 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002297 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002298 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002299 struct nc_rpc_getconfig *rpc_gc;
2300 struct nc_rpc_edit *rpc_e;
2301 struct nc_rpc_copy *rpc_cp;
2302 struct nc_rpc_delete *rpc_del;
2303 struct nc_rpc_lock *rpc_lock;
2304 struct nc_rpc_get *rpc_g;
2305 struct nc_rpc_kill *rpc_k;
2306 struct nc_rpc_commit *rpc_com;
2307 struct nc_rpc_cancel *rpc_can;
2308 struct nc_rpc_validate *rpc_val;
2309 struct nc_rpc_getschema *rpc_gs;
2310 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002311 struct nc_rpc_getdata *rpc_getd;
2312 struct nc_rpc_editdata *rpc_editd;
Michal Vasko086311b2016-01-08 09:53:11 +01002313 struct lyd_node *data, *node;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002314 const struct lys_module *mod = NULL, *ietfncwd;
2315 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002316 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002317 uint64_t cur_msgid;
2318
Michal Vasko45e53ae2016-04-07 11:46:03 +02002319 if (!session) {
2320 ERRARG("session");
2321 return NC_MSG_ERROR;
2322 } else if (!rpc) {
2323 ERRARG("rpc");
2324 return NC_MSG_ERROR;
2325 } else if (!msgid) {
2326 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002327 return NC_MSG_ERROR;
2328 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002329 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002330 return NC_MSG_ERROR;
2331 }
2332
Michal Vaskoc1171a42019-11-05 12:06:46 +01002333 switch (rpc->type) {
2334 case NC_RPC_ACT_GENERIC:
2335 /* checked when parsing */
2336 break;
2337 case NC_RPC_GETCONFIG:
2338 case NC_RPC_EDIT:
2339 case NC_RPC_COPY:
2340 case NC_RPC_DELETE:
2341 case NC_RPC_LOCK:
2342 case NC_RPC_UNLOCK:
2343 case NC_RPC_GET:
2344 case NC_RPC_KILL:
2345 case NC_RPC_COMMIT:
2346 case NC_RPC_DISCARD:
2347 case NC_RPC_CANCEL:
2348 case NC_RPC_VALIDATE:
2349 mod = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
2350 if (!mod) {
Michal Vasko086cd572017-01-12 12:19:05 +01002351 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002352 return NC_MSG_ERROR;
2353 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002354 break;
2355 case NC_RPC_GETSCHEMA:
2356 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
2357 if (!mod) {
2358 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
2359 return NC_MSG_ERROR;
2360 }
2361 break;
2362 case NC_RPC_SUBSCRIBE:
2363 mod = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
2364 if (!mod) {
2365 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
2366 return NC_MSG_ERROR;
2367 }
2368 break;
2369 case NC_RPC_GETDATA:
2370 case NC_RPC_EDITDATA:
2371 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-nmda", NULL, 1);
2372 if (!mod) {
2373 ERR("Session %u: missing \"ietf-netconf-nmda\" schema in the context.", session->id);
2374 return NC_MSG_ERROR;
2375 }
2376 break;
2377 case NC_RPC_UNKNOWN:
2378 ERRINT;
2379 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002380 }
2381
2382 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002383 case NC_RPC_ACT_GENERIC:
2384 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002385
2386 if (rpc_gen->has_data) {
2387 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002388 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002389 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002390 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 +01002391 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002392 if (!data) {
2393 return NC_MSG_ERROR;
2394 }
Michal Vasko086311b2016-01-08 09:53:11 +01002395 }
2396 break;
2397
2398 case NC_RPC_GETCONFIG:
2399 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2400
Michal Vaskoc1171a42019-11-05 12:06:46 +01002401 data = lyd_new(NULL, mod, "get-config");
2402 node = lyd_new(data, mod, "source");
2403 node = lyd_new_leaf(node, mod, ncds2str[rpc_gc->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002404 if (!node) {
2405 lyd_free(data);
2406 return NC_MSG_ERROR;
2407 }
2408 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002409 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002410 node = lyd_new_anydata(data, mod, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002411 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002412 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002413 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002414 lyd_insert_attr(node, NULL, "type", "xpath");
2415 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002416 }
2417 if (!node) {
2418 lyd_free(data);
2419 return NC_MSG_ERROR;
2420 }
2421 }
2422
2423 if (rpc_gc->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002424 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002425 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002426 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2427 lyd_free(data);
2428 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002429 }
2430 switch (rpc_gc->wd_mode) {
2431 case NC_WD_UNKNOWN:
2432 /* cannot get here */
2433 break;
2434 case NC_WD_ALL:
2435 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2436 break;
2437 case NC_WD_ALL_TAG:
2438 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2439 break;
2440 case NC_WD_TRIM:
2441 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2442 break;
2443 case NC_WD_EXPLICIT:
2444 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2445 break;
2446 }
2447 if (!node) {
2448 lyd_free(data);
2449 return NC_MSG_ERROR;
2450 }
2451 }
2452 break;
2453
2454 case NC_RPC_EDIT:
2455 rpc_e = (struct nc_rpc_edit *)rpc;
2456
Michal Vaskoc1171a42019-11-05 12:06:46 +01002457 data = lyd_new(NULL, mod, "edit-config");
2458 node = lyd_new(data, mod, "target");
2459 node = lyd_new_leaf(node, mod, ncds2str[rpc_e->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002460 if (!node) {
2461 lyd_free(data);
2462 return NC_MSG_ERROR;
2463 }
2464
2465 if (rpc_e->default_op) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002466 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
Michal Vasko086311b2016-01-08 09:53:11 +01002467 if (!node) {
2468 lyd_free(data);
2469 return NC_MSG_ERROR;
2470 }
2471 }
2472
2473 if (rpc_e->test_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002474 node = lyd_new_leaf(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002475 if (!node) {
2476 lyd_free(data);
2477 return NC_MSG_ERROR;
2478 }
2479 }
2480
2481 if (rpc_e->error_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002482 node = lyd_new_leaf(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002483 if (!node) {
2484 lyd_free(data);
2485 return NC_MSG_ERROR;
2486 }
2487 }
2488
Michal Vasko7793bc62016-09-16 11:58:41 +02002489 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002490 node = lyd_new_anydata(data, mod, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002491 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002492 node = lyd_new_leaf(data, mod, "url", rpc_e->edit_cont);
Michal Vasko086311b2016-01-08 09:53:11 +01002493 }
2494 if (!node) {
2495 lyd_free(data);
2496 return NC_MSG_ERROR;
2497 }
2498 break;
2499
2500 case NC_RPC_COPY:
2501 rpc_cp = (struct nc_rpc_copy *)rpc;
2502
Michal Vaskoc1171a42019-11-05 12:06:46 +01002503 data = lyd_new(NULL, mod, "copy-config");
2504 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002505 if (rpc_cp->url_trg) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002506 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_trg);
Michal Vasko086311b2016-01-08 09:53:11 +01002507 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002508 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002509 }
2510 if (!node) {
2511 lyd_free(data);
2512 return NC_MSG_ERROR;
2513 }
2514
Michal Vaskoc1171a42019-11-05 12:06:46 +01002515 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002516 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002517 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002518 node = lyd_new_anydata(node, mod, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002519 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002520 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002521 }
2522 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002523 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002524 }
2525 if (!node) {
2526 lyd_free(data);
2527 return NC_MSG_ERROR;
2528 }
2529
2530 if (rpc_cp->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002531 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002532 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002533 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2534 lyd_free(data);
2535 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002536 }
2537 switch (rpc_cp->wd_mode) {
2538 case NC_WD_UNKNOWN:
2539 /* cannot get here */
2540 break;
2541 case NC_WD_ALL:
2542 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2543 break;
2544 case NC_WD_ALL_TAG:
2545 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2546 break;
2547 case NC_WD_TRIM:
2548 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2549 break;
2550 case NC_WD_EXPLICIT:
2551 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2552 break;
2553 }
2554 if (!node) {
2555 lyd_free(data);
2556 return NC_MSG_ERROR;
2557 }
2558 }
2559 break;
2560
2561 case NC_RPC_DELETE:
2562 rpc_del = (struct nc_rpc_delete *)rpc;
2563
Michal Vaskoc1171a42019-11-05 12:06:46 +01002564 data = lyd_new(NULL, mod, "delete-config");
2565 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002566 if (rpc_del->url) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002567 node = lyd_new_leaf(node, mod, "url", rpc_del->url);
Michal Vasko086311b2016-01-08 09:53:11 +01002568 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002569 node = lyd_new_leaf(node, mod, ncds2str[rpc_del->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002570 }
2571 if (!node) {
2572 lyd_free(data);
2573 return NC_MSG_ERROR;
2574 }
2575 break;
2576
2577 case NC_RPC_LOCK:
2578 rpc_lock = (struct nc_rpc_lock *)rpc;
2579
Michal Vaskoc1171a42019-11-05 12:06:46 +01002580 data = lyd_new(NULL, mod, "lock");
2581 node = lyd_new(data, mod, "target");
2582 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002583 if (!node) {
2584 lyd_free(data);
2585 return NC_MSG_ERROR;
2586 }
2587 break;
2588
2589 case NC_RPC_UNLOCK:
2590 rpc_lock = (struct nc_rpc_lock *)rpc;
2591
Michal Vaskoc1171a42019-11-05 12:06:46 +01002592 data = lyd_new(NULL, mod, "unlock");
2593 node = lyd_new(data, mod, "target");
2594 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002595 if (!node) {
2596 lyd_free(data);
2597 return NC_MSG_ERROR;
2598 }
2599 break;
2600
2601 case NC_RPC_GET:
2602 rpc_g = (struct nc_rpc_get *)rpc;
2603
Michal Vaskoc1171a42019-11-05 12:06:46 +01002604 data = lyd_new(NULL, mod, "get");
Michal Vasko086311b2016-01-08 09:53:11 +01002605 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002606 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002607 node = lyd_new_anydata(data, mod, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002608 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002609 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002610 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002611 lyd_insert_attr(node, NULL, "type", "xpath");
2612 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002613 }
2614 if (!node) {
2615 lyd_free(data);
2616 return NC_MSG_ERROR;
2617 }
2618 }
2619
2620 if (rpc_g->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002621 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002622 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002623 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2624 lyd_free(data);
2625 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002626 }
2627 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002628 case NC_WD_ALL:
2629 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2630 break;
2631 case NC_WD_ALL_TAG:
2632 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2633 break;
2634 case NC_WD_TRIM:
2635 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2636 break;
2637 case NC_WD_EXPLICIT:
2638 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2639 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002640 default:
2641 /* cannot get here */
2642 node = NULL;
2643 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002644 }
2645 if (!node) {
2646 lyd_free(data);
2647 return NC_MSG_ERROR;
2648 }
2649 }
2650 break;
2651
2652 case NC_RPC_KILL:
2653 rpc_k = (struct nc_rpc_kill *)rpc;
2654
Michal Vaskoc1171a42019-11-05 12:06:46 +01002655 data = lyd_new(NULL, mod, "kill-session");
Michal Vasko086311b2016-01-08 09:53:11 +01002656 sprintf(str, "%u", rpc_k->sid);
Michal Vasko283c3c02021-01-13 14:12:19 +01002657 node = lyd_new_leaf(data, mod, "session-id", str);
2658 if (!node) {
2659 lyd_free(data);
2660 return NC_MSG_ERROR;
2661 }
Michal Vasko086311b2016-01-08 09:53:11 +01002662 break;
2663
2664 case NC_RPC_COMMIT:
2665 rpc_com = (struct nc_rpc_commit *)rpc;
2666
Michal Vaskoc1171a42019-11-05 12:06:46 +01002667 data = lyd_new(NULL, mod, "commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002668 if (rpc_com->confirmed) {
Michal Vasko283c3c02021-01-13 14:12:19 +01002669 node = lyd_new_leaf(data, mod, "confirmed", NULL);
2670 if (!node) {
2671 lyd_free(data);
2672 return NC_MSG_ERROR;
2673 }
Michal Vasko086311b2016-01-08 09:53:11 +01002674 }
2675
2676 if (rpc_com->confirm_timeout) {
2677 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vasko283c3c02021-01-13 14:12:19 +01002678 node = lyd_new_leaf(data, mod, "confirm-timeout", str);
2679 if (!node) {
2680 lyd_free(data);
2681 return NC_MSG_ERROR;
2682 }
Michal Vasko086311b2016-01-08 09:53:11 +01002683 }
2684
2685 if (rpc_com->persist) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002686 node = lyd_new_leaf(data, mod, "persist", rpc_com->persist);
Michal Vasko086311b2016-01-08 09:53:11 +01002687 if (!node) {
2688 lyd_free(data);
2689 return NC_MSG_ERROR;
2690 }
2691 }
2692
2693 if (rpc_com->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002694 node = lyd_new_leaf(data, mod, "persist-id", rpc_com->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002695 if (!node) {
2696 lyd_free(data);
2697 return NC_MSG_ERROR;
2698 }
2699 }
2700 break;
2701
2702 case NC_RPC_DISCARD:
Michal Vaskoc1171a42019-11-05 12:06:46 +01002703 data = lyd_new(NULL, mod, "discard-changes");
Michal Vasko086311b2016-01-08 09:53:11 +01002704 break;
2705
2706 case NC_RPC_CANCEL:
2707 rpc_can = (struct nc_rpc_cancel *)rpc;
2708
Michal Vaskoc1171a42019-11-05 12:06:46 +01002709 data = lyd_new(NULL, mod, "cancel-commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002710 if (rpc_can->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002711 node = lyd_new_leaf(data, mod, "persist-id", rpc_can->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002712 if (!node) {
2713 lyd_free(data);
2714 return NC_MSG_ERROR;
2715 }
2716 }
2717 break;
2718
2719 case NC_RPC_VALIDATE:
2720 rpc_val = (struct nc_rpc_validate *)rpc;
2721
Michal Vaskoc1171a42019-11-05 12:06:46 +01002722 data = lyd_new(NULL, mod, "validate");
2723 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002724 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002725 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002726 node = lyd_new_anydata(node, mod, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002727 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002728 node = lyd_new_leaf(node, mod, "url", rpc_val->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002729 }
2730 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002731 node = lyd_new_leaf(node, mod, ncds2str[rpc_val->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002732 }
2733 if (!node) {
2734 lyd_free(data);
2735 return NC_MSG_ERROR;
2736 }
2737 break;
2738
2739 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002740 rpc_gs = (struct nc_rpc_getschema *)rpc;
2741
Michal Vaskoc1171a42019-11-05 12:06:46 +01002742 data = lyd_new(NULL, mod, "get-schema");
2743 node = lyd_new_leaf(data, mod, "identifier", rpc_gs->identifier);
Michal Vasko086311b2016-01-08 09:53:11 +01002744 if (!node) {
2745 lyd_free(data);
2746 return NC_MSG_ERROR;
2747 }
2748 if (rpc_gs->version) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002749 node = lyd_new_leaf(data, mod, "version", rpc_gs->version);
Michal Vasko086311b2016-01-08 09:53:11 +01002750 if (!node) {
2751 lyd_free(data);
2752 return NC_MSG_ERROR;
2753 }
2754 }
2755 if (rpc_gs->format) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002756 node = lyd_new_leaf(data, mod, "format", rpc_gs->format);
Michal Vasko086311b2016-01-08 09:53:11 +01002757 if (!node) {
2758 lyd_free(data);
2759 return NC_MSG_ERROR;
2760 }
2761 }
2762 break;
2763
2764 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002765 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2766
Michal Vaskoc1171a42019-11-05 12:06:46 +01002767 data = lyd_new(NULL, mod, "create-subscription");
Michal Vasko086311b2016-01-08 09:53:11 +01002768 if (rpc_sub->stream) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002769 node = lyd_new_leaf(data, mod, "stream", rpc_sub->stream);
Michal Vasko086311b2016-01-08 09:53:11 +01002770 if (!node) {
2771 lyd_free(data);
2772 return NC_MSG_ERROR;
2773 }
2774 }
2775
2776 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002777 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002778 node = lyd_new_anydata(data, mod, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002779 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002780 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002781 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002782 lyd_insert_attr(node, NULL, "type", "xpath");
2783 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002784 }
2785 if (!node) {
2786 lyd_free(data);
2787 return NC_MSG_ERROR;
2788 }
2789 }
2790
2791 if (rpc_sub->start) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002792 node = lyd_new_leaf(data, mod, "startTime", rpc_sub->start);
Michal Vasko086311b2016-01-08 09:53:11 +01002793 if (!node) {
2794 lyd_free(data);
2795 return NC_MSG_ERROR;
2796 }
2797 }
2798
2799 if (rpc_sub->stop) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002800 node = lyd_new_leaf(data, mod, "stopTime", rpc_sub->stop);
Michal Vasko086311b2016-01-08 09:53:11 +01002801 if (!node) {
2802 lyd_free(data);
2803 return NC_MSG_ERROR;
2804 }
2805 }
2806 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002807
2808 case NC_RPC_GETDATA:
2809 rpc_getd = (struct nc_rpc_getdata *)rpc;
2810
2811 data = lyd_new(NULL, mod, "get-data");
2812 node = lyd_new_leaf(data, mod, "datastore", rpc_getd->datastore);
2813 if (!node) {
2814 lyd_free(data);
2815 return NC_MSG_ERROR;
2816 }
2817 if (rpc_getd->filter) {
2818 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
2819 node = lyd_new_anydata(data, mod, "subtree-filter", rpc_getd->filter, LYD_ANYDATA_SXML);
2820 } else {
2821 node = lyd_new_leaf(data, mod, "xpath-filter", rpc_getd->filter);
2822 }
2823 if (!node) {
2824 lyd_free(data);
2825 return NC_MSG_ERROR;
2826 }
2827 }
2828 if (rpc_getd->config_filter) {
2829 node = lyd_new_leaf(data, mod, "config-filter", rpc_getd->config_filter);
2830 if (!node) {
2831 lyd_free(data);
2832 return NC_MSG_ERROR;
2833 }
2834 }
2835 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
2836 node = lyd_new_leaf(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" : "origin-filter",
2837 rpc_getd->origin_filter[i]);
2838 if (!node) {
2839 lyd_free(data);
2840 return NC_MSG_ERROR;
2841 }
2842 }
2843 if (rpc_getd->max_depth) {
2844 sprintf(str, "%u", rpc_getd->max_depth);
2845 node = lyd_new_leaf(data, mod, "max-depth", str);
2846 if (!node) {
2847 lyd_free(data);
2848 return NC_MSG_ERROR;
2849 }
2850 }
2851 if (rpc_getd->with_origin) {
2852 node = lyd_new_leaf(data, mod, "with-origin", NULL);
2853 if (!node) {
2854 lyd_free(data);
2855 return NC_MSG_ERROR;
2856 }
2857 }
2858
2859 if (rpc_getd->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002860 switch (rpc_getd->wd_mode) {
2861 case NC_WD_UNKNOWN:
2862 /* cannot get here */
2863 break;
2864 case NC_WD_ALL:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002865 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
2866 node = lyd_new_leaf(data, mod, "with-defaults", "report-all");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002867 break;
2868 case NC_WD_ALL_TAG:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002869 node = lyd_new_leaf(data, mod, "with-defaults", "report-all-tagged");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002870 break;
2871 case NC_WD_TRIM:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002872 node = lyd_new_leaf(data, mod, "with-defaults", "trim");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002873 break;
2874 case NC_WD_EXPLICIT:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002875 node = lyd_new_leaf(data, mod, "with-defaults", "explicit");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002876 break;
2877 }
2878 if (!node) {
2879 lyd_free(data);
2880 return NC_MSG_ERROR;
2881 }
2882 }
2883 break;
2884
2885 case NC_RPC_EDITDATA:
2886 rpc_editd = (struct nc_rpc_editdata *)rpc;
2887
2888 data = lyd_new(NULL, mod, "edit-data");
2889 node = lyd_new_leaf(data, mod, "datastore", rpc_editd->datastore);
2890 if (!node) {
2891 lyd_free(data);
2892 return NC_MSG_ERROR;
2893 }
2894
2895 if (rpc_editd->default_op) {
2896 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op]);
2897 if (!node) {
2898 lyd_free(data);
2899 return NC_MSG_ERROR;
2900 }
2901 }
2902
2903 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
2904 node = lyd_new_anydata(data, mod, "config", rpc_editd->edit_cont, LYD_ANYDATA_SXML);
2905 } else {
2906 node = lyd_new_leaf(data, mod, "url", rpc_editd->edit_cont);
2907 }
2908 if (!node) {
2909 lyd_free(data);
2910 return NC_MSG_ERROR;
2911 }
2912 break;
2913
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002914 default:
2915 ERRINT;
2916 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002917 }
2918
Michal Vasko131120a2018-05-29 15:44:02 +02002919 if (!data) {
2920 /* error was already printed */
2921 return NC_MSG_ERROR;
2922 }
2923
Michal Vasko41adf392017-01-17 10:39:04 +01002924 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2925 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002926 if (dofree) {
2927 lyd_free(data);
2928 }
Michal Vasko086311b2016-01-08 09:53:11 +01002929 return NC_MSG_ERROR;
2930 }
2931
Michal Vasko131120a2018-05-29 15:44:02 +02002932 /* send RPC, store its message ID */
2933 r = nc_send_msg_io(session, timeout, data);
2934 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002935
Radek Krejcib4b19062018-02-07 16:33:06 +01002936 if (dofree) {
2937 lyd_free(data);
2938 }
Michal Vasko086311b2016-01-08 09:53:11 +01002939
Michal Vasko131120a2018-05-29 15:44:02 +02002940 if (r == NC_MSG_RPC) {
2941 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002942 }
Michal Vasko131120a2018-05-29 15:44:02 +02002943 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002944}
Michal Vaskode2946c2017-01-12 12:19:26 +01002945
2946API void
2947nc_client_session_set_not_strict(struct nc_session *session)
2948{
2949 if (session->side != NC_CLIENT) {
2950 ERRARG("session");
2951 return;
2952 }
2953
2954 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2955}