blob: d7523c98f6e58d52722a1282e24a1aa472db0b50 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
Radek Krejcifd5b6682017-06-13 15:52:53 +020015#define _GNU_SOURCE
Michal Vasko086311b2016-01-08 09:53:11 +010016#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010020#include <netinet/in.h>
Michal Vasko83ad17e2019-01-30 10:11:37 +010021#include <netinet/tcp.h>
Michal Vasko086311b2016-01-08 09:53:11 +010022#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/socket.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020026#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020028#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010029#include <sys/types.h>
30#include <unistd.h>
31#include <arpa/inet.h>
32#include <poll.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020033#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010034
35#include <libyang/libyang.h>
36
Michal Vasko086311b2016-01-08 09:53:11 +010037#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010038#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010039#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010040
Michal Vasko80ef5d22016-01-18 09:21:02 +010041static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
42
Radek Krejci62aa0642017-05-25 16:33:49 +020043#ifdef NC_ENABLED_SSH
44int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
45char *sshauth_password(const char *username, const char *hostname, void *priv);
46char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
47char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
48#endif /* NC_ENABLED_SSH */
49
50static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
51static pthread_key_t nc_client_context_key;
52#ifdef __linux__
53static struct nc_client_context context_main = {
Michal Vaskoe49a15f2019-05-27 14:18:36 +020054 .opts.ka = {
55 .enabled = 1,
56 .idle_time = 1,
57 .max_probes = 10,
58 .probe_interval = 5
59 },
Radek Krejci62aa0642017-05-25 16:33:49 +020060#ifdef NC_ENABLED_SSH
61 .ssh_opts = {
62 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
63 .auth_hostkey_check = sshauth_hostkey_check,
64 .auth_password = sshauth_password,
65 .auth_interactive = sshauth_interactive,
66 .auth_privkey_passphrase = sshauth_privkey_passphrase
67 },
68 .ssh_ch_opts = {
69 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
70 .auth_hostkey_check = sshauth_hostkey_check,
71 .auth_password = sshauth_password,
72 .auth_interactive = sshauth_interactive,
73 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020074 },
Radek Krejci62aa0642017-05-25 16:33:49 +020075#endif /* NC_ENABLED_SSH */
76 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020077 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020078};
79#endif
80
81static void
82nc_client_context_free(void *ptr)
83{
84 struct nc_client_context *c = (struct nc_client_context *)ptr;
85
Radek Krejci5cebc6b2017-05-26 13:24:38 +020086 if (--(c->refcount)) {
87 /* still used */
88 return;
89 }
90
Radek Krejci62aa0642017-05-25 16:33:49 +020091#ifdef __linux__
92 /* in __linux__ we use static memory in the main thread,
93 * so this check is for programs terminating the main()
94 * function by pthread_exit() :)
95 */
96 if (c != &context_main)
97#endif
98 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +020099 /* for the main thread the same is done in nc_client_destroy() */
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400100 free(c->opts.schema_searchpath);
101
Radek Krejci62aa0642017-05-25 16:33:49 +0200102#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400103 int i;
104 for (i = 0; i < c->opts.ch_bind_count; ++i) {
105 close(c->opts.ch_binds[i].sock);
106 free((char *)c->opts.ch_binds[i].address);
107 }
108 free(c->opts.ch_binds);
109 c->opts.ch_binds = NULL;
110 c->opts.ch_bind_count = 0;
Radek Krejci62aa0642017-05-25 16:33:49 +0200111#endif
112#ifdef NC_ENABLED_SSH
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400113 _nc_client_ssh_destroy_opts(&c->ssh_opts);
114 _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200115#endif
116#ifdef NC_ENABLED_TLS
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400117 _nc_client_tls_destroy_opts(&c->tls_opts);
118 _nc_client_tls_destroy_opts(&c->tls_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200119#endif
120 free(c);
121 }
122}
123
124static void
125nc_client_context_createkey(void)
126{
127 int r;
128
129 /* initiate */
130 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
131 pthread_setspecific(nc_client_context_key, NULL);
132}
133
134struct nc_client_context *
135nc_client_context_location(void)
136{
137 struct nc_client_context *e;
138
139 pthread_once(&nc_client_context_once, nc_client_context_createkey);
140 e = pthread_getspecific(nc_client_context_key);
141 if (!e) {
142 /* prepare ly_err storage */
143#ifdef __linux__
144 if (getpid() == syscall(SYS_gettid)) {
145 /* main thread - use global variable instead of thread-specific variable. */
146 e = &context_main;
147 } else
148#endif /* __linux__ */
149 {
150 e = calloc(1, sizeof *e);
151 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200152 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200153#ifdef NC_ENABLED_SSH
154 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
155 e->ssh_opts.auth_pref[0].value = 3;
156 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
157 e->ssh_opts.auth_pref[1].value = 2;
158 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
159 e->ssh_opts.auth_pref[2].value = 1;
160 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
161 e->ssh_opts.auth_password = sshauth_password;
162 e->ssh_opts.auth_interactive = sshauth_interactive;
163 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
164
165 /* callhome settings are the same except the inverted auth methods preferences */
166 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
167 e->ssh_ch_opts.auth_pref[0].value = 1;
168 e->ssh_ch_opts.auth_pref[1].value = 2;
169 e->ssh_ch_opts.auth_pref[2].value = 3;
170#endif /* NC_ENABLED_SSH */
171 }
172 pthread_setspecific(nc_client_context_key, e);
173 }
174
175 return e;
176}
177
178#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100179
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200180API void *
181nc_client_get_thread_context(void)
182{
183 return nc_client_context_location();
184}
185
186API void
187nc_client_set_thread_context(void *context)
188{
189 struct nc_client_context *old, *new;
190
191 if (!context) {
192 ERRARG(context);
193 return;
194 }
195
196 new = (struct nc_client_context *)context;
197 old = nc_client_context_location();
198 if (old == new) {
199 /* nothing to change */
200 return;
201 }
202
203 /* replace old by new, increase reference counter in the newly set context */
204 nc_client_context_free(old);
205 new->refcount++;
206 pthread_setspecific(nc_client_context_key, new);
207}
208
Radek Krejcifd5b6682017-06-13 15:52:53 +0200209int
210nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
211{
212 /* assign context (dicionary needed for handshake) */
213 if (!ctx) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200214 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200215 if (!ctx) {
216 return EXIT_FAILURE;
217 }
218
219 /* user path must be first, the first path is used to store schemas retreived via get-schema */
220 if (client_opts.schema_searchpath) {
221 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
222 }
Michal Vaskoff7e3562018-02-15 13:41:22 +0100223 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200224
225 /* set callback for getting schemas, if provided */
226 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
227 } else {
228 session->flags |= NC_SESSION_SHAREDCTX;
229 }
230
231 session->ctx = ctx;
232
233 return EXIT_SUCCESS;
234}
235
Michal Vasko086311b2016-01-08 09:53:11 +0100236API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100237nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100238{
Michal Vasko3031aae2016-01-27 16:07:18 +0100239 if (client_opts.schema_searchpath) {
240 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100241 }
Michal Vasko086311b2016-01-08 09:53:11 +0100242
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100243 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100244 client_opts.schema_searchpath = strdup(path);
245 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100246 ERRMEM;
247 return 1;
248 }
249 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100250 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100251 }
252
253 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100254}
255
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100256API const char *
257nc_client_get_schema_searchpath(void)
258{
259 return client_opts.schema_searchpath;
260}
261
Radek Krejcifd5b6682017-06-13 15:52:53 +0200262API int
263nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100264{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200265 client_opts.schema_clb = clb;
266 if (clb) {
267 client_opts.schema_clb_data = user_data;
268 } else {
269 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100270 }
271
272 return 0;
273}
274
Radek Krejcifd5b6682017-06-13 15:52:53 +0200275API ly_module_imp_clb
276nc_client_get_schema_callback(void **user_data)
277{
278 if (user_data) {
279 (*user_data) = client_opts.schema_clb_data;
280 }
281 return client_opts.schema_clb;
282}
283
Radek Krejci65ef6d52018-08-16 16:35:02 +0200284
285struct schema_info {
286 char *name;
287 char *revision;
288 struct {
289 char *name;
290 char *revision;
291 } *submodules;
292 struct ly_set features;
293 int implemented;
294};
295
296struct clb_data_s {
297 void *user_data;
298 ly_module_imp_clb user_clb;
299 struct schema_info *schemas;
300 struct nc_session *session;
301 int has_get_schema;
302};
303
304static char *
305retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
306 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100307{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200308 char *localfile = NULL;
309 FILE *f;
310 long length, l;
311 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100312
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200313 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
314 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
315 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200316 return NULL;
317 }
318 if (localfile) {
319 VRB("Session %u: reading schema from localfile \"%s\".", clb_data->session->id, localfile);
320 f = fopen(localfile, "r");
321 if (!f) {
322 ERR("Session %u: unable to open \"%s\" file to get schema (%s).",
323 clb_data->session->id, localfile, strerror(errno));
324 free(localfile);
325 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100326 }
Michal Vasko086311b2016-01-08 09:53:11 +0100327
Radek Krejci65ef6d52018-08-16 16:35:02 +0200328 fseek(f, 0, SEEK_END);
329 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200330 if (length < 0) {
331 ERR("Session %u: unable to get size of schema file \"%s\".",
332 clb_data->session->id, localfile);
333 free(localfile);
334 fclose(f);
335 return NULL;
336 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200337 fseek(f, 0, SEEK_SET);
338
339 model_data = malloc(length + 1);
340 if (!model_data) {
341 ERRMEM;
342 } else if ((l = fread(model_data, 1, length, f)) != length) {
343 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
344 clb_data->session->id, localfile, l, length);
345 free(model_data);
346 model_data = NULL;
347 } else {
348 /* terminating NULL byte */
349 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100350 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200351 fclose(f);
352 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100353 }
354
Radek Krejci65ef6d52018-08-16 16:35:02 +0200355 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100356}
357
358static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200359retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
360 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100361{
Michal Vasko086311b2016-01-08 09:53:11 +0100362 struct nc_rpc *rpc;
363 struct nc_reply *reply;
364 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200365 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200366 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100367 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100368 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200369 char *localfile = NULL;
370 FILE *f;
371 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100372
Radek Krejci65ef6d52018-08-16 16:35:02 +0200373 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
374 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100375
Radek Krejci65ef6d52018-08-16 16:35:02 +0200376 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100377 usleep(1000);
378 }
379 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200380 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100381 nc_rpc_free(rpc);
382 return NULL;
383 }
384
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200385 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200386 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Robin Jarry52ddb952019-10-15 16:23:01 +0200387 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100388 nc_rpc_free(rpc);
389 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200390 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100391 return NULL;
Robin Jarry52ddb952019-10-15 16:23:01 +0200392 } else if (msg == NC_MSG_ERROR || reply == NULL) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200393 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100394 return NULL;
395 }
396
Michal Vasko998ba412016-09-16 12:00:07 +0200397 switch (reply->type) {
398 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200399 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200400 nc_reply_free(reply);
401 return NULL;
402 case NC_RPL_DATA:
403 /* fine */
404 break;
405 case NC_RPL_ERROR:
406 error_rpl = (struct nc_reply_error *)reply;
407 if (error_rpl->count) {
408 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200409 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200410 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200411 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200412 }
413 nc_reply_free(reply);
414 return NULL;
415 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200416 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100417 nc_reply_free(reply);
418 return NULL;
419 }
420
Michal Vasko086311b2016-01-08 09:53:11 +0100421 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko1c042702018-11-29 08:27:51 +0100422 if (!data_rpl->data || (data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
Michal Vaskob2583f12016-05-12 11:40:23 +0200423 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200424 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200425 nc_reply_free(reply);
426 return NULL;
427 }
Radek Krejci539efb62016-08-24 15:05:16 +0200428 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
429 switch (get_schema_data->value_type) {
430 case LYD_ANYDATA_CONSTSTRING:
431 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200432 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200433 break;
434 case LYD_ANYDATA_DATATREE:
435 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
436 break;
437 case LYD_ANYDATA_XML:
438 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
439 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200440 case LYD_ANYDATA_JSON:
441 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200442 case LYD_ANYDATA_SXML:
443 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200444 case LYD_ANYDATA_LYB:
445 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200446 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200447 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200448 }
Michal Vasko086311b2016-01-08 09:53:11 +0100449 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100450
Radek Krejci488b0702018-08-29 14:47:15 +0200451 if (model_data && !model_data[0]) {
452 /* empty data */
453 free(model_data);
454 model_data = NULL;
455 }
456
Radek Krejcifd5b6682017-06-13 15:52:53 +0200457 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200458 if (model_data) {
459 *format = LYS_IN_YANG;
460 if (client_opts.schema_searchpath) {
461 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
462 rev ? "@" : "", rev ? rev : "") == -1) {
463 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100464 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200465 f = fopen(localfile, "w");
466 if (!f) {
467 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
468 localfile, strerror(errno));
469 } else {
470 fputs(model_data, f);
471 fclose(f);
472 }
473 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100474 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200475 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200476 }
477
Michal Vasko086311b2016-01-08 09:53:11 +0100478 return model_data;
479}
480
Jan Kundrát35972df2018-09-06 19:00:01 +0200481static void free_with_user_data(void *data, void *user_data)
482{
483 free(data);
484 (void)user_data;
485}
486
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200487static const char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200488retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
Jan Kundrát35972df2018-09-06 19:00:01 +0200489 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200490{
491 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
492 unsigned int u, v, match = 1;
493 const char *name = NULL, *rev = NULL;
494 char *model_data = NULL;
495
496 /* get and check the final name and revision of the schema to be retrieved */
497 if (!mod_rev || !mod_rev[0]) {
498 /* newest revision requested - get the newest revision from the list of available modules on server */
499 match = 0;
500 for (u = 0; clb_data->schemas[u].name; ++u) {
501 if (strcmp(mod_name, clb_data->schemas[u].name)) {
502 continue;
503 }
504 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
505 mod_rev = clb_data->schemas[u].revision;
506 }
507 match = u + 1;
508 }
509 if (!match) {
510 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
511 clb_data->session->id, mod_name);
512 }
513 }
514 if (submod_name) {
515 name = submod_name;
516 if (sub_rev) {
517 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200518 } else if (match) {
519 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200520 WRN("Session %u: Unable to identify revision of the requested submodule \"%s\", in schema \"%s\", from the available server side information.",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200521 clb_data->session->id, submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200522 } else {
523 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
524 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
525 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
526 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200527 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200528 if (!rev) {
529 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
530 clb_data->session->id, submod_name, mod_name);
531 return NULL;
532 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200533 }
534 }
535 } else {
536 name = mod_name;
537 rev = mod_rev;
538 }
539
540 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
541
542 if (match) {
543 /* we have enough information to avoid communication with server and try to get
544 * the schema locally */
545
546 /* 1. try to get data locally */
547 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
548
549 /* 2. try to use <get-schema> */
550 if (!model_data && clb_data->has_get_schema) {
551 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
552 }
553 } else {
554 /* we are unsure which revision of the schema we should load, so first try to get
555 * the newest revision from the server via get-schema and only if the server does not
556 * implement get-schema, try to load the newest revision locally. This is imperfect
557 * solution, but there are situation when a client does not know what revision is
558 * actually implemented by the server. */
559
560 /* 1. try to use <get-schema> */
561 if (clb_data->has_get_schema) {
562 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
563 }
564
565 /* 2. try to get data locally */
566 if (!model_data) {
567 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
568 }
569 }
570
571 /* 3. try to use user callback */
572 if (!model_data && clb_data->user_clb) {
573 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
574 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
575 }
576
Jan Kundrát35972df2018-09-06 19:00:01 +0200577 *free_module_data = free_with_user_data;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200578 return model_data;
579}
580
Michal Vaskoceae0152018-02-14 16:03:59 +0100581static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200582nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200583 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, const struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100584{
585 int ret = 0;
586 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200587 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200588 LYS_INFORMAT format;
Jan Kundrát35972df2018-09-06 19:00:01 +0200589 void (*free_module_data)(void*, void*) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200590 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100591
Radek Krejci65ef6d52018-08-16 16:35:02 +0200592 *mod = NULL;
593 if (revision) {
594 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
595 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100596 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200597 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100598 /* make the present module implemented */
599 if (lys_set_implemented(*mod)) {
600 ERR("Failed to implement model \"%s\".", (*mod)->name);
601 ret = -1;
602 }
603 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200604 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100605 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200606 clb_data.has_get_schema = has_get_schema;
607 clb_data.schemas = schemas;
608 clb_data.session = session;
609 clb_data.user_clb = user_clb;
610 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100611
612 /* clear all the errors and just collect them for now */
613 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200614 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100615
Radek Krejci65ef6d52018-08-16 16:35:02 +0200616 /* get module data */
617 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100618
Radek Krejci65ef6d52018-08-16 16:35:02 +0200619 if (module_data) {
620 /* parse the schema */
621 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100622
Radek Krejci65ef6d52018-08-16 16:35:02 +0200623 *mod = lys_parse_mem(session->ctx, module_data, format);
624 if (*free_module_data) {
Jan Kundrát35972df2018-09-06 19:00:01 +0200625 (*free_module_data)((char*)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100626 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100627
Radek Krejci65ef6d52018-08-16 16:35:02 +0200628 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
629 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100630
Michal Vaskodbf7c272018-02-19 09:07:59 +0100631 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200632 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100633 if (!(*mod)) {
634 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
635 ly_err_print(eitem);
636 }
637 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200638 } else {
639 /* print only warnings */
640 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
641 if (eitem->level == LY_LLWRN) {
642 ly_err_print(eitem);
643 }
644 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100645 }
646
Michal Vaskodbf7c272018-02-19 09:07:59 +0100647 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100648 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100649 }
650
651 return ret;
652}
653
Radek Krejci65ef6d52018-08-16 16:35:02 +0200654static void
655free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200656{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200657 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200658
Radek Krejci65ef6d52018-08-16 16:35:02 +0200659 if (!list) {
660 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200661 }
662
Radek Krejci65ef6d52018-08-16 16:35:02 +0200663 for (u = 0; list[u].name; ++u) {
664 free(list[u].name);
665 free(list[u].revision);
666 for (v = 0; v < list[u].features.number; ++v) {
667 free(list[u].features.set.g[v]);
668 }
669 free(list[u].features.set.g);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200670 if (list[u].submodules) {
671 for (v = 0; list[u].submodules[v].name; ++v) {
672 free(list[u].submodules[v].name);
673 free(list[u].submodules[v].revision);
674 }
675 free(list[u].submodules);
676 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200677 }
678 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200679}
680
Radek Krejci235d8cb2018-08-17 14:04:32 +0200681
682static int
683build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200684{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200685 struct nc_rpc *rpc = NULL;
686 struct nc_reply *reply = NULL;
687 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200688 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200689 NC_MSG_TYPE msg;
690 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200691 struct ly_set *modules = NULL;
Radek Krejci2d088832018-08-21 13:40:14 +0200692 unsigned int u, v, submodules_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200693 struct lyd_node *iter, *child;
694 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200695 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200696
697 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200698 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
699 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
700 } else {
701 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0, NC_PARAMTYPE_CONST);
702 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200703 if (!rpc) {
704 goto cleanup;
705 }
706
707 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
708 usleep(1000);
709 }
710 if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200711 WRN("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200712 session->id);
713 goto cleanup;
714 }
715
716 do {
717 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Robin Jarry52ddb952019-10-15 16:23:01 +0200718 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200719 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200720 WRN("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200721 goto cleanup;
Robin Jarry52ddb952019-10-15 16:23:01 +0200722 } else if (msg == NC_MSG_ERROR || reply == NULL) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200723 WRN("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200724 goto cleanup;
725 }
726
727 switch (reply->type) {
728 case NC_RPL_OK:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200729 WRN("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200730 goto cleanup;
731 case NC_RPL_DATA:
732 /* fine */
733 break;
734 case NC_RPL_ERROR:
735 error_rpl = (struct nc_reply_error *)reply;
736 if (error_rpl->count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200737 WRN("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200738 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
739 } else {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200740 WRN("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200741 }
742 goto cleanup;
743 case NC_RPL_NOTIF:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200744 WRN("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200745 goto cleanup;
746 }
747
Radek Krejci65ef6d52018-08-16 16:35:02 +0200748 yldata = ((struct nc_reply_data *)reply)->data;
749 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200750 WRN("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200751 goto cleanup;
752 }
753
Radek Krejci65ef6d52018-08-16 16:35:02 +0200754 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200755 if (!modules) {
Radek Krejci2d088832018-08-21 13:40:14 +0200756 WRN("Session %u: no module information in reply to a yang-library <get> RPC.", session->id);
757 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200758 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200759
Radek Krejci2d088832018-08-21 13:40:14 +0200760 (*result) = calloc(modules->number + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200761 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200762 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200763 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200764 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200765 }
766
Radek Krejci2d088832018-08-21 13:40:14 +0200767 for (u = 0; u < modules->number; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200768 submodules_count = 0;
769 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200770 LY_TREE_FOR(modules->set.d[u]->child, iter) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200771 if (iter->schema->module != mod) {
772 /* ignore node from other schemas (augments) */
773 continue;
774 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200775 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
776 /* ignore empty nodes */
777 continue;
778 }
779 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200780 (*result)[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200781 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200782 (*result)[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200783 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200784 (*result)[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200785 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200786 ly_set_add(&(*result)[u].features, (void *)strdup(((struct lyd_node_leaf_list *)iter)->value_str), LY_SET_OPT_USEASLIST);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200787 } else if (!strcmp(iter->schema->name, "submodule")) {
788 submodules_count++;
789 }
790 }
791
792 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200793 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
794 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200795 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200796 free_schema_info(*result);
797 *result = NULL;
798 ret = EXIT_FAILURE;
799 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200800 } else {
801 v = 0;
802 LY_TREE_FOR(modules->set.d[u]->child, iter) {
803 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
804 if (mod == iter->schema->module && !strcmp(iter->schema->name, "submodule")) {
805 LY_TREE_FOR(iter->child, child) {
806 if (mod != child->schema->module) {
807 continue;
808 } else if (!strcmp(child->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200809 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200810 } else if (!strcmp(child->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200811 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200812 }
813 }
814 }
815 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200816 }
817 }
818 }
819
Radek Krejcifd5b6682017-06-13 15:52:53 +0200820cleanup:
821 nc_rpc_free(rpc);
822 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200823 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200824
Radek Krejci235d8cb2018-08-17 14:04:32 +0200825 if (session->status != NC_STATUS_RUNNING) {
826 /* something bad heppened, discard the session */
827 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
828 ret = EXIT_FAILURE;
829 }
830
831 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200832}
833
Radek Krejci235d8cb2018-08-17 14:04:32 +0200834static int
835build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200836{
837 unsigned int u, v;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200838 char *module_cpblt, *ptr, *ptr2;
839
840 for (u = 0; cpblts[u]; ++u);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200841 (*result) = calloc(u + 1, sizeof **result);
842 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200843 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200844 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200845 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200846
847 for (u = v = 0; cpblts[u]; ++u) {
848 module_cpblt = strstr(cpblts[u], "module=");
849 /* this capability requires a module */
850 if (!module_cpblt) {
851 continue;
852 }
853
854 /* get module's name */
855 ptr = (char *)module_cpblt + 7;
856 ptr2 = strchr(ptr, '&');
857 if (!ptr2) {
858 ptr2 = ptr + strlen(ptr);
859 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200860 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200861
862 /* get module's revision */
863 ptr = strstr(module_cpblt, "revision=");
864 if (ptr) {
865 ptr += 9;
866 ptr2 = strchr(ptr, '&');
867 if (!ptr2) {
868 ptr2 = ptr + strlen(ptr);
869 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200870 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200871 }
872
873 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200874 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200875
876 /* get module's features */
877 ptr = strstr(module_cpblt, "features=");
878 if (ptr) {
879 ptr += 9;
880 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
881 if (*ptr == ',') {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200882 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200883 ptr2 = ptr + 1;
884 }
885 }
886 /* the last one */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200887 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200888 }
889 ++v;
890 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200891
892 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200893}
894
895static int
896nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
897{
898 int ret = EXIT_FAILURE;
899 const struct lys_module *mod;
900 unsigned int u, v;
901
902 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200903 /* skip import-only modules */
904 if (!modules[u].implemented) {
905 continue;
906 }
907
Radek Krejci65ef6d52018-08-16 16:35:02 +0200908 /* we can continue even if it fails */
909 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
910
911 if (!mod) {
912 if (session->status != NC_STATUS_RUNNING) {
913 /* something bad heppened, discard the session */
914 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
915 goto cleanup;
916 }
917
918 /* all loading ways failed, the schema will be ignored in the received data */
919 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
920 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
921 } else {
922 /* set features - first disable all to enable specified then */
923 lys_features_disable(mod, "*");
924 for (v = 0; v < modules[u].features.number; v++) {
925 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
926 }
927 }
Michal Vasko60f66602017-10-17 13:52:18 +0200928 }
929
Radek Krejci65ef6d52018-08-16 16:35:02 +0200930 /* done */
931 ret = EXIT_SUCCESS;
932
933cleanup:
934
Radek Krejcifd5b6682017-06-13 15:52:53 +0200935 return ret;
936}
937
Radek Krejci65ef6d52018-08-16 16:35:02 +0200938static int
939nc_ctx_fill_ietf_netconf(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
940{
941 unsigned int u, v;
942 const struct lys_module *ietfnc;
943
944 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
945 if (!ietfnc) {
946 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
947 if (!ietfnc) {
948 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
949 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
950 }
951 }
952 if (!ietfnc) {
953 ERR("Loading base NETCONF schema failed.");
954 return 1;
955 }
956
957 /* set supported capabilities from ietf-netconf */
958 for (u = 0; modules[u].name; ++u) {
959 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
960 continue;
961 }
962
963 lys_features_disable(ietfnc, "*");
964 for (v = 0; v < modules[u].features.number; v++) {
965 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
966 }
967 }
968
969 return 0;
970}
971
Michal Vasko086311b2016-01-08 09:53:11 +0100972int
973nc_ctx_check_and_fill(struct nc_session *session)
974{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200975 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100976 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100977 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200978 const struct lys_module *mod = NULL;
979 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200980 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100981
Michal Vasko2e6defd2016-10-07 15:48:15 +0200982 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100983
Radek Krejci65ef6d52018-08-16 16:35:02 +0200984 /* store the original user's callback, we will be switching between local search, get-schema and user callback */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200985 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200986
Radek Krejci65ef6d52018-08-16 16:35:02 +0200987 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
988 * schemas only to those present on the server side */
989 ly_ctx_set_disable_searchdirs(session->ctx);
990
991 /* our callback is set later with appropriate data */
992 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
993
994 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200995 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200996 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200997 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200998 if (yanglib_support) {
999 break;
1000 }
Radek Krejcicde410d2018-08-21 09:56:49 +02001001 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:1.0", 51)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001002 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +02001003 if (get_schema_support) {
1004 break;
1005 }
Michal Vasko086311b2016-01-08 09:53:11 +01001006 }
1007 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001008
1009 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001010 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
1011 ERR("Session %u: unable to get server's schema information from the <hello>'s capabilities.", session->id);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001012 goto cleanup;
1013 }
1014
Michal Vasko086311b2016-01-08 09:53:11 +01001015 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001016 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001017 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
1018 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001019 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001020 }
1021 }
1022
1023 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001024 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001025 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001026 }
1027
Radek Krejci65ef6d52018-08-16 16:35:02 +02001028 /* get correct version of ietf-yang-library into context */
1029 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001030 /* use get schema to get server's ietf-yang-library */
1031 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1032 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001033 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
1034 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001035 yanglib_support = 0;
1036 } else {
1037 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001038 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
1039 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001040 yanglib_support = 0;
1041 }
1042 free(revision);
1043 }
1044 }
1045
Radek Krejci65ef6d52018-08-16 16:35:02 +02001046 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001047 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001048 if (build_schema_info_yl(session, &sm)) {
1049 goto cleanup;
1050 } else if (!sm) {
1051 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
1052 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001053 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
1054 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001055 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001056 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001057 }
Michal Vaskoef578332016-01-25 13:20:09 +01001058
Radek Krejci65ef6d52018-08-16 16:35:02 +02001059 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1060 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001061 }
1062
Radek Krejcifd5b6682017-06-13 15:52:53 +02001063 /* succsess */
1064 ret = 0;
1065
Michal Vaskoeee99412016-11-21 10:19:43 +01001066 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001067 WRN("Session %u: some models failed to be loaded, any data from these models (and any other unknown) will be ignored.", session->id);
Michal Vaskoef578332016-01-25 13:20:09 +01001068 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001069
1070cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001071 free_schema_info(server_modules);
1072
Radek Krejcifd5b6682017-06-13 15:52:53 +02001073 /* set user callback back */
1074 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001075 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001076
Michal Vaskoef578332016-01-25 13:20:09 +01001077 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001078}
1079
1080API struct nc_session *
1081nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1082{
Michal Vaskod083db62016-01-19 10:31:29 +01001083 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001084
Michal Vasko45e53ae2016-04-07 11:46:03 +02001085 if (fdin < 0) {
1086 ERRARG("fdin");
1087 return NULL;
1088 } else if (fdout < 0) {
1089 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001090 return NULL;
1091 }
1092
1093 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001094 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001095 if (!session) {
1096 ERRMEM;
1097 return NULL;
1098 }
1099 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001100
1101 /* transport specific data */
1102 session->ti_type = NC_TI_FD;
1103 session->ti.fd.in = fdin;
1104 session->ti.fd.out = fdout;
1105
1106 /* assign context (dicionary needed for handshake) */
1107 if (!ctx) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001108 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001109 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001110 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001111 /* that's just it */
1112 goto fail;
1113 }
Michal Vasko086311b2016-01-08 09:53:11 +01001114 } else {
1115 session->flags |= NC_SESSION_SHAREDCTX;
1116 }
1117 session->ctx = ctx;
1118
1119 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001120 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001121 goto fail;
1122 }
1123 session->status = NC_STATUS_RUNNING;
1124
Michal Vaskoef578332016-01-25 13:20:09 +01001125 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001126 goto fail;
1127 }
1128
1129 return session;
1130
1131fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001132 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001133 return NULL;
1134}
1135
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001136API struct nc_session *
1137nc_connect_unix(const char *address, struct ly_ctx *ctx)
1138{
1139 struct nc_session *session = NULL;
1140 struct sockaddr_un sun;
1141 const struct passwd *pw;
1142 char *username;
1143 int sock = -1;
1144
1145 if (address == NULL) {
1146 ERRARG("address");
1147 return NULL;
1148 }
1149
1150 pw = getpwuid(geteuid());
1151 if (pw == NULL) {
1152 ERR("Failed to find username for euid=%u.\n", geteuid());
1153 goto fail;
1154 }
1155
1156 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1157 if (sock < 0) {
1158 ERR("Failed to create socket (%s).", strerror(errno));
1159 goto fail;
1160 }
1161
1162 memset(&sun, 0, sizeof(sun));
1163 sun.sun_family = AF_UNIX;
1164 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1165
1166 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
1167 ERR("cannot connect to sock server %s (%s)",
1168 address, strerror(errno));
1169 goto fail;
1170 }
1171
1172 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
1173 ERR("Fcntl failed (%s).", strerror(errno));
1174 goto fail;
1175 }
1176
1177 /* prepare session structure */
1178 session = nc_new_session(NC_CLIENT, 0);
1179 if (!session) {
1180 ERRMEM;
1181 goto fail;
1182 }
1183 session->status = NC_STATUS_STARTING;
1184
1185 /* transport specific data */
1186 session->ti_type = NC_TI_UNIX;
1187 session->ti.unixsock.sock = sock;
1188 sock = -1; /* do not close sock in fail label anymore */
1189
1190 /* assign context (dictionary needed for handshake) */
1191 if (!ctx) {
1192 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
1193 /* definitely should not happen, but be ready */
1194 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
1195 /* that's just it */
1196 goto fail;
1197 }
1198 } else {
1199 session->flags |= NC_SESSION_SHAREDCTX;
1200 }
1201 session->ctx = ctx;
1202
1203 session->path = lydict_insert(ctx, address, 0);
1204
1205 username = strdup(pw->pw_name);
1206 if (username == NULL) {
1207 ERRMEM;
1208 goto fail;
1209 }
1210 session->username = lydict_insert_zc(ctx, username);
1211
1212 /* NETCONF handshake */
1213 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1214 goto fail;
1215 }
1216 session->status = NC_STATUS_RUNNING;
1217
1218 if (nc_ctx_check_and_fill(session) == -1) {
1219 goto fail;
1220 }
1221
1222 return session;
1223
1224fail:
1225 nc_session_free(session, NULL);
1226 if (sock >= 0)
1227 close(sock);
1228 return NULL;
1229}
1230
Frank Rimpler9f838b02018-07-25 06:44:03 +00001231/*
1232 Helper for a non-blocking connect (which is required because of the locking
1233 concept for e.g. call home settings). For more details see nc_sock_connect().
1234 */
1235static int
Michal Vasko5e8d0192019-06-24 19:19:49 +02001236_non_blocking_connect(int timeout, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001237{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001238 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001239 int sock = -1;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001240 fd_set wset;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001241 struct timeval ts;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001242 socklen_t len = sizeof(int);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001243 struct in_addr *addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001244 uint16_t port;
Michal Vasko5e8d0192019-06-24 19:19:49 +02001245 char str[INET6_ADDRSTRLEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001246
Frank Rimpler9f838b02018-07-25 06:44:03 +00001247 if (sock_pending && *sock_pending != -1) {
1248 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1249 sock = *sock_pending;
1250 } else {
1251 assert(res);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001252 if (res->ai_family == AF_INET6) {
1253 addr = (struct in_addr *) &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001254 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001255 } else {
1256 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001257 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001258 }
1259 if (!inet_ntop(res->ai_family, addr, str, res->ai_addrlen)) {
1260 WRN("inet_ntop() failed (%s).", strerror(errno));
1261 } else {
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001262 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 +02001263 }
1264
1265 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001266 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1267 if (sock == -1) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001268 ERR("Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001269 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001270 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001271 /* make the socket non-blocking */
1272 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001273 ERR("fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001274 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001275 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001276 /* non-blocking connect! */
1277 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1278 if (errno != EINPROGRESS) {
1279 /* network connection failed, try another resource */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001280 ERR("connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001281 goto cleanup;
1282 }
1283 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001284 }
1285 ts.tv_sec = timeout;
1286 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001287
Frank Rimpler9f838b02018-07-25 06:44:03 +00001288 FD_ZERO(&wset);
1289 FD_SET(sock, &wset);
1290
1291 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001292 ERR("select() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001293 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001294 }
1295
Michal Vasko5e8d0192019-06-24 19:19:49 +02001296 if (ret == 0) {
1297 /* there was a timeout */
1298 VRB("Timed out after %ds (%s).", timeout, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001299 if (sock_pending) {
1300 /* no sock-close, we'll try it again */
1301 *sock_pending = sock;
1302 } else {
1303 close(sock);
1304 }
1305 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001306 }
Radek Krejci782041a2018-08-20 10:09:45 +02001307
1308 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001309 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001310 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001311 ERR("getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001312 goto cleanup;
1313 }
1314 if (error == ECONNREFUSED) {
1315 /* network connection failed, try another resource */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001316 VRB("getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001317 errno = error;
1318 goto cleanup;
1319 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001320
1321 /* enable keep-alive */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001322 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001323 goto cleanup;
1324 }
1325
Michal Vasko086311b2016-01-08 09:53:11 +01001326 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001327
Frank Rimpler9f838b02018-07-25 06:44:03 +00001328cleanup:
1329 if (sock_pending) {
1330 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001331 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001332 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001333 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001334}
1335
Frank Rimpler9f838b02018-07-25 06:44:03 +00001336/* A given timeout value limits the time how long the function blocks. If it has to block
1337 only for some seconds, a socket connection might not yet have been fully established.
1338 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1339 value will be -1. In such a case a subsequent invokation is required, by providing the
1340 stored sock_pending, again.
1341 In general, if this function returns -1, when a timeout has been given, this function
1342 has to be invoked, until it returns a valid socket.
1343 */
1344int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001345nc_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 +00001346{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001347 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001348 int sock = sock_pending ? *sock_pending : -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001349 struct addrinfo hints, *res_list, *res;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001350 char *buf, port_s[6]; /* length of string representation of short int */
Michal Vasko66032bc2019-01-22 15:03:12 +01001351 void *addr;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001352
Michal Vasko66032bc2019-01-22 15:03:12 +01001353 DBG("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001354
1355 /* no pending socket */
1356 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001357 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001358 snprintf(port_s, 6, "%u", port);
1359 memset(&hints, 0, sizeof hints);
1360 hints.ai_family = AF_UNSPEC;
1361 hints.ai_socktype = SOCK_STREAM;
1362 hints.ai_protocol = IPPROTO_TCP;
1363 i = getaddrinfo(host, port_s, &hints, &res_list);
1364 if (i != 0) {
1365 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1366 return -1;
1367 }
1368
1369 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001370 sock = _non_blocking_connect(timeout, sock_pending, res, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001371 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1372 /* try the next resource */
1373 continue;
1374 }
1375 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 +01001376
1377 opt = 1;
1378 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
1379 ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno));
1380 close(sock);
1381 return -1;
1382 }
1383
Michal Vasko66032bc2019-01-22 15:03:12 +01001384 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1385 buf = malloc(INET6_ADDRSTRLEN);
1386 if (!buf) {
1387 ERRMEM;
1388 close(sock);
1389 return -1;
1390 }
1391 if (res->ai_family == AF_INET) {
1392 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1393 } else {
1394 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1395 }
1396 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
1397 ERR("Converting host to IP address failed (%s).", strerror(errno));
1398 free(buf);
1399 close(sock);
1400 return -1;
1401 }
1402
1403 *ip_host = buf;
1404 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001405 break;
1406 }
1407 freeaddrinfo(res_list);
1408
1409 } else {
1410 /* try to get a connection with the pending socket */
1411 assert(sock_pending);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001412 sock = _non_blocking_connect(timeout, sock_pending, NULL, ka);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001413 }
1414
1415 return sock;
1416}
1417
Michal Vasko086311b2016-01-08 09:53:11 +01001418static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001419get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001420{
Michal Vasko086311b2016-01-08 09:53:11 +01001421 char *ptr;
1422 const char *str_msgid;
1423 uint64_t cur_msgid;
1424 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001425 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001426 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1427
Michal Vasko086311b2016-01-08 09:53:11 +01001428 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001429 if (!msgid && session->opts.client.notifs) {
1430 cont = session->opts.client.notifs;
1431 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001432
Michal Vasko71ba2da2016-05-04 10:53:16 +02001433 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001434 free(cont);
1435
Michal Vasko71ba2da2016-05-04 10:53:16 +02001436 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001437 }
1438
1439 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001440 if (msgid && session->opts.client.replies) {
1441 cont = session->opts.client.replies;
1442 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001443
Michal Vasko71ba2da2016-05-04 10:53:16 +02001444 xml = cont->msg;
1445 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001446
Michal Vasko71ba2da2016-05-04 10:53:16 +02001447 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001448 }
1449
Michal Vasko71ba2da2016-05-04 10:53:16 +02001450 if (!msgtype) {
1451 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001452 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001453 }
Michal Vasko086311b2016-01-08 09:53:11 +01001454
1455 /* we read rpc-reply, want a notif */
1456 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001457 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001458 while (*cont_ptr) {
1459 cont_ptr = &((*cont_ptr)->next);
1460 }
1461 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001462 if (!*cont_ptr) {
1463 ERRMEM;
1464 lyxml_free(session->ctx, xml);
1465 return NC_MSG_ERROR;
1466 }
Michal Vasko086311b2016-01-08 09:53:11 +01001467 (*cont_ptr)->msg = xml;
1468 (*cont_ptr)->next = NULL;
1469 }
1470
1471 /* we read notif, want a rpc-reply */
1472 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001473 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001474 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001475 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001476 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001477 }
Michal Vasko086311b2016-01-08 09:53:11 +01001478
Michal Vasko2e6defd2016-10-07 15:48:15 +02001479 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001480 while (*cont_ptr) {
1481 cont_ptr = &((*cont_ptr)->next);
1482 }
1483 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001484 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001485 ERRMEM;
1486 lyxml_free(session->ctx, xml);
1487 return NC_MSG_ERROR;
1488 }
Michal Vasko086311b2016-01-08 09:53:11 +01001489 (*cont_ptr)->msg = xml;
1490 (*cont_ptr)->next = NULL;
1491 }
1492
Michal Vasko086311b2016-01-08 09:53:11 +01001493 switch (msgtype) {
1494 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001495 if (!msgid) {
1496 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001497 }
Michal Vasko086311b2016-01-08 09:53:11 +01001498 break;
1499
1500 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001501 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001502 /* check message-id */
1503 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1504 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001505 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001506 } else {
1507 cur_msgid = strtoul(str_msgid, &ptr, 10);
1508 if (cur_msgid != msgid) {
1509 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1510 session->id, str_msgid);
1511 msgtype = NC_MSG_REPLY_ERR_MSGID;
1512 }
1513 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001514 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001515 }
Michal Vasko086311b2016-01-08 09:53:11 +01001516 break;
1517
1518 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001519 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001520 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001521 msgtype = NC_MSG_ERROR;
1522 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001523
1524 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001525 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001526 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001527 msgtype = NC_MSG_ERROR;
1528 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001529
1530 default:
1531 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1532 * NC_MSG_NONE is not returned by nc_read_msg()
1533 */
1534 break;
1535 }
1536
1537 return msgtype;
1538}
1539
1540/* cannot strictly fail, but does not need to fill any error parameter at all */
1541static void
1542parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1543{
1544 struct lyxml_elem *iter, *next, *info;
1545
1546 LY_TREE_FOR(xml->child, iter) {
1547 if (!iter->ns) {
1548 if (iter->content) {
1549 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1550 } else {
1551 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1552 }
1553 continue;
1554 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1555 if (iter->content) {
1556 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1557 iter->name, iter->content, iter->ns->value);
1558 } else {
1559 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1560 }
1561 continue;
1562 }
1563
1564 if (!strcmp(iter->name, "error-type")) {
1565 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1566 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1567 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1568 } else if (err->type) {
1569 WRN("<rpc-error> <error-type> duplicated.");
1570 } else {
1571 err->type = lydict_insert(ctx, iter->content, 0);
1572 }
1573 } else if (!strcmp(iter->name, "error-tag")) {
1574 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1575 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1576 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1577 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1578 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1579 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1580 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1581 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1582 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1583 && strcmp(iter->content, "malformed-message"))) {
1584 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1585 } else if (err->tag) {
1586 WRN("<rpc-error> <error-tag> duplicated.");
1587 } else {
1588 err->tag = lydict_insert(ctx, iter->content, 0);
1589 }
1590 } else if (!strcmp(iter->name, "error-severity")) {
1591 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1592 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1593 } else if (err->severity) {
1594 WRN("<rpc-error> <error-severity> duplicated.");
1595 } else {
1596 err->severity = lydict_insert(ctx, iter->content, 0);
1597 }
1598 } else if (!strcmp(iter->name, "error-app-tag")) {
1599 if (err->apptag) {
1600 WRN("<rpc-error> <error-app-tag> duplicated.");
1601 } else {
1602 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1603 }
1604 } else if (!strcmp(iter->name, "error-path")) {
1605 if (err->path) {
1606 WRN("<rpc-error> <error-path> duplicated.");
1607 } else {
1608 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1609 }
1610 } else if (!strcmp(iter->name, "error-message")) {
1611 if (err->message) {
1612 WRN("<rpc-error> <error-message> duplicated.");
1613 } else {
1614 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001615 if (err->message_lang) {
1616 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1617 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001618 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1619 }
1620 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1621 }
1622 } else if (!strcmp(iter->name, "error-info")) {
1623 LY_TREE_FOR_SAFE(iter->child, next, info) {
1624 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1625 if (!strcmp(info->name, "session-id")) {
1626 if (err->sid) {
1627 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1628 } else {
1629 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1630 }
Michal Vasko630485f2018-01-03 14:28:32 +01001631 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001632 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001633 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1634 if (!err->attr) {
1635 ERRMEM;
1636 return;
1637 }
Michal Vasko086311b2016-01-08 09:53:11 +01001638 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1639 } else if (!strcmp(info->name, "bad-element")) {
1640 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001641 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1642 if (!err->elem) {
1643 ERRMEM;
1644 return;
1645 }
Michal Vasko086311b2016-01-08 09:53:11 +01001646 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1647 } else if (!strcmp(info->name, "bad-namespace")) {
1648 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001649 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1650 if (!err->ns) {
1651 ERRMEM;
1652 return;
1653 }
Michal Vasko086311b2016-01-08 09:53:11 +01001654 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1655 } else {
1656 if (info->content) {
1657 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1658 info->name, info->content);
1659 } else {
1660 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1661 }
1662 }
1663 } else {
1664 lyxml_unlink(ctx, info);
1665 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001666 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1667 if (!err->other) {
1668 ERRMEM;
1669 return;
1670 }
Michal Vasko086311b2016-01-08 09:53:11 +01001671 err->other[err->other_count - 1] = info;
1672 }
1673 }
1674 } else {
1675 if (iter->content) {
1676 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1677 } else {
1678 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1679 }
1680 }
1681 }
1682}
1683
1684static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001685parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001686{
1687 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001688 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001689 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001690 struct nc_reply_data *data_rpl;
1691 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001692 struct nc_rpc_act_generic *rpc_gen;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001693 int i, data_parsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001694
Michal Vasko086311b2016-01-08 09:53:11 +01001695 /* rpc-error */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001696 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 +01001697 /* count and check elements */
1698 i = 0;
1699 LY_TREE_FOR(xml->child, iter) {
1700 if (strcmp(iter->name, "rpc-error")) {
1701 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1702 return NULL;
1703 } else if (!iter->ns) {
1704 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1705 return NULL;
1706 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1707 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1708 return NULL;
1709 }
1710 ++i;
1711 }
1712
1713 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001714 if (!error_rpl) {
1715 ERRMEM;
1716 return NULL;
1717 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001718 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001719 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001720 if (!error_rpl->err) {
1721 ERRMEM;
1722 free(error_rpl);
1723 return NULL;
1724 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001725 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001726 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001727 reply = (struct nc_reply *)error_rpl;
1728
1729 i = 0;
1730 LY_TREE_FOR(xml->child, iter) {
1731 parse_rpc_error(ctx, iter, error_rpl->err + i);
1732 ++i;
1733 }
1734
1735 /* ok */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001736 } 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 +01001737 if (xml->child->next) {
1738 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1739 return NULL;
1740 }
1741 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001742 if (!reply) {
1743 ERRMEM;
1744 return NULL;
1745 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001746 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001747
1748 /* some RPC output */
1749 } else {
1750 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001751 case NC_RPC_ACT_GENERIC:
1752 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001753
1754 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001755 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1756 if (!rpc_act) {
1757 ERR("Failed to duplicate a generic RPC/action.");
1758 return NULL;
1759 }
Michal Vasko086311b2016-01-08 09:53:11 +01001760 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001761 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 +02001762 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001763 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001764 return NULL;
1765 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001766 }
Michal Vasko086311b2016-01-08 09:53:11 +01001767 break;
1768
1769 case NC_RPC_GETCONFIG:
1770 case NC_RPC_GET:
Michal Vasko6b281ff2019-02-14 10:15:19 +01001771 /* we should definitely have received at least an empty "data" element even on empty reply, but fine */
1772 if (!xml->child || !xml->child->child) {
Michal Vasko13ed2942016-02-29 16:21:00 +01001773 /* we did not receive any data */
1774 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001775 if (!data_rpl) {
1776 ERRMEM;
1777 return NULL;
1778 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001779 data_rpl->type = NC_RPL_DATA;
1780 data_rpl->data = NULL;
1781 return (struct nc_reply *)data_rpl;
1782 }
1783
Michal Vasko086311b2016-01-08 09:53:11 +01001784 /* special treatment */
Michal Vaskof45e0c12018-11-28 08:38:23 +01001785 ly_errno = 0;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001786 data = lyd_parse_xml(ctx, &xml->child->child,
1787 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001788 | parseroptions);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001789 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001790 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1791 return NULL;
1792 }
Michal Vaskof45e0c12018-11-28 08:38:23 +01001793 data_parsed = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001794 break;
1795
1796 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001797 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001798 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001799 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001800 return NULL;
1801 }
1802 break;
1803
1804 case NC_RPC_EDIT:
1805 case NC_RPC_COPY:
1806 case NC_RPC_DELETE:
1807 case NC_RPC_LOCK:
1808 case NC_RPC_UNLOCK:
1809 case NC_RPC_KILL:
1810 case NC_RPC_COMMIT:
1811 case NC_RPC_DISCARD:
1812 case NC_RPC_CANCEL:
1813 case NC_RPC_VALIDATE:
1814 case NC_RPC_SUBSCRIBE:
1815 /* there is no output defined */
Michal Vasko6b281ff2019-02-14 10:15:19 +01001816 ERR("Unexpected data reply (root elem \"%s\").", xml->child ? xml->child->name : NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001817 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001818 default:
1819 ERRINT;
1820 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001821 }
1822
1823 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001824 if (!data_rpl) {
1825 ERRMEM;
1826 return NULL;
1827 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001828 data_rpl->type = NC_RPL_DATA;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001829
1830 ly_errno = 0;
1831 if (!data_parsed) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001832 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001833 rpc_act, NULL);
Michal Vasko6b281ff2019-02-14 10:15:19 +01001834 if (!ly_errno && !data_rpl->data->child) {
1835 ERR("An empty data <rpc-reply>.");
1836 lyd_free_withsiblings(rpc_act);
1837 lyd_free(data_rpl->data);
1838 free(data_rpl);
1839 return NULL;
1840 }
Michal Vasko086311b2016-01-08 09:53:11 +01001841 } else {
1842 /* <get>, <get-config> */
1843 data_rpl->data = data;
1844 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001845 lyd_free_withsiblings(rpc_act);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001846 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001847 ERR("Failed to parse <rpc-reply>.");
1848 free(data_rpl);
1849 return NULL;
1850 }
1851 reply = (struct nc_reply *)data_rpl;
1852 }
1853
1854 return reply;
1855}
1856
Radek Krejci53691be2016-02-22 13:58:37 +01001857#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001858
Michal Vasko3031aae2016-01-27 16:07:18 +01001859int
1860nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1861{
1862 int sock;
1863
Michal Vasko45e53ae2016-04-07 11:46:03 +02001864 if (!address) {
1865 ERRARG("address");
1866 return -1;
1867 } else if (!port) {
1868 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001869 return -1;
1870 }
1871
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001872 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001873 if (sock == -1) {
1874 return -1;
1875 }
1876
1877 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001878 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1879 if (!client_opts.ch_binds) {
1880 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001881 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001882 return -1;
1883 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001884
Michal Vasko2e6defd2016-10-07 15:48:15 +02001885 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1886 if (!client_opts.ch_bind_ti) {
1887 ERRMEM;
1888 close(sock);
1889 return -1;
1890 }
1891 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1892
Michal Vasko3031aae2016-01-27 16:07:18 +01001893 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001894 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1895 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001896 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001897 return -1;
1898 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001899 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1900 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001901 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001902
1903 return 0;
1904}
1905
1906int
1907nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1908{
1909 uint32_t i;
1910 int ret = -1;
1911
1912 if (!address && !port && !ti) {
1913 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1914 close(client_opts.ch_binds[i].sock);
1915 free((char *)client_opts.ch_binds[i].address);
1916
1917 ret = 0;
1918 }
1919 free(client_opts.ch_binds);
1920 client_opts.ch_binds = NULL;
1921 client_opts.ch_bind_count = 0;
1922 } else {
1923 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1924 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1925 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001926 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001927 close(client_opts.ch_binds[i].sock);
1928 free((char *)client_opts.ch_binds[i].address);
1929
1930 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001931 if (!client_opts.ch_bind_count) {
1932 free(client_opts.ch_binds);
1933 client_opts.ch_binds = NULL;
1934 } else if (i < client_opts.ch_bind_count) {
1935 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1936 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1937 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001938
1939 ret = 0;
1940 }
1941 }
1942 }
1943
1944 return ret;
1945}
1946
1947API int
1948nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1949{
1950 int sock;
1951 char *host = NULL;
1952 uint16_t port, idx;
1953
Michal Vasko45e53ae2016-04-07 11:46:03 +02001954 if (!client_opts.ch_binds) {
1955 ERRINIT;
1956 return -1;
1957 } else if (!session) {
1958 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001959 return -1;
1960 }
1961
1962 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1963
Michal Vasko50456e82016-02-02 12:16:08 +01001964 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001965 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001966 return sock;
1967 }
1968
Radek Krejci53691be2016-02-22 13:58:37 +01001969#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001970 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001971 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001972 } else
1973#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001974#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001975 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001976 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001977 } else
1978#endif
1979 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001980 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001981 *session = NULL;
1982 }
1983
1984 free(host);
1985
1986 if (!(*session)) {
1987 return -1;
1988 }
1989
1990 return 1;
1991}
1992
Radek Krejci53691be2016-02-22 13:58:37 +01001993#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001994
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001995API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001996nc_session_get_cpblts(const struct nc_session *session)
1997{
1998 if (!session) {
1999 ERRARG("session");
2000 return NULL;
2001 }
2002
Michal Vasko1b2ddc92017-05-24 08:59:49 +02002003 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02002004}
2005
2006API const char *
2007nc_session_cpblt(const struct nc_session *session, const char *capab)
2008{
2009 int i, len;
2010
2011 if (!session) {
2012 ERRARG("session");
2013 return NULL;
2014 } else if (!capab) {
2015 ERRARG("capab");
2016 return NULL;
2017 }
2018
2019 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002020 for (i = 0; session->opts.client.cpblts[i]; ++i) {
2021 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
2022 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02002023 }
2024 }
2025
2026 return NULL;
2027}
2028
Michal Vasko9cd26a82016-05-31 08:58:48 +02002029API int
2030nc_session_ntf_thread_running(const struct nc_session *session)
2031{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002032 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02002033 ERRARG("session");
2034 return 0;
2035 }
2036
Michal Vasko2e6defd2016-10-07 15:48:15 +02002037 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02002038}
2039
Michal Vaskob7558c52016-02-26 15:04:19 +01002040API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002041nc_client_init(void)
2042{
2043 nc_init();
2044}
2045
2046API void
Michal Vaskob7558c52016-02-26 15:04:19 +01002047nc_client_destroy(void)
2048{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02002049 nc_client_set_schema_searchpath(NULL);
2050#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
2051 nc_client_ch_del_bind(NULL, 0, 0);
2052#endif
2053#ifdef NC_ENABLED_SSH
2054 nc_client_ssh_destroy_opts();
2055#endif
2056#ifdef NC_ENABLED_TLS
2057 nc_client_tls_destroy_opts();
2058#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002059 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01002060}
2061
Michal Vasko086311b2016-01-08 09:53:11 +01002062API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002063nc_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 +01002064{
2065 struct lyxml_elem *xml;
2066 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2067
Michal Vasko45e53ae2016-04-07 11:46:03 +02002068 if (!session) {
2069 ERRARG("session");
2070 return NC_MSG_ERROR;
2071 } else if (!rpc) {
2072 ERRARG("rpc");
2073 return NC_MSG_ERROR;
2074 } else if (!reply) {
2075 ERRARG("reply");
2076 return NC_MSG_ERROR;
2077 } else if (parseroptions & LYD_OPT_TYPEMASK) {
2078 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01002079 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002080 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01002081 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002082 return NC_MSG_ERROR;
2083 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002084 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01002085 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
2086 parseroptions &= LYD_OPT_STRICT;
2087 }
Michal Vasko41adf392017-01-17 10:39:04 +01002088 /* no mechanism to check external dependencies is provided */
2089 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01002090 *reply = NULL;
2091
2092 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002093
Michal Vasko21fc9cb2019-06-20 10:15:59 +02002094 if (msgtype == NC_MSG_REPLY) {
Michal Vaskoeee99412016-11-21 10:19:43 +01002095 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01002096 lyxml_free(session->ctx, xml);
2097 if (!(*reply)) {
2098 return NC_MSG_ERROR;
2099 }
2100 }
2101
2102 return msgtype;
2103}
2104
2105API NC_MSG_TYPE
2106nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
2107{
2108 struct lyxml_elem *xml, *ev_time;
2109 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2110
Michal Vasko45e53ae2016-04-07 11:46:03 +02002111 if (!session) {
2112 ERRARG("session");
2113 return NC_MSG_ERROR;
2114 } else if (!notif) {
2115 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01002116 return NC_MSG_ERROR;
2117 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002118 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002119 return NC_MSG_ERROR;
2120 }
2121
2122 msgtype = get_msg(session, timeout, 0, &xml);
2123
2124 if (msgtype == NC_MSG_NOTIF) {
2125 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002126 if (!*notif) {
2127 ERRMEM;
2128 lyxml_free(session->ctx, xml);
2129 return NC_MSG_ERROR;
2130 }
Michal Vasko086311b2016-01-08 09:53:11 +01002131
2132 /* eventTime */
2133 LY_TREE_FOR(xml->child, ev_time) {
2134 if (!strcmp(ev_time->name, "eventTime")) {
2135 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
2136 /* lyd_parse does not know this element */
2137 lyxml_free(session->ctx, ev_time);
2138 break;
2139 }
2140 }
2141 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01002142 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002143 goto fail;
2144 }
2145
2146 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01002147 (*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 +01002148 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002149 lyxml_free(session->ctx, xml);
2150 xml = NULL;
2151 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01002152 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002153 goto fail;
2154 }
2155 }
2156
2157 return msgtype;
2158
2159fail:
2160 lydict_remove(session->ctx, (*notif)->datetime);
2161 lyd_free((*notif)->tree);
2162 free(*notif);
2163 *notif = NULL;
2164 lyxml_free(session->ctx, xml);
2165
2166 return NC_MSG_ERROR;
2167}
2168
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002169static void *
2170nc_recv_notif_thread(void *arg)
2171{
2172 struct nc_ntf_thread_arg *ntarg;
2173 struct nc_session *session;
2174 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
2175 struct nc_notif *notif;
2176 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002177 pthread_t *ntf_tid;
2178
2179 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002180
2181 ntarg = (struct nc_ntf_thread_arg *)arg;
2182 session = ntarg->session;
2183 notif_clb = ntarg->notif_clb;
2184 free(ntarg);
2185
Michal Vasko131120a2018-05-29 15:44:02 +02002186 /* remember our allocated tid, we will be freeing it */
2187 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
2188
Michal Vasko2e6defd2016-10-07 15:48:15 +02002189 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002190 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002191 if (msgtype == NC_MSG_NOTIF) {
2192 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002193 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2194 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2195 nc_notif_free(notif);
2196 break;
2197 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002198 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002199 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002200 /* quit this thread once the session is broken */
2201 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002202 }
2203
2204 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2205 }
2206
Michal Vasko0651c902016-05-19 15:55:42 +02002207 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002208 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02002209 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002210 return NULL;
2211}
2212
2213API int
2214nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2215{
2216 struct nc_ntf_thread_arg *ntarg;
2217 int ret;
2218
Michal Vasko45e53ae2016-04-07 11:46:03 +02002219 if (!session) {
2220 ERRARG("session");
2221 return -1;
2222 } else if (!notif_clb) {
2223 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002224 return -1;
2225 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2226 ERR("Session %u: invalid session to receive Notifications.", session->id);
2227 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002228 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002229 ERR("Session %u: separate notification thread is already running.", session->id);
2230 return -1;
2231 }
2232
2233 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002234 if (!ntarg) {
2235 ERRMEM;
2236 return -1;
2237 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002238 ntarg->session = session;
2239 ntarg->notif_clb = notif_clb;
2240
2241 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002242 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2243 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002244 ERRMEM;
2245 free(ntarg);
2246 return -1;
2247 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002248
Michal Vasko2e6defd2016-10-07 15:48:15 +02002249 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002250 if (ret) {
2251 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2252 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002253 free((pthread_t *)session->opts.client.ntf_tid);
2254 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002255 return -1;
2256 }
2257
2258 return 0;
2259}
2260
Michal Vasko086311b2016-01-08 09:53:11 +01002261API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002262nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002263{
2264 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002265 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002266 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002267 struct nc_rpc_getconfig *rpc_gc;
2268 struct nc_rpc_edit *rpc_e;
2269 struct nc_rpc_copy *rpc_cp;
2270 struct nc_rpc_delete *rpc_del;
2271 struct nc_rpc_lock *rpc_lock;
2272 struct nc_rpc_get *rpc_g;
2273 struct nc_rpc_kill *rpc_k;
2274 struct nc_rpc_commit *rpc_com;
2275 struct nc_rpc_cancel *rpc_can;
2276 struct nc_rpc_validate *rpc_val;
2277 struct nc_rpc_getschema *rpc_gs;
2278 struct nc_rpc_subscribe *rpc_sub;
2279 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01002280 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
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 Vasko90e8e692016-07-13 12:27:57 +02002298 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002299 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002300 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01002301 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002302 return NC_MSG_ERROR;
2303 }
2304 }
2305
2306 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002307 case NC_RPC_ACT_GENERIC:
2308 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002309
2310 if (rpc_gen->has_data) {
2311 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002312 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002313 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002314 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 +01002315 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002316 if (!data) {
2317 return NC_MSG_ERROR;
2318 }
Michal Vasko086311b2016-01-08 09:53:11 +01002319 }
2320 break;
2321
2322 case NC_RPC_GETCONFIG:
2323 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2324
2325 data = lyd_new(NULL, ietfnc, "get-config");
2326 node = lyd_new(data, ietfnc, "source");
2327 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
2328 if (!node) {
2329 lyd_free(data);
2330 return NC_MSG_ERROR;
2331 }
2332 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002333 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002334 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002335 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002336 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002337 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002338 lyd_insert_attr(node, NULL, "type", "xpath");
2339 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002340 }
2341 if (!node) {
2342 lyd_free(data);
2343 return NC_MSG_ERROR;
2344 }
2345 }
2346
2347 if (rpc_gc->wd_mode) {
2348 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002349 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002350 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002351 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002352 return NC_MSG_ERROR;
2353 }
2354 }
2355 switch (rpc_gc->wd_mode) {
2356 case NC_WD_UNKNOWN:
2357 /* cannot get here */
2358 break;
2359 case NC_WD_ALL:
2360 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2361 break;
2362 case NC_WD_ALL_TAG:
2363 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2364 break;
2365 case NC_WD_TRIM:
2366 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2367 break;
2368 case NC_WD_EXPLICIT:
2369 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2370 break;
2371 }
2372 if (!node) {
2373 lyd_free(data);
2374 return NC_MSG_ERROR;
2375 }
2376 }
2377 break;
2378
2379 case NC_RPC_EDIT:
2380 rpc_e = (struct nc_rpc_edit *)rpc;
2381
2382 data = lyd_new(NULL, ietfnc, "edit-config");
2383 node = lyd_new(data, ietfnc, "target");
2384 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
2385 if (!node) {
2386 lyd_free(data);
2387 return NC_MSG_ERROR;
2388 }
2389
2390 if (rpc_e->default_op) {
2391 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
2392 if (!node) {
2393 lyd_free(data);
2394 return NC_MSG_ERROR;
2395 }
2396 }
2397
2398 if (rpc_e->test_opt) {
2399 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
2400 if (!node) {
2401 lyd_free(data);
2402 return NC_MSG_ERROR;
2403 }
2404 }
2405
2406 if (rpc_e->error_opt) {
2407 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
2408 if (!node) {
2409 lyd_free(data);
2410 return NC_MSG_ERROR;
2411 }
2412 }
2413
Michal Vasko7793bc62016-09-16 11:58:41 +02002414 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002415 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002416 } else {
2417 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
2418 }
2419 if (!node) {
2420 lyd_free(data);
2421 return NC_MSG_ERROR;
2422 }
2423 break;
2424
2425 case NC_RPC_COPY:
2426 rpc_cp = (struct nc_rpc_copy *)rpc;
2427
2428 data = lyd_new(NULL, ietfnc, "copy-config");
2429 node = lyd_new(data, ietfnc, "target");
2430 if (rpc_cp->url_trg) {
2431 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
2432 } else {
2433 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
2434 }
2435 if (!node) {
2436 lyd_free(data);
2437 return NC_MSG_ERROR;
2438 }
2439
2440 node = lyd_new(data, ietfnc, "source");
2441 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002442 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002443 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002444 } else {
2445 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
2446 }
2447 } else {
2448 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
2449 }
2450 if (!node) {
2451 lyd_free(data);
2452 return NC_MSG_ERROR;
2453 }
2454
2455 if (rpc_cp->wd_mode) {
2456 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002457 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002458 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002459 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002460 return NC_MSG_ERROR;
2461 }
2462 }
2463 switch (rpc_cp->wd_mode) {
2464 case NC_WD_UNKNOWN:
2465 /* cannot get here */
2466 break;
2467 case NC_WD_ALL:
2468 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2469 break;
2470 case NC_WD_ALL_TAG:
2471 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2472 break;
2473 case NC_WD_TRIM:
2474 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2475 break;
2476 case NC_WD_EXPLICIT:
2477 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2478 break;
2479 }
2480 if (!node) {
2481 lyd_free(data);
2482 return NC_MSG_ERROR;
2483 }
2484 }
2485 break;
2486
2487 case NC_RPC_DELETE:
2488 rpc_del = (struct nc_rpc_delete *)rpc;
2489
2490 data = lyd_new(NULL, ietfnc, "delete-config");
2491 node = lyd_new(data, ietfnc, "target");
2492 if (rpc_del->url) {
2493 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2494 } else {
2495 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2496 }
2497 if (!node) {
2498 lyd_free(data);
2499 return NC_MSG_ERROR;
2500 }
2501 break;
2502
2503 case NC_RPC_LOCK:
2504 rpc_lock = (struct nc_rpc_lock *)rpc;
2505
2506 data = lyd_new(NULL, ietfnc, "lock");
2507 node = lyd_new(data, ietfnc, "target");
2508 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2509 if (!node) {
2510 lyd_free(data);
2511 return NC_MSG_ERROR;
2512 }
2513 break;
2514
2515 case NC_RPC_UNLOCK:
2516 rpc_lock = (struct nc_rpc_lock *)rpc;
2517
2518 data = lyd_new(NULL, ietfnc, "unlock");
2519 node = lyd_new(data, ietfnc, "target");
2520 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2521 if (!node) {
2522 lyd_free(data);
2523 return NC_MSG_ERROR;
2524 }
2525 break;
2526
2527 case NC_RPC_GET:
2528 rpc_g = (struct nc_rpc_get *)rpc;
2529
2530 data = lyd_new(NULL, ietfnc, "get");
2531 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002532 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002533 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002534 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002535 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002536 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002537 lyd_insert_attr(node, NULL, "type", "xpath");
2538 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002539 }
2540 if (!node) {
2541 lyd_free(data);
2542 return NC_MSG_ERROR;
2543 }
2544 }
2545
2546 if (rpc_g->wd_mode) {
2547 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002548 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002549 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002550 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002551 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002552 return NC_MSG_ERROR;
2553 }
2554 }
2555 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002556 case NC_WD_ALL:
2557 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2558 break;
2559 case NC_WD_ALL_TAG:
2560 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2561 break;
2562 case NC_WD_TRIM:
2563 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2564 break;
2565 case NC_WD_EXPLICIT:
2566 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2567 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002568 default:
2569 /* cannot get here */
2570 node = NULL;
2571 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002572 }
2573 if (!node) {
2574 lyd_free(data);
2575 return NC_MSG_ERROR;
2576 }
2577 }
2578 break;
2579
2580 case NC_RPC_KILL:
2581 rpc_k = (struct nc_rpc_kill *)rpc;
2582
2583 data = lyd_new(NULL, ietfnc, "kill-session");
2584 sprintf(str, "%u", rpc_k->sid);
2585 lyd_new_leaf(data, ietfnc, "session-id", str);
2586 break;
2587
2588 case NC_RPC_COMMIT:
2589 rpc_com = (struct nc_rpc_commit *)rpc;
2590
2591 data = lyd_new(NULL, ietfnc, "commit");
2592 if (rpc_com->confirmed) {
2593 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2594 }
2595
2596 if (rpc_com->confirm_timeout) {
2597 sprintf(str, "%u", rpc_com->confirm_timeout);
2598 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2599 }
2600
2601 if (rpc_com->persist) {
2602 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2603 if (!node) {
2604 lyd_free(data);
2605 return NC_MSG_ERROR;
2606 }
2607 }
2608
2609 if (rpc_com->persist_id) {
2610 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2611 if (!node) {
2612 lyd_free(data);
2613 return NC_MSG_ERROR;
2614 }
2615 }
2616 break;
2617
2618 case NC_RPC_DISCARD:
2619 data = lyd_new(NULL, ietfnc, "discard-changes");
2620 break;
2621
2622 case NC_RPC_CANCEL:
2623 rpc_can = (struct nc_rpc_cancel *)rpc;
2624
2625 data = lyd_new(NULL, ietfnc, "cancel-commit");
2626 if (rpc_can->persist_id) {
2627 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2628 if (!node) {
2629 lyd_free(data);
2630 return NC_MSG_ERROR;
2631 }
2632 }
2633 break;
2634
2635 case NC_RPC_VALIDATE:
2636 rpc_val = (struct nc_rpc_validate *)rpc;
2637
2638 data = lyd_new(NULL, ietfnc, "validate");
2639 node = lyd_new(data, ietfnc, "source");
2640 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002641 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002642 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002643 } else {
2644 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2645 }
2646 } else {
2647 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2648 }
2649 if (!node) {
2650 lyd_free(data);
2651 return NC_MSG_ERROR;
2652 }
2653 break;
2654
2655 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002656 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002657 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002658 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002659 return NC_MSG_ERROR;
2660 }
2661
2662 rpc_gs = (struct nc_rpc_getschema *)rpc;
2663
2664 data = lyd_new(NULL, ietfncmon, "get-schema");
2665 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2666 if (!node) {
2667 lyd_free(data);
2668 return NC_MSG_ERROR;
2669 }
2670 if (rpc_gs->version) {
2671 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2672 if (!node) {
2673 lyd_free(data);
2674 return NC_MSG_ERROR;
2675 }
2676 }
2677 if (rpc_gs->format) {
2678 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2679 if (!node) {
2680 lyd_free(data);
2681 return NC_MSG_ERROR;
2682 }
2683 }
2684 break;
2685
2686 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002687 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002688 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002689 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002690 return NC_MSG_ERROR;
2691 }
2692
2693 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2694
2695 data = lyd_new(NULL, notifs, "create-subscription");
2696 if (rpc_sub->stream) {
2697 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2698 if (!node) {
2699 lyd_free(data);
2700 return NC_MSG_ERROR;
2701 }
2702 }
2703
2704 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002705 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002706 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002707 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002708 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002709 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002710 lyd_insert_attr(node, NULL, "type", "xpath");
2711 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002712 }
2713 if (!node) {
2714 lyd_free(data);
2715 return NC_MSG_ERROR;
2716 }
2717 }
2718
2719 if (rpc_sub->start) {
2720 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2721 if (!node) {
2722 lyd_free(data);
2723 return NC_MSG_ERROR;
2724 }
2725 }
2726
2727 if (rpc_sub->stop) {
2728 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2729 if (!node) {
2730 lyd_free(data);
2731 return NC_MSG_ERROR;
2732 }
2733 }
2734 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002735 default:
2736 ERRINT;
2737 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002738 }
2739
Michal Vasko131120a2018-05-29 15:44:02 +02002740 if (!data) {
2741 /* error was already printed */
2742 return NC_MSG_ERROR;
2743 }
2744
Michal Vasko41adf392017-01-17 10:39:04 +01002745 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2746 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002747 if (dofree) {
2748 lyd_free(data);
2749 }
Michal Vasko086311b2016-01-08 09:53:11 +01002750 return NC_MSG_ERROR;
2751 }
2752
Michal Vasko131120a2018-05-29 15:44:02 +02002753 /* send RPC, store its message ID */
2754 r = nc_send_msg_io(session, timeout, data);
2755 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002756
Radek Krejcib4b19062018-02-07 16:33:06 +01002757 if (dofree) {
2758 lyd_free(data);
2759 }
Michal Vasko086311b2016-01-08 09:53:11 +01002760
Michal Vasko131120a2018-05-29 15:44:02 +02002761 if (r == NC_MSG_RPC) {
2762 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002763 }
Michal Vasko131120a2018-05-29 15:44:02 +02002764 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002765}
Michal Vaskode2946c2017-01-12 12:19:26 +01002766
2767API void
2768nc_client_session_set_not_strict(struct nc_session *session)
2769{
2770 if (session->side != NC_CLIENT) {
2771 ERRARG("session");
2772 return;
2773 }
2774
2775 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2776}