blob: 02221664ac8f840e8d1c7a3cdab95cc3443ca760 [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 Vasko086311b2016-01-08 09:53:11 +010021#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020026#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <sys/types.h>
28#include <unistd.h>
29#include <arpa/inet.h>
30#include <poll.h>
31
32#include <libyang/libyang.h>
33
Michal Vasko086311b2016-01-08 09:53:11 +010034#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010035#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010036#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010037
Michal Vasko80ef5d22016-01-18 09:21:02 +010038static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
39
Radek Krejci62aa0642017-05-25 16:33:49 +020040#ifdef NC_ENABLED_SSH
41int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
42char *sshauth_password(const char *username, const char *hostname, void *priv);
43char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
44char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
45#endif /* NC_ENABLED_SSH */
46
47static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
48static pthread_key_t nc_client_context_key;
49#ifdef __linux__
50static struct nc_client_context context_main = {
51 /* .opts zeroed */
52#ifdef NC_ENABLED_SSH
53 .ssh_opts = {
54 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
55 .auth_hostkey_check = sshauth_hostkey_check,
56 .auth_password = sshauth_password,
57 .auth_interactive = sshauth_interactive,
58 .auth_privkey_passphrase = sshauth_privkey_passphrase
59 },
60 .ssh_ch_opts = {
61 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
62 .auth_hostkey_check = sshauth_hostkey_check,
63 .auth_password = sshauth_password,
64 .auth_interactive = sshauth_interactive,
65 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020066 },
Radek Krejci62aa0642017-05-25 16:33:49 +020067#endif /* NC_ENABLED_SSH */
68 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020069 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020070};
71#endif
72
73static void
74nc_client_context_free(void *ptr)
75{
76 struct nc_client_context *c = (struct nc_client_context *)ptr;
77
Radek Krejci5cebc6b2017-05-26 13:24:38 +020078 if (--(c->refcount)) {
79 /* still used */
80 return;
81 }
82
Radek Krejci62aa0642017-05-25 16:33:49 +020083#ifdef __linux__
84 /* in __linux__ we use static memory in the main thread,
85 * so this check is for programs terminating the main()
86 * function by pthread_exit() :)
87 */
88 if (c != &context_main)
89#endif
90 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +020091 /* for the main thread the same is done in nc_client_destroy() */
Radek Krejci62aa0642017-05-25 16:33:49 +020092 nc_client_set_schema_searchpath(NULL);
93#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
94 nc_client_ch_del_bind(NULL, 0, 0);
95#endif
96#ifdef NC_ENABLED_SSH
97 nc_client_ssh_destroy_opts();
98#endif
99#ifdef NC_ENABLED_TLS
100 nc_client_tls_destroy_opts();
101#endif
102 free(c);
103 }
104}
105
106static void
107nc_client_context_createkey(void)
108{
109 int r;
110
111 /* initiate */
112 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
113 pthread_setspecific(nc_client_context_key, NULL);
114}
115
116struct nc_client_context *
117nc_client_context_location(void)
118{
119 struct nc_client_context *e;
120
121 pthread_once(&nc_client_context_once, nc_client_context_createkey);
122 e = pthread_getspecific(nc_client_context_key);
123 if (!e) {
124 /* prepare ly_err storage */
125#ifdef __linux__
126 if (getpid() == syscall(SYS_gettid)) {
127 /* main thread - use global variable instead of thread-specific variable. */
128 e = &context_main;
129 } else
130#endif /* __linux__ */
131 {
132 e = calloc(1, sizeof *e);
133 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200134 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200135#ifdef NC_ENABLED_SSH
136 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
137 e->ssh_opts.auth_pref[0].value = 3;
138 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
139 e->ssh_opts.auth_pref[1].value = 2;
140 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
141 e->ssh_opts.auth_pref[2].value = 1;
142 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
143 e->ssh_opts.auth_password = sshauth_password;
144 e->ssh_opts.auth_interactive = sshauth_interactive;
145 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
146
147 /* callhome settings are the same except the inverted auth methods preferences */
148 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
149 e->ssh_ch_opts.auth_pref[0].value = 1;
150 e->ssh_ch_opts.auth_pref[1].value = 2;
151 e->ssh_ch_opts.auth_pref[2].value = 3;
152#endif /* NC_ENABLED_SSH */
153 }
154 pthread_setspecific(nc_client_context_key, e);
155 }
156
157 return e;
158}
159
160#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100161
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162API void *
163nc_client_get_thread_context(void)
164{
165 return nc_client_context_location();
166}
167
168API void
169nc_client_set_thread_context(void *context)
170{
171 struct nc_client_context *old, *new;
172
173 if (!context) {
174 ERRARG(context);
175 return;
176 }
177
178 new = (struct nc_client_context *)context;
179 old = nc_client_context_location();
180 if (old == new) {
181 /* nothing to change */
182 return;
183 }
184
185 /* replace old by new, increase reference counter in the newly set context */
186 nc_client_context_free(old);
187 new->refcount++;
188 pthread_setspecific(nc_client_context_key, new);
189}
190
Radek Krejcifd5b6682017-06-13 15:52:53 +0200191int
192nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
193{
194 /* assign context (dicionary needed for handshake) */
195 if (!ctx) {
Radek Krejci3222b7d2017-09-21 16:04:30 +0200196 ctx = ly_ctx_new(NULL, 0);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200197 if (!ctx) {
198 return EXIT_FAILURE;
199 }
200
201 /* user path must be first, the first path is used to store schemas retreived via get-schema */
202 if (client_opts.schema_searchpath) {
203 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
204 }
205 ly_ctx_set_searchdir(ctx, SCHEMAS_DIR);
206
207 /* set callback for getting schemas, if provided */
208 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
209 } else {
210 session->flags |= NC_SESSION_SHAREDCTX;
211 }
212
213 session->ctx = ctx;
214
215 return EXIT_SUCCESS;
216}
217
Michal Vasko086311b2016-01-08 09:53:11 +0100218API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100219nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100220{
Michal Vasko3031aae2016-01-27 16:07:18 +0100221 if (client_opts.schema_searchpath) {
222 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 }
Michal Vasko086311b2016-01-08 09:53:11 +0100224
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100225 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100226 client_opts.schema_searchpath = strdup(path);
227 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100228 ERRMEM;
229 return 1;
230 }
231 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100232 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100233 }
234
235 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100236}
237
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100238API const char *
239nc_client_get_schema_searchpath(void)
240{
241 return client_opts.schema_searchpath;
242}
243
Radek Krejcifd5b6682017-06-13 15:52:53 +0200244API int
245nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100246{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200247 client_opts.schema_clb = clb;
248 if (clb) {
249 client_opts.schema_clb_data = user_data;
250 } else {
251 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100252 }
253
254 return 0;
255}
256
Radek Krejcifd5b6682017-06-13 15:52:53 +0200257API ly_module_imp_clb
258nc_client_get_schema_callback(void **user_data)
259{
260 if (user_data) {
261 (*user_data) = client_opts.schema_clb_data;
262 }
263 return client_opts.schema_clb;
264}
265
Michal Vasko1aaa6602016-02-09 11:04:33 +0100266/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100267static int
Michal Vasko1b2ddc92017-05-24 08:59:49 +0200268ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100269{
270 int i;
271 const struct lys_module *ietfnc;
272
Radek Krejci3222b7d2017-09-21 16:04:30 +0200273 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100274 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100275 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
276 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100277 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100278 }
279 }
280 if (!ietfnc) {
281 ERR("Loading base NETCONF schema failed.");
282 return 1;
283 }
284
285 /* set supported capabilities from ietf-netconf */
286 for (i = 0; cpblts[i]; ++i) {
287 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
288 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
289 lys_features_enable(ietfnc, "writable-running");
290 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
291 lys_features_enable(ietfnc, "candidate");
292 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
293 lys_features_enable(ietfnc, "confirmed-commit");
294 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
295 lys_features_enable(ietfnc, "rollback-on-error");
296 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
297 lys_features_enable(ietfnc, "validate");
298 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
299 lys_features_enable(ietfnc, "startup");
300 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
301 lys_features_enable(ietfnc, "url");
302 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
303 lys_features_enable(ietfnc, "xpath");
304 }
305 }
306 }
307
308 return 0;
309}
310
311static char *
Radek Krejcifd5b6682017-06-13 15:52:53 +0200312getschema_module_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev,
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200313 void *user_data, LYS_INFORMAT *format, void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100314{
315 struct nc_session *session = (struct nc_session *)user_data;
316 struct nc_rpc *rpc;
317 struct nc_reply *reply;
318 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200319 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200320 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100321 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200322 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100323 uint64_t msgid;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200324 char *filename = NULL;
325 const char * const *searchdirs;
326 FILE *output;
Michal Vasko086311b2016-01-08 09:53:11 +0100327
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200328 if (submod_name) {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200329 rpc = nc_rpc_getschema(submod_name, submod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200330 } else {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200331 rpc = nc_rpc_getschema(mod_name, mod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200332 }
Michal Vasko086311b2016-01-08 09:53:11 +0100333
334 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
335 usleep(1000);
336 }
337 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100338 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100339 nc_rpc_free(rpc);
340 return NULL;
341 }
342
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200343 do {
Michal Vaskof471fa02017-02-15 10:48:12 +0100344 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200345 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100346 nc_rpc_free(rpc);
347 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100348 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100349 return NULL;
350 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100351 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100352 return NULL;
353 }
354
Michal Vasko998ba412016-09-16 12:00:07 +0200355 switch (reply->type) {
356 case NC_RPL_OK:
357 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", session->id);
358 nc_reply_free(reply);
359 return NULL;
360 case NC_RPL_DATA:
361 /* fine */
362 break;
363 case NC_RPL_ERROR:
364 error_rpl = (struct nc_reply_error *)reply;
365 if (error_rpl->count) {
366 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
367 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
368 } else {
369 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", session->id);
370 }
371 nc_reply_free(reply);
372 return NULL;
373 case NC_RPL_NOTIF:
374 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100375 nc_reply_free(reply);
376 return NULL;
377 }
378
Michal Vasko086311b2016-01-08 09:53:11 +0100379 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200380 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
381 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
382 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
383 nc_reply_free(reply);
384 return NULL;
385 }
Radek Krejci539efb62016-08-24 15:05:16 +0200386 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
387 switch (get_schema_data->value_type) {
388 case LYD_ANYDATA_CONSTSTRING:
389 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200390 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200391 break;
392 case LYD_ANYDATA_DATATREE:
393 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
394 break;
395 case LYD_ANYDATA_XML:
396 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
397 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200398 case LYD_ANYDATA_JSON:
399 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200400 case LYD_ANYDATA_SXML:
401 case LYD_ANYDATA_SXMLD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200402 ERRINT;
403 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200404 }
Michal Vasko086311b2016-01-08 09:53:11 +0100405 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100406 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200407 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100408
Radek Krejcifd5b6682017-06-13 15:52:53 +0200409 /* try to store the model_data into local schema repository */
410 if (model_data) {
411 searchdirs = ly_ctx_get_searchdirs(session->ctx);
412 asprintf(&filename, "%s/%s%s%s.yang", searchdirs ? searchdirs[0] : ".", mod_name,
413 mod_rev ? "@" : "", mod_rev ? mod_rev : "");
414 output = fopen(filename, "w");
415 if (!output) {
416 WRN("Unable to store \"%s\" as a local copy of schema retreived via <get-schema> (%s).",
417 filename, strerror(errno));
418 } else {
419 fputs(model_data, output);
420 fclose(output);
421 }
422 free(filename);
423 }
424
Michal Vasko086311b2016-01-08 09:53:11 +0100425 return model_data;
426}
427
Radek Krejcifd5b6682017-06-13 15:52:53 +0200428/* SCHEMAS_DIR not used (implicitly) */
429static int
430nc_ctx_fill_cpblts(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
431{
432 int ret = 1;
433 LY_LOG_LEVEL verb;
434 const struct lys_module *mod;
435 char *ptr, *ptr2;
436 const char *module_cpblt;
437 char *name = NULL, *revision = NULL, *features = NULL;
438 unsigned int u;
439
440 for (u = 0; session->opts.client.cpblts[u]; ++u) {
441 module_cpblt = strstr(session->opts.client.cpblts[u], "module=");
442 /* this capability requires a module */
443 if (!module_cpblt) {
444 continue;
445 }
446
447 /* get module name */
448 ptr = (char *)module_cpblt + 7;
449 ptr2 = strchr(ptr, '&');
450 if (!ptr2) {
451 ptr2 = ptr + strlen(ptr);
452 }
453 free(name);
454 name = strndup(ptr, ptr2 - ptr);
455
456 /* get module revision */
457 free(revision); revision = NULL;
458 ptr = strstr(module_cpblt, "revision=");
459 if (ptr) {
460 ptr += 9;
461 ptr2 = strchr(ptr, '&');
462 if (!ptr2) {
463 ptr2 = ptr + strlen(ptr);
464 }
465 revision = strndup(ptr, ptr2 - ptr);
466 }
467
Radek Krejci3222b7d2017-09-21 16:04:30 +0200468 mod = ly_ctx_get_module(session->ctx, name, revision, 0);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200469 if (mod) {
470 if (!mod->implemented) {
471 /* make the present module implemented */
472 if (lys_set_implemented(mod)) {
473 ERR("Failed to implement model \"%s\".", mod->name);
474 goto cleanup;
475 }
476 }
477 } else {
478 /* missing implemented module, load it ... */
479
480 /* expecting errors here */
481 verb = ly_verb(LY_LLSILENT);
482
483 /* 1) using only searchpaths */
484 mod = ly_ctx_load_module(session->ctx, name, revision);
485
486 /* 2) using user callback */
487 if (!mod && user_clb) {
488 ly_ctx_set_module_imp_clb(session->ctx, user_clb, user_data);
489 mod = ly_ctx_load_module(session->ctx, name, revision);
490 }
491
492 /* 3) using get-schema callback */
493 if (!mod) {
494 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
495 mod = ly_ctx_load_module(session->ctx, name, revision);
496 }
497
498 /* revert the changed verbosity level */
499 ly_verb(verb);
500
501 /* unset callback back to use searchpath */
502 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
503 }
504
505 if (!mod) {
Michal Vasko60f66602017-10-17 13:52:18 +0200506 if (session->status != NC_STATUS_RUNNING) {
507 /* something bad heppened, discard the session */
508 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
509 ret = 1;
510 goto cleanup;
511 }
512
Radek Krejcifd5b6682017-06-13 15:52:53 +0200513 /* all loading ways failed, the schema will be ignored in the received data */
514 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
515 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
516
517 /* TODO: maybe re-print hidden error messages */
518 } else {
519 /* set features - first disable all to enable specified then */
520 lys_features_disable(mod, "*");
521
522 ptr = strstr(module_cpblt, "features=");
523 if (ptr) {
524 ptr += 9;
525 ptr2 = strchr(ptr, '&');
526 if (!ptr2) {
527 ptr2 = ptr + strlen(ptr);
528 }
529 free(features);
530 features = strndup(ptr, ptr2 - ptr);
531
532 /* basically manual strtok_r (to avoid macro) */
533 ptr2 = features;
534 for (ptr = features; *ptr; ++ptr) {
535 if (*ptr == ',') {
536 *ptr = '\0';
537 /* remember last feature */
538 ptr2 = ptr + 1;
539 }
540 }
541
542 ptr = features;
543 while (1) {
544 lys_features_enable(mod, ptr);
545 if (ptr != ptr2) {
546 ptr += strlen(ptr) + 1;
547 } else {
548 break;
549 }
550 }
551 }
552 }
553 }
554
555 ret = 0;
556
557cleanup:
558 free(name);
559 free(revision);
560 free(features);
561
562 return ret;
563}
564
565static int
566nc_ctx_fill_yl(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
567{
568 int ret = 1;
569 LY_LOG_LEVEL verb;
570 struct nc_rpc *rpc = NULL;
571 struct nc_reply *reply = NULL;
572 struct nc_reply_error *error_rpl;
573 struct nc_reply_data *data_rpl;
574 NC_MSG_TYPE msg;
575 uint64_t msgid;
576 struct lyd_node *data = NULL, *iter;
577 struct ly_set *modules = NULL, *imports = NULL, *features = NULL;
578 unsigned int u, v;
579 const char *name, *revision;
580 int implemented, imports_flag = 0;
581 const struct lys_module *mod;
582
583 /* get yang-library data from the server */
584 rpc = nc_rpc_get("/ietf-yang-library:*//.", 0, NC_PARAMTYPE_CONST);
585 if (!rpc) {
586 goto cleanup;
587 }
588
589 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
590 usleep(1000);
591 }
592 if (msg == NC_MSG_ERROR) {
Michal Vasko60f66602017-10-17 13:52:18 +0200593 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200594 session->id);
595 goto cleanup;
596 }
597
598 do {
599 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
600 } while (msg == NC_MSG_NOTIF);
601 if (msg == NC_MSG_WOULDBLOCK) {
602 ERR("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
603 goto cleanup;
604 } else if (msg == NC_MSG_ERROR) {
605 ERR("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
606 goto cleanup;
607 }
608
609 switch (reply->type) {
610 case NC_RPL_OK:
611 ERR("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
612 goto cleanup;
613 case NC_RPL_DATA:
614 /* fine */
615 break;
616 case NC_RPL_ERROR:
617 error_rpl = (struct nc_reply_error *)reply;
618 if (error_rpl->count) {
619 ERR("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
620 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
621 } else {
622 ERR("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
623 }
624 goto cleanup;
625 case NC_RPL_NOTIF:
626 ERR("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
627 goto cleanup;
628 }
629
630 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko09319322017-10-02 09:26:54 +0200631 if (!data_rpl->data || strcmp(data_rpl->data->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200632 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
633 goto cleanup;
634 }
635
Michal Vaskof4c8b792017-07-28 14:57:22 +0200636 modules = lyd_find_path(data_rpl->data, "/ietf-yang-library:modules-state/module");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200637 if (!modules || !modules->number) {
638 ERR("No yang-library modules information for session %u.", session->id);
639 goto cleanup;
640 }
641
642 features = ly_set_new();
643 imports = ly_set_new();
644
645parse:
646 for (u = modules->number - 1; u < modules->number; u--) {
647 name = revision = NULL;
648 ly_set_clean(features);
649 implemented = 0;
650
651 /* store the data */
652 LY_TREE_FOR(modules->set.d[u]->child, iter) {
653 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
654 /* ignore empty nodes */
655 continue;
656 }
657 if (!strcmp(iter->schema->name, "name")) {
658 name = ((struct lyd_node_leaf_list *)iter)->value_str;
659 } else if (!strcmp(iter->schema->name, "revision")) {
660 revision = ((struct lyd_node_leaf_list *)iter)->value_str;
661 } else if (!strcmp(iter->schema->name, "conformance-type")) {
662 implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
663 } else if (!strcmp(iter->schema->name, "feature")) {
664 ly_set_add(features, (void*)((struct lyd_node_leaf_list *)iter)->value_str, LY_SET_OPT_USEASLIST);
665 }
666 }
667
Radek Krejci3222b7d2017-09-21 16:04:30 +0200668 mod = ly_ctx_get_module(session->ctx, name, revision, 0);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200669 if (mod) {
670 if (implemented && !mod->implemented) {
671 /* make the present module implemented */
672 if (lys_set_implemented(mod)) {
673 ERR("Failed to implement model \"%s\".", mod->name);
674 ret = -1; /* fatal error, not caused just by missing schema but by something in the context */
675 goto cleanup;
676 }
677 }
678 } else if (!mod && implemented) {
679 /* missing implemented module, load it ... */
680
681 /* expecting errors here */
682 verb = ly_verb(LY_LLSILENT);
683
684 /* 1) using only searchpaths */
685 mod = ly_ctx_load_module(session->ctx, name, revision);
686
687 /* 2) using user callback */
688 if (!mod && user_clb) {
689 ly_ctx_set_module_imp_clb(session->ctx, user_clb, user_data);
690 mod = ly_ctx_load_module(session->ctx, name, revision);
691 }
692
693 /* 3) using get-schema callback */
694 if (!mod) {
695 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
696 mod = ly_ctx_load_module(session->ctx, name, revision);
697 }
698
699 /* revert the changed verbosity level */
700 ly_verb(verb);
701
702 /* unset callback back to use searchpath */
703 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
704 } else { /* !mod && !implemented - will be loaded automatically, but remember to set features in the end */
Michal Vasko3c556372017-08-09 10:17:34 +0200705 if (imports_flag) {
706 ERR("Module \"%s@%s\" is supposed to be imported, but no other module imports it.",
707 name, revision ? revision : "<latest>");
708 ret = -1;
709 goto cleanup;
710 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200711 ly_set_add(imports, modules->set.d[u], LY_SET_OPT_USEASLIST);
712 continue;
713 }
714
715 if (!mod) {
716 /* all loading ways failed, the schema will be ignored in the received data */
717 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
718 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
719
720 /* TODO: maybe re-print hidden error messages */
721 } else {
722 /* set features - first disable all to enable specified then */
723 lys_features_disable(mod, "*");
724 for (v = 0; v < features->number; v++) {
725 lys_features_enable(mod, (const char*)features->set.g[v]);
726 }
727 }
728 }
729
730 if (!imports_flag && imports->number) {
731 /* even imported modules should be now loaded as dependency, so just go through
732 * the parsing again and just set the features */
733 ly_set_free(modules);
734 modules = imports;
735 imports = NULL;
736 imports_flag = 1;
737 goto parse;
738 }
739
740 /* done */
741 ret = 0;
742
743cleanup:
744 nc_rpc_free(rpc);
745 nc_reply_free(reply);
746 lyd_free_withsiblings(data);
747
748 ly_set_free(modules);
749 ly_set_free(imports);
750 ly_set_free(features);
751
Michal Vasko60f66602017-10-17 13:52:18 +0200752 if (session->status != NC_STATUS_RUNNING) {
753 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
754 ret = -1;
755 }
756
Radek Krejcifd5b6682017-06-13 15:52:53 +0200757 return ret;
758}
759
Michal Vasko086311b2016-01-08 09:53:11 +0100760int
761nc_ctx_check_and_fill(struct nc_session *session)
762{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200763 int i, get_schema_support = 0, yanglib_support = 0, ret = -1, r;
Michal Vaskocdeee432017-01-13 13:51:01 +0100764 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100765 void *old_data = NULL;
766
Michal Vasko2e6defd2016-10-07 15:48:15 +0200767 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100768
Radek Krejcifd5b6682017-06-13 15:52:53 +0200769 /* store the original user's callback, here we will be switching between searchpath, user callback
770 * and get-schema callback */
771 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
772 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); /* unset callback, so we prefer local searchpath */
773
Michal Vasko086311b2016-01-08 09:53:11 +0100774 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200775 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200776 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100777 get_schema_support = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200778 if (yanglib_support) {
779 break;
780 }
781 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-yang-library?", 46)) {
782 yanglib_support = 1;
783 if (get_schema_support) {
784 break;
785 }
Michal Vasko086311b2016-01-08 09:53:11 +0100786 }
787 }
788
789 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200790 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200791 if (!lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100792 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200793 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100794 }
795 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200796 /* yang-library present does not need to be checked, it is one of the libyang's internal modules,
797 * so it is always present */
Michal Vasko086311b2016-01-08 09:53:11 +0100798
799 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200800 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200801 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100802 }
803
Radek Krejcifd5b6682017-06-13 15:52:53 +0200804 if (yanglib_support && get_schema_support) {
805 /* load schemas according to the ietf-yang-library data, which are more precise than capabilities list */
806 r = nc_ctx_fill_yl(session, old_clb, old_data);
807 if (r == -1) {
808 goto cleanup;
809 } else if (r == 1) {
Michal Vasko60f66602017-10-17 13:52:18 +0200810 VRB("Session %d: trying to use capabilities instead of ietf-yang-library data.", nc_session_get_id(session));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200811 /* try to use standard capabilities */
812 goto capabilities;
813 }
814 } else {
815capabilities:
Michal Vaskoef578332016-01-25 13:20:09 +0100816
Michal Vasko60f66602017-10-17 13:52:18 +0200817 if (nc_ctx_fill_cpblts(session, old_clb, old_data)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200818 goto cleanup;
Michal Vaskoef578332016-01-25 13:20:09 +0100819 }
Michal Vasko086311b2016-01-08 09:53:11 +0100820 }
821
Radek Krejcifd5b6682017-06-13 15:52:53 +0200822 /* succsess */
823 ret = 0;
824
Michal Vaskoeee99412016-11-21 10:19:43 +0100825 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
826 WRN("Some models failed to be loaded, any data from these models (and any other unknown) will be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +0100827 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200828
829cleanup:
830 /* set user callback back */
831 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
832
Michal Vaskoef578332016-01-25 13:20:09 +0100833 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100834}
835
836API struct nc_session *
837nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
838{
Michal Vaskod083db62016-01-19 10:31:29 +0100839 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100840
Michal Vasko45e53ae2016-04-07 11:46:03 +0200841 if (fdin < 0) {
842 ERRARG("fdin");
843 return NULL;
844 } else if (fdout < 0) {
845 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100846 return NULL;
847 }
848
849 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +0100850 session = nc_new_session(0);
Michal Vasko086311b2016-01-08 09:53:11 +0100851 if (!session) {
852 ERRMEM;
853 return NULL;
854 }
855 session->status = NC_STATUS_STARTING;
856 session->side = NC_CLIENT;
857
858 /* transport specific data */
859 session->ti_type = NC_TI_FD;
Michal Vaskoade892d2017-02-22 13:40:35 +0100860 pthread_mutex_init(session->ti_lock, NULL);
861 pthread_cond_init(session->ti_cond, NULL);
862 *session->ti_inuse = 0;
863
Michal Vasko086311b2016-01-08 09:53:11 +0100864 session->ti.fd.in = fdin;
865 session->ti.fd.out = fdout;
866
867 /* assign context (dicionary needed for handshake) */
868 if (!ctx) {
Radek Krejci3222b7d2017-09-21 16:04:30 +0200869 ctx = ly_ctx_new(SCHEMAS_DIR, 0);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100870 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200871 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100872 /* that's just it */
873 goto fail;
874 }
Michal Vasko086311b2016-01-08 09:53:11 +0100875 } else {
876 session->flags |= NC_SESSION_SHAREDCTX;
877 }
878 session->ctx = ctx;
879
880 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200881 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100882 goto fail;
883 }
884 session->status = NC_STATUS_RUNNING;
885
Michal Vaskoef578332016-01-25 13:20:09 +0100886 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100887 goto fail;
888 }
889
890 return session;
891
892fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100893 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100894 return NULL;
895}
896
897int
Michal Vaskof05562c2016-01-20 12:06:43 +0100898nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100899{
Michal Vasko0190bc32016-03-02 15:47:49 +0100900 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100901 struct addrinfo hints, *res_list, *res;
902 char port_s[6]; /* length of string representation of short int */
903
904 snprintf(port_s, 6, "%u", port);
905
906 /* Connect to a server */
907 memset(&hints, 0, sizeof hints);
908 hints.ai_family = AF_UNSPEC;
909 hints.ai_socktype = SOCK_STREAM;
910 hints.ai_protocol = IPPROTO_TCP;
911 i = getaddrinfo(host, port_s, &hints, &res_list);
912 if (i != 0) {
913 ERR("Unable to translate the host address (%s).", gai_strerror(i));
914 return -1;
915 }
916
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200917 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100918 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
919 if (sock == -1) {
920 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200921 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100922 }
923
924 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
925 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100926 close(sock);
927 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200928 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100929 }
930
Michal Vasko0190bc32016-03-02 15:47:49 +0100931 /* make the socket non-blocking */
932 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
933 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100934 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200935 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100936 return -1;
937 }
938
Michal Vasko086311b2016-01-08 09:53:11 +0100939 /* we're done, network connection established */
940 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100941 }
942
Michal Vasko29af44b2016-10-13 10:59:55 +0200943 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100944 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
Michal Vasko086311b2016-01-08 09:53:11 +0100945 }
946 freeaddrinfo(res_list);
947
948 return sock;
949}
950
Michal Vasko086311b2016-01-08 09:53:11 +0100951static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100952get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100953{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100954 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100955 char *ptr;
956 const char *str_msgid;
957 uint64_t cur_msgid;
958 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100959 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100960 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
961
Michal Vaskoade892d2017-02-22 13:40:35 +0100962 r = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100963 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100964 /* error */
965 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100966 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100967 /* timeout */
968 return NC_MSG_WOULDBLOCK;
969 }
970
971 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200972 if (!msgid && session->opts.client.notifs) {
973 cont = session->opts.client.notifs;
974 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100975
Michal Vasko71ba2da2016-05-04 10:53:16 +0200976 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100977 free(cont);
978
Michal Vasko71ba2da2016-05-04 10:53:16 +0200979 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100980 }
981
982 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200983 if (msgid && session->opts.client.replies) {
984 cont = session->opts.client.replies;
985 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100986
Michal Vasko71ba2da2016-05-04 10:53:16 +0200987 xml = cont->msg;
988 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100989
Michal Vasko71ba2da2016-05-04 10:53:16 +0200990 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100991 }
992
Michal Vasko71ba2da2016-05-04 10:53:16 +0200993 if (!msgtype) {
994 /* read message from wire */
995 msgtype = nc_read_msg_poll(session, timeout, &xml);
996 }
Michal Vasko086311b2016-01-08 09:53:11 +0100997
998 /* we read rpc-reply, want a notif */
999 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001000 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001001 while (*cont_ptr) {
1002 cont_ptr = &((*cont_ptr)->next);
1003 }
1004 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001005 if (!*cont_ptr) {
1006 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001007 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001008 lyxml_free(session->ctx, xml);
1009 return NC_MSG_ERROR;
1010 }
Michal Vasko086311b2016-01-08 09:53:11 +01001011 (*cont_ptr)->msg = xml;
1012 (*cont_ptr)->next = NULL;
1013 }
1014
1015 /* we read notif, want a rpc-reply */
1016 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001017 if (!session->opts.client.ntf_tid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001018 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +01001019 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001020 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001021 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001022 }
Michal Vasko086311b2016-01-08 09:53:11 +01001023
Michal Vasko2e6defd2016-10-07 15:48:15 +02001024 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001025 while (*cont_ptr) {
1026 cont_ptr = &((*cont_ptr)->next);
1027 }
1028 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001029 if (!cont_ptr) {
1030 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001031 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001032 lyxml_free(session->ctx, xml);
1033 return NC_MSG_ERROR;
1034 }
Michal Vasko086311b2016-01-08 09:53:11 +01001035 (*cont_ptr)->msg = xml;
1036 (*cont_ptr)->next = NULL;
1037 }
1038
Michal Vaskoade892d2017-02-22 13:40:35 +01001039 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01001040
1041 switch (msgtype) {
1042 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001043 if (!msgid) {
1044 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001045 }
Michal Vasko086311b2016-01-08 09:53:11 +01001046 break;
1047
1048 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001049 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001050 /* check message-id */
1051 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1052 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001053 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001054 } else {
1055 cur_msgid = strtoul(str_msgid, &ptr, 10);
1056 if (cur_msgid != msgid) {
1057 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1058 session->id, str_msgid);
1059 msgtype = NC_MSG_REPLY_ERR_MSGID;
1060 }
1061 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001062 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001063 }
Michal Vasko086311b2016-01-08 09:53:11 +01001064 break;
1065
1066 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001067 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001068 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001069 msgtype = NC_MSG_ERROR;
1070 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001071
1072 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001073 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001074 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001075 msgtype = NC_MSG_ERROR;
1076 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001077
1078 default:
1079 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1080 * NC_MSG_NONE is not returned by nc_read_msg()
1081 */
1082 break;
1083 }
1084
1085 return msgtype;
1086}
1087
1088/* cannot strictly fail, but does not need to fill any error parameter at all */
1089static void
1090parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1091{
1092 struct lyxml_elem *iter, *next, *info;
1093
1094 LY_TREE_FOR(xml->child, iter) {
1095 if (!iter->ns) {
1096 if (iter->content) {
1097 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1098 } else {
1099 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1100 }
1101 continue;
1102 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1103 if (iter->content) {
1104 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1105 iter->name, iter->content, iter->ns->value);
1106 } else {
1107 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1108 }
1109 continue;
1110 }
1111
1112 if (!strcmp(iter->name, "error-type")) {
1113 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1114 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1115 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1116 } else if (err->type) {
1117 WRN("<rpc-error> <error-type> duplicated.");
1118 } else {
1119 err->type = lydict_insert(ctx, iter->content, 0);
1120 }
1121 } else if (!strcmp(iter->name, "error-tag")) {
1122 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1123 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1124 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1125 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1126 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1127 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1128 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1129 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1130 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1131 && strcmp(iter->content, "malformed-message"))) {
1132 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1133 } else if (err->tag) {
1134 WRN("<rpc-error> <error-tag> duplicated.");
1135 } else {
1136 err->tag = lydict_insert(ctx, iter->content, 0);
1137 }
1138 } else if (!strcmp(iter->name, "error-severity")) {
1139 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1140 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1141 } else if (err->severity) {
1142 WRN("<rpc-error> <error-severity> duplicated.");
1143 } else {
1144 err->severity = lydict_insert(ctx, iter->content, 0);
1145 }
1146 } else if (!strcmp(iter->name, "error-app-tag")) {
1147 if (err->apptag) {
1148 WRN("<rpc-error> <error-app-tag> duplicated.");
1149 } else {
1150 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1151 }
1152 } else if (!strcmp(iter->name, "error-path")) {
1153 if (err->path) {
1154 WRN("<rpc-error> <error-path> duplicated.");
1155 } else {
1156 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1157 }
1158 } else if (!strcmp(iter->name, "error-message")) {
1159 if (err->message) {
1160 WRN("<rpc-error> <error-message> duplicated.");
1161 } else {
1162 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001163 if (err->message_lang) {
1164 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1165 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001166 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1167 }
1168 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1169 }
1170 } else if (!strcmp(iter->name, "error-info")) {
1171 LY_TREE_FOR_SAFE(iter->child, next, info) {
1172 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1173 if (!strcmp(info->name, "session-id")) {
1174 if (err->sid) {
1175 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1176 } else {
1177 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1178 }
Michal Vasko630485f2018-01-03 14:28:32 +01001179 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001180 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001181 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1182 if (!err->attr) {
1183 ERRMEM;
1184 return;
1185 }
Michal Vasko086311b2016-01-08 09:53:11 +01001186 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1187 } else if (!strcmp(info->name, "bad-element")) {
1188 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001189 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1190 if (!err->elem) {
1191 ERRMEM;
1192 return;
1193 }
Michal Vasko086311b2016-01-08 09:53:11 +01001194 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1195 } else if (!strcmp(info->name, "bad-namespace")) {
1196 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001197 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1198 if (!err->ns) {
1199 ERRMEM;
1200 return;
1201 }
Michal Vasko086311b2016-01-08 09:53:11 +01001202 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1203 } else {
1204 if (info->content) {
1205 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1206 info->name, info->content);
1207 } else {
1208 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1209 }
1210 }
1211 } else {
1212 lyxml_unlink(ctx, info);
1213 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001214 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1215 if (!err->other) {
1216 ERRMEM;
1217 return;
1218 }
Michal Vasko086311b2016-01-08 09:53:11 +01001219 err->other[err->other_count - 1] = info;
1220 }
1221 }
1222 } else {
1223 if (iter->content) {
1224 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1225 } else {
1226 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1227 }
1228 }
1229 }
1230}
1231
1232static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001233parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001234{
1235 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001236 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001237 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001238 struct nc_reply_data *data_rpl;
1239 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001240 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001241 int i;
1242
1243 if (!xml->child) {
1244 ERR("An empty <rpc-reply>.");
1245 return NULL;
1246 }
1247
1248 /* rpc-error */
1249 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1250 /* count and check elements */
1251 i = 0;
1252 LY_TREE_FOR(xml->child, iter) {
1253 if (strcmp(iter->name, "rpc-error")) {
1254 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1255 return NULL;
1256 } else if (!iter->ns) {
1257 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1258 return NULL;
1259 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1260 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1261 return NULL;
1262 }
1263 ++i;
1264 }
1265
1266 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001267 if (!error_rpl) {
1268 ERRMEM;
1269 return NULL;
1270 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001271 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001272 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001273 if (!error_rpl->err) {
1274 ERRMEM;
1275 free(error_rpl);
1276 return NULL;
1277 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001278 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001279 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001280 reply = (struct nc_reply *)error_rpl;
1281
1282 i = 0;
1283 LY_TREE_FOR(xml->child, iter) {
1284 parse_rpc_error(ctx, iter, error_rpl->err + i);
1285 ++i;
1286 }
1287
1288 /* ok */
1289 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1290 if (xml->child->next) {
1291 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1292 return NULL;
1293 }
1294 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001295 if (!reply) {
1296 ERRMEM;
1297 return NULL;
1298 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001299 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001300
1301 /* some RPC output */
1302 } else {
1303 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001304 case NC_RPC_ACT_GENERIC:
1305 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001306
1307 if (rpc_gen->has_data) {
Michal Vaskoe1708602016-10-18 12:17:22 +02001308 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +01001309 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001310 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 +02001311 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001312 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001313 return NULL;
1314 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001315 }
Michal Vasko086311b2016-01-08 09:53:11 +01001316 break;
1317
1318 case NC_RPC_GETCONFIG:
1319 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001320 if (!xml->child->child) {
1321 /* we did not receive any data */
1322 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001323 if (!data_rpl) {
1324 ERRMEM;
1325 return NULL;
1326 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001327 data_rpl->type = NC_RPL_DATA;
1328 data_rpl->data = NULL;
1329 return (struct nc_reply *)data_rpl;
1330 }
1331
Michal Vasko086311b2016-01-08 09:53:11 +01001332 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001333 data = lyd_parse_xml(ctx, &xml->child->child,
1334 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001335 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001336 if (!data) {
1337 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1338 return NULL;
1339 }
1340 break;
1341
1342 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001343 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001344 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001345 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001346 return NULL;
1347 }
1348 break;
1349
1350 case NC_RPC_EDIT:
1351 case NC_RPC_COPY:
1352 case NC_RPC_DELETE:
1353 case NC_RPC_LOCK:
1354 case NC_RPC_UNLOCK:
1355 case NC_RPC_KILL:
1356 case NC_RPC_COMMIT:
1357 case NC_RPC_DISCARD:
1358 case NC_RPC_CANCEL:
1359 case NC_RPC_VALIDATE:
1360 case NC_RPC_SUBSCRIBE:
1361 /* there is no output defined */
1362 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1363 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001364 default:
1365 ERRINT;
1366 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001367 }
1368
1369 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001370 if (!data_rpl) {
1371 ERRMEM;
1372 return NULL;
1373 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001374 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001375 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001376 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001377 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001378 } else {
1379 /* <get>, <get-config> */
1380 data_rpl->data = data;
1381 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001382 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001383 if (!data_rpl->data) {
1384 ERR("Failed to parse <rpc-reply>.");
1385 free(data_rpl);
1386 return NULL;
1387 }
1388 reply = (struct nc_reply *)data_rpl;
1389 }
1390
1391 return reply;
1392}
1393
Radek Krejci53691be2016-02-22 13:58:37 +01001394#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001395
Michal Vasko3031aae2016-01-27 16:07:18 +01001396int
1397nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1398{
1399 int sock;
1400
Michal Vasko45e53ae2016-04-07 11:46:03 +02001401 if (!address) {
1402 ERRARG("address");
1403 return -1;
1404 } else if (!port) {
1405 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001406 return -1;
1407 }
1408
1409 sock = nc_sock_listen(address, port);
1410 if (sock == -1) {
1411 return -1;
1412 }
1413
1414 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001415 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1416 if (!client_opts.ch_binds) {
1417 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001418 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001419 return -1;
1420 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001421
Michal Vasko2e6defd2016-10-07 15:48:15 +02001422 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1423 if (!client_opts.ch_bind_ti) {
1424 ERRMEM;
1425 close(sock);
1426 return -1;
1427 }
1428 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1429
Michal Vasko3031aae2016-01-27 16:07:18 +01001430 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001431 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1432 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001433 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001434 return -1;
1435 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001436 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1437 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001438 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001439
1440 return 0;
1441}
1442
1443int
1444nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1445{
1446 uint32_t i;
1447 int ret = -1;
1448
1449 if (!address && !port && !ti) {
1450 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1451 close(client_opts.ch_binds[i].sock);
1452 free((char *)client_opts.ch_binds[i].address);
1453
1454 ret = 0;
1455 }
1456 free(client_opts.ch_binds);
1457 client_opts.ch_binds = NULL;
1458 client_opts.ch_bind_count = 0;
1459 } else {
1460 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1461 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1462 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001463 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001464 close(client_opts.ch_binds[i].sock);
1465 free((char *)client_opts.ch_binds[i].address);
1466
1467 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001468 if (!client_opts.ch_bind_count) {
1469 free(client_opts.ch_binds);
1470 client_opts.ch_binds = NULL;
1471 } else if (i < client_opts.ch_bind_count) {
1472 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1473 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1474 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001475
1476 ret = 0;
1477 }
1478 }
1479 }
1480
1481 return ret;
1482}
1483
1484API int
1485nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1486{
1487 int sock;
1488 char *host = NULL;
1489 uint16_t port, idx;
1490
Michal Vasko45e53ae2016-04-07 11:46:03 +02001491 if (!client_opts.ch_binds) {
1492 ERRINIT;
1493 return -1;
1494 } else if (!session) {
1495 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001496 return -1;
1497 }
1498
1499 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1500
Michal Vasko50456e82016-02-02 12:16:08 +01001501 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001502 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001503 return sock;
1504 }
1505
Radek Krejci53691be2016-02-22 13:58:37 +01001506#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001507 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001508 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001509 } else
1510#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001511#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001512 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001513 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001514 } else
1515#endif
1516 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001517 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001518 *session = NULL;
1519 }
1520
1521 free(host);
1522
1523 if (!(*session)) {
1524 return -1;
1525 }
1526
1527 return 1;
1528}
1529
Radek Krejci53691be2016-02-22 13:58:37 +01001530#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001531
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001532API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001533nc_session_get_cpblts(const struct nc_session *session)
1534{
1535 if (!session) {
1536 ERRARG("session");
1537 return NULL;
1538 }
1539
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001540 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001541}
1542
1543API const char *
1544nc_session_cpblt(const struct nc_session *session, const char *capab)
1545{
1546 int i, len;
1547
1548 if (!session) {
1549 ERRARG("session");
1550 return NULL;
1551 } else if (!capab) {
1552 ERRARG("capab");
1553 return NULL;
1554 }
1555
1556 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001557 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1558 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1559 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001560 }
1561 }
1562
1563 return NULL;
1564}
1565
Michal Vasko9cd26a82016-05-31 08:58:48 +02001566API int
1567nc_session_ntf_thread_running(const struct nc_session *session)
1568{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001569 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001570 ERRARG("session");
1571 return 0;
1572 }
1573
Michal Vasko2e6defd2016-10-07 15:48:15 +02001574 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001575}
1576
Michal Vaskob7558c52016-02-26 15:04:19 +01001577API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001578nc_client_init(void)
1579{
1580 nc_init();
1581}
1582
1583API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001584nc_client_destroy(void)
1585{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001586 nc_client_set_schema_searchpath(NULL);
1587#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1588 nc_client_ch_del_bind(NULL, 0, 0);
1589#endif
1590#ifdef NC_ENABLED_SSH
1591 nc_client_ssh_destroy_opts();
1592#endif
1593#ifdef NC_ENABLED_TLS
1594 nc_client_tls_destroy_opts();
1595#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001596 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001597}
1598
Michal Vasko086311b2016-01-08 09:53:11 +01001599API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001600nc_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 +01001601{
1602 struct lyxml_elem *xml;
1603 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1604
Michal Vasko45e53ae2016-04-07 11:46:03 +02001605 if (!session) {
1606 ERRARG("session");
1607 return NC_MSG_ERROR;
1608 } else if (!rpc) {
1609 ERRARG("rpc");
1610 return NC_MSG_ERROR;
1611 } else if (!reply) {
1612 ERRARG("reply");
1613 return NC_MSG_ERROR;
1614 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1615 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001616 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001617 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001618 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001619 return NC_MSG_ERROR;
1620 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001621 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001622 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1623 parseroptions &= LYD_OPT_STRICT;
1624 }
Michal Vasko41adf392017-01-17 10:39:04 +01001625 /* no mechanism to check external dependencies is provided */
1626 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001627 *reply = NULL;
1628
1629 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001630
Michal Vasko71ba2da2016-05-04 10:53:16 +02001631 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001632 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001633 lyxml_free(session->ctx, xml);
1634 if (!(*reply)) {
1635 return NC_MSG_ERROR;
1636 }
1637 }
1638
1639 return msgtype;
1640}
1641
1642API NC_MSG_TYPE
1643nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1644{
1645 struct lyxml_elem *xml, *ev_time;
1646 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1647
Michal Vasko45e53ae2016-04-07 11:46:03 +02001648 if (!session) {
1649 ERRARG("session");
1650 return NC_MSG_ERROR;
1651 } else if (!notif) {
1652 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001653 return NC_MSG_ERROR;
1654 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001655 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001656 return NC_MSG_ERROR;
1657 }
1658
1659 msgtype = get_msg(session, timeout, 0, &xml);
1660
1661 if (msgtype == NC_MSG_NOTIF) {
1662 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001663 if (!*notif) {
1664 ERRMEM;
1665 lyxml_free(session->ctx, xml);
1666 return NC_MSG_ERROR;
1667 }
Michal Vasko086311b2016-01-08 09:53:11 +01001668
1669 /* eventTime */
1670 LY_TREE_FOR(xml->child, ev_time) {
1671 if (!strcmp(ev_time->name, "eventTime")) {
1672 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1673 /* lyd_parse does not know this element */
1674 lyxml_free(session->ctx, ev_time);
1675 break;
1676 }
1677 }
1678 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001679 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001680 goto fail;
1681 }
1682
1683 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001684 (*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 +01001685 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001686 lyxml_free(session->ctx, xml);
1687 xml = NULL;
1688 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001689 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001690 goto fail;
1691 }
1692 }
1693
1694 return msgtype;
1695
1696fail:
1697 lydict_remove(session->ctx, (*notif)->datetime);
1698 lyd_free((*notif)->tree);
1699 free(*notif);
1700 *notif = NULL;
1701 lyxml_free(session->ctx, xml);
1702
1703 return NC_MSG_ERROR;
1704}
1705
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001706static void *
1707nc_recv_notif_thread(void *arg)
1708{
1709 struct nc_ntf_thread_arg *ntarg;
1710 struct nc_session *session;
1711 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1712 struct nc_notif *notif;
1713 NC_MSG_TYPE msgtype;
1714
1715 ntarg = (struct nc_ntf_thread_arg *)arg;
1716 session = ntarg->session;
1717 notif_clb = ntarg->notif_clb;
1718 free(ntarg);
1719
Michal Vasko2e6defd2016-10-07 15:48:15 +02001720 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001721 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001722 if (msgtype == NC_MSG_NOTIF) {
1723 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001724 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1725 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1726 nc_notif_free(notif);
1727 break;
1728 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001729 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001730 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001731 /* quit this thread once the session is broken */
1732 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001733 }
1734
1735 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1736 }
1737
Michal Vasko0651c902016-05-19 15:55:42 +02001738 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001739 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001740 return NULL;
1741}
1742
1743API int
1744nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1745{
1746 struct nc_ntf_thread_arg *ntarg;
1747 int ret;
1748
Michal Vasko45e53ae2016-04-07 11:46:03 +02001749 if (!session) {
1750 ERRARG("session");
1751 return -1;
1752 } else if (!notif_clb) {
1753 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001754 return -1;
1755 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1756 ERR("Session %u: invalid session to receive Notifications.", session->id);
1757 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001758 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001759 ERR("Session %u: separate notification thread is already running.", session->id);
1760 return -1;
1761 }
1762
1763 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001764 if (!ntarg) {
1765 ERRMEM;
1766 return -1;
1767 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001768 ntarg->session = session;
1769 ntarg->notif_clb = notif_clb;
1770
1771 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001772 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1773 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001774 ERRMEM;
1775 free(ntarg);
1776 return -1;
1777 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001778
Michal Vasko2e6defd2016-10-07 15:48:15 +02001779 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001780 if (ret) {
1781 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1782 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001783 free((pthread_t *)session->opts.client.ntf_tid);
1784 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001785 return -1;
1786 }
1787
1788 return 0;
1789}
1790
Michal Vasko086311b2016-01-08 09:53:11 +01001791API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001792nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001793{
1794 NC_MSG_TYPE r;
Radek Krejcib4b19062018-02-07 16:33:06 +01001795 int ret, dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02001796 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001797 struct nc_rpc_getconfig *rpc_gc;
1798 struct nc_rpc_edit *rpc_e;
1799 struct nc_rpc_copy *rpc_cp;
1800 struct nc_rpc_delete *rpc_del;
1801 struct nc_rpc_lock *rpc_lock;
1802 struct nc_rpc_get *rpc_g;
1803 struct nc_rpc_kill *rpc_k;
1804 struct nc_rpc_commit *rpc_com;
1805 struct nc_rpc_cancel *rpc_can;
1806 struct nc_rpc_validate *rpc_val;
1807 struct nc_rpc_getschema *rpc_gs;
1808 struct nc_rpc_subscribe *rpc_sub;
1809 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001810 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001811 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001812 uint64_t cur_msgid;
1813
Michal Vasko45e53ae2016-04-07 11:46:03 +02001814 if (!session) {
1815 ERRARG("session");
1816 return NC_MSG_ERROR;
1817 } else if (!rpc) {
1818 ERRARG("rpc");
1819 return NC_MSG_ERROR;
1820 } else if (!msgid) {
1821 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001822 return NC_MSG_ERROR;
1823 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001824 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001825 return NC_MSG_ERROR;
1826 }
1827
Michal Vasko90e8e692016-07-13 12:27:57 +02001828 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001829 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001830 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01001831 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001832 return NC_MSG_ERROR;
1833 }
1834 }
1835
1836 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001837 case NC_RPC_ACT_GENERIC:
1838 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001839
1840 if (rpc_gen->has_data) {
1841 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01001842 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001843 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01001844 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 +01001845 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01001846 if (!data) {
1847 return NC_MSG_ERROR;
1848 }
Michal Vasko086311b2016-01-08 09:53:11 +01001849 }
1850 break;
1851
1852 case NC_RPC_GETCONFIG:
1853 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1854
1855 data = lyd_new(NULL, ietfnc, "get-config");
1856 node = lyd_new(data, ietfnc, "source");
1857 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1858 if (!node) {
1859 lyd_free(data);
1860 return NC_MSG_ERROR;
1861 }
1862 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001863 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001864 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001865 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001866 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001867 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001868 lyd_insert_attr(node, NULL, "type", "xpath");
1869 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001870 }
1871 if (!node) {
1872 lyd_free(data);
1873 return NC_MSG_ERROR;
1874 }
1875 }
1876
1877 if (rpc_gc->wd_mode) {
1878 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001879 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001880 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001881 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001882 return NC_MSG_ERROR;
1883 }
1884 }
1885 switch (rpc_gc->wd_mode) {
1886 case NC_WD_UNKNOWN:
1887 /* cannot get here */
1888 break;
1889 case NC_WD_ALL:
1890 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1891 break;
1892 case NC_WD_ALL_TAG:
1893 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1894 break;
1895 case NC_WD_TRIM:
1896 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1897 break;
1898 case NC_WD_EXPLICIT:
1899 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1900 break;
1901 }
1902 if (!node) {
1903 lyd_free(data);
1904 return NC_MSG_ERROR;
1905 }
1906 }
1907 break;
1908
1909 case NC_RPC_EDIT:
1910 rpc_e = (struct nc_rpc_edit *)rpc;
1911
1912 data = lyd_new(NULL, ietfnc, "edit-config");
1913 node = lyd_new(data, ietfnc, "target");
1914 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1915 if (!node) {
1916 lyd_free(data);
1917 return NC_MSG_ERROR;
1918 }
1919
1920 if (rpc_e->default_op) {
1921 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1922 if (!node) {
1923 lyd_free(data);
1924 return NC_MSG_ERROR;
1925 }
1926 }
1927
1928 if (rpc_e->test_opt) {
1929 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1930 if (!node) {
1931 lyd_free(data);
1932 return NC_MSG_ERROR;
1933 }
1934 }
1935
1936 if (rpc_e->error_opt) {
1937 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1938 if (!node) {
1939 lyd_free(data);
1940 return NC_MSG_ERROR;
1941 }
1942 }
1943
Michal Vasko7793bc62016-09-16 11:58:41 +02001944 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001945 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001946 } else {
1947 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1948 }
1949 if (!node) {
1950 lyd_free(data);
1951 return NC_MSG_ERROR;
1952 }
1953 break;
1954
1955 case NC_RPC_COPY:
1956 rpc_cp = (struct nc_rpc_copy *)rpc;
1957
1958 data = lyd_new(NULL, ietfnc, "copy-config");
1959 node = lyd_new(data, ietfnc, "target");
1960 if (rpc_cp->url_trg) {
1961 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1962 } else {
1963 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1964 }
1965 if (!node) {
1966 lyd_free(data);
1967 return NC_MSG_ERROR;
1968 }
1969
1970 node = lyd_new(data, ietfnc, "source");
1971 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001972 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001973 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001974 } else {
1975 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1976 }
1977 } else {
1978 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1979 }
1980 if (!node) {
1981 lyd_free(data);
1982 return NC_MSG_ERROR;
1983 }
1984
1985 if (rpc_cp->wd_mode) {
1986 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001987 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001988 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001989 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001990 return NC_MSG_ERROR;
1991 }
1992 }
1993 switch (rpc_cp->wd_mode) {
1994 case NC_WD_UNKNOWN:
1995 /* cannot get here */
1996 break;
1997 case NC_WD_ALL:
1998 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1999 break;
2000 case NC_WD_ALL_TAG:
2001 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2002 break;
2003 case NC_WD_TRIM:
2004 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2005 break;
2006 case NC_WD_EXPLICIT:
2007 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2008 break;
2009 }
2010 if (!node) {
2011 lyd_free(data);
2012 return NC_MSG_ERROR;
2013 }
2014 }
2015 break;
2016
2017 case NC_RPC_DELETE:
2018 rpc_del = (struct nc_rpc_delete *)rpc;
2019
2020 data = lyd_new(NULL, ietfnc, "delete-config");
2021 node = lyd_new(data, ietfnc, "target");
2022 if (rpc_del->url) {
2023 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2024 } else {
2025 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2026 }
2027 if (!node) {
2028 lyd_free(data);
2029 return NC_MSG_ERROR;
2030 }
2031 break;
2032
2033 case NC_RPC_LOCK:
2034 rpc_lock = (struct nc_rpc_lock *)rpc;
2035
2036 data = lyd_new(NULL, ietfnc, "lock");
2037 node = lyd_new(data, ietfnc, "target");
2038 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2039 if (!node) {
2040 lyd_free(data);
2041 return NC_MSG_ERROR;
2042 }
2043 break;
2044
2045 case NC_RPC_UNLOCK:
2046 rpc_lock = (struct nc_rpc_lock *)rpc;
2047
2048 data = lyd_new(NULL, ietfnc, "unlock");
2049 node = lyd_new(data, ietfnc, "target");
2050 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2051 if (!node) {
2052 lyd_free(data);
2053 return NC_MSG_ERROR;
2054 }
2055 break;
2056
2057 case NC_RPC_GET:
2058 rpc_g = (struct nc_rpc_get *)rpc;
2059
2060 data = lyd_new(NULL, ietfnc, "get");
2061 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002062 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002063 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002064 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002065 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002066 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002067 lyd_insert_attr(node, NULL, "type", "xpath");
2068 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002069 }
2070 if (!node) {
2071 lyd_free(data);
2072 return NC_MSG_ERROR;
2073 }
2074 }
2075
2076 if (rpc_g->wd_mode) {
2077 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002078 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002079 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002080 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002081 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002082 return NC_MSG_ERROR;
2083 }
2084 }
2085 switch (rpc_g->wd_mode) {
2086 case NC_WD_UNKNOWN:
2087 /* cannot get here */
2088 break;
2089 case NC_WD_ALL:
2090 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2091 break;
2092 case NC_WD_ALL_TAG:
2093 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2094 break;
2095 case NC_WD_TRIM:
2096 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2097 break;
2098 case NC_WD_EXPLICIT:
2099 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2100 break;
2101 }
2102 if (!node) {
2103 lyd_free(data);
2104 return NC_MSG_ERROR;
2105 }
2106 }
2107 break;
2108
2109 case NC_RPC_KILL:
2110 rpc_k = (struct nc_rpc_kill *)rpc;
2111
2112 data = lyd_new(NULL, ietfnc, "kill-session");
2113 sprintf(str, "%u", rpc_k->sid);
2114 lyd_new_leaf(data, ietfnc, "session-id", str);
2115 break;
2116
2117 case NC_RPC_COMMIT:
2118 rpc_com = (struct nc_rpc_commit *)rpc;
2119
2120 data = lyd_new(NULL, ietfnc, "commit");
2121 if (rpc_com->confirmed) {
2122 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2123 }
2124
2125 if (rpc_com->confirm_timeout) {
2126 sprintf(str, "%u", rpc_com->confirm_timeout);
2127 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2128 }
2129
2130 if (rpc_com->persist) {
2131 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2132 if (!node) {
2133 lyd_free(data);
2134 return NC_MSG_ERROR;
2135 }
2136 }
2137
2138 if (rpc_com->persist_id) {
2139 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2140 if (!node) {
2141 lyd_free(data);
2142 return NC_MSG_ERROR;
2143 }
2144 }
2145 break;
2146
2147 case NC_RPC_DISCARD:
2148 data = lyd_new(NULL, ietfnc, "discard-changes");
2149 break;
2150
2151 case NC_RPC_CANCEL:
2152 rpc_can = (struct nc_rpc_cancel *)rpc;
2153
2154 data = lyd_new(NULL, ietfnc, "cancel-commit");
2155 if (rpc_can->persist_id) {
2156 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2157 if (!node) {
2158 lyd_free(data);
2159 return NC_MSG_ERROR;
2160 }
2161 }
2162 break;
2163
2164 case NC_RPC_VALIDATE:
2165 rpc_val = (struct nc_rpc_validate *)rpc;
2166
2167 data = lyd_new(NULL, ietfnc, "validate");
2168 node = lyd_new(data, ietfnc, "source");
2169 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002170 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002171 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002172 } else {
2173 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2174 }
2175 } else {
2176 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2177 }
2178 if (!node) {
2179 lyd_free(data);
2180 return NC_MSG_ERROR;
2181 }
2182 break;
2183
2184 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002185 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002186 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002187 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002188 return NC_MSG_ERROR;
2189 }
2190
2191 rpc_gs = (struct nc_rpc_getschema *)rpc;
2192
2193 data = lyd_new(NULL, ietfncmon, "get-schema");
2194 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2195 if (!node) {
2196 lyd_free(data);
2197 return NC_MSG_ERROR;
2198 }
2199 if (rpc_gs->version) {
2200 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2201 if (!node) {
2202 lyd_free(data);
2203 return NC_MSG_ERROR;
2204 }
2205 }
2206 if (rpc_gs->format) {
2207 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2208 if (!node) {
2209 lyd_free(data);
2210 return NC_MSG_ERROR;
2211 }
2212 }
2213 break;
2214
2215 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002216 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002217 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002218 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002219 return NC_MSG_ERROR;
2220 }
2221
2222 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2223
2224 data = lyd_new(NULL, notifs, "create-subscription");
2225 if (rpc_sub->stream) {
2226 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2227 if (!node) {
2228 lyd_free(data);
2229 return NC_MSG_ERROR;
2230 }
2231 }
2232
2233 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002234 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002235 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002236 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002237 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002238 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002239 lyd_insert_attr(node, NULL, "type", "xpath");
2240 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002241 }
2242 if (!node) {
2243 lyd_free(data);
2244 return NC_MSG_ERROR;
2245 }
2246 }
2247
2248 if (rpc_sub->start) {
2249 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2250 if (!node) {
2251 lyd_free(data);
2252 return NC_MSG_ERROR;
2253 }
2254 }
2255
2256 if (rpc_sub->stop) {
2257 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2258 if (!node) {
2259 lyd_free(data);
2260 return NC_MSG_ERROR;
2261 }
2262 }
2263 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002264 default:
2265 ERRINT;
2266 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002267 }
2268
Michal Vasko41adf392017-01-17 10:39:04 +01002269 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2270 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002271 if (dofree) {
2272 lyd_free(data);
2273 }
Michal Vasko086311b2016-01-08 09:53:11 +01002274 return NC_MSG_ERROR;
2275 }
2276
Michal Vaskoade892d2017-02-22 13:40:35 +01002277 ret = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002278 if (ret == -1) {
2279 /* error */
2280 r = NC_MSG_ERROR;
2281 } else if (!ret) {
2282 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01002283 r = NC_MSG_WOULDBLOCK;
2284 } else {
2285 /* send RPC, store its message ID */
2286 r = nc_send_msg(session, data);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002287 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002288 }
Michal Vaskoade892d2017-02-22 13:40:35 +01002289 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01002290
Radek Krejcib4b19062018-02-07 16:33:06 +01002291 if (dofree) {
2292 lyd_free(data);
2293 }
Michal Vasko086311b2016-01-08 09:53:11 +01002294
2295 if (r != NC_MSG_RPC) {
2296 return r;
2297 }
2298
2299 *msgid = cur_msgid;
2300 return NC_MSG_RPC;
2301}
Michal Vaskode2946c2017-01-12 12:19:26 +01002302
2303API void
2304nc_client_session_set_not_strict(struct nc_session *session)
2305{
2306 if (session->side != NC_CLIENT) {
2307 ERRARG("session");
2308 return;
2309 }
2310
2311 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2312}