blob: 6a07473af33951f8a4995795173993970b2ea118 [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);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001383 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1384 /* try the next resource */
1385 continue;
1386 }
1387 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 +01001388
1389 opt = 1;
1390 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
1391 ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001392 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001393 }
1394
Michal Vasko66032bc2019-01-22 15:03:12 +01001395 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1396 buf = malloc(INET6_ADDRSTRLEN);
1397 if (!buf) {
1398 ERRMEM;
Michal Vaskocb846632019-12-13 15:12:45 +01001399 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001400 }
1401 if (res->ai_family == AF_INET) {
1402 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1403 } else {
1404 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1405 }
1406 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
1407 ERR("Converting host to IP address failed (%s).", strerror(errno));
1408 free(buf);
Michal Vaskocb846632019-12-13 15:12:45 +01001409 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001410 }
1411
1412 *ip_host = buf;
1413 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001414 break;
1415 }
1416 freeaddrinfo(res_list);
1417
1418 } else {
1419 /* try to get a connection with the pending socket */
1420 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001421 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001422 }
1423
1424 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001425
1426error:
1427 if (sock != -1) {
1428 close(sock);
1429 }
1430 if (sock_pending) {
1431 *sock_pending = -1;
1432 }
1433 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001434}
1435
Michal Vasko086311b2016-01-08 09:53:11 +01001436static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001437get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001438{
Michal Vasko086311b2016-01-08 09:53:11 +01001439 char *ptr;
1440 const char *str_msgid;
1441 uint64_t cur_msgid;
1442 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001443 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001444 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1445
Michal Vasko086311b2016-01-08 09:53:11 +01001446 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001447 if (!msgid && session->opts.client.notifs) {
1448 cont = session->opts.client.notifs;
1449 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001450
Michal Vasko71ba2da2016-05-04 10:53:16 +02001451 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001452 free(cont);
1453
Michal Vasko71ba2da2016-05-04 10:53:16 +02001454 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001455 }
1456
1457 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001458 if (msgid && session->opts.client.replies) {
1459 cont = session->opts.client.replies;
1460 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001461
Michal Vasko71ba2da2016-05-04 10:53:16 +02001462 xml = cont->msg;
1463 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001464
Michal Vasko71ba2da2016-05-04 10:53:16 +02001465 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001466 }
1467
Michal Vasko71ba2da2016-05-04 10:53:16 +02001468 if (!msgtype) {
1469 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001470 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001471 }
Michal Vasko086311b2016-01-08 09:53:11 +01001472
1473 /* we read rpc-reply, want a notif */
1474 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001475 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001476 while (*cont_ptr) {
1477 cont_ptr = &((*cont_ptr)->next);
1478 }
1479 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001480 if (!*cont_ptr) {
1481 ERRMEM;
1482 lyxml_free(session->ctx, xml);
1483 return NC_MSG_ERROR;
1484 }
Michal Vasko086311b2016-01-08 09:53:11 +01001485 (*cont_ptr)->msg = xml;
1486 (*cont_ptr)->next = NULL;
1487 }
1488
1489 /* we read notif, want a rpc-reply */
1490 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001491 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001492 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001493 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001494 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001495 }
Michal Vasko086311b2016-01-08 09:53:11 +01001496
Michal Vasko2e6defd2016-10-07 15:48:15 +02001497 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001498 while (*cont_ptr) {
1499 cont_ptr = &((*cont_ptr)->next);
1500 }
1501 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001502 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001503 ERRMEM;
1504 lyxml_free(session->ctx, xml);
1505 return NC_MSG_ERROR;
1506 }
Michal Vasko086311b2016-01-08 09:53:11 +01001507 (*cont_ptr)->msg = xml;
1508 (*cont_ptr)->next = NULL;
1509 }
1510
Michal Vasko086311b2016-01-08 09:53:11 +01001511 switch (msgtype) {
1512 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001513 if (!msgid) {
1514 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001515 }
Michal Vasko086311b2016-01-08 09:53:11 +01001516 break;
1517
1518 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001519 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001520 /* check message-id */
1521 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1522 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001523 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001524 } else {
1525 cur_msgid = strtoul(str_msgid, &ptr, 10);
1526 if (cur_msgid != msgid) {
1527 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1528 session->id, str_msgid);
1529 msgtype = NC_MSG_REPLY_ERR_MSGID;
1530 }
1531 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001532 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001533 }
Michal Vasko086311b2016-01-08 09:53:11 +01001534 break;
1535
1536 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001537 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001538 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001539 msgtype = NC_MSG_ERROR;
1540 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001541
1542 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001543 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001544 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001545 msgtype = NC_MSG_ERROR;
1546 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001547
1548 default:
1549 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1550 * NC_MSG_NONE is not returned by nc_read_msg()
1551 */
1552 break;
1553 }
1554
1555 return msgtype;
1556}
1557
1558/* cannot strictly fail, but does not need to fill any error parameter at all */
1559static void
1560parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1561{
1562 struct lyxml_elem *iter, *next, *info;
1563
1564 LY_TREE_FOR(xml->child, iter) {
1565 if (!iter->ns) {
1566 if (iter->content) {
1567 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1568 } else {
1569 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1570 }
1571 continue;
1572 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1573 if (iter->content) {
1574 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1575 iter->name, iter->content, iter->ns->value);
1576 } else {
1577 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1578 }
1579 continue;
1580 }
1581
1582 if (!strcmp(iter->name, "error-type")) {
1583 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1584 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1585 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1586 } else if (err->type) {
1587 WRN("<rpc-error> <error-type> duplicated.");
1588 } else {
1589 err->type = lydict_insert(ctx, iter->content, 0);
1590 }
1591 } else if (!strcmp(iter->name, "error-tag")) {
1592 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1593 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1594 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1595 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1596 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1597 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1598 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1599 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1600 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1601 && strcmp(iter->content, "malformed-message"))) {
1602 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1603 } else if (err->tag) {
1604 WRN("<rpc-error> <error-tag> duplicated.");
1605 } else {
1606 err->tag = lydict_insert(ctx, iter->content, 0);
1607 }
1608 } else if (!strcmp(iter->name, "error-severity")) {
1609 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1610 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1611 } else if (err->severity) {
1612 WRN("<rpc-error> <error-severity> duplicated.");
1613 } else {
1614 err->severity = lydict_insert(ctx, iter->content, 0);
1615 }
1616 } else if (!strcmp(iter->name, "error-app-tag")) {
1617 if (err->apptag) {
1618 WRN("<rpc-error> <error-app-tag> duplicated.");
1619 } else {
1620 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1621 }
1622 } else if (!strcmp(iter->name, "error-path")) {
1623 if (err->path) {
1624 WRN("<rpc-error> <error-path> duplicated.");
1625 } else {
1626 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1627 }
1628 } else if (!strcmp(iter->name, "error-message")) {
1629 if (err->message) {
1630 WRN("<rpc-error> <error-message> duplicated.");
1631 } else {
1632 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001633 if (err->message_lang) {
1634 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1635 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001636 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1637 }
1638 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1639 }
1640 } else if (!strcmp(iter->name, "error-info")) {
1641 LY_TREE_FOR_SAFE(iter->child, next, info) {
1642 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1643 if (!strcmp(info->name, "session-id")) {
1644 if (err->sid) {
1645 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1646 } else {
1647 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1648 }
Michal Vasko630485f2018-01-03 14:28:32 +01001649 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001650 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001651 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1652 if (!err->attr) {
1653 ERRMEM;
1654 return;
1655 }
Michal Vasko086311b2016-01-08 09:53:11 +01001656 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1657 } else if (!strcmp(info->name, "bad-element")) {
1658 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001659 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1660 if (!err->elem) {
1661 ERRMEM;
1662 return;
1663 }
Michal Vasko086311b2016-01-08 09:53:11 +01001664 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1665 } else if (!strcmp(info->name, "bad-namespace")) {
1666 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001667 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1668 if (!err->ns) {
1669 ERRMEM;
1670 return;
1671 }
Michal Vasko086311b2016-01-08 09:53:11 +01001672 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1673 } else {
1674 if (info->content) {
1675 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1676 info->name, info->content);
1677 } else {
1678 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1679 }
1680 }
1681 } else {
1682 lyxml_unlink(ctx, info);
1683 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001684 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1685 if (!err->other) {
1686 ERRMEM;
1687 return;
1688 }
Michal Vasko086311b2016-01-08 09:53:11 +01001689 err->other[err->other_count - 1] = info;
1690 }
1691 }
1692 } else {
1693 if (iter->content) {
1694 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1695 } else {
1696 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1697 }
1698 }
1699 }
1700}
1701
1702static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001703parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001704{
1705 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001706 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001707 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001708 struct nc_reply_data *data_rpl;
1709 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001710 struct nc_rpc_act_generic *rpc_gen;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001711 int i, data_parsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001712
Michal Vasko086311b2016-01-08 09:53:11 +01001713 /* rpc-error */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001714 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 +01001715 /* count and check elements */
1716 i = 0;
1717 LY_TREE_FOR(xml->child, iter) {
1718 if (strcmp(iter->name, "rpc-error")) {
1719 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1720 return NULL;
1721 } else if (!iter->ns) {
1722 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1723 return NULL;
1724 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1725 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1726 return NULL;
1727 }
1728 ++i;
1729 }
1730
1731 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001732 if (!error_rpl) {
1733 ERRMEM;
1734 return NULL;
1735 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001736 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001737 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001738 if (!error_rpl->err) {
1739 ERRMEM;
1740 free(error_rpl);
1741 return NULL;
1742 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001743 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001744 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001745 reply = (struct nc_reply *)error_rpl;
1746
1747 i = 0;
1748 LY_TREE_FOR(xml->child, iter) {
1749 parse_rpc_error(ctx, iter, error_rpl->err + i);
1750 ++i;
1751 }
1752
1753 /* ok */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001754 } 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 +01001755 if (xml->child->next) {
1756 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1757 return NULL;
1758 }
1759 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001760 if (!reply) {
1761 ERRMEM;
1762 return NULL;
1763 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001764 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001765
1766 /* some RPC output */
1767 } else {
1768 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001769 case NC_RPC_ACT_GENERIC:
1770 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001771
1772 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001773 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1774 if (!rpc_act) {
1775 ERR("Failed to duplicate a generic RPC/action.");
1776 return NULL;
1777 }
Michal Vasko086311b2016-01-08 09:53:11 +01001778 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001779 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 +02001780 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001781 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001782 return NULL;
1783 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001784 }
Michal Vasko086311b2016-01-08 09:53:11 +01001785 break;
1786
1787 case NC_RPC_GETCONFIG:
1788 case NC_RPC_GET:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001789 case NC_RPC_GETDATA:
Michal Vasko6b281ff2019-02-14 10:15:19 +01001790 /* we should definitely have received at least an empty "data" element even on empty reply, but fine */
1791 if (!xml->child || !xml->child->child) {
Michal Vasko13ed2942016-02-29 16:21:00 +01001792 /* we did not receive any data */
1793 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001794 if (!data_rpl) {
1795 ERRMEM;
1796 return NULL;
1797 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001798 data_rpl->type = NC_RPL_DATA;
1799 data_rpl->data = NULL;
1800 return (struct nc_reply *)data_rpl;
1801 }
1802
Michal Vasko086311b2016-01-08 09:53:11 +01001803 /* special treatment */
Michal Vaskof45e0c12018-11-28 08:38:23 +01001804 ly_errno = 0;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001805 data = lyd_parse_xml(ctx, &xml->child->child,
1806 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001807 | parseroptions);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001808 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001809 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1810 return NULL;
1811 }
Michal Vaskof45e0c12018-11-28 08:38:23 +01001812 data_parsed = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001813 break;
1814
1815 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001816 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001817 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001818 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001819 return NULL;
1820 }
1821 break;
1822
1823 case NC_RPC_EDIT:
1824 case NC_RPC_COPY:
1825 case NC_RPC_DELETE:
1826 case NC_RPC_LOCK:
1827 case NC_RPC_UNLOCK:
1828 case NC_RPC_KILL:
1829 case NC_RPC_COMMIT:
1830 case NC_RPC_DISCARD:
1831 case NC_RPC_CANCEL:
1832 case NC_RPC_VALIDATE:
1833 case NC_RPC_SUBSCRIBE:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001834 case NC_RPC_EDITDATA:
Michal Vasko086311b2016-01-08 09:53:11 +01001835 /* there is no output defined */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001836 ERR("Unexpected data reply (root elem \"%s\").", xml->child ? xml->child->name : NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001837 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001838 default:
1839 ERRINT;
1840 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001841 }
1842
1843 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001844 if (!data_rpl) {
1845 ERRMEM;
1846 return NULL;
1847 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001848 data_rpl->type = NC_RPL_DATA;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001849
1850 ly_errno = 0;
1851 if (!data_parsed) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001852 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001853 rpc_act, NULL);
Michal Vasko6b281ff2019-02-14 10:15:19 +01001854 if (!ly_errno && !data_rpl->data->child) {
1855 ERR("An empty data <rpc-reply>.");
1856 lyd_free_withsiblings(rpc_act);
1857 lyd_free(data_rpl->data);
1858 free(data_rpl);
1859 return NULL;
1860 }
Michal Vasko086311b2016-01-08 09:53:11 +01001861 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01001862 /* <get>, <get-config>, <get-data> */
Michal Vasko086311b2016-01-08 09:53:11 +01001863 data_rpl->data = data;
1864 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001865 lyd_free_withsiblings(rpc_act);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001866 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001867 ERR("Failed to parse <rpc-reply>.");
1868 free(data_rpl);
1869 return NULL;
1870 }
1871 reply = (struct nc_reply *)data_rpl;
1872 }
1873
1874 return reply;
1875}
1876
Radek Krejci53691be2016-02-22 13:58:37 +01001877#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001878
Michal Vasko3031aae2016-01-27 16:07:18 +01001879int
1880nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1881{
1882 int sock;
1883
Michal Vasko45e53ae2016-04-07 11:46:03 +02001884 if (!address) {
1885 ERRARG("address");
1886 return -1;
1887 } else if (!port) {
1888 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001889 return -1;
1890 }
1891
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001892 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001893 if (sock == -1) {
1894 return -1;
1895 }
1896
1897 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001898 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1899 if (!client_opts.ch_binds) {
1900 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001901 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001902 return -1;
1903 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001904
Michal Vasko2e6defd2016-10-07 15:48:15 +02001905 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1906 if (!client_opts.ch_bind_ti) {
1907 ERRMEM;
1908 close(sock);
1909 return -1;
1910 }
1911 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1912
Michal Vasko3031aae2016-01-27 16:07:18 +01001913 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001914 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1915 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001916 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001917 return -1;
1918 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001919 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1920 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001921 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001922
1923 return 0;
1924}
1925
1926int
1927nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1928{
1929 uint32_t i;
1930 int ret = -1;
1931
1932 if (!address && !port && !ti) {
1933 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1934 close(client_opts.ch_binds[i].sock);
1935 free((char *)client_opts.ch_binds[i].address);
1936
1937 ret = 0;
1938 }
1939 free(client_opts.ch_binds);
1940 client_opts.ch_binds = NULL;
1941 client_opts.ch_bind_count = 0;
1942 } else {
1943 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1944 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1945 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001946 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001947 close(client_opts.ch_binds[i].sock);
1948 free((char *)client_opts.ch_binds[i].address);
1949
1950 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001951 if (!client_opts.ch_bind_count) {
1952 free(client_opts.ch_binds);
1953 client_opts.ch_binds = NULL;
1954 } else if (i < client_opts.ch_bind_count) {
1955 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1956 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1957 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001958
1959 ret = 0;
1960 }
1961 }
1962 }
1963
1964 return ret;
1965}
1966
1967API int
1968nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1969{
1970 int sock;
1971 char *host = NULL;
1972 uint16_t port, idx;
1973
Michal Vasko45e53ae2016-04-07 11:46:03 +02001974 if (!client_opts.ch_binds) {
1975 ERRINIT;
1976 return -1;
1977 } else if (!session) {
1978 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001979 return -1;
1980 }
1981
1982 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1983
Michal Vasko50456e82016-02-02 12:16:08 +01001984 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001985 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001986 return sock;
1987 }
1988
Radek Krejci53691be2016-02-22 13:58:37 +01001989#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001990 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001991 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001992 } else
1993#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001994#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001995 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001996 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001997 } else
1998#endif
1999 {
Michal Vaskofee717c2016-02-01 13:25:43 +01002000 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01002001 *session = NULL;
2002 }
2003
2004 free(host);
2005
2006 if (!(*session)) {
2007 return -1;
2008 }
2009
2010 return 1;
2011}
2012
Radek Krejci53691be2016-02-22 13:58:37 +01002013#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01002014
Michal Vasko1b2ddc92017-05-24 08:59:49 +02002015API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02002016nc_session_get_cpblts(const struct nc_session *session)
2017{
2018 if (!session) {
2019 ERRARG("session");
2020 return NULL;
2021 }
2022
Michal Vasko1b2ddc92017-05-24 08:59:49 +02002023 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02002024}
2025
2026API const char *
2027nc_session_cpblt(const struct nc_session *session, const char *capab)
2028{
2029 int i, len;
2030
2031 if (!session) {
2032 ERRARG("session");
2033 return NULL;
2034 } else if (!capab) {
2035 ERRARG("capab");
2036 return NULL;
2037 }
2038
2039 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002040 for (i = 0; session->opts.client.cpblts[i]; ++i) {
2041 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
2042 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02002043 }
2044 }
2045
2046 return NULL;
2047}
2048
Michal Vasko9cd26a82016-05-31 08:58:48 +02002049API int
2050nc_session_ntf_thread_running(const struct nc_session *session)
2051{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002052 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02002053 ERRARG("session");
2054 return 0;
2055 }
2056
Michal Vasko2e6defd2016-10-07 15:48:15 +02002057 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02002058}
2059
Michal Vaskob7558c52016-02-26 15:04:19 +01002060API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002061nc_client_init(void)
2062{
2063 nc_init();
2064}
2065
2066API void
Michal Vaskob7558c52016-02-26 15:04:19 +01002067nc_client_destroy(void)
2068{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02002069 nc_client_set_schema_searchpath(NULL);
2070#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
2071 nc_client_ch_del_bind(NULL, 0, 0);
2072#endif
2073#ifdef NC_ENABLED_SSH
2074 nc_client_ssh_destroy_opts();
2075#endif
2076#ifdef NC_ENABLED_TLS
2077 nc_client_tls_destroy_opts();
2078#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002079 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01002080}
2081
Michal Vasko086311b2016-01-08 09:53:11 +01002082API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002083nc_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 +01002084{
Michal Vasko5a2c7162020-01-30 11:24:17 +01002085 struct lyxml_elem *xml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01002086 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2087
Michal Vasko45e53ae2016-04-07 11:46:03 +02002088 if (!session) {
2089 ERRARG("session");
2090 return NC_MSG_ERROR;
2091 } else if (!rpc) {
2092 ERRARG("rpc");
2093 return NC_MSG_ERROR;
Michal Vasko5a2c7162020-01-30 11:24:17 +01002094 } else if (!msgid) {
2095 ERRARG("msgid");
2096 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02002097 } else if (!reply) {
2098 ERRARG("reply");
2099 return NC_MSG_ERROR;
2100 } else if (parseroptions & LYD_OPT_TYPEMASK) {
2101 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01002102 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002103 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01002104 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002105 return NC_MSG_ERROR;
2106 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002107 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01002108 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
2109 parseroptions &= LYD_OPT_STRICT;
2110 }
Michal Vasko41adf392017-01-17 10:39:04 +01002111 /* no mechanism to check external dependencies is provided */
2112 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01002113 *reply = NULL;
2114
2115 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002116
Michal Vasko5a2c7162020-01-30 11:24:17 +01002117 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01002118 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko5a2c7162020-01-30 11:24:17 +01002119 lyxml_free_withsiblings(session->ctx, xml);
2120 xml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01002121 if (!(*reply)) {
2122 return NC_MSG_ERROR;
2123 }
2124 }
Michal Vasko5a2c7162020-01-30 11:24:17 +01002125 assert(!xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002126
2127 return msgtype;
2128}
2129
2130API NC_MSG_TYPE
2131nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
2132{
2133 struct lyxml_elem *xml, *ev_time;
2134 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2135
Michal Vasko45e53ae2016-04-07 11:46:03 +02002136 if (!session) {
2137 ERRARG("session");
2138 return NC_MSG_ERROR;
2139 } else if (!notif) {
2140 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01002141 return NC_MSG_ERROR;
2142 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002143 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002144 return NC_MSG_ERROR;
2145 }
2146
2147 msgtype = get_msg(session, timeout, 0, &xml);
2148
2149 if (msgtype == NC_MSG_NOTIF) {
2150 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002151 if (!*notif) {
2152 ERRMEM;
2153 lyxml_free(session->ctx, xml);
2154 return NC_MSG_ERROR;
2155 }
Michal Vasko086311b2016-01-08 09:53:11 +01002156
2157 /* eventTime */
2158 LY_TREE_FOR(xml->child, ev_time) {
2159 if (!strcmp(ev_time->name, "eventTime")) {
2160 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
2161 /* lyd_parse does not know this element */
2162 lyxml_free(session->ctx, ev_time);
2163 break;
2164 }
2165 }
2166 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01002167 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002168 goto fail;
2169 }
2170
2171 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01002172 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01002173 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002174 lyxml_free(session->ctx, xml);
2175 xml = NULL;
2176 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01002177 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002178 goto fail;
2179 }
2180 }
2181
2182 return msgtype;
2183
2184fail:
2185 lydict_remove(session->ctx, (*notif)->datetime);
2186 lyd_free((*notif)->tree);
2187 free(*notif);
2188 *notif = NULL;
2189 lyxml_free(session->ctx, xml);
2190
2191 return NC_MSG_ERROR;
2192}
2193
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002194static void *
2195nc_recv_notif_thread(void *arg)
2196{
2197 struct nc_ntf_thread_arg *ntarg;
2198 struct nc_session *session;
2199 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
2200 struct nc_notif *notif;
2201 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002202 pthread_t *ntf_tid;
2203
2204 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002205
2206 ntarg = (struct nc_ntf_thread_arg *)arg;
2207 session = ntarg->session;
2208 notif_clb = ntarg->notif_clb;
2209 free(ntarg);
2210
Michal Vasko131120a2018-05-29 15:44:02 +02002211 /* remember our allocated tid, we will be freeing it */
2212 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
2213
Michal Vasko2e6defd2016-10-07 15:48:15 +02002214 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002215 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002216 if (msgtype == NC_MSG_NOTIF) {
2217 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002218 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2219 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2220 nc_notif_free(notif);
2221 break;
2222 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002223 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002224 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002225 /* quit this thread once the session is broken */
2226 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002227 }
2228
2229 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2230 }
2231
Michal Vasko0651c902016-05-19 15:55:42 +02002232 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002233 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02002234 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002235 return NULL;
2236}
2237
2238API int
2239nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2240{
2241 struct nc_ntf_thread_arg *ntarg;
2242 int ret;
2243
Michal Vasko45e53ae2016-04-07 11:46:03 +02002244 if (!session) {
2245 ERRARG("session");
2246 return -1;
2247 } else if (!notif_clb) {
2248 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002249 return -1;
2250 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2251 ERR("Session %u: invalid session to receive Notifications.", session->id);
2252 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002253 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002254 ERR("Session %u: separate notification thread is already running.", session->id);
2255 return -1;
2256 }
2257
2258 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002259 if (!ntarg) {
2260 ERRMEM;
2261 return -1;
2262 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002263 ntarg->session = session;
2264 ntarg->notif_clb = notif_clb;
2265
2266 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002267 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2268 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002269 ERRMEM;
2270 free(ntarg);
2271 return -1;
2272 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002273
Michal Vasko2e6defd2016-10-07 15:48:15 +02002274 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002275 if (ret) {
2276 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2277 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002278 free((pthread_t *)session->opts.client.ntf_tid);
2279 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002280 return -1;
2281 }
2282
2283 return 0;
2284}
2285
Michal Vasko086311b2016-01-08 09:53:11 +01002286API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002287nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002288{
2289 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002290 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002291 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002292 struct nc_rpc_getconfig *rpc_gc;
2293 struct nc_rpc_edit *rpc_e;
2294 struct nc_rpc_copy *rpc_cp;
2295 struct nc_rpc_delete *rpc_del;
2296 struct nc_rpc_lock *rpc_lock;
2297 struct nc_rpc_get *rpc_g;
2298 struct nc_rpc_kill *rpc_k;
2299 struct nc_rpc_commit *rpc_com;
2300 struct nc_rpc_cancel *rpc_can;
2301 struct nc_rpc_validate *rpc_val;
2302 struct nc_rpc_getschema *rpc_gs;
2303 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002304 struct nc_rpc_getdata *rpc_getd;
2305 struct nc_rpc_editdata *rpc_editd;
Michal Vasko086311b2016-01-08 09:53:11 +01002306 struct lyd_node *data, *node;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002307 const struct lys_module *mod = NULL, *ietfncwd;
2308 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002309 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002310 uint64_t cur_msgid;
2311
Michal Vasko45e53ae2016-04-07 11:46:03 +02002312 if (!session) {
2313 ERRARG("session");
2314 return NC_MSG_ERROR;
2315 } else if (!rpc) {
2316 ERRARG("rpc");
2317 return NC_MSG_ERROR;
2318 } else if (!msgid) {
2319 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002320 return NC_MSG_ERROR;
2321 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002322 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002323 return NC_MSG_ERROR;
2324 }
2325
Michal Vaskoc1171a42019-11-05 12:06:46 +01002326 switch (rpc->type) {
2327 case NC_RPC_ACT_GENERIC:
2328 /* checked when parsing */
2329 break;
2330 case NC_RPC_GETCONFIG:
2331 case NC_RPC_EDIT:
2332 case NC_RPC_COPY:
2333 case NC_RPC_DELETE:
2334 case NC_RPC_LOCK:
2335 case NC_RPC_UNLOCK:
2336 case NC_RPC_GET:
2337 case NC_RPC_KILL:
2338 case NC_RPC_COMMIT:
2339 case NC_RPC_DISCARD:
2340 case NC_RPC_CANCEL:
2341 case NC_RPC_VALIDATE:
2342 mod = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
2343 if (!mod) {
Michal Vasko086cd572017-01-12 12:19:05 +01002344 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002345 return NC_MSG_ERROR;
2346 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002347 break;
2348 case NC_RPC_GETSCHEMA:
2349 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
2350 if (!mod) {
2351 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
2352 return NC_MSG_ERROR;
2353 }
2354 break;
2355 case NC_RPC_SUBSCRIBE:
2356 mod = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
2357 if (!mod) {
2358 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
2359 return NC_MSG_ERROR;
2360 }
2361 break;
2362 case NC_RPC_GETDATA:
2363 case NC_RPC_EDITDATA:
2364 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-nmda", NULL, 1);
2365 if (!mod) {
2366 ERR("Session %u: missing \"ietf-netconf-nmda\" schema in the context.", session->id);
2367 return NC_MSG_ERROR;
2368 }
2369 break;
2370 case NC_RPC_UNKNOWN:
2371 ERRINT;
2372 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002373 }
2374
2375 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002376 case NC_RPC_ACT_GENERIC:
2377 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002378
2379 if (rpc_gen->has_data) {
2380 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002381 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002382 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002383 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 +01002384 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002385 if (!data) {
2386 return NC_MSG_ERROR;
2387 }
Michal Vasko086311b2016-01-08 09:53:11 +01002388 }
2389 break;
2390
2391 case NC_RPC_GETCONFIG:
2392 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2393
Michal Vaskoc1171a42019-11-05 12:06:46 +01002394 data = lyd_new(NULL, mod, "get-config");
2395 node = lyd_new(data, mod, "source");
2396 node = lyd_new_leaf(node, mod, ncds2str[rpc_gc->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002397 if (!node) {
2398 lyd_free(data);
2399 return NC_MSG_ERROR;
2400 }
2401 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002402 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002403 node = lyd_new_anydata(data, mod, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002404 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002405 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002406 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002407 lyd_insert_attr(node, NULL, "type", "xpath");
2408 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002409 }
2410 if (!node) {
2411 lyd_free(data);
2412 return NC_MSG_ERROR;
2413 }
2414 }
2415
2416 if (rpc_gc->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002417 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002418 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002419 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2420 lyd_free(data);
2421 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002422 }
2423 switch (rpc_gc->wd_mode) {
2424 case NC_WD_UNKNOWN:
2425 /* cannot get here */
2426 break;
2427 case NC_WD_ALL:
2428 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2429 break;
2430 case NC_WD_ALL_TAG:
2431 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2432 break;
2433 case NC_WD_TRIM:
2434 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2435 break;
2436 case NC_WD_EXPLICIT:
2437 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2438 break;
2439 }
2440 if (!node) {
2441 lyd_free(data);
2442 return NC_MSG_ERROR;
2443 }
2444 }
2445 break;
2446
2447 case NC_RPC_EDIT:
2448 rpc_e = (struct nc_rpc_edit *)rpc;
2449
Michal Vaskoc1171a42019-11-05 12:06:46 +01002450 data = lyd_new(NULL, mod, "edit-config");
2451 node = lyd_new(data, mod, "target");
2452 node = lyd_new_leaf(node, mod, ncds2str[rpc_e->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002453 if (!node) {
2454 lyd_free(data);
2455 return NC_MSG_ERROR;
2456 }
2457
2458 if (rpc_e->default_op) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002459 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
Michal Vasko086311b2016-01-08 09:53:11 +01002460 if (!node) {
2461 lyd_free(data);
2462 return NC_MSG_ERROR;
2463 }
2464 }
2465
2466 if (rpc_e->test_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002467 node = lyd_new_leaf(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002468 if (!node) {
2469 lyd_free(data);
2470 return NC_MSG_ERROR;
2471 }
2472 }
2473
2474 if (rpc_e->error_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002475 node = lyd_new_leaf(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002476 if (!node) {
2477 lyd_free(data);
2478 return NC_MSG_ERROR;
2479 }
2480 }
2481
Michal Vasko7793bc62016-09-16 11:58:41 +02002482 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002483 node = lyd_new_anydata(data, mod, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002484 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002485 node = lyd_new_leaf(data, mod, "url", rpc_e->edit_cont);
Michal Vasko086311b2016-01-08 09:53:11 +01002486 }
2487 if (!node) {
2488 lyd_free(data);
2489 return NC_MSG_ERROR;
2490 }
2491 break;
2492
2493 case NC_RPC_COPY:
2494 rpc_cp = (struct nc_rpc_copy *)rpc;
2495
Michal Vaskoc1171a42019-11-05 12:06:46 +01002496 data = lyd_new(NULL, mod, "copy-config");
2497 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002498 if (rpc_cp->url_trg) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002499 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_trg);
Michal Vasko086311b2016-01-08 09:53:11 +01002500 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002501 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002502 }
2503 if (!node) {
2504 lyd_free(data);
2505 return NC_MSG_ERROR;
2506 }
2507
Michal Vaskoc1171a42019-11-05 12:06:46 +01002508 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002509 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002510 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002511 node = lyd_new_anydata(node, mod, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002512 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002513 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002514 }
2515 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002516 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002517 }
2518 if (!node) {
2519 lyd_free(data);
2520 return NC_MSG_ERROR;
2521 }
2522
2523 if (rpc_cp->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002524 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002525 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002526 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2527 lyd_free(data);
2528 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002529 }
2530 switch (rpc_cp->wd_mode) {
2531 case NC_WD_UNKNOWN:
2532 /* cannot get here */
2533 break;
2534 case NC_WD_ALL:
2535 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2536 break;
2537 case NC_WD_ALL_TAG:
2538 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2539 break;
2540 case NC_WD_TRIM:
2541 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2542 break;
2543 case NC_WD_EXPLICIT:
2544 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2545 break;
2546 }
2547 if (!node) {
2548 lyd_free(data);
2549 return NC_MSG_ERROR;
2550 }
2551 }
2552 break;
2553
2554 case NC_RPC_DELETE:
2555 rpc_del = (struct nc_rpc_delete *)rpc;
2556
Michal Vaskoc1171a42019-11-05 12:06:46 +01002557 data = lyd_new(NULL, mod, "delete-config");
2558 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002559 if (rpc_del->url) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002560 node = lyd_new_leaf(node, mod, "url", rpc_del->url);
Michal Vasko086311b2016-01-08 09:53:11 +01002561 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002562 node = lyd_new_leaf(node, mod, ncds2str[rpc_del->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002563 }
2564 if (!node) {
2565 lyd_free(data);
2566 return NC_MSG_ERROR;
2567 }
2568 break;
2569
2570 case NC_RPC_LOCK:
2571 rpc_lock = (struct nc_rpc_lock *)rpc;
2572
Michal Vaskoc1171a42019-11-05 12:06:46 +01002573 data = lyd_new(NULL, mod, "lock");
2574 node = lyd_new(data, mod, "target");
2575 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002576 if (!node) {
2577 lyd_free(data);
2578 return NC_MSG_ERROR;
2579 }
2580 break;
2581
2582 case NC_RPC_UNLOCK:
2583 rpc_lock = (struct nc_rpc_lock *)rpc;
2584
Michal Vaskoc1171a42019-11-05 12:06:46 +01002585 data = lyd_new(NULL, mod, "unlock");
2586 node = lyd_new(data, mod, "target");
2587 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002588 if (!node) {
2589 lyd_free(data);
2590 return NC_MSG_ERROR;
2591 }
2592 break;
2593
2594 case NC_RPC_GET:
2595 rpc_g = (struct nc_rpc_get *)rpc;
2596
Michal Vaskoc1171a42019-11-05 12:06:46 +01002597 data = lyd_new(NULL, mod, "get");
Michal Vasko086311b2016-01-08 09:53:11 +01002598 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002599 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002600 node = lyd_new_anydata(data, mod, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002601 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002602 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002603 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002604 lyd_insert_attr(node, NULL, "type", "xpath");
2605 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002606 }
2607 if (!node) {
2608 lyd_free(data);
2609 return NC_MSG_ERROR;
2610 }
2611 }
2612
2613 if (rpc_g->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002614 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002615 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002616 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2617 lyd_free(data);
2618 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002619 }
2620 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002621 case NC_WD_ALL:
2622 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2623 break;
2624 case NC_WD_ALL_TAG:
2625 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2626 break;
2627 case NC_WD_TRIM:
2628 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2629 break;
2630 case NC_WD_EXPLICIT:
2631 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2632 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002633 default:
2634 /* cannot get here */
2635 node = NULL;
2636 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002637 }
2638 if (!node) {
2639 lyd_free(data);
2640 return NC_MSG_ERROR;
2641 }
2642 }
2643 break;
2644
2645 case NC_RPC_KILL:
2646 rpc_k = (struct nc_rpc_kill *)rpc;
2647
Michal Vaskoc1171a42019-11-05 12:06:46 +01002648 data = lyd_new(NULL, mod, "kill-session");
Michal Vasko086311b2016-01-08 09:53:11 +01002649 sprintf(str, "%u", rpc_k->sid);
Michal Vaskoc1171a42019-11-05 12:06:46 +01002650 lyd_new_leaf(data, mod, "session-id", str);
Michal Vasko086311b2016-01-08 09:53:11 +01002651 break;
2652
2653 case NC_RPC_COMMIT:
2654 rpc_com = (struct nc_rpc_commit *)rpc;
2655
Michal Vaskoc1171a42019-11-05 12:06:46 +01002656 data = lyd_new(NULL, mod, "commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002657 if (rpc_com->confirmed) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002658 lyd_new_leaf(data, mod, "confirmed", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002659 }
2660
2661 if (rpc_com->confirm_timeout) {
2662 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vaskoc1171a42019-11-05 12:06:46 +01002663 lyd_new_leaf(data, mod, "confirm-timeout", str);
Michal Vasko086311b2016-01-08 09:53:11 +01002664 }
2665
2666 if (rpc_com->persist) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002667 node = lyd_new_leaf(data, mod, "persist", rpc_com->persist);
Michal Vasko086311b2016-01-08 09:53:11 +01002668 if (!node) {
2669 lyd_free(data);
2670 return NC_MSG_ERROR;
2671 }
2672 }
2673
2674 if (rpc_com->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002675 node = lyd_new_leaf(data, mod, "persist-id", rpc_com->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002676 if (!node) {
2677 lyd_free(data);
2678 return NC_MSG_ERROR;
2679 }
2680 }
2681 break;
2682
2683 case NC_RPC_DISCARD:
Michal Vaskoc1171a42019-11-05 12:06:46 +01002684 data = lyd_new(NULL, mod, "discard-changes");
Michal Vasko086311b2016-01-08 09:53:11 +01002685 break;
2686
2687 case NC_RPC_CANCEL:
2688 rpc_can = (struct nc_rpc_cancel *)rpc;
2689
Michal Vaskoc1171a42019-11-05 12:06:46 +01002690 data = lyd_new(NULL, mod, "cancel-commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002691 if (rpc_can->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002692 node = lyd_new_leaf(data, mod, "persist-id", rpc_can->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002693 if (!node) {
2694 lyd_free(data);
2695 return NC_MSG_ERROR;
2696 }
2697 }
2698 break;
2699
2700 case NC_RPC_VALIDATE:
2701 rpc_val = (struct nc_rpc_validate *)rpc;
2702
Michal Vaskoc1171a42019-11-05 12:06:46 +01002703 data = lyd_new(NULL, mod, "validate");
2704 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002705 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002706 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002707 node = lyd_new_anydata(node, mod, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002708 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002709 node = lyd_new_leaf(node, mod, "url", rpc_val->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002710 }
2711 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002712 node = lyd_new_leaf(node, mod, ncds2str[rpc_val->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002713 }
2714 if (!node) {
2715 lyd_free(data);
2716 return NC_MSG_ERROR;
2717 }
2718 break;
2719
2720 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002721 rpc_gs = (struct nc_rpc_getschema *)rpc;
2722
Michal Vaskoc1171a42019-11-05 12:06:46 +01002723 data = lyd_new(NULL, mod, "get-schema");
2724 node = lyd_new_leaf(data, mod, "identifier", rpc_gs->identifier);
Michal Vasko086311b2016-01-08 09:53:11 +01002725 if (!node) {
2726 lyd_free(data);
2727 return NC_MSG_ERROR;
2728 }
2729 if (rpc_gs->version) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002730 node = lyd_new_leaf(data, mod, "version", rpc_gs->version);
Michal Vasko086311b2016-01-08 09:53:11 +01002731 if (!node) {
2732 lyd_free(data);
2733 return NC_MSG_ERROR;
2734 }
2735 }
2736 if (rpc_gs->format) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002737 node = lyd_new_leaf(data, mod, "format", rpc_gs->format);
Michal Vasko086311b2016-01-08 09:53:11 +01002738 if (!node) {
2739 lyd_free(data);
2740 return NC_MSG_ERROR;
2741 }
2742 }
2743 break;
2744
2745 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002746 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2747
Michal Vaskoc1171a42019-11-05 12:06:46 +01002748 data = lyd_new(NULL, mod, "create-subscription");
Michal Vasko086311b2016-01-08 09:53:11 +01002749 if (rpc_sub->stream) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002750 node = lyd_new_leaf(data, mod, "stream", rpc_sub->stream);
Michal Vasko086311b2016-01-08 09:53:11 +01002751 if (!node) {
2752 lyd_free(data);
2753 return NC_MSG_ERROR;
2754 }
2755 }
2756
2757 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002758 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002759 node = lyd_new_anydata(data, mod, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002760 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002761 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002762 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002763 lyd_insert_attr(node, NULL, "type", "xpath");
2764 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002765 }
2766 if (!node) {
2767 lyd_free(data);
2768 return NC_MSG_ERROR;
2769 }
2770 }
2771
2772 if (rpc_sub->start) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002773 node = lyd_new_leaf(data, mod, "startTime", rpc_sub->start);
Michal Vasko086311b2016-01-08 09:53:11 +01002774 if (!node) {
2775 lyd_free(data);
2776 return NC_MSG_ERROR;
2777 }
2778 }
2779
2780 if (rpc_sub->stop) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002781 node = lyd_new_leaf(data, mod, "stopTime", rpc_sub->stop);
Michal Vasko086311b2016-01-08 09:53:11 +01002782 if (!node) {
2783 lyd_free(data);
2784 return NC_MSG_ERROR;
2785 }
2786 }
2787 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002788
2789 case NC_RPC_GETDATA:
2790 rpc_getd = (struct nc_rpc_getdata *)rpc;
2791
2792 data = lyd_new(NULL, mod, "get-data");
2793 node = lyd_new_leaf(data, mod, "datastore", rpc_getd->datastore);
2794 if (!node) {
2795 lyd_free(data);
2796 return NC_MSG_ERROR;
2797 }
2798 if (rpc_getd->filter) {
2799 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
2800 node = lyd_new_anydata(data, mod, "subtree-filter", rpc_getd->filter, LYD_ANYDATA_SXML);
2801 } else {
2802 node = lyd_new_leaf(data, mod, "xpath-filter", rpc_getd->filter);
2803 }
2804 if (!node) {
2805 lyd_free(data);
2806 return NC_MSG_ERROR;
2807 }
2808 }
2809 if (rpc_getd->config_filter) {
2810 node = lyd_new_leaf(data, mod, "config-filter", rpc_getd->config_filter);
2811 if (!node) {
2812 lyd_free(data);
2813 return NC_MSG_ERROR;
2814 }
2815 }
2816 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
2817 node = lyd_new_leaf(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" : "origin-filter",
2818 rpc_getd->origin_filter[i]);
2819 if (!node) {
2820 lyd_free(data);
2821 return NC_MSG_ERROR;
2822 }
2823 }
2824 if (rpc_getd->max_depth) {
2825 sprintf(str, "%u", rpc_getd->max_depth);
2826 node = lyd_new_leaf(data, mod, "max-depth", str);
2827 if (!node) {
2828 lyd_free(data);
2829 return NC_MSG_ERROR;
2830 }
2831 }
2832 if (rpc_getd->with_origin) {
2833 node = lyd_new_leaf(data, mod, "with-origin", NULL);
2834 if (!node) {
2835 lyd_free(data);
2836 return NC_MSG_ERROR;
2837 }
2838 }
2839
2840 if (rpc_getd->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002841 switch (rpc_getd->wd_mode) {
2842 case NC_WD_UNKNOWN:
2843 /* cannot get here */
2844 break;
2845 case NC_WD_ALL:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002846 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
2847 node = lyd_new_leaf(data, mod, "with-defaults", "report-all");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002848 break;
2849 case NC_WD_ALL_TAG:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002850 node = lyd_new_leaf(data, mod, "with-defaults", "report-all-tagged");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002851 break;
2852 case NC_WD_TRIM:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002853 node = lyd_new_leaf(data, mod, "with-defaults", "trim");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002854 break;
2855 case NC_WD_EXPLICIT:
Michal Vaskoc5fa07b2020-03-04 08:37:46 +01002856 node = lyd_new_leaf(data, mod, "with-defaults", "explicit");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002857 break;
2858 }
2859 if (!node) {
2860 lyd_free(data);
2861 return NC_MSG_ERROR;
2862 }
2863 }
2864 break;
2865
2866 case NC_RPC_EDITDATA:
2867 rpc_editd = (struct nc_rpc_editdata *)rpc;
2868
2869 data = lyd_new(NULL, mod, "edit-data");
2870 node = lyd_new_leaf(data, mod, "datastore", rpc_editd->datastore);
2871 if (!node) {
2872 lyd_free(data);
2873 return NC_MSG_ERROR;
2874 }
2875
2876 if (rpc_editd->default_op) {
2877 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op]);
2878 if (!node) {
2879 lyd_free(data);
2880 return NC_MSG_ERROR;
2881 }
2882 }
2883
2884 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
2885 node = lyd_new_anydata(data, mod, "config", rpc_editd->edit_cont, LYD_ANYDATA_SXML);
2886 } else {
2887 node = lyd_new_leaf(data, mod, "url", rpc_editd->edit_cont);
2888 }
2889 if (!node) {
2890 lyd_free(data);
2891 return NC_MSG_ERROR;
2892 }
2893 break;
2894
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002895 default:
2896 ERRINT;
2897 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002898 }
2899
Michal Vasko131120a2018-05-29 15:44:02 +02002900 if (!data) {
2901 /* error was already printed */
2902 return NC_MSG_ERROR;
2903 }
2904
Michal Vasko41adf392017-01-17 10:39:04 +01002905 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2906 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002907 if (dofree) {
2908 lyd_free(data);
2909 }
Michal Vasko086311b2016-01-08 09:53:11 +01002910 return NC_MSG_ERROR;
2911 }
2912
Michal Vasko131120a2018-05-29 15:44:02 +02002913 /* send RPC, store its message ID */
2914 r = nc_send_msg_io(session, timeout, data);
2915 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002916
Radek Krejcib4b19062018-02-07 16:33:06 +01002917 if (dofree) {
2918 lyd_free(data);
2919 }
Michal Vasko086311b2016-01-08 09:53:11 +01002920
Michal Vasko131120a2018-05-29 15:44:02 +02002921 if (r == NC_MSG_RPC) {
2922 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002923 }
Michal Vasko131120a2018-05-29 15:44:02 +02002924 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002925}
Michal Vaskode2946c2017-01-12 12:19:26 +01002926
2927API void
2928nc_client_session_set_not_strict(struct nc_session *session)
2929{
2930 if (session->side != NC_CLIENT) {
2931 ERRARG("session");
2932 return;
2933 }
2934
2935 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2936}