blob: d8606c7e76c411db8102b9a4767464132feb1340 [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 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001001 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
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));
Michal Vaskocb846632019-12-13 15:12:45 +01001350 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001351 }
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));
Michal Vaskocb846632019-12-13 15:12:45 +01001364 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001365 }
1366
Michal Vasko66032bc2019-01-22 15:03:12 +01001367 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1368 buf = malloc(INET6_ADDRSTRLEN);
1369 if (!buf) {
1370 ERRMEM;
Michal Vaskocb846632019-12-13 15:12:45 +01001371 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001372 }
1373 if (res->ai_family == AF_INET) {
1374 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1375 } else {
1376 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1377 }
1378 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
1379 ERR("Converting host to IP address failed (%s).", strerror(errno));
1380 free(buf);
Michal Vaskocb846632019-12-13 15:12:45 +01001381 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001382 }
1383
1384 *ip_host = buf;
1385 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001386 break;
1387 }
1388 freeaddrinfo(res_list);
1389
1390 } else {
1391 /* try to get a connection with the pending socket */
1392 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001393 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001394 }
1395
1396 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001397
1398error:
1399 if (sock != -1) {
1400 close(sock);
1401 }
1402 if (sock_pending) {
1403 *sock_pending = -1;
1404 }
1405 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001406}
1407
Michal Vasko086311b2016-01-08 09:53:11 +01001408static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001409get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001410{
Michal Vasko086311b2016-01-08 09:53:11 +01001411 char *ptr;
1412 const char *str_msgid;
1413 uint64_t cur_msgid;
1414 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001415 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001416 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1417
Michal Vasko086311b2016-01-08 09:53:11 +01001418 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001419 if (!msgid && session->opts.client.notifs) {
1420 cont = session->opts.client.notifs;
1421 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001422
Michal Vasko71ba2da2016-05-04 10:53:16 +02001423 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001424 free(cont);
1425
Michal Vasko71ba2da2016-05-04 10:53:16 +02001426 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001427 }
1428
1429 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001430 if (msgid && session->opts.client.replies) {
1431 cont = session->opts.client.replies;
1432 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001433
Michal Vasko71ba2da2016-05-04 10:53:16 +02001434 xml = cont->msg;
1435 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001436
Michal Vasko71ba2da2016-05-04 10:53:16 +02001437 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001438 }
1439
Michal Vasko71ba2da2016-05-04 10:53:16 +02001440 if (!msgtype) {
1441 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001442 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001443 }
Michal Vasko086311b2016-01-08 09:53:11 +01001444
1445 /* we read rpc-reply, want a notif */
1446 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001447 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001448 while (*cont_ptr) {
1449 cont_ptr = &((*cont_ptr)->next);
1450 }
1451 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001452 if (!*cont_ptr) {
1453 ERRMEM;
1454 lyxml_free(session->ctx, xml);
1455 return NC_MSG_ERROR;
1456 }
Michal Vasko086311b2016-01-08 09:53:11 +01001457 (*cont_ptr)->msg = xml;
1458 (*cont_ptr)->next = NULL;
1459 }
1460
1461 /* we read notif, want a rpc-reply */
1462 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001463 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001464 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001465 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001466 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001467 }
Michal Vasko086311b2016-01-08 09:53:11 +01001468
Michal Vasko2e6defd2016-10-07 15:48:15 +02001469 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001470 while (*cont_ptr) {
1471 cont_ptr = &((*cont_ptr)->next);
1472 }
1473 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001474 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001475 ERRMEM;
1476 lyxml_free(session->ctx, xml);
1477 return NC_MSG_ERROR;
1478 }
Michal Vasko086311b2016-01-08 09:53:11 +01001479 (*cont_ptr)->msg = xml;
1480 (*cont_ptr)->next = NULL;
1481 }
1482
Michal Vasko086311b2016-01-08 09:53:11 +01001483 switch (msgtype) {
1484 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001485 if (!msgid) {
1486 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001487 }
Michal Vasko086311b2016-01-08 09:53:11 +01001488 break;
1489
1490 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001491 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001492 /* check message-id */
1493 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1494 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001495 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001496 } else {
1497 cur_msgid = strtoul(str_msgid, &ptr, 10);
1498 if (cur_msgid != msgid) {
1499 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1500 session->id, str_msgid);
1501 msgtype = NC_MSG_REPLY_ERR_MSGID;
1502 }
1503 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001504 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001505 }
Michal Vasko086311b2016-01-08 09:53:11 +01001506 break;
1507
1508 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001509 ERR("Session %u: received another <hello> message.", 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 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001515 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001516 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001517 msgtype = NC_MSG_ERROR;
1518 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001519
1520 default:
1521 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1522 * NC_MSG_NONE is not returned by nc_read_msg()
1523 */
1524 break;
1525 }
1526
1527 return msgtype;
1528}
1529
1530/* cannot strictly fail, but does not need to fill any error parameter at all */
1531static void
1532parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1533{
1534 struct lyxml_elem *iter, *next, *info;
1535
1536 LY_TREE_FOR(xml->child, iter) {
1537 if (!iter->ns) {
1538 if (iter->content) {
1539 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1540 } else {
1541 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1542 }
1543 continue;
1544 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1545 if (iter->content) {
1546 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1547 iter->name, iter->content, iter->ns->value);
1548 } else {
1549 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1550 }
1551 continue;
1552 }
1553
1554 if (!strcmp(iter->name, "error-type")) {
1555 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1556 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1557 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1558 } else if (err->type) {
1559 WRN("<rpc-error> <error-type> duplicated.");
1560 } else {
1561 err->type = lydict_insert(ctx, iter->content, 0);
1562 }
1563 } else if (!strcmp(iter->name, "error-tag")) {
1564 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1565 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1566 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1567 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1568 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1569 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1570 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1571 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1572 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1573 && strcmp(iter->content, "malformed-message"))) {
1574 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1575 } else if (err->tag) {
1576 WRN("<rpc-error> <error-tag> duplicated.");
1577 } else {
1578 err->tag = lydict_insert(ctx, iter->content, 0);
1579 }
1580 } else if (!strcmp(iter->name, "error-severity")) {
1581 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1582 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1583 } else if (err->severity) {
1584 WRN("<rpc-error> <error-severity> duplicated.");
1585 } else {
1586 err->severity = lydict_insert(ctx, iter->content, 0);
1587 }
1588 } else if (!strcmp(iter->name, "error-app-tag")) {
1589 if (err->apptag) {
1590 WRN("<rpc-error> <error-app-tag> duplicated.");
1591 } else {
1592 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1593 }
1594 } else if (!strcmp(iter->name, "error-path")) {
1595 if (err->path) {
1596 WRN("<rpc-error> <error-path> duplicated.");
1597 } else {
1598 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1599 }
1600 } else if (!strcmp(iter->name, "error-message")) {
1601 if (err->message) {
1602 WRN("<rpc-error> <error-message> duplicated.");
1603 } else {
1604 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001605 if (err->message_lang) {
1606 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1607 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001608 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1609 }
1610 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1611 }
1612 } else if (!strcmp(iter->name, "error-info")) {
1613 LY_TREE_FOR_SAFE(iter->child, next, info) {
1614 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1615 if (!strcmp(info->name, "session-id")) {
1616 if (err->sid) {
1617 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1618 } else {
1619 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1620 }
Michal Vasko630485f2018-01-03 14:28:32 +01001621 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001622 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001623 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1624 if (!err->attr) {
1625 ERRMEM;
1626 return;
1627 }
Michal Vasko086311b2016-01-08 09:53:11 +01001628 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1629 } else if (!strcmp(info->name, "bad-element")) {
1630 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001631 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1632 if (!err->elem) {
1633 ERRMEM;
1634 return;
1635 }
Michal Vasko086311b2016-01-08 09:53:11 +01001636 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1637 } else if (!strcmp(info->name, "bad-namespace")) {
1638 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001639 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1640 if (!err->ns) {
1641 ERRMEM;
1642 return;
1643 }
Michal Vasko086311b2016-01-08 09:53:11 +01001644 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1645 } else {
1646 if (info->content) {
1647 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1648 info->name, info->content);
1649 } else {
1650 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1651 }
1652 }
1653 } else {
1654 lyxml_unlink(ctx, info);
1655 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001656 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1657 if (!err->other) {
1658 ERRMEM;
1659 return;
1660 }
Michal Vasko086311b2016-01-08 09:53:11 +01001661 err->other[err->other_count - 1] = info;
1662 }
1663 }
1664 } else {
1665 if (iter->content) {
1666 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1667 } else {
1668 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1669 }
1670 }
1671 }
1672}
1673
1674static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001675parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001676{
1677 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001678 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001679 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001680 struct nc_reply_data *data_rpl;
1681 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001682 struct nc_rpc_act_generic *rpc_gen;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001683 int i, data_parsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001684
Michal Vasko086311b2016-01-08 09:53:11 +01001685 /* rpc-error */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001686 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 +01001687 /* count and check elements */
1688 i = 0;
1689 LY_TREE_FOR(xml->child, iter) {
1690 if (strcmp(iter->name, "rpc-error")) {
1691 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1692 return NULL;
1693 } else if (!iter->ns) {
1694 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1695 return NULL;
1696 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1697 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1698 return NULL;
1699 }
1700 ++i;
1701 }
1702
1703 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001704 if (!error_rpl) {
1705 ERRMEM;
1706 return NULL;
1707 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001708 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001709 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001710 if (!error_rpl->err) {
1711 ERRMEM;
1712 free(error_rpl);
1713 return NULL;
1714 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001715 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001716 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001717 reply = (struct nc_reply *)error_rpl;
1718
1719 i = 0;
1720 LY_TREE_FOR(xml->child, iter) {
1721 parse_rpc_error(ctx, iter, error_rpl->err + i);
1722 ++i;
1723 }
1724
1725 /* ok */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001726 } 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 +01001727 if (xml->child->next) {
1728 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1729 return NULL;
1730 }
1731 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001732 if (!reply) {
1733 ERRMEM;
1734 return NULL;
1735 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001736 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001737
1738 /* some RPC output */
1739 } else {
1740 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001741 case NC_RPC_ACT_GENERIC:
1742 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001743
1744 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001745 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1746 if (!rpc_act) {
1747 ERR("Failed to duplicate a generic RPC/action.");
1748 return NULL;
1749 }
Michal Vasko086311b2016-01-08 09:53:11 +01001750 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001751 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 +02001752 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001753 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001754 return NULL;
1755 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001756 }
Michal Vasko086311b2016-01-08 09:53:11 +01001757 break;
1758
1759 case NC_RPC_GETCONFIG:
1760 case NC_RPC_GET:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001761 case NC_RPC_GETDATA:
Michal Vasko6b281ff2019-02-14 10:15:19 +01001762 /* we should definitely have received at least an empty "data" element even on empty reply, but fine */
1763 if (!xml->child || !xml->child->child) {
Michal Vasko13ed2942016-02-29 16:21:00 +01001764 /* we did not receive any data */
1765 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001766 if (!data_rpl) {
1767 ERRMEM;
1768 return NULL;
1769 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001770 data_rpl->type = NC_RPL_DATA;
1771 data_rpl->data = NULL;
1772 return (struct nc_reply *)data_rpl;
1773 }
1774
Michal Vasko086311b2016-01-08 09:53:11 +01001775 /* special treatment */
Michal Vaskof45e0c12018-11-28 08:38:23 +01001776 ly_errno = 0;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001777 data = lyd_parse_xml(ctx, &xml->child->child,
1778 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001779 | parseroptions);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001780 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001781 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1782 return NULL;
1783 }
Michal Vaskof45e0c12018-11-28 08:38:23 +01001784 data_parsed = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001785 break;
1786
1787 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001788 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001789 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001790 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001791 return NULL;
1792 }
1793 break;
1794
1795 case NC_RPC_EDIT:
1796 case NC_RPC_COPY:
1797 case NC_RPC_DELETE:
1798 case NC_RPC_LOCK:
1799 case NC_RPC_UNLOCK:
1800 case NC_RPC_KILL:
1801 case NC_RPC_COMMIT:
1802 case NC_RPC_DISCARD:
1803 case NC_RPC_CANCEL:
1804 case NC_RPC_VALIDATE:
1805 case NC_RPC_SUBSCRIBE:
Michal Vaskoc1171a42019-11-05 12:06:46 +01001806 case NC_RPC_EDITDATA:
Michal Vasko086311b2016-01-08 09:53:11 +01001807 /* there is no output defined */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001808 ERR("Unexpected data reply (root elem \"%s\").", xml->child ? xml->child->name : NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001809 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001810 default:
1811 ERRINT;
1812 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001813 }
1814
1815 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001816 if (!data_rpl) {
1817 ERRMEM;
1818 return NULL;
1819 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001820 data_rpl->type = NC_RPL_DATA;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001821
1822 ly_errno = 0;
1823 if (!data_parsed) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001824 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001825 rpc_act, NULL);
Michal Vasko6b281ff2019-02-14 10:15:19 +01001826 if (!ly_errno && !data_rpl->data->child) {
1827 ERR("An empty data <rpc-reply>.");
1828 lyd_free_withsiblings(rpc_act);
1829 lyd_free(data_rpl->data);
1830 free(data_rpl);
1831 return NULL;
1832 }
Michal Vasko086311b2016-01-08 09:53:11 +01001833 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01001834 /* <get>, <get-config>, <get-data> */
Michal Vasko086311b2016-01-08 09:53:11 +01001835 data_rpl->data = data;
1836 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001837 lyd_free_withsiblings(rpc_act);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001838 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001839 ERR("Failed to parse <rpc-reply>.");
1840 free(data_rpl);
1841 return NULL;
1842 }
1843 reply = (struct nc_reply *)data_rpl;
1844 }
1845
1846 return reply;
1847}
1848
Radek Krejci53691be2016-02-22 13:58:37 +01001849#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001850
Michal Vasko3031aae2016-01-27 16:07:18 +01001851int
1852nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1853{
1854 int sock;
1855
Michal Vasko45e53ae2016-04-07 11:46:03 +02001856 if (!address) {
1857 ERRARG("address");
1858 return -1;
1859 } else if (!port) {
1860 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001861 return -1;
1862 }
1863
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001864 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001865 if (sock == -1) {
1866 return -1;
1867 }
1868
1869 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001870 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1871 if (!client_opts.ch_binds) {
1872 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001873 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001874 return -1;
1875 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001876
Michal Vasko2e6defd2016-10-07 15:48:15 +02001877 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1878 if (!client_opts.ch_bind_ti) {
1879 ERRMEM;
1880 close(sock);
1881 return -1;
1882 }
1883 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1884
Michal Vasko3031aae2016-01-27 16:07:18 +01001885 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001886 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1887 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001888 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001889 return -1;
1890 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001891 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1892 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001893 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001894
1895 return 0;
1896}
1897
1898int
1899nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1900{
1901 uint32_t i;
1902 int ret = -1;
1903
1904 if (!address && !port && !ti) {
1905 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1906 close(client_opts.ch_binds[i].sock);
1907 free((char *)client_opts.ch_binds[i].address);
1908
1909 ret = 0;
1910 }
1911 free(client_opts.ch_binds);
1912 client_opts.ch_binds = NULL;
1913 client_opts.ch_bind_count = 0;
1914 } else {
1915 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1916 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1917 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001918 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001919 close(client_opts.ch_binds[i].sock);
1920 free((char *)client_opts.ch_binds[i].address);
1921
1922 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001923 if (!client_opts.ch_bind_count) {
1924 free(client_opts.ch_binds);
1925 client_opts.ch_binds = NULL;
1926 } else if (i < client_opts.ch_bind_count) {
1927 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1928 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1929 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001930
1931 ret = 0;
1932 }
1933 }
1934 }
1935
1936 return ret;
1937}
1938
1939API int
1940nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1941{
1942 int sock;
1943 char *host = NULL;
1944 uint16_t port, idx;
1945
Michal Vasko45e53ae2016-04-07 11:46:03 +02001946 if (!client_opts.ch_binds) {
1947 ERRINIT;
1948 return -1;
1949 } else if (!session) {
1950 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001951 return -1;
1952 }
1953
1954 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1955
Michal Vasko50456e82016-02-02 12:16:08 +01001956 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001957 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001958 return sock;
1959 }
1960
Radek Krejci53691be2016-02-22 13:58:37 +01001961#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001962 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001963 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001964 } else
1965#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001966#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001967 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001968 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001969 } else
1970#endif
1971 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001972 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001973 *session = NULL;
1974 }
1975
1976 free(host);
1977
1978 if (!(*session)) {
1979 return -1;
1980 }
1981
1982 return 1;
1983}
1984
Radek Krejci53691be2016-02-22 13:58:37 +01001985#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001986
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001987API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001988nc_session_get_cpblts(const struct nc_session *session)
1989{
1990 if (!session) {
1991 ERRARG("session");
1992 return NULL;
1993 }
1994
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001995 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001996}
1997
1998API const char *
1999nc_session_cpblt(const struct nc_session *session, const char *capab)
2000{
2001 int i, len;
2002
2003 if (!session) {
2004 ERRARG("session");
2005 return NULL;
2006 } else if (!capab) {
2007 ERRARG("capab");
2008 return NULL;
2009 }
2010
2011 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002012 for (i = 0; session->opts.client.cpblts[i]; ++i) {
2013 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
2014 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02002015 }
2016 }
2017
2018 return NULL;
2019}
2020
Michal Vasko9cd26a82016-05-31 08:58:48 +02002021API int
2022nc_session_ntf_thread_running(const struct nc_session *session)
2023{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002024 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02002025 ERRARG("session");
2026 return 0;
2027 }
2028
Michal Vasko2e6defd2016-10-07 15:48:15 +02002029 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02002030}
2031
Michal Vaskob7558c52016-02-26 15:04:19 +01002032API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002033nc_client_init(void)
2034{
2035 nc_init();
2036}
2037
2038API void
Michal Vaskob7558c52016-02-26 15:04:19 +01002039nc_client_destroy(void)
2040{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02002041 nc_client_set_schema_searchpath(NULL);
2042#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
2043 nc_client_ch_del_bind(NULL, 0, 0);
2044#endif
2045#ifdef NC_ENABLED_SSH
2046 nc_client_ssh_destroy_opts();
2047#endif
2048#ifdef NC_ENABLED_TLS
2049 nc_client_tls_destroy_opts();
2050#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002051 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01002052}
2053
Michal Vasko086311b2016-01-08 09:53:11 +01002054API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002055nc_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 +01002056{
Michal Vasko5a2c7162020-01-30 11:24:17 +01002057 struct lyxml_elem *xml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01002058 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2059
Michal Vasko45e53ae2016-04-07 11:46:03 +02002060 if (!session) {
2061 ERRARG("session");
2062 return NC_MSG_ERROR;
2063 } else if (!rpc) {
2064 ERRARG("rpc");
2065 return NC_MSG_ERROR;
Michal Vasko5a2c7162020-01-30 11:24:17 +01002066 } else if (!msgid) {
2067 ERRARG("msgid");
2068 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02002069 } else if (!reply) {
2070 ERRARG("reply");
2071 return NC_MSG_ERROR;
2072 } else if (parseroptions & LYD_OPT_TYPEMASK) {
2073 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01002074 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002075 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01002076 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002077 return NC_MSG_ERROR;
2078 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002079 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01002080 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
2081 parseroptions &= LYD_OPT_STRICT;
2082 }
Michal Vasko41adf392017-01-17 10:39:04 +01002083 /* no mechanism to check external dependencies is provided */
2084 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01002085 *reply = NULL;
2086
2087 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002088
Michal Vasko5a2c7162020-01-30 11:24:17 +01002089 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01002090 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko5a2c7162020-01-30 11:24:17 +01002091 lyxml_free_withsiblings(session->ctx, xml);
2092 xml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01002093 if (!(*reply)) {
2094 return NC_MSG_ERROR;
2095 }
2096 }
Michal Vasko5a2c7162020-01-30 11:24:17 +01002097 assert(!xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002098
2099 return msgtype;
2100}
2101
2102API NC_MSG_TYPE
2103nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
2104{
2105 struct lyxml_elem *xml, *ev_time;
2106 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2107
Michal Vasko45e53ae2016-04-07 11:46:03 +02002108 if (!session) {
2109 ERRARG("session");
2110 return NC_MSG_ERROR;
2111 } else if (!notif) {
2112 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01002113 return NC_MSG_ERROR;
2114 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002115 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002116 return NC_MSG_ERROR;
2117 }
2118
2119 msgtype = get_msg(session, timeout, 0, &xml);
2120
2121 if (msgtype == NC_MSG_NOTIF) {
2122 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002123 if (!*notif) {
2124 ERRMEM;
2125 lyxml_free(session->ctx, xml);
2126 return NC_MSG_ERROR;
2127 }
Michal Vasko086311b2016-01-08 09:53:11 +01002128
2129 /* eventTime */
2130 LY_TREE_FOR(xml->child, ev_time) {
2131 if (!strcmp(ev_time->name, "eventTime")) {
2132 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
2133 /* lyd_parse does not know this element */
2134 lyxml_free(session->ctx, ev_time);
2135 break;
2136 }
2137 }
2138 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01002139 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002140 goto fail;
2141 }
2142
2143 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01002144 (*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 +01002145 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002146 lyxml_free(session->ctx, xml);
2147 xml = NULL;
2148 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01002149 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002150 goto fail;
2151 }
2152 }
2153
2154 return msgtype;
2155
2156fail:
2157 lydict_remove(session->ctx, (*notif)->datetime);
2158 lyd_free((*notif)->tree);
2159 free(*notif);
2160 *notif = NULL;
2161 lyxml_free(session->ctx, xml);
2162
2163 return NC_MSG_ERROR;
2164}
2165
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002166static void *
2167nc_recv_notif_thread(void *arg)
2168{
2169 struct nc_ntf_thread_arg *ntarg;
2170 struct nc_session *session;
2171 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
2172 struct nc_notif *notif;
2173 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002174 pthread_t *ntf_tid;
2175
2176 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002177
2178 ntarg = (struct nc_ntf_thread_arg *)arg;
2179 session = ntarg->session;
2180 notif_clb = ntarg->notif_clb;
2181 free(ntarg);
2182
Michal Vasko131120a2018-05-29 15:44:02 +02002183 /* remember our allocated tid, we will be freeing it */
2184 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
2185
Michal Vasko2e6defd2016-10-07 15:48:15 +02002186 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002187 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002188 if (msgtype == NC_MSG_NOTIF) {
2189 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002190 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2191 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2192 nc_notif_free(notif);
2193 break;
2194 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002195 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002196 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002197 /* quit this thread once the session is broken */
2198 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002199 }
2200
2201 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2202 }
2203
Michal Vasko0651c902016-05-19 15:55:42 +02002204 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002205 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02002206 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002207 return NULL;
2208}
2209
2210API int
2211nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2212{
2213 struct nc_ntf_thread_arg *ntarg;
2214 int ret;
2215
Michal Vasko45e53ae2016-04-07 11:46:03 +02002216 if (!session) {
2217 ERRARG("session");
2218 return -1;
2219 } else if (!notif_clb) {
2220 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002221 return -1;
2222 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2223 ERR("Session %u: invalid session to receive Notifications.", session->id);
2224 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002225 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002226 ERR("Session %u: separate notification thread is already running.", session->id);
2227 return -1;
2228 }
2229
2230 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002231 if (!ntarg) {
2232 ERRMEM;
2233 return -1;
2234 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002235 ntarg->session = session;
2236 ntarg->notif_clb = notif_clb;
2237
2238 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002239 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2240 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002241 ERRMEM;
2242 free(ntarg);
2243 return -1;
2244 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002245
Michal Vasko2e6defd2016-10-07 15:48:15 +02002246 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002247 if (ret) {
2248 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2249 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002250 free((pthread_t *)session->opts.client.ntf_tid);
2251 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002252 return -1;
2253 }
2254
2255 return 0;
2256}
2257
Michal Vasko086311b2016-01-08 09:53:11 +01002258API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002259nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002260{
2261 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002262 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002263 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002264 struct nc_rpc_getconfig *rpc_gc;
2265 struct nc_rpc_edit *rpc_e;
2266 struct nc_rpc_copy *rpc_cp;
2267 struct nc_rpc_delete *rpc_del;
2268 struct nc_rpc_lock *rpc_lock;
2269 struct nc_rpc_get *rpc_g;
2270 struct nc_rpc_kill *rpc_k;
2271 struct nc_rpc_commit *rpc_com;
2272 struct nc_rpc_cancel *rpc_can;
2273 struct nc_rpc_validate *rpc_val;
2274 struct nc_rpc_getschema *rpc_gs;
2275 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002276 struct nc_rpc_getdata *rpc_getd;
2277 struct nc_rpc_editdata *rpc_editd;
Michal Vasko086311b2016-01-08 09:53:11 +01002278 struct lyd_node *data, *node;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002279 const struct lys_module *mod = NULL, *ietfncwd;
2280 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002281 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002282 uint64_t cur_msgid;
2283
Michal Vasko45e53ae2016-04-07 11:46:03 +02002284 if (!session) {
2285 ERRARG("session");
2286 return NC_MSG_ERROR;
2287 } else if (!rpc) {
2288 ERRARG("rpc");
2289 return NC_MSG_ERROR;
2290 } else if (!msgid) {
2291 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002292 return NC_MSG_ERROR;
2293 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002294 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002295 return NC_MSG_ERROR;
2296 }
2297
Michal Vaskoc1171a42019-11-05 12:06:46 +01002298 switch (rpc->type) {
2299 case NC_RPC_ACT_GENERIC:
2300 /* checked when parsing */
2301 break;
2302 case NC_RPC_GETCONFIG:
2303 case NC_RPC_EDIT:
2304 case NC_RPC_COPY:
2305 case NC_RPC_DELETE:
2306 case NC_RPC_LOCK:
2307 case NC_RPC_UNLOCK:
2308 case NC_RPC_GET:
2309 case NC_RPC_KILL:
2310 case NC_RPC_COMMIT:
2311 case NC_RPC_DISCARD:
2312 case NC_RPC_CANCEL:
2313 case NC_RPC_VALIDATE:
2314 mod = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
2315 if (!mod) {
Michal Vasko086cd572017-01-12 12:19:05 +01002316 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002317 return NC_MSG_ERROR;
2318 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002319 break;
2320 case NC_RPC_GETSCHEMA:
2321 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
2322 if (!mod) {
2323 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
2324 return NC_MSG_ERROR;
2325 }
2326 break;
2327 case NC_RPC_SUBSCRIBE:
2328 mod = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
2329 if (!mod) {
2330 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
2331 return NC_MSG_ERROR;
2332 }
2333 break;
2334 case NC_RPC_GETDATA:
2335 case NC_RPC_EDITDATA:
2336 mod = ly_ctx_get_module(session->ctx, "ietf-netconf-nmda", NULL, 1);
2337 if (!mod) {
2338 ERR("Session %u: missing \"ietf-netconf-nmda\" schema in the context.", session->id);
2339 return NC_MSG_ERROR;
2340 }
2341 break;
2342 case NC_RPC_UNKNOWN:
2343 ERRINT;
2344 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002345 }
2346
2347 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002348 case NC_RPC_ACT_GENERIC:
2349 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002350
2351 if (rpc_gen->has_data) {
2352 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002353 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002354 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002355 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 +01002356 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002357 if (!data) {
2358 return NC_MSG_ERROR;
2359 }
Michal Vasko086311b2016-01-08 09:53:11 +01002360 }
2361 break;
2362
2363 case NC_RPC_GETCONFIG:
2364 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2365
Michal Vaskoc1171a42019-11-05 12:06:46 +01002366 data = lyd_new(NULL, mod, "get-config");
2367 node = lyd_new(data, mod, "source");
2368 node = lyd_new_leaf(node, mod, ncds2str[rpc_gc->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002369 if (!node) {
2370 lyd_free(data);
2371 return NC_MSG_ERROR;
2372 }
2373 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002374 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002375 node = lyd_new_anydata(data, mod, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002376 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002377 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002378 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002379 lyd_insert_attr(node, NULL, "type", "xpath");
2380 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002381 }
2382 if (!node) {
2383 lyd_free(data);
2384 return NC_MSG_ERROR;
2385 }
2386 }
2387
2388 if (rpc_gc->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002389 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002390 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002391 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2392 lyd_free(data);
2393 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002394 }
2395 switch (rpc_gc->wd_mode) {
2396 case NC_WD_UNKNOWN:
2397 /* cannot get here */
2398 break;
2399 case NC_WD_ALL:
2400 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2401 break;
2402 case NC_WD_ALL_TAG:
2403 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2404 break;
2405 case NC_WD_TRIM:
2406 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2407 break;
2408 case NC_WD_EXPLICIT:
2409 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2410 break;
2411 }
2412 if (!node) {
2413 lyd_free(data);
2414 return NC_MSG_ERROR;
2415 }
2416 }
2417 break;
2418
2419 case NC_RPC_EDIT:
2420 rpc_e = (struct nc_rpc_edit *)rpc;
2421
Michal Vaskoc1171a42019-11-05 12:06:46 +01002422 data = lyd_new(NULL, mod, "edit-config");
2423 node = lyd_new(data, mod, "target");
2424 node = lyd_new_leaf(node, mod, ncds2str[rpc_e->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002425 if (!node) {
2426 lyd_free(data);
2427 return NC_MSG_ERROR;
2428 }
2429
2430 if (rpc_e->default_op) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002431 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
Michal Vasko086311b2016-01-08 09:53:11 +01002432 if (!node) {
2433 lyd_free(data);
2434 return NC_MSG_ERROR;
2435 }
2436 }
2437
2438 if (rpc_e->test_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002439 node = lyd_new_leaf(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002440 if (!node) {
2441 lyd_free(data);
2442 return NC_MSG_ERROR;
2443 }
2444 }
2445
2446 if (rpc_e->error_opt) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002447 node = lyd_new_leaf(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
Michal Vasko086311b2016-01-08 09:53:11 +01002448 if (!node) {
2449 lyd_free(data);
2450 return NC_MSG_ERROR;
2451 }
2452 }
2453
Michal Vasko7793bc62016-09-16 11:58:41 +02002454 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002455 node = lyd_new_anydata(data, mod, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002456 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002457 node = lyd_new_leaf(data, mod, "url", rpc_e->edit_cont);
Michal Vasko086311b2016-01-08 09:53:11 +01002458 }
2459 if (!node) {
2460 lyd_free(data);
2461 return NC_MSG_ERROR;
2462 }
2463 break;
2464
2465 case NC_RPC_COPY:
2466 rpc_cp = (struct nc_rpc_copy *)rpc;
2467
Michal Vaskoc1171a42019-11-05 12:06:46 +01002468 data = lyd_new(NULL, mod, "copy-config");
2469 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002470 if (rpc_cp->url_trg) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002471 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_trg);
Michal Vasko086311b2016-01-08 09:53:11 +01002472 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002473 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002474 }
2475 if (!node) {
2476 lyd_free(data);
2477 return NC_MSG_ERROR;
2478 }
2479
Michal Vaskoc1171a42019-11-05 12:06:46 +01002480 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002481 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002482 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002483 node = lyd_new_anydata(node, mod, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002484 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002485 node = lyd_new_leaf(node, mod, "url", rpc_cp->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002486 }
2487 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002488 node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002489 }
2490 if (!node) {
2491 lyd_free(data);
2492 return NC_MSG_ERROR;
2493 }
2494
2495 if (rpc_cp->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002496 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002497 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002498 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2499 lyd_free(data);
2500 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002501 }
2502 switch (rpc_cp->wd_mode) {
2503 case NC_WD_UNKNOWN:
2504 /* cannot get here */
2505 break;
2506 case NC_WD_ALL:
2507 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2508 break;
2509 case NC_WD_ALL_TAG:
2510 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2511 break;
2512 case NC_WD_TRIM:
2513 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2514 break;
2515 case NC_WD_EXPLICIT:
2516 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2517 break;
2518 }
2519 if (!node) {
2520 lyd_free(data);
2521 return NC_MSG_ERROR;
2522 }
2523 }
2524 break;
2525
2526 case NC_RPC_DELETE:
2527 rpc_del = (struct nc_rpc_delete *)rpc;
2528
Michal Vaskoc1171a42019-11-05 12:06:46 +01002529 data = lyd_new(NULL, mod, "delete-config");
2530 node = lyd_new(data, mod, "target");
Michal Vasko086311b2016-01-08 09:53:11 +01002531 if (rpc_del->url) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002532 node = lyd_new_leaf(node, mod, "url", rpc_del->url);
Michal Vasko086311b2016-01-08 09:53:11 +01002533 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002534 node = lyd_new_leaf(node, mod, ncds2str[rpc_del->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002535 }
2536 if (!node) {
2537 lyd_free(data);
2538 return NC_MSG_ERROR;
2539 }
2540 break;
2541
2542 case NC_RPC_LOCK:
2543 rpc_lock = (struct nc_rpc_lock *)rpc;
2544
Michal Vaskoc1171a42019-11-05 12:06:46 +01002545 data = lyd_new(NULL, mod, "lock");
2546 node = lyd_new(data, mod, "target");
2547 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002548 if (!node) {
2549 lyd_free(data);
2550 return NC_MSG_ERROR;
2551 }
2552 break;
2553
2554 case NC_RPC_UNLOCK:
2555 rpc_lock = (struct nc_rpc_lock *)rpc;
2556
Michal Vaskoc1171a42019-11-05 12:06:46 +01002557 data = lyd_new(NULL, mod, "unlock");
2558 node = lyd_new(data, mod, "target");
2559 node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002560 if (!node) {
2561 lyd_free(data);
2562 return NC_MSG_ERROR;
2563 }
2564 break;
2565
2566 case NC_RPC_GET:
2567 rpc_g = (struct nc_rpc_get *)rpc;
2568
Michal Vaskoc1171a42019-11-05 12:06:46 +01002569 data = lyd_new(NULL, mod, "get");
Michal Vasko086311b2016-01-08 09:53:11 +01002570 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002571 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002572 node = lyd_new_anydata(data, mod, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002573 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002574 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002575 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002576 lyd_insert_attr(node, NULL, "type", "xpath");
2577 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002578 }
2579 if (!node) {
2580 lyd_free(data);
2581 return NC_MSG_ERROR;
2582 }
2583 }
2584
2585 if (rpc_g->wd_mode) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002586 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002587 if (!ietfncwd) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002588 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2589 lyd_free(data);
2590 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002591 }
2592 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002593 case NC_WD_ALL:
2594 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2595 break;
2596 case NC_WD_ALL_TAG:
2597 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2598 break;
2599 case NC_WD_TRIM:
2600 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2601 break;
2602 case NC_WD_EXPLICIT:
2603 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2604 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002605 default:
2606 /* cannot get here */
2607 node = NULL;
2608 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002609 }
2610 if (!node) {
2611 lyd_free(data);
2612 return NC_MSG_ERROR;
2613 }
2614 }
2615 break;
2616
2617 case NC_RPC_KILL:
2618 rpc_k = (struct nc_rpc_kill *)rpc;
2619
Michal Vaskoc1171a42019-11-05 12:06:46 +01002620 data = lyd_new(NULL, mod, "kill-session");
Michal Vasko086311b2016-01-08 09:53:11 +01002621 sprintf(str, "%u", rpc_k->sid);
Michal Vaskoc1171a42019-11-05 12:06:46 +01002622 lyd_new_leaf(data, mod, "session-id", str);
Michal Vasko086311b2016-01-08 09:53:11 +01002623 break;
2624
2625 case NC_RPC_COMMIT:
2626 rpc_com = (struct nc_rpc_commit *)rpc;
2627
Michal Vaskoc1171a42019-11-05 12:06:46 +01002628 data = lyd_new(NULL, mod, "commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002629 if (rpc_com->confirmed) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002630 lyd_new_leaf(data, mod, "confirmed", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002631 }
2632
2633 if (rpc_com->confirm_timeout) {
2634 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vaskoc1171a42019-11-05 12:06:46 +01002635 lyd_new_leaf(data, mod, "confirm-timeout", str);
Michal Vasko086311b2016-01-08 09:53:11 +01002636 }
2637
2638 if (rpc_com->persist) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002639 node = lyd_new_leaf(data, mod, "persist", rpc_com->persist);
Michal Vasko086311b2016-01-08 09:53:11 +01002640 if (!node) {
2641 lyd_free(data);
2642 return NC_MSG_ERROR;
2643 }
2644 }
2645
2646 if (rpc_com->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002647 node = lyd_new_leaf(data, mod, "persist-id", rpc_com->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002648 if (!node) {
2649 lyd_free(data);
2650 return NC_MSG_ERROR;
2651 }
2652 }
2653 break;
2654
2655 case NC_RPC_DISCARD:
Michal Vaskoc1171a42019-11-05 12:06:46 +01002656 data = lyd_new(NULL, mod, "discard-changes");
Michal Vasko086311b2016-01-08 09:53:11 +01002657 break;
2658
2659 case NC_RPC_CANCEL:
2660 rpc_can = (struct nc_rpc_cancel *)rpc;
2661
Michal Vaskoc1171a42019-11-05 12:06:46 +01002662 data = lyd_new(NULL, mod, "cancel-commit");
Michal Vasko086311b2016-01-08 09:53:11 +01002663 if (rpc_can->persist_id) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002664 node = lyd_new_leaf(data, mod, "persist-id", rpc_can->persist_id);
Michal Vasko086311b2016-01-08 09:53:11 +01002665 if (!node) {
2666 lyd_free(data);
2667 return NC_MSG_ERROR;
2668 }
2669 }
2670 break;
2671
2672 case NC_RPC_VALIDATE:
2673 rpc_val = (struct nc_rpc_validate *)rpc;
2674
Michal Vaskoc1171a42019-11-05 12:06:46 +01002675 data = lyd_new(NULL, mod, "validate");
2676 node = lyd_new(data, mod, "source");
Michal Vasko086311b2016-01-08 09:53:11 +01002677 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002678 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002679 node = lyd_new_anydata(node, mod, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002680 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002681 node = lyd_new_leaf(node, mod, "url", rpc_val->url_config_src);
Michal Vasko086311b2016-01-08 09:53:11 +01002682 }
2683 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002684 node = lyd_new_leaf(node, mod, ncds2str[rpc_val->source], NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002685 }
2686 if (!node) {
2687 lyd_free(data);
2688 return NC_MSG_ERROR;
2689 }
2690 break;
2691
2692 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002693 rpc_gs = (struct nc_rpc_getschema *)rpc;
2694
Michal Vaskoc1171a42019-11-05 12:06:46 +01002695 data = lyd_new(NULL, mod, "get-schema");
2696 node = lyd_new_leaf(data, mod, "identifier", rpc_gs->identifier);
Michal Vasko086311b2016-01-08 09:53:11 +01002697 if (!node) {
2698 lyd_free(data);
2699 return NC_MSG_ERROR;
2700 }
2701 if (rpc_gs->version) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002702 node = lyd_new_leaf(data, mod, "version", rpc_gs->version);
Michal Vasko086311b2016-01-08 09:53:11 +01002703 if (!node) {
2704 lyd_free(data);
2705 return NC_MSG_ERROR;
2706 }
2707 }
2708 if (rpc_gs->format) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002709 node = lyd_new_leaf(data, mod, "format", rpc_gs->format);
Michal Vasko086311b2016-01-08 09:53:11 +01002710 if (!node) {
2711 lyd_free(data);
2712 return NC_MSG_ERROR;
2713 }
2714 }
2715 break;
2716
2717 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002718 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2719
Michal Vaskoc1171a42019-11-05 12:06:46 +01002720 data = lyd_new(NULL, mod, "create-subscription");
Michal Vasko086311b2016-01-08 09:53:11 +01002721 if (rpc_sub->stream) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002722 node = lyd_new_leaf(data, mod, "stream", rpc_sub->stream);
Michal Vasko086311b2016-01-08 09:53:11 +01002723 if (!node) {
2724 lyd_free(data);
2725 return NC_MSG_ERROR;
2726 }
2727 }
2728
2729 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002730 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002731 node = lyd_new_anydata(data, mod, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002732 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002733 } else {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002734 node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002735 lyd_insert_attr(node, NULL, "type", "xpath");
2736 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002737 }
2738 if (!node) {
2739 lyd_free(data);
2740 return NC_MSG_ERROR;
2741 }
2742 }
2743
2744 if (rpc_sub->start) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002745 node = lyd_new_leaf(data, mod, "startTime", rpc_sub->start);
Michal Vasko086311b2016-01-08 09:53:11 +01002746 if (!node) {
2747 lyd_free(data);
2748 return NC_MSG_ERROR;
2749 }
2750 }
2751
2752 if (rpc_sub->stop) {
Michal Vaskoc1171a42019-11-05 12:06:46 +01002753 node = lyd_new_leaf(data, mod, "stopTime", rpc_sub->stop);
Michal Vasko086311b2016-01-08 09:53:11 +01002754 if (!node) {
2755 lyd_free(data);
2756 return NC_MSG_ERROR;
2757 }
2758 }
2759 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002760
2761 case NC_RPC_GETDATA:
2762 rpc_getd = (struct nc_rpc_getdata *)rpc;
2763
2764 data = lyd_new(NULL, mod, "get-data");
2765 node = lyd_new_leaf(data, mod, "datastore", rpc_getd->datastore);
2766 if (!node) {
2767 lyd_free(data);
2768 return NC_MSG_ERROR;
2769 }
2770 if (rpc_getd->filter) {
2771 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
2772 node = lyd_new_anydata(data, mod, "subtree-filter", rpc_getd->filter, LYD_ANYDATA_SXML);
2773 } else {
2774 node = lyd_new_leaf(data, mod, "xpath-filter", rpc_getd->filter);
2775 }
2776 if (!node) {
2777 lyd_free(data);
2778 return NC_MSG_ERROR;
2779 }
2780 }
2781 if (rpc_getd->config_filter) {
2782 node = lyd_new_leaf(data, mod, "config-filter", rpc_getd->config_filter);
2783 if (!node) {
2784 lyd_free(data);
2785 return NC_MSG_ERROR;
2786 }
2787 }
2788 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
2789 node = lyd_new_leaf(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" : "origin-filter",
2790 rpc_getd->origin_filter[i]);
2791 if (!node) {
2792 lyd_free(data);
2793 return NC_MSG_ERROR;
2794 }
2795 }
2796 if (rpc_getd->max_depth) {
2797 sprintf(str, "%u", rpc_getd->max_depth);
2798 node = lyd_new_leaf(data, mod, "max-depth", str);
2799 if (!node) {
2800 lyd_free(data);
2801 return NC_MSG_ERROR;
2802 }
2803 }
2804 if (rpc_getd->with_origin) {
2805 node = lyd_new_leaf(data, mod, "with-origin", NULL);
2806 if (!node) {
2807 lyd_free(data);
2808 return NC_MSG_ERROR;
2809 }
2810 }
2811
2812 if (rpc_getd->wd_mode) {
2813 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
2814 if (!ietfncwd) {
2815 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
2816 lyd_free(data);
2817 return NC_MSG_ERROR;
2818 }
2819 switch (rpc_getd->wd_mode) {
2820 case NC_WD_UNKNOWN:
2821 /* cannot get here */
2822 break;
2823 case NC_WD_ALL:
2824 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2825 break;
2826 case NC_WD_ALL_TAG:
2827 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2828 break;
2829 case NC_WD_TRIM:
2830 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2831 break;
2832 case NC_WD_EXPLICIT:
2833 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2834 break;
2835 }
2836 if (!node) {
2837 lyd_free(data);
2838 return NC_MSG_ERROR;
2839 }
2840 }
2841 break;
2842
2843 case NC_RPC_EDITDATA:
2844 rpc_editd = (struct nc_rpc_editdata *)rpc;
2845
2846 data = lyd_new(NULL, mod, "edit-data");
2847 node = lyd_new_leaf(data, mod, "datastore", rpc_editd->datastore);
2848 if (!node) {
2849 lyd_free(data);
2850 return NC_MSG_ERROR;
2851 }
2852
2853 if (rpc_editd->default_op) {
2854 node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op]);
2855 if (!node) {
2856 lyd_free(data);
2857 return NC_MSG_ERROR;
2858 }
2859 }
2860
2861 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
2862 node = lyd_new_anydata(data, mod, "config", rpc_editd->edit_cont, LYD_ANYDATA_SXML);
2863 } else {
2864 node = lyd_new_leaf(data, mod, "url", rpc_editd->edit_cont);
2865 }
2866 if (!node) {
2867 lyd_free(data);
2868 return NC_MSG_ERROR;
2869 }
2870 break;
2871
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002872 default:
2873 ERRINT;
2874 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002875 }
2876
Michal Vasko131120a2018-05-29 15:44:02 +02002877 if (!data) {
2878 /* error was already printed */
2879 return NC_MSG_ERROR;
2880 }
2881
Michal Vasko41adf392017-01-17 10:39:04 +01002882 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2883 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002884 if (dofree) {
2885 lyd_free(data);
2886 }
Michal Vasko086311b2016-01-08 09:53:11 +01002887 return NC_MSG_ERROR;
2888 }
2889
Michal Vasko131120a2018-05-29 15:44:02 +02002890 /* send RPC, store its message ID */
2891 r = nc_send_msg_io(session, timeout, data);
2892 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002893
Radek Krejcib4b19062018-02-07 16:33:06 +01002894 if (dofree) {
2895 lyd_free(data);
2896 }
Michal Vasko086311b2016-01-08 09:53:11 +01002897
Michal Vasko131120a2018-05-29 15:44:02 +02002898 if (r == NC_MSG_RPC) {
2899 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002900 }
Michal Vasko131120a2018-05-29 15:44:02 +02002901 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002902}
Michal Vaskode2946c2017-01-12 12:19:26 +01002903
2904API void
2905nc_client_session_set_not_strict(struct nc_session *session)
2906{
2907 if (session->side != NC_CLIENT) {
2908 ERRARG("session");
2909 return;
2910 }
2911
2912 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2913}