blob: 83abb923c46d382730e23c2a79ffe9dd7be3a2c5 [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 Vasko086311b2016-01-08 09:53:11 +010016#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010020#include <netinet/in.h>
Michal Vasko83ad17e2019-01-30 10:11:37 +010021#include <netinet/tcp.h>
Michal Vasko086311b2016-01-08 09:53:11 +010022#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/socket.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020026#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020028#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010029#include <sys/types.h>
30#include <unistd.h>
31#include <arpa/inet.h>
32#include <poll.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020033#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010034
35#include <libyang/libyang.h>
36
Michal Vasko086311b2016-01-08 09:53:11 +010037#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010038#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010039#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010040
Michal Vasko80ef5d22016-01-18 09:21:02 +010041static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
42
Radek Krejci62aa0642017-05-25 16:33:49 +020043#ifdef NC_ENABLED_SSH
44int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
45char *sshauth_password(const char *username, const char *hostname, void *priv);
46char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
47char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
48#endif /* NC_ENABLED_SSH */
49
50static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
51static pthread_key_t nc_client_context_key;
52#ifdef __linux__
53static struct nc_client_context context_main = {
Michal Vaskoe49a15f2019-05-27 14:18:36 +020054 .opts.ka = {
55 .enabled = 1,
56 .idle_time = 1,
57 .max_probes = 10,
58 .probe_interval = 5
59 },
Radek Krejci62aa0642017-05-25 16:33:49 +020060#ifdef NC_ENABLED_SSH
61 .ssh_opts = {
62 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
63 .auth_hostkey_check = sshauth_hostkey_check,
64 .auth_password = sshauth_password,
65 .auth_interactive = sshauth_interactive,
66 .auth_privkey_passphrase = sshauth_privkey_passphrase
67 },
68 .ssh_ch_opts = {
69 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
70 .auth_hostkey_check = sshauth_hostkey_check,
71 .auth_password = sshauth_password,
72 .auth_interactive = sshauth_interactive,
73 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020074 },
Radek Krejci62aa0642017-05-25 16:33:49 +020075#endif /* NC_ENABLED_SSH */
76 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020077 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020078};
79#endif
80
81static void
82nc_client_context_free(void *ptr)
83{
84 struct nc_client_context *c = (struct nc_client_context *)ptr;
85
Radek Krejci5cebc6b2017-05-26 13:24:38 +020086 if (--(c->refcount)) {
87 /* still used */
88 return;
89 }
90
Radek Krejci62aa0642017-05-25 16:33:49 +020091#ifdef __linux__
92 /* in __linux__ we use static memory in the main thread,
93 * so this check is for programs terminating the main()
94 * function by pthread_exit() :)
95 */
96 if (c != &context_main)
97#endif
98 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +020099 /* for the main thread the same is done in nc_client_destroy() */
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400100 free(c->opts.schema_searchpath);
101
Radek Krejci62aa0642017-05-25 16:33:49 +0200102#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400103 int i;
104 for (i = 0; i < c->opts.ch_bind_count; ++i) {
105 close(c->opts.ch_binds[i].sock);
106 free((char *)c->opts.ch_binds[i].address);
107 }
108 free(c->opts.ch_binds);
109 c->opts.ch_binds = NULL;
110 c->opts.ch_bind_count = 0;
Radek Krejci62aa0642017-05-25 16:33:49 +0200111#endif
112#ifdef NC_ENABLED_SSH
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400113 _nc_client_ssh_destroy_opts(&c->ssh_opts);
114 _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200115#endif
116#ifdef NC_ENABLED_TLS
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400117 _nc_client_tls_destroy_opts(&c->tls_opts);
118 _nc_client_tls_destroy_opts(&c->tls_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200119#endif
120 free(c);
121 }
122}
123
124static void
125nc_client_context_createkey(void)
126{
127 int r;
128
129 /* initiate */
130 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
131 pthread_setspecific(nc_client_context_key, NULL);
132}
133
134struct nc_client_context *
135nc_client_context_location(void)
136{
137 struct nc_client_context *e;
138
139 pthread_once(&nc_client_context_once, nc_client_context_createkey);
140 e = pthread_getspecific(nc_client_context_key);
141 if (!e) {
142 /* prepare ly_err storage */
143#ifdef __linux__
144 if (getpid() == syscall(SYS_gettid)) {
145 /* main thread - use global variable instead of thread-specific variable. */
146 e = &context_main;
147 } else
148#endif /* __linux__ */
149 {
150 e = calloc(1, sizeof *e);
151 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200152 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200153#ifdef NC_ENABLED_SSH
154 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
155 e->ssh_opts.auth_pref[0].value = 3;
156 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
157 e->ssh_opts.auth_pref[1].value = 2;
158 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
159 e->ssh_opts.auth_pref[2].value = 1;
160 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
161 e->ssh_opts.auth_password = sshauth_password;
162 e->ssh_opts.auth_interactive = sshauth_interactive;
163 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
164
165 /* callhome settings are the same except the inverted auth methods preferences */
166 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
167 e->ssh_ch_opts.auth_pref[0].value = 1;
168 e->ssh_ch_opts.auth_pref[1].value = 2;
169 e->ssh_ch_opts.auth_pref[2].value = 3;
170#endif /* NC_ENABLED_SSH */
171 }
172 pthread_setspecific(nc_client_context_key, e);
173 }
174
175 return e;
176}
177
178#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100179
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200180API void *
181nc_client_get_thread_context(void)
182{
183 return nc_client_context_location();
184}
185
186API void
187nc_client_set_thread_context(void *context)
188{
189 struct nc_client_context *old, *new;
190
191 if (!context) {
192 ERRARG(context);
193 return;
194 }
195
196 new = (struct nc_client_context *)context;
197 old = nc_client_context_location();
198 if (old == new) {
199 /* nothing to change */
200 return;
201 }
202
203 /* replace old by new, increase reference counter in the newly set context */
204 nc_client_context_free(old);
205 new->refcount++;
206 pthread_setspecific(nc_client_context_key, new);
207}
208
Radek Krejcifd5b6682017-06-13 15:52:53 +0200209int
210nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
211{
212 /* assign context (dicionary needed for handshake) */
213 if (!ctx) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200214 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200215 if (!ctx) {
216 return EXIT_FAILURE;
217 }
218
219 /* user path must be first, the first path is used to store schemas retreived via get-schema */
220 if (client_opts.schema_searchpath) {
221 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
222 }
Michal Vaskoff7e3562018-02-15 13:41:22 +0100223 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200224
225 /* set callback for getting schemas, if provided */
226 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
227 } else {
228 session->flags |= NC_SESSION_SHAREDCTX;
229 }
230
231 session->ctx = ctx;
232
233 return EXIT_SUCCESS;
234}
235
Michal Vasko086311b2016-01-08 09:53:11 +0100236API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100237nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100238{
Michal Vasko3031aae2016-01-27 16:07:18 +0100239 if (client_opts.schema_searchpath) {
240 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100241 }
Michal Vasko086311b2016-01-08 09:53:11 +0100242
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100243 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100244 client_opts.schema_searchpath = strdup(path);
245 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100246 ERRMEM;
247 return 1;
248 }
249 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100250 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100251 }
252
253 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100254}
255
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100256API const char *
257nc_client_get_schema_searchpath(void)
258{
259 return client_opts.schema_searchpath;
260}
261
Radek Krejcifd5b6682017-06-13 15:52:53 +0200262API int
263nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100264{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200265 client_opts.schema_clb = clb;
266 if (clb) {
267 client_opts.schema_clb_data = user_data;
268 } else {
269 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100270 }
271
272 return 0;
273}
274
Radek Krejcifd5b6682017-06-13 15:52:53 +0200275API ly_module_imp_clb
276nc_client_get_schema_callback(void **user_data)
277{
278 if (user_data) {
279 (*user_data) = client_opts.schema_clb_data;
280 }
281 return client_opts.schema_clb;
282}
283
Radek Krejci65ef6d52018-08-16 16:35:02 +0200284
285struct schema_info {
286 char *name;
287 char *revision;
288 struct {
289 char *name;
290 char *revision;
291 } *submodules;
292 struct ly_set features;
293 int implemented;
294};
295
296struct clb_data_s {
297 void *user_data;
298 ly_module_imp_clb user_clb;
299 struct schema_info *schemas;
300 struct nc_session *session;
301 int has_get_schema;
302};
303
304static char *
305retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
306 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100307{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200308 char *localfile = NULL;
309 FILE *f;
310 long length, l;
311 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100312
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200313 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
314 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
315 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200316 return NULL;
317 }
318 if (localfile) {
319 VRB("Session %u: reading schema from localfile \"%s\".", clb_data->session->id, localfile);
320 f = fopen(localfile, "r");
321 if (!f) {
322 ERR("Session %u: unable to open \"%s\" file to get schema (%s).",
323 clb_data->session->id, localfile, strerror(errno));
324 free(localfile);
325 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100326 }
Michal Vasko086311b2016-01-08 09:53:11 +0100327
Radek Krejci65ef6d52018-08-16 16:35:02 +0200328 fseek(f, 0, SEEK_END);
329 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200330 if (length < 0) {
331 ERR("Session %u: unable to get size of schema file \"%s\".",
332 clb_data->session->id, localfile);
333 free(localfile);
334 fclose(f);
335 return NULL;
336 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200337 fseek(f, 0, SEEK_SET);
338
339 model_data = malloc(length + 1);
340 if (!model_data) {
341 ERRMEM;
342 } else if ((l = fread(model_data, 1, length, f)) != length) {
343 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
344 clb_data->session->id, localfile, l, length);
345 free(model_data);
346 model_data = NULL;
347 } else {
348 /* terminating NULL byte */
349 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100350 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200351 fclose(f);
352 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100353 }
354
Radek Krejci65ef6d52018-08-16 16:35:02 +0200355 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100356}
357
358static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200359retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
360 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100361{
Michal Vasko086311b2016-01-08 09:53:11 +0100362 struct nc_rpc *rpc;
363 struct nc_reply *reply;
364 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200365 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200366 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100367 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100368 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200369 char *localfile = NULL;
370 FILE *f;
371 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100372
Radek Krejci65ef6d52018-08-16 16:35:02 +0200373 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
374 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100375
Radek Krejci65ef6d52018-08-16 16:35:02 +0200376 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100377 usleep(1000);
378 }
379 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200380 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100381 nc_rpc_free(rpc);
382 return NULL;
383 }
384
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200385 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200386 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Robin Jarry52ddb952019-10-15 16:23:01 +0200387 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100388 nc_rpc_free(rpc);
389 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200390 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100391 return NULL;
Robin Jarry52ddb952019-10-15 16:23:01 +0200392 } else if (msg == NC_MSG_ERROR || reply == NULL) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200393 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100394 return NULL;
395 }
396
Michal Vasko998ba412016-09-16 12:00:07 +0200397 switch (reply->type) {
398 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200399 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200400 nc_reply_free(reply);
401 return NULL;
402 case NC_RPL_DATA:
403 /* fine */
404 break;
405 case NC_RPL_ERROR:
406 error_rpl = (struct nc_reply_error *)reply;
407 if (error_rpl->count) {
408 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200409 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200410 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200411 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200412 }
413 nc_reply_free(reply);
414 return NULL;
415 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200416 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100417 nc_reply_free(reply);
418 return NULL;
419 }
420
Michal Vasko086311b2016-01-08 09:53:11 +0100421 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko1c042702018-11-29 08:27:51 +0100422 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 +0200423 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200424 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200425 nc_reply_free(reply);
426 return NULL;
427 }
Radek Krejci539efb62016-08-24 15:05:16 +0200428 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
429 switch (get_schema_data->value_type) {
430 case LYD_ANYDATA_CONSTSTRING:
431 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200432 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200433 break;
434 case LYD_ANYDATA_DATATREE:
435 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
436 break;
437 case LYD_ANYDATA_XML:
438 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
439 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200440 case LYD_ANYDATA_JSON:
441 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200442 case LYD_ANYDATA_SXML:
443 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200444 case LYD_ANYDATA_LYB:
445 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200446 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200447 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200448 }
Michal Vasko086311b2016-01-08 09:53:11 +0100449 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100450
Radek Krejci488b0702018-08-29 14:47:15 +0200451 if (model_data && !model_data[0]) {
452 /* empty data */
453 free(model_data);
454 model_data = NULL;
455 }
456
Radek Krejcifd5b6682017-06-13 15:52:53 +0200457 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200458 if (model_data) {
459 *format = LYS_IN_YANG;
460 if (client_opts.schema_searchpath) {
461 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
462 rev ? "@" : "", rev ? rev : "") == -1) {
463 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100464 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200465 f = fopen(localfile, "w");
466 if (!f) {
467 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
468 localfile, strerror(errno));
469 } else {
470 fputs(model_data, f);
471 fclose(f);
472 }
473 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100474 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200475 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200476 }
477
Michal Vasko086311b2016-01-08 09:53:11 +0100478 return model_data;
479}
480
Jan Kundrát35972df2018-09-06 19:00:01 +0200481static void free_with_user_data(void *data, void *user_data)
482{
483 free(data);
484 (void)user_data;
485}
486
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200487static const char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200488retrieve_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 +0200489 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200490{
491 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
492 unsigned int u, v, match = 1;
493 const char *name = NULL, *rev = NULL;
494 char *model_data = NULL;
495
496 /* get and check the final name and revision of the schema to be retrieved */
497 if (!mod_rev || !mod_rev[0]) {
498 /* newest revision requested - get the newest revision from the list of available modules on server */
499 match = 0;
500 for (u = 0; clb_data->schemas[u].name; ++u) {
501 if (strcmp(mod_name, clb_data->schemas[u].name)) {
502 continue;
503 }
504 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
505 mod_rev = clb_data->schemas[u].revision;
506 }
507 match = u + 1;
508 }
509 if (!match) {
510 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
511 clb_data->session->id, mod_name);
512 }
513 }
514 if (submod_name) {
515 name = submod_name;
516 if (sub_rev) {
517 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200518 } else if (match) {
519 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200520 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 +0200521 clb_data->session->id, submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200522 } else {
523 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
524 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
525 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
526 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200527 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200528 if (!rev) {
529 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
530 clb_data->session->id, submod_name, mod_name);
531 return NULL;
532 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200533 }
534 }
535 } else {
536 name = mod_name;
537 rev = mod_rev;
538 }
539
540 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
541
542 if (match) {
543 /* we have enough information to avoid communication with server and try to get
544 * the schema locally */
545
546 /* 1. try to get data locally */
547 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
548
549 /* 2. try to use <get-schema> */
550 if (!model_data && clb_data->has_get_schema) {
551 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
552 }
553 } else {
554 /* we are unsure which revision of the schema we should load, so first try to get
555 * the newest revision from the server via get-schema and only if the server does not
556 * implement get-schema, try to load the newest revision locally. This is imperfect
557 * solution, but there are situation when a client does not know what revision is
558 * actually implemented by the server. */
559
560 /* 1. try to use <get-schema> */
561 if (clb_data->has_get_schema) {
562 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
563 }
564
565 /* 2. try to get data locally */
566 if (!model_data) {
567 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
568 }
569 }
570
571 /* 3. try to use user callback */
572 if (!model_data && clb_data->user_clb) {
573 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
574 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
575 }
576
Jan Kundrát35972df2018-09-06 19:00:01 +0200577 *free_module_data = free_with_user_data;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200578 return model_data;
579}
580
Michal Vaskoceae0152018-02-14 16:03:59 +0100581static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200582nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200583 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 +0100584{
585 int ret = 0;
586 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200587 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200588 LYS_INFORMAT format;
Jan Kundrát35972df2018-09-06 19:00:01 +0200589 void (*free_module_data)(void*, void*) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200590 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100591
Radek Krejci65ef6d52018-08-16 16:35:02 +0200592 *mod = NULL;
593 if (revision) {
594 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
595 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100596 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200597 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100598 /* make the present module implemented */
599 if (lys_set_implemented(*mod)) {
600 ERR("Failed to implement model \"%s\".", (*mod)->name);
601 ret = -1;
602 }
603 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200604 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100605 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200606 clb_data.has_get_schema = has_get_schema;
607 clb_data.schemas = schemas;
608 clb_data.session = session;
609 clb_data.user_clb = user_clb;
610 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100611
612 /* clear all the errors and just collect them for now */
613 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200614 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100615
Radek Krejci65ef6d52018-08-16 16:35:02 +0200616 /* get module data */
617 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100618
Radek Krejci65ef6d52018-08-16 16:35:02 +0200619 if (module_data) {
620 /* parse the schema */
621 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100622
Radek Krejci65ef6d52018-08-16 16:35:02 +0200623 *mod = lys_parse_mem(session->ctx, module_data, format);
624 if (*free_module_data) {
Jan Kundrát35972df2018-09-06 19:00:01 +0200625 (*free_module_data)((char*)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100626 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100627
Radek Krejci65ef6d52018-08-16 16:35:02 +0200628 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
629 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100630
Michal Vaskodbf7c272018-02-19 09:07:59 +0100631 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200632 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100633 if (!(*mod)) {
634 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
635 ly_err_print(eitem);
636 }
637 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200638 } else {
639 /* print only warnings */
640 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
641 if (eitem->level == LY_LLWRN) {
642 ly_err_print(eitem);
643 }
644 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100645 }
646
Michal Vaskodbf7c272018-02-19 09:07:59 +0100647 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100648 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100649 }
650
651 return ret;
652}
653
Radek Krejci65ef6d52018-08-16 16:35:02 +0200654static void
655free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200656{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200657 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200658
Radek Krejci65ef6d52018-08-16 16:35:02 +0200659 if (!list) {
660 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200661 }
662
Radek Krejci65ef6d52018-08-16 16:35:02 +0200663 for (u = 0; list[u].name; ++u) {
664 free(list[u].name);
665 free(list[u].revision);
666 for (v = 0; v < list[u].features.number; ++v) {
667 free(list[u].features.set.g[v]);
668 }
669 free(list[u].features.set.g);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200670 if (list[u].submodules) {
671 for (v = 0; list[u].submodules[v].name; ++v) {
672 free(list[u].submodules[v].name);
673 free(list[u].submodules[v].revision);
674 }
675 free(list[u].submodules);
676 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200677 }
678 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200679}
680
Radek Krejci235d8cb2018-08-17 14:04:32 +0200681
682static int
683build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200684{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200685 struct nc_rpc *rpc = NULL;
686 struct nc_reply *reply = NULL;
687 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200688 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200689 NC_MSG_TYPE msg;
690 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200691 struct ly_set *modules = NULL;
Radek Krejci2d088832018-08-21 13:40:14 +0200692 unsigned int u, v, submodules_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200693 struct lyd_node *iter, *child;
694 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200695 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200696
697 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200698 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
699 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
700 } else {
701 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0, NC_PARAMTYPE_CONST);
702 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200703 if (!rpc) {
704 goto cleanup;
705 }
706
707 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
708 usleep(1000);
709 }
710 if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200711 WRN("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200712 session->id);
713 goto cleanup;
714 }
715
716 do {
717 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Robin Jarry52ddb952019-10-15 16:23:01 +0200718 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200719 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200720 WRN("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200721 goto cleanup;
Robin Jarry52ddb952019-10-15 16:23:01 +0200722 } else if (msg == NC_MSG_ERROR || reply == NULL) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200723 WRN("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200724 goto cleanup;
725 }
726
727 switch (reply->type) {
728 case NC_RPL_OK:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200729 WRN("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200730 goto cleanup;
731 case NC_RPL_DATA:
732 /* fine */
733 break;
734 case NC_RPL_ERROR:
735 error_rpl = (struct nc_reply_error *)reply;
736 if (error_rpl->count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200737 WRN("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200738 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
739 } else {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200740 WRN("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200741 }
742 goto cleanup;
743 case NC_RPL_NOTIF:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200744 WRN("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200745 goto cleanup;
746 }
747
Radek Krejci65ef6d52018-08-16 16:35:02 +0200748 yldata = ((struct nc_reply_data *)reply)->data;
749 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200750 WRN("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200751 goto cleanup;
752 }
753
Radek Krejci65ef6d52018-08-16 16:35:02 +0200754 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200755 if (!modules) {
Radek Krejci2d088832018-08-21 13:40:14 +0200756 WRN("Session %u: no module information in reply to a yang-library <get> RPC.", session->id);
757 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200758 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200759
Radek Krejci2d088832018-08-21 13:40:14 +0200760 (*result) = calloc(modules->number + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200761 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200762 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200763 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200764 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200765 }
766
Radek Krejci2d088832018-08-21 13:40:14 +0200767 for (u = 0; u < modules->number; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200768 submodules_count = 0;
769 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200770 LY_TREE_FOR(modules->set.d[u]->child, iter) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200771 if (iter->schema->module != mod) {
772 /* ignore node from other schemas (augments) */
773 continue;
774 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200775 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
776 /* ignore empty nodes */
777 continue;
778 }
779 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200780 (*result)[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200781 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200782 (*result)[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200783 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200784 (*result)[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200785 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200786 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 +0200787 } else if (!strcmp(iter->schema->name, "submodule")) {
788 submodules_count++;
789 }
790 }
791
792 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200793 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
794 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200795 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200796 free_schema_info(*result);
797 *result = NULL;
798 ret = EXIT_FAILURE;
799 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200800 } else {
801 v = 0;
802 LY_TREE_FOR(modules->set.d[u]->child, iter) {
803 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
804 if (mod == iter->schema->module && !strcmp(iter->schema->name, "submodule")) {
805 LY_TREE_FOR(iter->child, child) {
806 if (mod != child->schema->module) {
807 continue;
808 } else if (!strcmp(child->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200809 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200810 } else if (!strcmp(child->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200811 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200812 }
813 }
814 }
815 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200816 }
817 }
818 }
819
Radek Krejcifd5b6682017-06-13 15:52:53 +0200820cleanup:
821 nc_rpc_free(rpc);
822 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200823 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200824
Radek Krejci235d8cb2018-08-17 14:04:32 +0200825 if (session->status != NC_STATUS_RUNNING) {
826 /* something bad heppened, discard the session */
827 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
828 ret = EXIT_FAILURE;
829 }
830
831 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200832}
833
Radek Krejci235d8cb2018-08-17 14:04:32 +0200834static int
835build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200836{
837 unsigned int u, v;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200838 char *module_cpblt, *ptr, *ptr2;
839
840 for (u = 0; cpblts[u]; ++u);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200841 (*result) = calloc(u + 1, sizeof **result);
842 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200843 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200844 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200845 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200846
847 for (u = v = 0; cpblts[u]; ++u) {
848 module_cpblt = strstr(cpblts[u], "module=");
849 /* this capability requires a module */
850 if (!module_cpblt) {
851 continue;
852 }
853
854 /* get module's name */
855 ptr = (char *)module_cpblt + 7;
856 ptr2 = strchr(ptr, '&');
857 if (!ptr2) {
858 ptr2 = ptr + strlen(ptr);
859 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200860 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200861
862 /* get module's revision */
863 ptr = strstr(module_cpblt, "revision=");
864 if (ptr) {
865 ptr += 9;
866 ptr2 = strchr(ptr, '&');
867 if (!ptr2) {
868 ptr2 = ptr + strlen(ptr);
869 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200870 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200871 }
872
873 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200874 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200875
876 /* get module's features */
877 ptr = strstr(module_cpblt, "features=");
878 if (ptr) {
879 ptr += 9;
880 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
881 if (*ptr == ',') {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200882 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200883 ptr2 = ptr + 1;
884 }
885 }
886 /* the last one */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200887 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200888 }
889 ++v;
890 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200891
892 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200893}
894
895static int
896nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
897{
898 int ret = EXIT_FAILURE;
899 const struct lys_module *mod;
900 unsigned int u, v;
901
902 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200903 /* skip import-only modules */
904 if (!modules[u].implemented) {
905 continue;
906 }
907
Radek Krejci65ef6d52018-08-16 16:35:02 +0200908 /* we can continue even if it fails */
909 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
910
911 if (!mod) {
912 if (session->status != NC_STATUS_RUNNING) {
913 /* something bad heppened, discard the session */
914 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
915 goto cleanup;
916 }
917
918 /* all loading ways failed, the schema will be ignored in the received data */
919 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
920 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
921 } else {
922 /* set features - first disable all to enable specified then */
923 lys_features_disable(mod, "*");
924 for (v = 0; v < modules[u].features.number; v++) {
925 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
926 }
927 }
Michal Vasko60f66602017-10-17 13:52:18 +0200928 }
929
Radek Krejci65ef6d52018-08-16 16:35:02 +0200930 /* done */
931 ret = EXIT_SUCCESS;
932
933cleanup:
934
Radek Krejcifd5b6682017-06-13 15:52:53 +0200935 return ret;
936}
937
Radek Krejci65ef6d52018-08-16 16:35:02 +0200938static int
939nc_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)
940{
941 unsigned int u, v;
942 const struct lys_module *ietfnc;
943
944 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
945 if (!ietfnc) {
946 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
947 if (!ietfnc) {
948 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
949 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
950 }
951 }
952 if (!ietfnc) {
953 ERR("Loading base NETCONF schema failed.");
954 return 1;
955 }
956
957 /* set supported capabilities from ietf-netconf */
958 for (u = 0; modules[u].name; ++u) {
959 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
960 continue;
961 }
962
963 lys_features_disable(ietfnc, "*");
964 for (v = 0; v < modules[u].features.number; v++) {
965 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
966 }
967 }
968
969 return 0;
970}
971
Michal Vasko086311b2016-01-08 09:53:11 +0100972int
973nc_ctx_check_and_fill(struct nc_session *session)
974{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200975 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100976 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100977 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200978 const struct lys_module *mod = NULL;
979 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200980 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100981
Michal Vasko2e6defd2016-10-07 15:48:15 +0200982 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100983
Radek Krejci65ef6d52018-08-16 16:35:02 +0200984 /* 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 +0200985 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200986
Radek Krejci65ef6d52018-08-16 16:35:02 +0200987 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
988 * schemas only to those present on the server side */
989 ly_ctx_set_disable_searchdirs(session->ctx);
990
991 /* our callback is set later with appropriate data */
992 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
993
994 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200995 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200996 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200997 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200998 if (yanglib_support) {
999 break;
1000 }
Radek Krejcicde410d2018-08-21 09:56:49 +02001001 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:1.0", 51)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001002 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +02001003 if (get_schema_support) {
1004 break;
1005 }
Michal Vasko086311b2016-01-08 09:53:11 +01001006 }
1007 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001008
1009 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001010 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
1011 ERR("Session %u: unable to get server's schema information from the <hello>'s capabilities.", session->id);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001012 goto cleanup;
1013 }
1014
Michal Vasko086311b2016-01-08 09:53:11 +01001015 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001016 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001017 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
1018 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001019 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001020 }
1021 }
1022
1023 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001024 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001025 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001026 }
1027
Radek Krejci65ef6d52018-08-16 16:35:02 +02001028 /* get correct version of ietf-yang-library into context */
1029 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001030 /* use get schema to get server's ietf-yang-library */
1031 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1032 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001033 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
1034 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001035 yanglib_support = 0;
1036 } else {
1037 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001038 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
1039 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001040 yanglib_support = 0;
1041 }
1042 free(revision);
1043 }
1044 }
1045
Radek Krejci65ef6d52018-08-16 16:35:02 +02001046 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001047 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001048 if (build_schema_info_yl(session, &sm)) {
1049 goto cleanup;
1050 } else if (!sm) {
1051 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
1052 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001053 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
1054 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001055 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001056 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001057 }
Michal Vaskoef578332016-01-25 13:20:09 +01001058
Radek Krejci65ef6d52018-08-16 16:35:02 +02001059 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1060 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001061 }
1062
Radek Krejcifd5b6682017-06-13 15:52:53 +02001063 /* succsess */
1064 ret = 0;
1065
Michal Vaskoeee99412016-11-21 10:19:43 +01001066 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001067 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 +01001068 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001069
1070cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001071 free_schema_info(server_modules);
1072
Radek Krejcifd5b6682017-06-13 15:52:53 +02001073 /* set user callback back */
1074 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001075 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001076
Michal Vaskoef578332016-01-25 13:20:09 +01001077 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001078}
1079
1080API struct nc_session *
1081nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1082{
Michal Vaskod083db62016-01-19 10:31:29 +01001083 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001084
Michal Vasko45e53ae2016-04-07 11:46:03 +02001085 if (fdin < 0) {
1086 ERRARG("fdin");
1087 return NULL;
1088 } else if (fdout < 0) {
1089 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001090 return NULL;
1091 }
1092
1093 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001094 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001095 if (!session) {
1096 ERRMEM;
1097 return NULL;
1098 }
1099 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001100
1101 /* transport specific data */
1102 session->ti_type = NC_TI_FD;
1103 session->ti.fd.in = fdin;
1104 session->ti.fd.out = fdout;
1105
Robin Jarry4e38e292019-10-15 17:14:14 +02001106 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1107 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001108 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001109 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001110
1111 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001112 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001113 goto fail;
1114 }
1115 session->status = NC_STATUS_RUNNING;
1116
Michal Vaskoef578332016-01-25 13:20:09 +01001117 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001118 goto fail;
1119 }
1120
1121 return session;
1122
1123fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001124 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001125 return NULL;
1126}
1127
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001128API struct nc_session *
1129nc_connect_unix(const char *address, struct ly_ctx *ctx)
1130{
1131 struct nc_session *session = NULL;
1132 struct sockaddr_un sun;
1133 const struct passwd *pw;
1134 char *username;
1135 int sock = -1;
1136
1137 if (address == NULL) {
1138 ERRARG("address");
1139 return NULL;
1140 }
1141
1142 pw = getpwuid(geteuid());
1143 if (pw == NULL) {
1144 ERR("Failed to find username for euid=%u.\n", geteuid());
1145 goto fail;
1146 }
1147
1148 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1149 if (sock < 0) {
1150 ERR("Failed to create socket (%s).", strerror(errno));
1151 goto fail;
1152 }
1153
1154 memset(&sun, 0, sizeof(sun));
1155 sun.sun_family = AF_UNIX;
1156 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1157
1158 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
1159 ERR("cannot connect to sock server %s (%s)",
1160 address, strerror(errno));
1161 goto fail;
1162 }
1163
1164 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
1165 ERR("Fcntl failed (%s).", strerror(errno));
1166 goto fail;
1167 }
1168
1169 /* prepare session structure */
1170 session = nc_new_session(NC_CLIENT, 0);
1171 if (!session) {
1172 ERRMEM;
1173 goto fail;
1174 }
1175 session->status = NC_STATUS_STARTING;
1176
1177 /* transport specific data */
1178 session->ti_type = NC_TI_UNIX;
1179 session->ti.unixsock.sock = sock;
1180 sock = -1; /* do not close sock in fail label anymore */
1181
Robin Jarry4e38e292019-10-15 17:14:14 +02001182 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1183 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001184 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001185 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001186
1187 session->path = lydict_insert(ctx, address, 0);
1188
1189 username = strdup(pw->pw_name);
1190 if (username == NULL) {
1191 ERRMEM;
1192 goto fail;
1193 }
1194 session->username = lydict_insert_zc(ctx, username);
1195
1196 /* NETCONF handshake */
1197 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1198 goto fail;
1199 }
1200 session->status = NC_STATUS_RUNNING;
1201
1202 if (nc_ctx_check_and_fill(session) == -1) {
1203 goto fail;
1204 }
1205
1206 return session;
1207
1208fail:
1209 nc_session_free(session, NULL);
1210 if (sock >= 0)
1211 close(sock);
1212 return NULL;
1213}
1214
Frank Rimpler9f838b02018-07-25 06:44:03 +00001215/*
1216 Helper for a non-blocking connect (which is required because of the locking
1217 concept for e.g. call home settings). For more details see nc_sock_connect().
1218 */
1219static int
Michal Vasko5e8d0192019-06-24 19:19:49 +02001220_non_blocking_connect(int timeout, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001221{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001222 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001223 int sock = -1;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001224 fd_set wset;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001225 struct timeval ts;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001226 socklen_t len = sizeof(int);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001227 struct in_addr *addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001228 uint16_t port;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001229 char str[INET6_ADDRSTRLEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001230
Frank Rimpler9f838b02018-07-25 06:44:03 +00001231 if (sock_pending && *sock_pending != -1) {
1232 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1233 sock = *sock_pending;
1234 } else {
1235 assert(res);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001236 if (res->ai_family == AF_INET6) {
1237 addr = (struct in_addr *) &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001238 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001239 } else {
1240 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001241 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001242 }
1243 if (!inet_ntop(res->ai_family, addr, str, res->ai_addrlen)) {
1244 WRN("inet_ntop() failed (%s).", strerror(errno));
1245 } else {
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001246 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 +02001247 }
1248
1249 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001250 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1251 if (sock == -1) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001252 ERR("Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001253 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001254 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001255 /* make the socket non-blocking */
1256 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001257 ERR("fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001258 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001259 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001260 /* non-blocking connect! */
1261 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1262 if (errno != EINPROGRESS) {
1263 /* network connection failed, try another resource */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001264 ERR("connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001265 goto cleanup;
1266 }
1267 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001268 }
1269 ts.tv_sec = timeout;
1270 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001271
Frank Rimpler9f838b02018-07-25 06:44:03 +00001272 FD_ZERO(&wset);
1273 FD_SET(sock, &wset);
1274
1275 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001276 ERR("select() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001277 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001278 }
1279
Michal Vasko5e8d0192019-06-24 19:19:49 +02001280 if (ret == 0) {
1281 /* there was a timeout */
1282 VRB("Timed out after %ds (%s).", timeout, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001283 if (sock_pending) {
1284 /* no sock-close, we'll try it again */
1285 *sock_pending = sock;
1286 } else {
1287 close(sock);
1288 }
1289 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001290 }
Radek Krejci782041a2018-08-20 10:09:45 +02001291
1292 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001293 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001294 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001295 ERR("getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001296 goto cleanup;
1297 }
1298 if (error == ECONNREFUSED) {
1299 /* network connection failed, try another resource */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001300 VRB("getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001301 errno = error;
1302 goto cleanup;
1303 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001304
1305 /* enable keep-alive */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001306 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001307 goto cleanup;
1308 }
1309
Michal Vasko086311b2016-01-08 09:53:11 +01001310 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001311
Frank Rimpler9f838b02018-07-25 06:44:03 +00001312cleanup:
1313 if (sock_pending) {
1314 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001315 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001316 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001317 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001318}
1319
Frank Rimpler9f838b02018-07-25 06:44:03 +00001320/* A given timeout value limits the time how long the function blocks. If it has to block
1321 only for some seconds, a socket connection might not yet have been fully established.
1322 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1323 value will be -1. In such a case a subsequent invokation is required, by providing the
1324 stored sock_pending, again.
1325 In general, if this function returns -1, when a timeout has been given, this function
1326 has to be invoked, until it returns a valid socket.
1327 */
1328int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001329nc_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 +00001330{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001331 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001332 int sock = sock_pending ? *sock_pending : -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001333 struct addrinfo hints, *res_list, *res;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001334 char *buf, port_s[6]; /* length of string representation of short int */
Michal Vasko66032bc2019-01-22 15:03:12 +01001335 void *addr;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001336
Michal Vasko66032bc2019-01-22 15:03:12 +01001337 DBG("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001338
1339 /* no pending socket */
1340 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001341 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001342 snprintf(port_s, 6, "%u", port);
1343 memset(&hints, 0, sizeof hints);
1344 hints.ai_family = AF_UNSPEC;
1345 hints.ai_socktype = SOCK_STREAM;
1346 hints.ai_protocol = IPPROTO_TCP;
1347 i = getaddrinfo(host, port_s, &hints, &res_list);
1348 if (i != 0) {
1349 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1350 return -1;
1351 }
1352
1353 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001354 sock = _non_blocking_connect(timeout, sock_pending, res, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001355 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1356 /* try the next resource */
1357 continue;
1358 }
1359 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 +01001360
1361 opt = 1;
1362 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
1363 ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno));
1364 close(sock);
1365 return -1;
1366 }
1367
Michal Vasko66032bc2019-01-22 15:03:12 +01001368 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1369 buf = malloc(INET6_ADDRSTRLEN);
1370 if (!buf) {
1371 ERRMEM;
1372 close(sock);
1373 return -1;
1374 }
1375 if (res->ai_family == AF_INET) {
1376 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1377 } else {
1378 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1379 }
1380 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
1381 ERR("Converting host to IP address failed (%s).", strerror(errno));
1382 free(buf);
1383 close(sock);
1384 return -1;
1385 }
1386
1387 *ip_host = buf;
1388 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001389 break;
1390 }
1391 freeaddrinfo(res_list);
1392
1393 } else {
1394 /* try to get a connection with the pending socket */
1395 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001396 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001397 }
1398
1399 return sock;
1400}
1401
Michal Vasko086311b2016-01-08 09:53:11 +01001402static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001403get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001404{
Michal Vasko086311b2016-01-08 09:53:11 +01001405 char *ptr;
1406 const char *str_msgid;
1407 uint64_t cur_msgid;
1408 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001409 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001410 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1411
Michal Vasko086311b2016-01-08 09:53:11 +01001412 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001413 if (!msgid && session->opts.client.notifs) {
1414 cont = session->opts.client.notifs;
1415 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001416
Michal Vasko71ba2da2016-05-04 10:53:16 +02001417 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001418 free(cont);
1419
Michal Vasko71ba2da2016-05-04 10:53:16 +02001420 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001421 }
1422
1423 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001424 if (msgid && session->opts.client.replies) {
1425 cont = session->opts.client.replies;
1426 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001427
Michal Vasko71ba2da2016-05-04 10:53:16 +02001428 xml = cont->msg;
1429 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001430
Michal Vasko71ba2da2016-05-04 10:53:16 +02001431 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001432 }
1433
Michal Vasko71ba2da2016-05-04 10:53:16 +02001434 if (!msgtype) {
1435 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001436 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001437 }
Michal Vasko086311b2016-01-08 09:53:11 +01001438
1439 /* we read rpc-reply, want a notif */
1440 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001441 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001442 while (*cont_ptr) {
1443 cont_ptr = &((*cont_ptr)->next);
1444 }
1445 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001446 if (!*cont_ptr) {
1447 ERRMEM;
1448 lyxml_free(session->ctx, xml);
1449 return NC_MSG_ERROR;
1450 }
Michal Vasko086311b2016-01-08 09:53:11 +01001451 (*cont_ptr)->msg = xml;
1452 (*cont_ptr)->next = NULL;
1453 }
1454
1455 /* we read notif, want a rpc-reply */
1456 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001457 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001458 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001459 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001460 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001461 }
Michal Vasko086311b2016-01-08 09:53:11 +01001462
Michal Vasko2e6defd2016-10-07 15:48:15 +02001463 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001464 while (*cont_ptr) {
1465 cont_ptr = &((*cont_ptr)->next);
1466 }
1467 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001468 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001469 ERRMEM;
1470 lyxml_free(session->ctx, xml);
1471 return NC_MSG_ERROR;
1472 }
Michal Vasko086311b2016-01-08 09:53:11 +01001473 (*cont_ptr)->msg = xml;
1474 (*cont_ptr)->next = NULL;
1475 }
1476
Michal Vasko086311b2016-01-08 09:53:11 +01001477 switch (msgtype) {
1478 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001479 if (!msgid) {
1480 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001481 }
Michal Vasko086311b2016-01-08 09:53:11 +01001482 break;
1483
1484 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001485 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001486 /* check message-id */
1487 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1488 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001489 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001490 } else {
1491 cur_msgid = strtoul(str_msgid, &ptr, 10);
1492 if (cur_msgid != msgid) {
1493 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1494 session->id, str_msgid);
1495 msgtype = NC_MSG_REPLY_ERR_MSGID;
1496 }
1497 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001498 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001499 }
Michal Vasko086311b2016-01-08 09:53:11 +01001500 break;
1501
1502 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001503 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001504 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001505 msgtype = NC_MSG_ERROR;
1506 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001507
1508 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001509 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001510 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001511 msgtype = NC_MSG_ERROR;
1512 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001513
1514 default:
1515 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1516 * NC_MSG_NONE is not returned by nc_read_msg()
1517 */
1518 break;
1519 }
1520
1521 return msgtype;
1522}
1523
1524/* cannot strictly fail, but does not need to fill any error parameter at all */
1525static void
1526parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1527{
1528 struct lyxml_elem *iter, *next, *info;
1529
1530 LY_TREE_FOR(xml->child, iter) {
1531 if (!iter->ns) {
1532 if (iter->content) {
1533 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1534 } else {
1535 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1536 }
1537 continue;
1538 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1539 if (iter->content) {
1540 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1541 iter->name, iter->content, iter->ns->value);
1542 } else {
1543 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1544 }
1545 continue;
1546 }
1547
1548 if (!strcmp(iter->name, "error-type")) {
1549 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1550 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1551 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1552 } else if (err->type) {
1553 WRN("<rpc-error> <error-type> duplicated.");
1554 } else {
1555 err->type = lydict_insert(ctx, iter->content, 0);
1556 }
1557 } else if (!strcmp(iter->name, "error-tag")) {
1558 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1559 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1560 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1561 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1562 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1563 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1564 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1565 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1566 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1567 && strcmp(iter->content, "malformed-message"))) {
1568 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1569 } else if (err->tag) {
1570 WRN("<rpc-error> <error-tag> duplicated.");
1571 } else {
1572 err->tag = lydict_insert(ctx, iter->content, 0);
1573 }
1574 } else if (!strcmp(iter->name, "error-severity")) {
1575 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1576 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1577 } else if (err->severity) {
1578 WRN("<rpc-error> <error-severity> duplicated.");
1579 } else {
1580 err->severity = lydict_insert(ctx, iter->content, 0);
1581 }
1582 } else if (!strcmp(iter->name, "error-app-tag")) {
1583 if (err->apptag) {
1584 WRN("<rpc-error> <error-app-tag> duplicated.");
1585 } else {
1586 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1587 }
1588 } else if (!strcmp(iter->name, "error-path")) {
1589 if (err->path) {
1590 WRN("<rpc-error> <error-path> duplicated.");
1591 } else {
1592 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1593 }
1594 } else if (!strcmp(iter->name, "error-message")) {
1595 if (err->message) {
1596 WRN("<rpc-error> <error-message> duplicated.");
1597 } else {
1598 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001599 if (err->message_lang) {
1600 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1601 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001602 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1603 }
1604 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1605 }
1606 } else if (!strcmp(iter->name, "error-info")) {
1607 LY_TREE_FOR_SAFE(iter->child, next, info) {
1608 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1609 if (!strcmp(info->name, "session-id")) {
1610 if (err->sid) {
1611 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1612 } else {
1613 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1614 }
Michal Vasko630485f2018-01-03 14:28:32 +01001615 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001616 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001617 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1618 if (!err->attr) {
1619 ERRMEM;
1620 return;
1621 }
Michal Vasko086311b2016-01-08 09:53:11 +01001622 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1623 } else if (!strcmp(info->name, "bad-element")) {
1624 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001625 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1626 if (!err->elem) {
1627 ERRMEM;
1628 return;
1629 }
Michal Vasko086311b2016-01-08 09:53:11 +01001630 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1631 } else if (!strcmp(info->name, "bad-namespace")) {
1632 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001633 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1634 if (!err->ns) {
1635 ERRMEM;
1636 return;
1637 }
Michal Vasko086311b2016-01-08 09:53:11 +01001638 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1639 } else {
1640 if (info->content) {
1641 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1642 info->name, info->content);
1643 } else {
1644 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1645 }
1646 }
1647 } else {
1648 lyxml_unlink(ctx, info);
1649 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001650 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1651 if (!err->other) {
1652 ERRMEM;
1653 return;
1654 }
Michal Vasko086311b2016-01-08 09:53:11 +01001655 err->other[err->other_count - 1] = info;
1656 }
1657 }
1658 } else {
1659 if (iter->content) {
1660 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1661 } else {
1662 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1663 }
1664 }
1665 }
1666}
1667
1668static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001669parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001670{
1671 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001672 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001673 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001674 struct nc_reply_data *data_rpl;
1675 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001676 struct nc_rpc_act_generic *rpc_gen;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001677 int i, data_parsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001678
Michal Vasko086311b2016-01-08 09:53:11 +01001679 /* rpc-error */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001680 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 +01001681 /* count and check elements */
1682 i = 0;
1683 LY_TREE_FOR(xml->child, iter) {
1684 if (strcmp(iter->name, "rpc-error")) {
1685 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1686 return NULL;
1687 } else if (!iter->ns) {
1688 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1689 return NULL;
1690 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1691 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1692 return NULL;
1693 }
1694 ++i;
1695 }
1696
1697 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001698 if (!error_rpl) {
1699 ERRMEM;
1700 return NULL;
1701 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001702 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001703 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001704 if (!error_rpl->err) {
1705 ERRMEM;
1706 free(error_rpl);
1707 return NULL;
1708 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001709 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001710 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001711 reply = (struct nc_reply *)error_rpl;
1712
1713 i = 0;
1714 LY_TREE_FOR(xml->child, iter) {
1715 parse_rpc_error(ctx, iter, error_rpl->err + i);
1716 ++i;
1717 }
1718
1719 /* ok */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001720 } 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 +01001721 if (xml->child->next) {
1722 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1723 return NULL;
1724 }
1725 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001726 if (!reply) {
1727 ERRMEM;
1728 return NULL;
1729 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001730 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001731
1732 /* some RPC output */
1733 } else {
1734 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001735 case NC_RPC_ACT_GENERIC:
1736 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001737
1738 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001739 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1740 if (!rpc_act) {
1741 ERR("Failed to duplicate a generic RPC/action.");
1742 return NULL;
1743 }
Michal Vasko086311b2016-01-08 09:53:11 +01001744 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001745 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 +02001746 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001747 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001748 return NULL;
1749 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001750 }
Michal Vasko086311b2016-01-08 09:53:11 +01001751 break;
1752
1753 case NC_RPC_GETCONFIG:
1754 case NC_RPC_GET:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001755 case NC_RPC_GETDATA:
Michal Vasko6b281ff2019-02-14 10:15:19 +01001756 /* we should definitely have received at least an empty "data" element even on empty reply, but fine */
1757 if (!xml->child || !xml->child->child) {
Michal Vasko13ed2942016-02-29 16:21:00 +01001758 /* we did not receive any data */
1759 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001760 if (!data_rpl) {
1761 ERRMEM;
1762 return NULL;
1763 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001764 data_rpl->type = NC_RPL_DATA;
1765 data_rpl->data = NULL;
1766 return (struct nc_reply *)data_rpl;
1767 }
1768
Michal Vasko086311b2016-01-08 09:53:11 +01001769 /* special treatment */
Michal Vaskof45e0c12018-11-28 08:38:23 +01001770 ly_errno = 0;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001771 data = lyd_parse_xml(ctx, &xml->child->child,
1772 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001773 | parseroptions);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001774 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001775 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1776 return NULL;
1777 }
Michal Vaskof45e0c12018-11-28 08:38:23 +01001778 data_parsed = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001779 break;
1780
1781 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001782 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001783 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001784 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001785 return NULL;
1786 }
1787 break;
1788
1789 case NC_RPC_EDIT:
1790 case NC_RPC_COPY:
1791 case NC_RPC_DELETE:
1792 case NC_RPC_LOCK:
1793 case NC_RPC_UNLOCK:
1794 case NC_RPC_KILL:
1795 case NC_RPC_COMMIT:
1796 case NC_RPC_DISCARD:
1797 case NC_RPC_CANCEL:
1798 case NC_RPC_VALIDATE:
1799 case NC_RPC_SUBSCRIBE:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001800 case NC_RPC_EDITDATA:
Michal Vasko086311b2016-01-08 09:53:11 +01001801 /* there is no output defined */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001802 ERR("Unexpected data reply (root elem \"%s\").", xml->child ? xml->child->name : NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001803 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001804 default:
1805 ERRINT;
1806 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001807 }
1808
1809 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001810 if (!data_rpl) {
1811 ERRMEM;
1812 return NULL;
1813 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001814 data_rpl->type = NC_RPL_DATA;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001815
1816 ly_errno = 0;
1817 if (!data_parsed) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001818 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001819 rpc_act, NULL);
Michal Vasko6b281ff2019-02-14 10:15:19 +01001820 if (!ly_errno && !data_rpl->data->child) {
1821 ERR("An empty data <rpc-reply>.");
1822 lyd_free_withsiblings(rpc_act);
1823 lyd_free(data_rpl->data);
1824 free(data_rpl);
1825 return NULL;
1826 }
Michal Vasko086311b2016-01-08 09:53:11 +01001827 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01001828 /* <get>, <get-config>, <get-data> */
Michal Vasko086311b2016-01-08 09:53:11 +01001829 data_rpl->data = data;
1830 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001831 lyd_free_withsiblings(rpc_act);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001832 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001833 ERR("Failed to parse <rpc-reply>.");
1834 free(data_rpl);
1835 return NULL;
1836 }
1837 reply = (struct nc_reply *)data_rpl;
1838 }
1839
1840 return reply;
1841}
1842
Radek Krejci53691be2016-02-22 13:58:37 +01001843#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001844
Michal Vasko3031aae2016-01-27 16:07:18 +01001845int
1846nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1847{
1848 int sock;
1849
Michal Vasko45e53ae2016-04-07 11:46:03 +02001850 if (!address) {
1851 ERRARG("address");
1852 return -1;
1853 } else if (!port) {
1854 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001855 return -1;
1856 }
1857
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001858 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001859 if (sock == -1) {
1860 return -1;
1861 }
1862
1863 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001864 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1865 if (!client_opts.ch_binds) {
1866 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001867 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001868 return -1;
1869 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001870
Michal Vasko2e6defd2016-10-07 15:48:15 +02001871 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1872 if (!client_opts.ch_bind_ti) {
1873 ERRMEM;
1874 close(sock);
1875 return -1;
1876 }
1877 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1878
Michal Vasko3031aae2016-01-27 16:07:18 +01001879 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001880 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1881 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001882 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001883 return -1;
1884 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001885 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1886 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001887 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001888
1889 return 0;
1890}
1891
1892int
1893nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1894{
1895 uint32_t i;
1896 int ret = -1;
1897
1898 if (!address && !port && !ti) {
1899 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1900 close(client_opts.ch_binds[i].sock);
1901 free((char *)client_opts.ch_binds[i].address);
1902
1903 ret = 0;
1904 }
1905 free(client_opts.ch_binds);
1906 client_opts.ch_binds = NULL;
1907 client_opts.ch_bind_count = 0;
1908 } else {
1909 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1910 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1911 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001912 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001913 close(client_opts.ch_binds[i].sock);
1914 free((char *)client_opts.ch_binds[i].address);
1915
1916 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001917 if (!client_opts.ch_bind_count) {
1918 free(client_opts.ch_binds);
1919 client_opts.ch_binds = NULL;
1920 } else if (i < client_opts.ch_bind_count) {
1921 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1922 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1923 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001924
1925 ret = 0;
1926 }
1927 }
1928 }
1929
1930 return ret;
1931}
1932
1933API int
1934nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1935{
1936 int sock;
1937 char *host = NULL;
1938 uint16_t port, idx;
1939
Michal Vasko45e53ae2016-04-07 11:46:03 +02001940 if (!client_opts.ch_binds) {
1941 ERRINIT;
1942 return -1;
1943 } else if (!session) {
1944 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001945 return -1;
1946 }
1947
1948 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1949
Michal Vasko50456e82016-02-02 12:16:08 +01001950 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001951 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001952 return sock;
1953 }
1954
Radek Krejci53691be2016-02-22 13:58:37 +01001955#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001956 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001957 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001958 } else
1959#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001960#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001961 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001962 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001963 } else
1964#endif
1965 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001966 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001967 *session = NULL;
1968 }
1969
1970 free(host);
1971
1972 if (!(*session)) {
1973 return -1;
1974 }
1975
1976 return 1;
1977}
1978
Radek Krejci53691be2016-02-22 13:58:37 +01001979#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001980
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001981API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001982nc_session_get_cpblts(const struct nc_session *session)
1983{
1984 if (!session) {
1985 ERRARG("session");
1986 return NULL;
1987 }
1988
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001989 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001990}
1991
1992API const char *
1993nc_session_cpblt(const struct nc_session *session, const char *capab)
1994{
1995 int i, len;
1996
1997 if (!session) {
1998 ERRARG("session");
1999 return NULL;
2000 } else if (!capab) {
2001 ERRARG("capab");
2002 return NULL;
2003 }
2004
2005 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002006 for (i = 0; session->opts.client.cpblts[i]; ++i) {
2007 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
2008 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02002009 }
2010 }
2011
2012 return NULL;
2013}
2014
Michal Vasko9cd26a82016-05-31 08:58:48 +02002015API int
2016nc_session_ntf_thread_running(const struct nc_session *session)
2017{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002018 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02002019 ERRARG("session");
2020 return 0;
2021 }
2022
Michal Vasko2e6defd2016-10-07 15:48:15 +02002023 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02002024}
2025
Michal Vaskob7558c52016-02-26 15:04:19 +01002026API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002027nc_client_init(void)
2028{
2029 nc_init();
2030}
2031
2032API void
Michal Vaskob7558c52016-02-26 15:04:19 +01002033nc_client_destroy(void)
2034{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02002035 nc_client_set_schema_searchpath(NULL);
2036#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
2037 nc_client_ch_del_bind(NULL, 0, 0);
2038#endif
2039#ifdef NC_ENABLED_SSH
2040 nc_client_ssh_destroy_opts();
2041#endif
2042#ifdef NC_ENABLED_TLS
2043 nc_client_tls_destroy_opts();
2044#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002045 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01002046}
2047
Michal Vasko086311b2016-01-08 09:53:11 +01002048API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002049nc_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 +01002050{
2051 struct lyxml_elem *xml;
2052 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2053
Michal Vasko45e53ae2016-04-07 11:46:03 +02002054 if (!session) {
2055 ERRARG("session");
2056 return NC_MSG_ERROR;
2057 } else if (!rpc) {
2058 ERRARG("rpc");
2059 return NC_MSG_ERROR;
2060 } else if (!reply) {
2061 ERRARG("reply");
2062 return NC_MSG_ERROR;
2063 } else if (parseroptions & LYD_OPT_TYPEMASK) {
2064 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01002065 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002066 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01002067 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002068 return NC_MSG_ERROR;
2069 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002070 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01002071 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
2072 parseroptions &= LYD_OPT_STRICT;
2073 }
Michal Vasko41adf392017-01-17 10:39:04 +01002074 /* no mechanism to check external dependencies is provided */
2075 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01002076 *reply = NULL;
2077
2078 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002079
Michal Vasko21fc9cb2019-06-20 10:15:59 +02002080 if (msgtype == NC_MSG_REPLY) {
Michal Vaskoeee99412016-11-21 10:19:43 +01002081 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01002082 lyxml_free(session->ctx, xml);
2083 if (!(*reply)) {
2084 return NC_MSG_ERROR;
2085 }
2086 }
2087
2088 return msgtype;
2089}
2090
2091API NC_MSG_TYPE
2092nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
2093{
2094 struct lyxml_elem *xml, *ev_time;
2095 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2096
Michal Vasko45e53ae2016-04-07 11:46:03 +02002097 if (!session) {
2098 ERRARG("session");
2099 return NC_MSG_ERROR;
2100 } else if (!notif) {
2101 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01002102 return NC_MSG_ERROR;
2103 } 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 Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002105 return NC_MSG_ERROR;
2106 }
2107
2108 msgtype = get_msg(session, timeout, 0, &xml);
2109
2110 if (msgtype == NC_MSG_NOTIF) {
2111 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002112 if (!*notif) {
2113 ERRMEM;
2114 lyxml_free(session->ctx, xml);
2115 return NC_MSG_ERROR;
2116 }
Michal Vasko086311b2016-01-08 09:53:11 +01002117
2118 /* eventTime */
2119 LY_TREE_FOR(xml->child, ev_time) {
2120 if (!strcmp(ev_time->name, "eventTime")) {
2121 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
2122 /* lyd_parse does not know this element */
2123 lyxml_free(session->ctx, ev_time);
2124 break;
2125 }
2126 }
2127 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01002128 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002129 goto fail;
2130 }
2131
2132 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01002133 (*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 +01002134 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002135 lyxml_free(session->ctx, xml);
2136 xml = NULL;
2137 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01002138 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002139 goto fail;
2140 }
2141 }
2142
2143 return msgtype;
2144
2145fail:
2146 lydict_remove(session->ctx, (*notif)->datetime);
2147 lyd_free((*notif)->tree);
2148 free(*notif);
2149 *notif = NULL;
2150 lyxml_free(session->ctx, xml);
2151
2152 return NC_MSG_ERROR;
2153}
2154
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002155static void *
2156nc_recv_notif_thread(void *arg)
2157{
2158 struct nc_ntf_thread_arg *ntarg;
2159 struct nc_session *session;
2160 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
2161 struct nc_notif *notif;
2162 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002163 pthread_t *ntf_tid;
2164
2165 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002166
2167 ntarg = (struct nc_ntf_thread_arg *)arg;
2168 session = ntarg->session;
2169 notif_clb = ntarg->notif_clb;
2170 free(ntarg);
2171
Michal Vasko131120a2018-05-29 15:44:02 +02002172 /* remember our allocated tid, we will be freeing it */
2173 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
2174
Michal Vasko2e6defd2016-10-07 15:48:15 +02002175 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002176 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002177 if (msgtype == NC_MSG_NOTIF) {
2178 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002179 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2180 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2181 nc_notif_free(notif);
2182 break;
2183 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002184 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002185 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002186 /* quit this thread once the session is broken */
2187 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002188 }
2189
2190 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2191 }
2192
Michal Vasko0651c902016-05-19 15:55:42 +02002193 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002194 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02002195 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002196 return NULL;
2197}
2198
2199API int
2200nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2201{
2202 struct nc_ntf_thread_arg *ntarg;
2203 int ret;
2204
Michal Vasko45e53ae2016-04-07 11:46:03 +02002205 if (!session) {
2206 ERRARG("session");
2207 return -1;
2208 } else if (!notif_clb) {
2209 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002210 return -1;
2211 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2212 ERR("Session %u: invalid session to receive Notifications.", session->id);
2213 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002214 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002215 ERR("Session %u: separate notification thread is already running.", session->id);
2216 return -1;
2217 }
2218
2219 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002220 if (!ntarg) {
2221 ERRMEM;
2222 return -1;
2223 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002224 ntarg->session = session;
2225 ntarg->notif_clb = notif_clb;
2226
2227 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002228 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2229 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002230 ERRMEM;
2231 free(ntarg);
2232 return -1;
2233 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002234
Michal Vasko2e6defd2016-10-07 15:48:15 +02002235 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002236 if (ret) {
2237 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2238 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002239 free((pthread_t *)session->opts.client.ntf_tid);
2240 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002241 return -1;
2242 }
2243
2244 return 0;
2245}
2246
Michal Vasko086311b2016-01-08 09:53:11 +01002247API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002248nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002249{
2250 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002251 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002252 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002253 struct nc_rpc_getconfig *rpc_gc;
2254 struct nc_rpc_edit *rpc_e;
2255 struct nc_rpc_copy *rpc_cp;
2256 struct nc_rpc_delete *rpc_del;
2257 struct nc_rpc_lock *rpc_lock;
2258 struct nc_rpc_get *rpc_g;
2259 struct nc_rpc_kill *rpc_k;
2260 struct nc_rpc_commit *rpc_com;
2261 struct nc_rpc_cancel *rpc_can;
2262 struct nc_rpc_validate *rpc_val;
2263 struct nc_rpc_getschema *rpc_gs;
2264 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002265 struct nc_rpc_getdata *rpc_getd;
2266 struct nc_rpc_editdata *rpc_editd;
Michal Vasko086311b2016-01-08 09:53:11 +01002267 struct lyd_node *data, *node;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002268 const struct lys_module *mod = NULL, *ietfncwd;
2269 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002270 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002271 uint64_t cur_msgid;
2272
Michal Vasko45e53ae2016-04-07 11:46:03 +02002273 if (!session) {
2274 ERRARG("session");
2275 return NC_MSG_ERROR;
2276 } else if (!rpc) {
2277 ERRARG("rpc");
2278 return NC_MSG_ERROR;
2279 } else if (!msgid) {
2280 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002281 return NC_MSG_ERROR;
2282 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002283 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002284 return NC_MSG_ERROR;
2285 }
2286
Michal Vaskoc1171a42019-11-05 12:06:46 +01002287 switch (rpc->type) {
2288 case NC_RPC_ACT_GENERIC:
2289 /* checked when parsing */
2290 break;
2291 case NC_RPC_GETCONFIG:
2292 case NC_RPC_EDIT:
2293 case NC_RPC_COPY:
2294 case NC_RPC_DELETE:
2295 case NC_RPC_LOCK:
2296 case NC_RPC_UNLOCK:
2297 case NC_RPC_GET:
2298 case NC_RPC_KILL:
2299 case NC_RPC_COMMIT:
2300 case NC_RPC_DISCARD:
2301 case NC_RPC_CANCEL:
2302 case NC_RPC_VALIDATE:
2303 mod = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
2304 if (!mod) {
Michal Vasko086cd572017-01-12 12:19:05 +01002305 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002306 return NC_MSG_ERROR;
2307 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002308 break;
2309 case NC_RPC_GETSCHEMA:
2310 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
2311 if (!mod) {
2312 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
2313 return NC_MSG_ERROR;
2314 }
2315 break;
2316 case NC_RPC_SUBSCRIBE:
2317 mod = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
2318 if (!mod) {
2319 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
2320 return NC_MSG_ERROR;
2321 }
2322 break;
2323 case NC_RPC_GETDATA:
2324 case NC_RPC_EDITDATA:
2325 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-nmda", NULL, 1);
2326 if (!mod) {
2327 ERR("Session %u: missing \"ietf-netconf-nmda\" schema in the context.", session->id);
2328 return NC_MSG_ERROR;
2329 }
2330 break;
2331 case NC_RPC_UNKNOWN:
2332 ERRINT;
2333 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002334 }
2335
2336 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002337 case NC_RPC_ACT_GENERIC:
2338 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002339
2340 if (rpc_gen->has_data) {
2341 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002342 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002343 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002344 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 +01002345 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002346 if (!data) {
2347 return NC_MSG_ERROR;
2348 }
Michal Vasko086311b2016-01-08 09:53:11 +01002349 }
2350 break;
2351
2352 case NC_RPC_GETCONFIG:
2353 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2354
Michal Vaskoc1171a42019-11-05 12:06:46 +01002355 data = lyd_new(NULL, mod, "get-config");
2356 node = lyd_new(data, mod, "source");
2357 node = lyd_new_leaf(node, mod, ncds2str[rpc_gc->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002358 if (!node) {
2359 lyd_free(data);
2360 return NC_MSG_ERROR;
2361 }
2362 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002363 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002364 node = lyd_new_anydata(data, mod, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002365 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002366 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002367 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002368 lyd_insert_attr(node, NULL, "type", "xpath");
2369 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002370 }
2371 if (!node) {
2372 lyd_free(data);
2373 return NC_MSG_ERROR;
2374 }
2375 }
2376
2377 if (rpc_gc->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002378 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002379 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002380 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2381 lyd_free(data);
2382 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002383 }
2384 switch (rpc_gc->wd_mode) {
2385 case NC_WD_UNKNOWN:
2386 /* cannot get here */
2387 break;
2388 case NC_WD_ALL:
2389 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2390 break;
2391 case NC_WD_ALL_TAG:
2392 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2393 break;
2394 case NC_WD_TRIM:
2395 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2396 break;
2397 case NC_WD_EXPLICIT:
2398 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2399 break;
2400 }
2401 if (!node) {
2402 lyd_free(data);
2403 return NC_MSG_ERROR;
2404 }
2405 }
2406 break;
2407
2408 case NC_RPC_EDIT:
2409 rpc_e = (struct nc_rpc_edit *)rpc;
2410
Michal Vaskoc1171a42019-11-05 12:06:46 +01002411 data = lyd_new(NULL, mod, "edit-config");
2412 node = lyd_new(data, mod, "target");
2413 node = lyd_new_leaf(node, mod, ncds2str[rpc_e->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002414 if (!node) {
2415 lyd_free(data);
2416 return NC_MSG_ERROR;
2417 }
2418
2419 if (rpc_e->default_op) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002420 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
Michal Vasko086311b2016-01-08 09:53:11 +01002421 if (!node) {
2422 lyd_free(data);
2423 return NC_MSG_ERROR;
2424 }
2425 }
2426
2427 if (rpc_e->test_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002428 node = lyd_new_leaf(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002429 if (!node) {
2430 lyd_free(data);
2431 return NC_MSG_ERROR;
2432 }
2433 }
2434
2435 if (rpc_e->error_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002436 node = lyd_new_leaf(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002437 if (!node) {
2438 lyd_free(data);
2439 return NC_MSG_ERROR;
2440 }
2441 }
2442
Michal Vasko7793bc62016-09-16 11:58:41 +02002443 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002444 node = lyd_new_anydata(data, mod, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002445 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002446 node = lyd_new_leaf(data, mod, "url", rpc_e->edit_cont);
Michal Vasko086311b2016-01-08 09:53:11 +01002447 }
2448 if (!node) {
2449 lyd_free(data);
2450 return NC_MSG_ERROR;
2451 }
2452 break;
2453
2454 case NC_RPC_COPY:
2455 rpc_cp = (struct nc_rpc_copy *)rpc;
2456
Michal Vaskoc1171a42019-11-05 12:06:46 +01002457 data = lyd_new(NULL, mod, "copy-config");
2458 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002459 if (rpc_cp->url_trg) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002460 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_trg);
Michal Vasko086311b2016-01-08 09:53:11 +01002461 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002462 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002463 }
2464 if (!node) {
2465 lyd_free(data);
2466 return NC_MSG_ERROR;
2467 }
2468
Michal Vaskoc1171a42019-11-05 12:06:46 +01002469 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002470 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002471 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002472 node = lyd_new_anydata(node, mod, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002473 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002474 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002475 }
2476 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002477 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002478 }
2479 if (!node) {
2480 lyd_free(data);
2481 return NC_MSG_ERROR;
2482 }
2483
2484 if (rpc_cp->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002485 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002486 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002487 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2488 lyd_free(data);
2489 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002490 }
2491 switch (rpc_cp->wd_mode) {
2492 case NC_WD_UNKNOWN:
2493 /* cannot get here */
2494 break;
2495 case NC_WD_ALL:
2496 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2497 break;
2498 case NC_WD_ALL_TAG:
2499 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2500 break;
2501 case NC_WD_TRIM:
2502 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2503 break;
2504 case NC_WD_EXPLICIT:
2505 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2506 break;
2507 }
2508 if (!node) {
2509 lyd_free(data);
2510 return NC_MSG_ERROR;
2511 }
2512 }
2513 break;
2514
2515 case NC_RPC_DELETE:
2516 rpc_del = (struct nc_rpc_delete *)rpc;
2517
Michal Vaskoc1171a42019-11-05 12:06:46 +01002518 data = lyd_new(NULL, mod, "delete-config");
2519 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002520 if (rpc_del->url) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002521 node = lyd_new_leaf(node, mod, "url", rpc_del->url);
Michal Vasko086311b2016-01-08 09:53:11 +01002522 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002523 node = lyd_new_leaf(node, mod, ncds2str[rpc_del->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002524 }
2525 if (!node) {
2526 lyd_free(data);
2527 return NC_MSG_ERROR;
2528 }
2529 break;
2530
2531 case NC_RPC_LOCK:
2532 rpc_lock = (struct nc_rpc_lock *)rpc;
2533
Michal Vaskoc1171a42019-11-05 12:06:46 +01002534 data = lyd_new(NULL, mod, "lock");
2535 node = lyd_new(data, mod, "target");
2536 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002537 if (!node) {
2538 lyd_free(data);
2539 return NC_MSG_ERROR;
2540 }
2541 break;
2542
2543 case NC_RPC_UNLOCK:
2544 rpc_lock = (struct nc_rpc_lock *)rpc;
2545
Michal Vaskoc1171a42019-11-05 12:06:46 +01002546 data = lyd_new(NULL, mod, "unlock");
2547 node = lyd_new(data, mod, "target");
2548 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002549 if (!node) {
2550 lyd_free(data);
2551 return NC_MSG_ERROR;
2552 }
2553 break;
2554
2555 case NC_RPC_GET:
2556 rpc_g = (struct nc_rpc_get *)rpc;
2557
Michal Vaskoc1171a42019-11-05 12:06:46 +01002558 data = lyd_new(NULL, mod, "get");
Michal Vasko086311b2016-01-08 09:53:11 +01002559 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002560 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002561 node = lyd_new_anydata(data, mod, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002562 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002563 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002564 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002565 lyd_insert_attr(node, NULL, "type", "xpath");
2566 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002567 }
2568 if (!node) {
2569 lyd_free(data);
2570 return NC_MSG_ERROR;
2571 }
2572 }
2573
2574 if (rpc_g->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002575 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002576 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002577 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2578 lyd_free(data);
2579 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002580 }
2581 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002582 case NC_WD_ALL:
2583 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2584 break;
2585 case NC_WD_ALL_TAG:
2586 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2587 break;
2588 case NC_WD_TRIM:
2589 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2590 break;
2591 case NC_WD_EXPLICIT:
2592 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2593 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002594 default:
2595 /* cannot get here */
2596 node = NULL;
2597 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002598 }
2599 if (!node) {
2600 lyd_free(data);
2601 return NC_MSG_ERROR;
2602 }
2603 }
2604 break;
2605
2606 case NC_RPC_KILL:
2607 rpc_k = (struct nc_rpc_kill *)rpc;
2608
Michal Vaskoc1171a42019-11-05 12:06:46 +01002609 data = lyd_new(NULL, mod, "kill-session");
Michal Vasko086311b2016-01-08 09:53:11 +01002610 sprintf(str, "%u", rpc_k->sid);
Michal Vaskoc1171a42019-11-05 12:06:46 +01002611 lyd_new_leaf(data, mod, "session-id", str);
Michal Vasko086311b2016-01-08 09:53:11 +01002612 break;
2613
2614 case NC_RPC_COMMIT:
2615 rpc_com = (struct nc_rpc_commit *)rpc;
2616
Michal Vaskoc1171a42019-11-05 12:06:46 +01002617 data = lyd_new(NULL, mod, "commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002618 if (rpc_com->confirmed) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002619 lyd_new_leaf(data, mod, "confirmed", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002620 }
2621
2622 if (rpc_com->confirm_timeout) {
2623 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vaskoc1171a42019-11-05 12:06:46 +01002624 lyd_new_leaf(data, mod, "confirm-timeout", str);
Michal Vasko086311b2016-01-08 09:53:11 +01002625 }
2626
2627 if (rpc_com->persist) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002628 node = lyd_new_leaf(data, mod, "persist", rpc_com->persist);
Michal Vasko086311b2016-01-08 09:53:11 +01002629 if (!node) {
2630 lyd_free(data);
2631 return NC_MSG_ERROR;
2632 }
2633 }
2634
2635 if (rpc_com->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002636 node = lyd_new_leaf(data, mod, "persist-id", rpc_com->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002637 if (!node) {
2638 lyd_free(data);
2639 return NC_MSG_ERROR;
2640 }
2641 }
2642 break;
2643
2644 case NC_RPC_DISCARD:
Michal Vaskoc1171a42019-11-05 12:06:46 +01002645 data = lyd_new(NULL, mod, "discard-changes");
Michal Vasko086311b2016-01-08 09:53:11 +01002646 break;
2647
2648 case NC_RPC_CANCEL:
2649 rpc_can = (struct nc_rpc_cancel *)rpc;
2650
Michal Vaskoc1171a42019-11-05 12:06:46 +01002651 data = lyd_new(NULL, mod, "cancel-commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002652 if (rpc_can->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002653 node = lyd_new_leaf(data, mod, "persist-id", rpc_can->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002654 if (!node) {
2655 lyd_free(data);
2656 return NC_MSG_ERROR;
2657 }
2658 }
2659 break;
2660
2661 case NC_RPC_VALIDATE:
2662 rpc_val = (struct nc_rpc_validate *)rpc;
2663
Michal Vaskoc1171a42019-11-05 12:06:46 +01002664 data = lyd_new(NULL, mod, "validate");
2665 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002666 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002667 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002668 node = lyd_new_anydata(node, mod, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002669 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002670 node = lyd_new_leaf(node, mod, "url", rpc_val->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002671 }
2672 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002673 node = lyd_new_leaf(node, mod, ncds2str[rpc_val->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002674 }
2675 if (!node) {
2676 lyd_free(data);
2677 return NC_MSG_ERROR;
2678 }
2679 break;
2680
2681 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002682 rpc_gs = (struct nc_rpc_getschema *)rpc;
2683
Michal Vaskoc1171a42019-11-05 12:06:46 +01002684 data = lyd_new(NULL, mod, "get-schema");
2685 node = lyd_new_leaf(data, mod, "identifier", rpc_gs->identifier);
Michal Vasko086311b2016-01-08 09:53:11 +01002686 if (!node) {
2687 lyd_free(data);
2688 return NC_MSG_ERROR;
2689 }
2690 if (rpc_gs->version) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002691 node = lyd_new_leaf(data, mod, "version", rpc_gs->version);
Michal Vasko086311b2016-01-08 09:53:11 +01002692 if (!node) {
2693 lyd_free(data);
2694 return NC_MSG_ERROR;
2695 }
2696 }
2697 if (rpc_gs->format) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002698 node = lyd_new_leaf(data, mod, "format", rpc_gs->format);
Michal Vasko086311b2016-01-08 09:53:11 +01002699 if (!node) {
2700 lyd_free(data);
2701 return NC_MSG_ERROR;
2702 }
2703 }
2704 break;
2705
2706 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002707 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2708
Michal Vaskoc1171a42019-11-05 12:06:46 +01002709 data = lyd_new(NULL, mod, "create-subscription");
Michal Vasko086311b2016-01-08 09:53:11 +01002710 if (rpc_sub->stream) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002711 node = lyd_new_leaf(data, mod, "stream", rpc_sub->stream);
Michal Vasko086311b2016-01-08 09:53:11 +01002712 if (!node) {
2713 lyd_free(data);
2714 return NC_MSG_ERROR;
2715 }
2716 }
2717
2718 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002719 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002720 node = lyd_new_anydata(data, mod, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002721 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002722 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002723 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002724 lyd_insert_attr(node, NULL, "type", "xpath");
2725 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002726 }
2727 if (!node) {
2728 lyd_free(data);
2729 return NC_MSG_ERROR;
2730 }
2731 }
2732
2733 if (rpc_sub->start) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002734 node = lyd_new_leaf(data, mod, "startTime", rpc_sub->start);
Michal Vasko086311b2016-01-08 09:53:11 +01002735 if (!node) {
2736 lyd_free(data);
2737 return NC_MSG_ERROR;
2738 }
2739 }
2740
2741 if (rpc_sub->stop) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002742 node = lyd_new_leaf(data, mod, "stopTime", rpc_sub->stop);
Michal Vasko086311b2016-01-08 09:53:11 +01002743 if (!node) {
2744 lyd_free(data);
2745 return NC_MSG_ERROR;
2746 }
2747 }
2748 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002749
2750 case NC_RPC_GETDATA:
2751 rpc_getd = (struct nc_rpc_getdata *)rpc;
2752
2753 data = lyd_new(NULL, mod, "get-data");
2754 node = lyd_new_leaf(data, mod, "datastore", rpc_getd->datastore);
2755 if (!node) {
2756 lyd_free(data);
2757 return NC_MSG_ERROR;
2758 }
2759 if (rpc_getd->filter) {
2760 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
2761 node = lyd_new_anydata(data, mod, "subtree-filter", rpc_getd->filter, LYD_ANYDATA_SXML);
2762 } else {
2763 node = lyd_new_leaf(data, mod, "xpath-filter", rpc_getd->filter);
2764 }
2765 if (!node) {
2766 lyd_free(data);
2767 return NC_MSG_ERROR;
2768 }
2769 }
2770 if (rpc_getd->config_filter) {
2771 node = lyd_new_leaf(data, mod, "config-filter", rpc_getd->config_filter);
2772 if (!node) {
2773 lyd_free(data);
2774 return NC_MSG_ERROR;
2775 }
2776 }
2777 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
2778 node = lyd_new_leaf(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" : "origin-filter",
2779 rpc_getd->origin_filter[i]);
2780 if (!node) {
2781 lyd_free(data);
2782 return NC_MSG_ERROR;
2783 }
2784 }
2785 if (rpc_getd->max_depth) {
2786 sprintf(str, "%u", rpc_getd->max_depth);
2787 node = lyd_new_leaf(data, mod, "max-depth", str);
2788 if (!node) {
2789 lyd_free(data);
2790 return NC_MSG_ERROR;
2791 }
2792 }
2793 if (rpc_getd->with_origin) {
2794 node = lyd_new_leaf(data, mod, "with-origin", NULL);
2795 if (!node) {
2796 lyd_free(data);
2797 return NC_MSG_ERROR;
2798 }
2799 }
2800
2801 if (rpc_getd->wd_mode) {
2802 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
2803 if (!ietfncwd) {
2804 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2805 lyd_free(data);
2806 return NC_MSG_ERROR;
2807 }
2808 switch (rpc_getd->wd_mode) {
2809 case NC_WD_UNKNOWN:
2810 /* cannot get here */
2811 break;
2812 case NC_WD_ALL:
2813 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2814 break;
2815 case NC_WD_ALL_TAG:
2816 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2817 break;
2818 case NC_WD_TRIM:
2819 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2820 break;
2821 case NC_WD_EXPLICIT:
2822 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2823 break;
2824 }
2825 if (!node) {
2826 lyd_free(data);
2827 return NC_MSG_ERROR;
2828 }
2829 }
2830 break;
2831
2832 case NC_RPC_EDITDATA:
2833 rpc_editd = (struct nc_rpc_editdata *)rpc;
2834
2835 data = lyd_new(NULL, mod, "edit-data");
2836 node = lyd_new_leaf(data, mod, "datastore", rpc_editd->datastore);
2837 if (!node) {
2838 lyd_free(data);
2839 return NC_MSG_ERROR;
2840 }
2841
2842 if (rpc_editd->default_op) {
2843 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op]);
2844 if (!node) {
2845 lyd_free(data);
2846 return NC_MSG_ERROR;
2847 }
2848 }
2849
2850 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
2851 node = lyd_new_anydata(data, mod, "config", rpc_editd->edit_cont, LYD_ANYDATA_SXML);
2852 } else {
2853 node = lyd_new_leaf(data, mod, "url", rpc_editd->edit_cont);
2854 }
2855 if (!node) {
2856 lyd_free(data);
2857 return NC_MSG_ERROR;
2858 }
2859 break;
2860
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002861 default:
2862 ERRINT;
2863 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002864 }
2865
Michal Vasko131120a2018-05-29 15:44:02 +02002866 if (!data) {
2867 /* error was already printed */
2868 return NC_MSG_ERROR;
2869 }
2870
Michal Vasko41adf392017-01-17 10:39:04 +01002871 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2872 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002873 if (dofree) {
2874 lyd_free(data);
2875 }
Michal Vasko086311b2016-01-08 09:53:11 +01002876 return NC_MSG_ERROR;
2877 }
2878
Michal Vasko131120a2018-05-29 15:44:02 +02002879 /* send RPC, store its message ID */
2880 r = nc_send_msg_io(session, timeout, data);
2881 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002882
Radek Krejcib4b19062018-02-07 16:33:06 +01002883 if (dofree) {
2884 lyd_free(data);
2885 }
Michal Vasko086311b2016-01-08 09:53:11 +01002886
Michal Vasko131120a2018-05-29 15:44:02 +02002887 if (r == NC_MSG_RPC) {
2888 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002889 }
Michal Vasko131120a2018-05-29 15:44:02 +02002890 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002891}
Michal Vaskode2946c2017-01-12 12:19:26 +01002892
2893API void
2894nc_client_session_set_not_strict(struct nc_session *session)
2895{
2896 if (session->side != NC_CLIENT) {
2897 ERRARG("session");
2898 return;
2899 }
2900
2901 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2902}