blob: 1cdf586bc80d6311e6815193e1901bf3d09f7eff [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
Michal Vaskoceae0152018-02-14 16:03:59 +0100428static int
429nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, int implement,
430 ly_module_imp_clb user_clb, void *user_data, const struct lys_module **mod)
431{
432 int ret = 0;
433 struct ly_err_item *eitem;
434
435 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
436 if (*mod) {
437 if (implement && !(*mod)->implemented) {
438 /* make the present module implemented */
439 if (lys_set_implemented(*mod)) {
440 ERR("Failed to implement model \"%s\".", (*mod)->name);
441 ret = -1;
442 }
443 }
444 } else if (!(*mod) && implement) {
445 /* missing implemented module, load it ... */
446
447 /* clear all the errors and just collect them for now */
448 ly_err_clean(session->ctx, NULL);
449 ly_log_options(LY_LOSTORE);
450
451 /* 1) using only searchpaths */
452 *mod = ly_ctx_load_module(session->ctx, name, revision);
453
454 /* 2) using user callback */
455 if (!(*mod) && user_clb) {
456 ly_ctx_set_module_imp_clb(session->ctx, user_clb, user_data);
457 *mod = ly_ctx_load_module(session->ctx, name, revision);
458 }
459
460 /* 3) using get-schema callback */
461 if (!(*mod)) {
462 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
463 *mod = ly_ctx_load_module(session->ctx, name, revision);
464 }
465
466 /* unset callback back to use searchpath */
467 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
468
469 /* print errors on definite failure */
470 if (!(*mod)) {
471 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
472 ly_err_print(eitem);
473 }
474 ret = -1;
475 }
476
477 /* clean the errors and restore logging */
478 ly_err_clean(session->ctx, NULL);
479 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
480 }
481
482 return ret;
483}
484
Radek Krejcifd5b6682017-06-13 15:52:53 +0200485/* SCHEMAS_DIR not used (implicitly) */
486static int
487nc_ctx_fill_cpblts(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
488{
489 int ret = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200490 const struct lys_module *mod;
491 char *ptr, *ptr2;
492 const char *module_cpblt;
493 char *name = NULL, *revision = NULL, *features = NULL;
494 unsigned int u;
495
496 for (u = 0; session->opts.client.cpblts[u]; ++u) {
497 module_cpblt = strstr(session->opts.client.cpblts[u], "module=");
498 /* this capability requires a module */
499 if (!module_cpblt) {
500 continue;
501 }
502
503 /* get module name */
504 ptr = (char *)module_cpblt + 7;
505 ptr2 = strchr(ptr, '&');
506 if (!ptr2) {
507 ptr2 = ptr + strlen(ptr);
508 }
509 free(name);
510 name = strndup(ptr, ptr2 - ptr);
511
512 /* get module revision */
513 free(revision); revision = NULL;
514 ptr = strstr(module_cpblt, "revision=");
515 if (ptr) {
516 ptr += 9;
517 ptr2 = strchr(ptr, '&');
518 if (!ptr2) {
519 ptr2 = ptr + strlen(ptr);
520 }
521 revision = strndup(ptr, ptr2 - ptr);
522 }
523
Michal Vaskoceae0152018-02-14 16:03:59 +0100524 if (nc_ctx_load_module(session, name, revision, 1, user_clb, user_data, &mod)) {
525 ret = 1;
526 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200527 }
528
529 if (!mod) {
Michal Vasko60f66602017-10-17 13:52:18 +0200530 if (session->status != NC_STATUS_RUNNING) {
531 /* something bad heppened, discard the session */
532 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
533 ret = 1;
534 goto cleanup;
535 }
536
Radek Krejcifd5b6682017-06-13 15:52:53 +0200537 /* all loading ways failed, the schema will be ignored in the received data */
538 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
539 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200540 } else {
541 /* set features - first disable all to enable specified then */
542 lys_features_disable(mod, "*");
543
544 ptr = strstr(module_cpblt, "features=");
545 if (ptr) {
546 ptr += 9;
547 ptr2 = strchr(ptr, '&');
548 if (!ptr2) {
549 ptr2 = ptr + strlen(ptr);
550 }
551 free(features);
552 features = strndup(ptr, ptr2 - ptr);
553
554 /* basically manual strtok_r (to avoid macro) */
555 ptr2 = features;
556 for (ptr = features; *ptr; ++ptr) {
557 if (*ptr == ',') {
558 *ptr = '\0';
559 /* remember last feature */
560 ptr2 = ptr + 1;
561 }
562 }
563
564 ptr = features;
565 while (1) {
566 lys_features_enable(mod, ptr);
567 if (ptr != ptr2) {
568 ptr += strlen(ptr) + 1;
569 } else {
570 break;
571 }
572 }
573 }
574 }
575 }
576
577 ret = 0;
578
579cleanup:
580 free(name);
581 free(revision);
582 free(features);
583
584 return ret;
585}
586
587static int
588nc_ctx_fill_yl(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
589{
590 int ret = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200591 struct nc_rpc *rpc = NULL;
592 struct nc_reply *reply = NULL;
593 struct nc_reply_error *error_rpl;
594 struct nc_reply_data *data_rpl;
595 NC_MSG_TYPE msg;
596 uint64_t msgid;
597 struct lyd_node *data = NULL, *iter;
598 struct ly_set *modules = NULL, *imports = NULL, *features = NULL;
599 unsigned int u, v;
600 const char *name, *revision;
601 int implemented, imports_flag = 0;
602 const struct lys_module *mod;
603
604 /* get yang-library data from the server */
605 rpc = nc_rpc_get("/ietf-yang-library:*//.", 0, NC_PARAMTYPE_CONST);
606 if (!rpc) {
607 goto cleanup;
608 }
609
610 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
611 usleep(1000);
612 }
613 if (msg == NC_MSG_ERROR) {
Michal Vasko60f66602017-10-17 13:52:18 +0200614 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200615 session->id);
616 goto cleanup;
617 }
618
619 do {
620 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
621 } while (msg == NC_MSG_NOTIF);
622 if (msg == NC_MSG_WOULDBLOCK) {
623 ERR("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
624 goto cleanup;
625 } else if (msg == NC_MSG_ERROR) {
626 ERR("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
627 goto cleanup;
628 }
629
630 switch (reply->type) {
631 case NC_RPL_OK:
632 ERR("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
633 goto cleanup;
634 case NC_RPL_DATA:
635 /* fine */
636 break;
637 case NC_RPL_ERROR:
638 error_rpl = (struct nc_reply_error *)reply;
639 if (error_rpl->count) {
640 ERR("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
641 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
642 } else {
643 ERR("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
644 }
645 goto cleanup;
646 case NC_RPL_NOTIF:
647 ERR("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
648 goto cleanup;
649 }
650
651 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko09319322017-10-02 09:26:54 +0200652 if (!data_rpl->data || strcmp(data_rpl->data->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200653 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
654 goto cleanup;
655 }
656
Michal Vaskof4c8b792017-07-28 14:57:22 +0200657 modules = lyd_find_path(data_rpl->data, "/ietf-yang-library:modules-state/module");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200658 if (!modules || !modules->number) {
659 ERR("No yang-library modules information for session %u.", session->id);
660 goto cleanup;
661 }
662
663 features = ly_set_new();
664 imports = ly_set_new();
665
666parse:
667 for (u = modules->number - 1; u < modules->number; u--) {
668 name = revision = NULL;
669 ly_set_clean(features);
670 implemented = 0;
671
672 /* store the data */
673 LY_TREE_FOR(modules->set.d[u]->child, iter) {
674 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
675 /* ignore empty nodes */
676 continue;
677 }
678 if (!strcmp(iter->schema->name, "name")) {
679 name = ((struct lyd_node_leaf_list *)iter)->value_str;
680 } else if (!strcmp(iter->schema->name, "revision")) {
681 revision = ((struct lyd_node_leaf_list *)iter)->value_str;
682 } else if (!strcmp(iter->schema->name, "conformance-type")) {
683 implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
684 } else if (!strcmp(iter->schema->name, "feature")) {
685 ly_set_add(features, (void*)((struct lyd_node_leaf_list *)iter)->value_str, LY_SET_OPT_USEASLIST);
686 }
687 }
688
Michal Vaskoceae0152018-02-14 16:03:59 +0100689 if (nc_ctx_load_module(session, name, revision, implemented, user_clb, user_data, &mod)) {
690 ret = -1;
691 goto cleanup;
692 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200693
Michal Vaskoceae0152018-02-14 16:03:59 +0100694 if (!mod) { /* !mod && !implemented - will be loaded automatically, but remember to set features in the end */
695 assert(!implemented);
Michal Vasko3c556372017-08-09 10:17:34 +0200696 if (imports_flag) {
697 ERR("Module \"%s@%s\" is supposed to be imported, but no other module imports it.",
698 name, revision ? revision : "<latest>");
699 ret = -1;
700 goto cleanup;
701 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200702 ly_set_add(imports, modules->set.d[u], LY_SET_OPT_USEASLIST);
703 continue;
704 }
705
706 if (!mod) {
707 /* all loading ways failed, the schema will be ignored in the received data */
708 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
709 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200710 } else {
711 /* set features - first disable all to enable specified then */
712 lys_features_disable(mod, "*");
713 for (v = 0; v < features->number; v++) {
714 lys_features_enable(mod, (const char*)features->set.g[v]);
715 }
716 }
717 }
718
719 if (!imports_flag && imports->number) {
720 /* even imported modules should be now loaded as dependency, so just go through
721 * the parsing again and just set the features */
722 ly_set_free(modules);
723 modules = imports;
724 imports = NULL;
725 imports_flag = 1;
726 goto parse;
727 }
728
729 /* done */
730 ret = 0;
731
732cleanup:
733 nc_rpc_free(rpc);
734 nc_reply_free(reply);
735 lyd_free_withsiblings(data);
736
737 ly_set_free(modules);
738 ly_set_free(imports);
739 ly_set_free(features);
740
Michal Vasko60f66602017-10-17 13:52:18 +0200741 if (session->status != NC_STATUS_RUNNING) {
742 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
743 ret = -1;
744 }
745
Radek Krejcifd5b6682017-06-13 15:52:53 +0200746 return ret;
747}
748
Michal Vasko086311b2016-01-08 09:53:11 +0100749int
750nc_ctx_check_and_fill(struct nc_session *session)
751{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200752 int i, get_schema_support = 0, yanglib_support = 0, ret = -1, r;
Michal Vaskocdeee432017-01-13 13:51:01 +0100753 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100754 void *old_data = NULL;
755
Michal Vasko2e6defd2016-10-07 15:48:15 +0200756 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100757
Radek Krejcifd5b6682017-06-13 15:52:53 +0200758 /* store the original user's callback, here we will be switching between searchpath, user callback
759 * and get-schema callback */
760 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
761 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); /* unset callback, so we prefer local searchpath */
762
Michal Vasko086311b2016-01-08 09:53:11 +0100763 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200764 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200765 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100766 get_schema_support = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200767 if (yanglib_support) {
768 break;
769 }
770 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-yang-library?", 46)) {
771 yanglib_support = 1;
772 if (get_schema_support) {
773 break;
774 }
Michal Vasko086311b2016-01-08 09:53:11 +0100775 }
776 }
777
778 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200779 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200780 if (!lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100781 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200782 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100783 }
784 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200785 /* yang-library present does not need to be checked, it is one of the libyang's internal modules,
786 * so it is always present */
Michal Vasko086311b2016-01-08 09:53:11 +0100787
788 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200789 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200790 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100791 }
792
Radek Krejcifd5b6682017-06-13 15:52:53 +0200793 if (yanglib_support && get_schema_support) {
794 /* load schemas according to the ietf-yang-library data, which are more precise than capabilities list */
795 r = nc_ctx_fill_yl(session, old_clb, old_data);
796 if (r == -1) {
797 goto cleanup;
798 } else if (r == 1) {
Michal Vasko60f66602017-10-17 13:52:18 +0200799 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 +0200800 /* try to use standard capabilities */
801 goto capabilities;
802 }
803 } else {
804capabilities:
Michal Vaskoef578332016-01-25 13:20:09 +0100805
Michal Vasko60f66602017-10-17 13:52:18 +0200806 if (nc_ctx_fill_cpblts(session, old_clb, old_data)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200807 goto cleanup;
Michal Vaskoef578332016-01-25 13:20:09 +0100808 }
Michal Vasko086311b2016-01-08 09:53:11 +0100809 }
810
Radek Krejcifd5b6682017-06-13 15:52:53 +0200811 /* succsess */
812 ret = 0;
813
Michal Vaskoeee99412016-11-21 10:19:43 +0100814 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
815 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 +0100816 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200817
818cleanup:
819 /* set user callback back */
820 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
821
Michal Vaskoef578332016-01-25 13:20:09 +0100822 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100823}
824
825API struct nc_session *
826nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
827{
Michal Vaskod083db62016-01-19 10:31:29 +0100828 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100829
Michal Vasko45e53ae2016-04-07 11:46:03 +0200830 if (fdin < 0) {
831 ERRARG("fdin");
832 return NULL;
833 } else if (fdout < 0) {
834 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100835 return NULL;
836 }
837
838 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +0100839 session = nc_new_session(0);
Michal Vasko086311b2016-01-08 09:53:11 +0100840 if (!session) {
841 ERRMEM;
842 return NULL;
843 }
844 session->status = NC_STATUS_STARTING;
845 session->side = NC_CLIENT;
846
847 /* transport specific data */
848 session->ti_type = NC_TI_FD;
Michal Vaskoade892d2017-02-22 13:40:35 +0100849 pthread_mutex_init(session->ti_lock, NULL);
850 pthread_cond_init(session->ti_cond, NULL);
851 *session->ti_inuse = 0;
852
Michal Vasko086311b2016-01-08 09:53:11 +0100853 session->ti.fd.in = fdin;
854 session->ti.fd.out = fdout;
855
856 /* assign context (dicionary needed for handshake) */
857 if (!ctx) {
Radek Krejci3222b7d2017-09-21 16:04:30 +0200858 ctx = ly_ctx_new(SCHEMAS_DIR, 0);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100859 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200860 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100861 /* that's just it */
862 goto fail;
863 }
Michal Vasko086311b2016-01-08 09:53:11 +0100864 } else {
865 session->flags |= NC_SESSION_SHAREDCTX;
866 }
867 session->ctx = ctx;
868
869 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200870 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100871 goto fail;
872 }
873 session->status = NC_STATUS_RUNNING;
874
Michal Vaskoef578332016-01-25 13:20:09 +0100875 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100876 goto fail;
877 }
878
879 return session;
880
881fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100882 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100883 return NULL;
884}
885
886int
Michal Vaskof05562c2016-01-20 12:06:43 +0100887nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100888{
Michal Vasko0190bc32016-03-02 15:47:49 +0100889 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100890 struct addrinfo hints, *res_list, *res;
891 char port_s[6]; /* length of string representation of short int */
892
893 snprintf(port_s, 6, "%u", port);
894
895 /* Connect to a server */
896 memset(&hints, 0, sizeof hints);
897 hints.ai_family = AF_UNSPEC;
898 hints.ai_socktype = SOCK_STREAM;
899 hints.ai_protocol = IPPROTO_TCP;
900 i = getaddrinfo(host, port_s, &hints, &res_list);
901 if (i != 0) {
902 ERR("Unable to translate the host address (%s).", gai_strerror(i));
903 return -1;
904 }
905
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200906 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100907 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
908 if (sock == -1) {
909 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200910 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100911 }
912
913 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
914 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100915 close(sock);
916 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200917 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100918 }
919
Michal Vasko0190bc32016-03-02 15:47:49 +0100920 /* make the socket non-blocking */
921 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
922 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100923 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200924 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100925 return -1;
926 }
927
Michal Vasko086311b2016-01-08 09:53:11 +0100928 /* we're done, network connection established */
929 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100930 }
931
Michal Vasko29af44b2016-10-13 10:59:55 +0200932 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100933 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 +0100934 }
935 freeaddrinfo(res_list);
936
937 return sock;
938}
939
Michal Vasko086311b2016-01-08 09:53:11 +0100940static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100941get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100942{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100943 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100944 char *ptr;
945 const char *str_msgid;
946 uint64_t cur_msgid;
947 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100948 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100949 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
950
Michal Vaskoade892d2017-02-22 13:40:35 +0100951 r = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100952 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100953 /* error */
954 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100955 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100956 /* timeout */
957 return NC_MSG_WOULDBLOCK;
958 }
959
960 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200961 if (!msgid && session->opts.client.notifs) {
962 cont = session->opts.client.notifs;
963 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100964
Michal Vasko71ba2da2016-05-04 10:53:16 +0200965 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100966 free(cont);
967
Michal Vasko71ba2da2016-05-04 10:53:16 +0200968 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100969 }
970
971 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200972 if (msgid && session->opts.client.replies) {
973 cont = session->opts.client.replies;
974 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100975
Michal Vasko71ba2da2016-05-04 10:53:16 +0200976 xml = cont->msg;
977 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100978
Michal Vasko71ba2da2016-05-04 10:53:16 +0200979 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100980 }
981
Michal Vasko71ba2da2016-05-04 10:53:16 +0200982 if (!msgtype) {
983 /* read message from wire */
984 msgtype = nc_read_msg_poll(session, timeout, &xml);
985 }
Michal Vasko086311b2016-01-08 09:53:11 +0100986
987 /* we read rpc-reply, want a notif */
988 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200989 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +0100990 while (*cont_ptr) {
991 cont_ptr = &((*cont_ptr)->next);
992 }
993 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100994 if (!*cont_ptr) {
995 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +0100996 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100997 lyxml_free(session->ctx, xml);
998 return NC_MSG_ERROR;
999 }
Michal Vasko086311b2016-01-08 09:53:11 +01001000 (*cont_ptr)->msg = xml;
1001 (*cont_ptr)->next = NULL;
1002 }
1003
1004 /* we read notif, want a rpc-reply */
1005 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001006 if (!session->opts.client.ntf_tid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001007 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +01001008 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001009 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001010 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001011 }
Michal Vasko086311b2016-01-08 09:53:11 +01001012
Michal Vasko2e6defd2016-10-07 15:48:15 +02001013 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001014 while (*cont_ptr) {
1015 cont_ptr = &((*cont_ptr)->next);
1016 }
1017 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001018 if (!cont_ptr) {
1019 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001020 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001021 lyxml_free(session->ctx, xml);
1022 return NC_MSG_ERROR;
1023 }
Michal Vasko086311b2016-01-08 09:53:11 +01001024 (*cont_ptr)->msg = xml;
1025 (*cont_ptr)->next = NULL;
1026 }
1027
Michal Vaskoade892d2017-02-22 13:40:35 +01001028 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01001029
1030 switch (msgtype) {
1031 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001032 if (!msgid) {
1033 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001034 }
Michal Vasko086311b2016-01-08 09:53:11 +01001035 break;
1036
1037 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001038 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001039 /* check message-id */
1040 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1041 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001042 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001043 } else {
1044 cur_msgid = strtoul(str_msgid, &ptr, 10);
1045 if (cur_msgid != msgid) {
1046 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1047 session->id, str_msgid);
1048 msgtype = NC_MSG_REPLY_ERR_MSGID;
1049 }
1050 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001051 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001052 }
Michal Vasko086311b2016-01-08 09:53:11 +01001053 break;
1054
1055 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001056 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001057 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001058 msgtype = NC_MSG_ERROR;
1059 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001060
1061 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001062 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001063 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001064 msgtype = NC_MSG_ERROR;
1065 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001066
1067 default:
1068 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1069 * NC_MSG_NONE is not returned by nc_read_msg()
1070 */
1071 break;
1072 }
1073
1074 return msgtype;
1075}
1076
1077/* cannot strictly fail, but does not need to fill any error parameter at all */
1078static void
1079parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1080{
1081 struct lyxml_elem *iter, *next, *info;
1082
1083 LY_TREE_FOR(xml->child, iter) {
1084 if (!iter->ns) {
1085 if (iter->content) {
1086 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1087 } else {
1088 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1089 }
1090 continue;
1091 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1092 if (iter->content) {
1093 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1094 iter->name, iter->content, iter->ns->value);
1095 } else {
1096 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1097 }
1098 continue;
1099 }
1100
1101 if (!strcmp(iter->name, "error-type")) {
1102 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1103 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1104 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1105 } else if (err->type) {
1106 WRN("<rpc-error> <error-type> duplicated.");
1107 } else {
1108 err->type = lydict_insert(ctx, iter->content, 0);
1109 }
1110 } else if (!strcmp(iter->name, "error-tag")) {
1111 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1112 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1113 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1114 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1115 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1116 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1117 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1118 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1119 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1120 && strcmp(iter->content, "malformed-message"))) {
1121 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1122 } else if (err->tag) {
1123 WRN("<rpc-error> <error-tag> duplicated.");
1124 } else {
1125 err->tag = lydict_insert(ctx, iter->content, 0);
1126 }
1127 } else if (!strcmp(iter->name, "error-severity")) {
1128 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1129 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1130 } else if (err->severity) {
1131 WRN("<rpc-error> <error-severity> duplicated.");
1132 } else {
1133 err->severity = lydict_insert(ctx, iter->content, 0);
1134 }
1135 } else if (!strcmp(iter->name, "error-app-tag")) {
1136 if (err->apptag) {
1137 WRN("<rpc-error> <error-app-tag> duplicated.");
1138 } else {
1139 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1140 }
1141 } else if (!strcmp(iter->name, "error-path")) {
1142 if (err->path) {
1143 WRN("<rpc-error> <error-path> duplicated.");
1144 } else {
1145 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1146 }
1147 } else if (!strcmp(iter->name, "error-message")) {
1148 if (err->message) {
1149 WRN("<rpc-error> <error-message> duplicated.");
1150 } else {
1151 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001152 if (err->message_lang) {
1153 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1154 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001155 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1156 }
1157 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1158 }
1159 } else if (!strcmp(iter->name, "error-info")) {
1160 LY_TREE_FOR_SAFE(iter->child, next, info) {
1161 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1162 if (!strcmp(info->name, "session-id")) {
1163 if (err->sid) {
1164 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1165 } else {
1166 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1167 }
Michal Vasko630485f2018-01-03 14:28:32 +01001168 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001169 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001170 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1171 if (!err->attr) {
1172 ERRMEM;
1173 return;
1174 }
Michal Vasko086311b2016-01-08 09:53:11 +01001175 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1176 } else if (!strcmp(info->name, "bad-element")) {
1177 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001178 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1179 if (!err->elem) {
1180 ERRMEM;
1181 return;
1182 }
Michal Vasko086311b2016-01-08 09:53:11 +01001183 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1184 } else if (!strcmp(info->name, "bad-namespace")) {
1185 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001186 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1187 if (!err->ns) {
1188 ERRMEM;
1189 return;
1190 }
Michal Vasko086311b2016-01-08 09:53:11 +01001191 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1192 } else {
1193 if (info->content) {
1194 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1195 info->name, info->content);
1196 } else {
1197 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1198 }
1199 }
1200 } else {
1201 lyxml_unlink(ctx, info);
1202 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001203 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1204 if (!err->other) {
1205 ERRMEM;
1206 return;
1207 }
Michal Vasko086311b2016-01-08 09:53:11 +01001208 err->other[err->other_count - 1] = info;
1209 }
1210 }
1211 } else {
1212 if (iter->content) {
1213 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1214 } else {
1215 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1216 }
1217 }
1218 }
1219}
1220
1221static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001222parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001223{
1224 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001225 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001226 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001227 struct nc_reply_data *data_rpl;
1228 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001229 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001230 int i;
1231
1232 if (!xml->child) {
1233 ERR("An empty <rpc-reply>.");
1234 return NULL;
1235 }
1236
1237 /* rpc-error */
1238 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1239 /* count and check elements */
1240 i = 0;
1241 LY_TREE_FOR(xml->child, iter) {
1242 if (strcmp(iter->name, "rpc-error")) {
1243 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1244 return NULL;
1245 } else if (!iter->ns) {
1246 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1247 return NULL;
1248 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1249 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1250 return NULL;
1251 }
1252 ++i;
1253 }
1254
1255 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001256 if (!error_rpl) {
1257 ERRMEM;
1258 return NULL;
1259 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001260 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001261 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001262 if (!error_rpl->err) {
1263 ERRMEM;
1264 free(error_rpl);
1265 return NULL;
1266 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001267 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001268 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001269 reply = (struct nc_reply *)error_rpl;
1270
1271 i = 0;
1272 LY_TREE_FOR(xml->child, iter) {
1273 parse_rpc_error(ctx, iter, error_rpl->err + i);
1274 ++i;
1275 }
1276
1277 /* ok */
1278 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1279 if (xml->child->next) {
1280 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1281 return NULL;
1282 }
1283 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001284 if (!reply) {
1285 ERRMEM;
1286 return NULL;
1287 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001288 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001289
1290 /* some RPC output */
1291 } else {
1292 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001293 case NC_RPC_ACT_GENERIC:
1294 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001295
1296 if (rpc_gen->has_data) {
Michal Vaskoe1708602016-10-18 12:17:22 +02001297 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +01001298 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001299 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 +02001300 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001301 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001302 return NULL;
1303 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001304 }
Michal Vasko086311b2016-01-08 09:53:11 +01001305 break;
1306
1307 case NC_RPC_GETCONFIG:
1308 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001309 if (!xml->child->child) {
1310 /* we did not receive any data */
1311 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001312 if (!data_rpl) {
1313 ERRMEM;
1314 return NULL;
1315 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001316 data_rpl->type = NC_RPL_DATA;
1317 data_rpl->data = NULL;
1318 return (struct nc_reply *)data_rpl;
1319 }
1320
Michal Vasko086311b2016-01-08 09:53:11 +01001321 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001322 data = lyd_parse_xml(ctx, &xml->child->child,
1323 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001324 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001325 if (!data) {
1326 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1327 return NULL;
1328 }
1329 break;
1330
1331 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001332 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001333 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001334 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001335 return NULL;
1336 }
1337 break;
1338
1339 case NC_RPC_EDIT:
1340 case NC_RPC_COPY:
1341 case NC_RPC_DELETE:
1342 case NC_RPC_LOCK:
1343 case NC_RPC_UNLOCK:
1344 case NC_RPC_KILL:
1345 case NC_RPC_COMMIT:
1346 case NC_RPC_DISCARD:
1347 case NC_RPC_CANCEL:
1348 case NC_RPC_VALIDATE:
1349 case NC_RPC_SUBSCRIBE:
1350 /* there is no output defined */
1351 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1352 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001353 default:
1354 ERRINT;
1355 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001356 }
1357
1358 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001359 if (!data_rpl) {
1360 ERRMEM;
1361 return NULL;
1362 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001363 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001364 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001365 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001366 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001367 } else {
1368 /* <get>, <get-config> */
1369 data_rpl->data = data;
1370 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001371 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001372 if (!data_rpl->data) {
1373 ERR("Failed to parse <rpc-reply>.");
1374 free(data_rpl);
1375 return NULL;
1376 }
1377 reply = (struct nc_reply *)data_rpl;
1378 }
1379
1380 return reply;
1381}
1382
Radek Krejci53691be2016-02-22 13:58:37 +01001383#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001384
Michal Vasko3031aae2016-01-27 16:07:18 +01001385int
1386nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1387{
1388 int sock;
1389
Michal Vasko45e53ae2016-04-07 11:46:03 +02001390 if (!address) {
1391 ERRARG("address");
1392 return -1;
1393 } else if (!port) {
1394 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001395 return -1;
1396 }
1397
1398 sock = nc_sock_listen(address, port);
1399 if (sock == -1) {
1400 return -1;
1401 }
1402
1403 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001404 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1405 if (!client_opts.ch_binds) {
1406 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001407 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001408 return -1;
1409 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001410
Michal Vasko2e6defd2016-10-07 15:48:15 +02001411 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1412 if (!client_opts.ch_bind_ti) {
1413 ERRMEM;
1414 close(sock);
1415 return -1;
1416 }
1417 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1418
Michal Vasko3031aae2016-01-27 16:07:18 +01001419 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001420 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1421 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001422 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001423 return -1;
1424 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001425 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1426 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001427 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001428
1429 return 0;
1430}
1431
1432int
1433nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1434{
1435 uint32_t i;
1436 int ret = -1;
1437
1438 if (!address && !port && !ti) {
1439 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1440 close(client_opts.ch_binds[i].sock);
1441 free((char *)client_opts.ch_binds[i].address);
1442
1443 ret = 0;
1444 }
1445 free(client_opts.ch_binds);
1446 client_opts.ch_binds = NULL;
1447 client_opts.ch_bind_count = 0;
1448 } else {
1449 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1450 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1451 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001452 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001453 close(client_opts.ch_binds[i].sock);
1454 free((char *)client_opts.ch_binds[i].address);
1455
1456 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001457 if (!client_opts.ch_bind_count) {
1458 free(client_opts.ch_binds);
1459 client_opts.ch_binds = NULL;
1460 } else if (i < client_opts.ch_bind_count) {
1461 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1462 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1463 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001464
1465 ret = 0;
1466 }
1467 }
1468 }
1469
1470 return ret;
1471}
1472
1473API int
1474nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1475{
1476 int sock;
1477 char *host = NULL;
1478 uint16_t port, idx;
1479
Michal Vasko45e53ae2016-04-07 11:46:03 +02001480 if (!client_opts.ch_binds) {
1481 ERRINIT;
1482 return -1;
1483 } else if (!session) {
1484 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001485 return -1;
1486 }
1487
1488 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1489
Michal Vasko50456e82016-02-02 12:16:08 +01001490 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001491 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001492 return sock;
1493 }
1494
Radek Krejci53691be2016-02-22 13:58:37 +01001495#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001496 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001497 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001498 } else
1499#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001500#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001501 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001502 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001503 } else
1504#endif
1505 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001506 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001507 *session = NULL;
1508 }
1509
1510 free(host);
1511
1512 if (!(*session)) {
1513 return -1;
1514 }
1515
1516 return 1;
1517}
1518
Radek Krejci53691be2016-02-22 13:58:37 +01001519#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001520
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001521API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001522nc_session_get_cpblts(const struct nc_session *session)
1523{
1524 if (!session) {
1525 ERRARG("session");
1526 return NULL;
1527 }
1528
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001529 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001530}
1531
1532API const char *
1533nc_session_cpblt(const struct nc_session *session, const char *capab)
1534{
1535 int i, len;
1536
1537 if (!session) {
1538 ERRARG("session");
1539 return NULL;
1540 } else if (!capab) {
1541 ERRARG("capab");
1542 return NULL;
1543 }
1544
1545 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001546 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1547 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1548 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001549 }
1550 }
1551
1552 return NULL;
1553}
1554
Michal Vasko9cd26a82016-05-31 08:58:48 +02001555API int
1556nc_session_ntf_thread_running(const struct nc_session *session)
1557{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001558 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001559 ERRARG("session");
1560 return 0;
1561 }
1562
Michal Vasko2e6defd2016-10-07 15:48:15 +02001563 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001564}
1565
Michal Vaskob7558c52016-02-26 15:04:19 +01001566API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001567nc_client_init(void)
1568{
1569 nc_init();
1570}
1571
1572API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001573nc_client_destroy(void)
1574{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001575 nc_client_set_schema_searchpath(NULL);
1576#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1577 nc_client_ch_del_bind(NULL, 0, 0);
1578#endif
1579#ifdef NC_ENABLED_SSH
1580 nc_client_ssh_destroy_opts();
1581#endif
1582#ifdef NC_ENABLED_TLS
1583 nc_client_tls_destroy_opts();
1584#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001585 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001586}
1587
Michal Vasko086311b2016-01-08 09:53:11 +01001588API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001589nc_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 +01001590{
1591 struct lyxml_elem *xml;
1592 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1593
Michal Vasko45e53ae2016-04-07 11:46:03 +02001594 if (!session) {
1595 ERRARG("session");
1596 return NC_MSG_ERROR;
1597 } else if (!rpc) {
1598 ERRARG("rpc");
1599 return NC_MSG_ERROR;
1600 } else if (!reply) {
1601 ERRARG("reply");
1602 return NC_MSG_ERROR;
1603 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1604 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001605 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001606 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001607 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001608 return NC_MSG_ERROR;
1609 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001610 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001611 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1612 parseroptions &= LYD_OPT_STRICT;
1613 }
Michal Vasko41adf392017-01-17 10:39:04 +01001614 /* no mechanism to check external dependencies is provided */
1615 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001616 *reply = NULL;
1617
1618 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001619
Michal Vasko71ba2da2016-05-04 10:53:16 +02001620 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001621 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001622 lyxml_free(session->ctx, xml);
1623 if (!(*reply)) {
1624 return NC_MSG_ERROR;
1625 }
1626 }
1627
1628 return msgtype;
1629}
1630
1631API NC_MSG_TYPE
1632nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1633{
1634 struct lyxml_elem *xml, *ev_time;
1635 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1636
Michal Vasko45e53ae2016-04-07 11:46:03 +02001637 if (!session) {
1638 ERRARG("session");
1639 return NC_MSG_ERROR;
1640 } else if (!notif) {
1641 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001642 return NC_MSG_ERROR;
1643 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001644 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001645 return NC_MSG_ERROR;
1646 }
1647
1648 msgtype = get_msg(session, timeout, 0, &xml);
1649
1650 if (msgtype == NC_MSG_NOTIF) {
1651 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001652 if (!*notif) {
1653 ERRMEM;
1654 lyxml_free(session->ctx, xml);
1655 return NC_MSG_ERROR;
1656 }
Michal Vasko086311b2016-01-08 09:53:11 +01001657
1658 /* eventTime */
1659 LY_TREE_FOR(xml->child, ev_time) {
1660 if (!strcmp(ev_time->name, "eventTime")) {
1661 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1662 /* lyd_parse does not know this element */
1663 lyxml_free(session->ctx, ev_time);
1664 break;
1665 }
1666 }
1667 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001668 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001669 goto fail;
1670 }
1671
1672 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001673 (*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 +01001674 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001675 lyxml_free(session->ctx, xml);
1676 xml = NULL;
1677 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001678 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001679 goto fail;
1680 }
1681 }
1682
1683 return msgtype;
1684
1685fail:
1686 lydict_remove(session->ctx, (*notif)->datetime);
1687 lyd_free((*notif)->tree);
1688 free(*notif);
1689 *notif = NULL;
1690 lyxml_free(session->ctx, xml);
1691
1692 return NC_MSG_ERROR;
1693}
1694
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001695static void *
1696nc_recv_notif_thread(void *arg)
1697{
1698 struct nc_ntf_thread_arg *ntarg;
1699 struct nc_session *session;
1700 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1701 struct nc_notif *notif;
1702 NC_MSG_TYPE msgtype;
1703
1704 ntarg = (struct nc_ntf_thread_arg *)arg;
1705 session = ntarg->session;
1706 notif_clb = ntarg->notif_clb;
1707 free(ntarg);
1708
Michal Vasko2e6defd2016-10-07 15:48:15 +02001709 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001710 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001711 if (msgtype == NC_MSG_NOTIF) {
1712 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001713 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1714 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1715 nc_notif_free(notif);
1716 break;
1717 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001718 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001719 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001720 /* quit this thread once the session is broken */
1721 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001722 }
1723
1724 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1725 }
1726
Michal Vasko0651c902016-05-19 15:55:42 +02001727 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001728 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001729 return NULL;
1730}
1731
1732API int
1733nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1734{
1735 struct nc_ntf_thread_arg *ntarg;
1736 int ret;
1737
Michal Vasko45e53ae2016-04-07 11:46:03 +02001738 if (!session) {
1739 ERRARG("session");
1740 return -1;
1741 } else if (!notif_clb) {
1742 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001743 return -1;
1744 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1745 ERR("Session %u: invalid session to receive Notifications.", session->id);
1746 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001747 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001748 ERR("Session %u: separate notification thread is already running.", session->id);
1749 return -1;
1750 }
1751
1752 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001753 if (!ntarg) {
1754 ERRMEM;
1755 return -1;
1756 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001757 ntarg->session = session;
1758 ntarg->notif_clb = notif_clb;
1759
1760 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001761 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1762 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001763 ERRMEM;
1764 free(ntarg);
1765 return -1;
1766 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001767
Michal Vasko2e6defd2016-10-07 15:48:15 +02001768 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001769 if (ret) {
1770 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1771 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001772 free((pthread_t *)session->opts.client.ntf_tid);
1773 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001774 return -1;
1775 }
1776
1777 return 0;
1778}
1779
Michal Vasko086311b2016-01-08 09:53:11 +01001780API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001781nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001782{
1783 NC_MSG_TYPE r;
Radek Krejcib4b19062018-02-07 16:33:06 +01001784 int ret, dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02001785 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001786 struct nc_rpc_getconfig *rpc_gc;
1787 struct nc_rpc_edit *rpc_e;
1788 struct nc_rpc_copy *rpc_cp;
1789 struct nc_rpc_delete *rpc_del;
1790 struct nc_rpc_lock *rpc_lock;
1791 struct nc_rpc_get *rpc_g;
1792 struct nc_rpc_kill *rpc_k;
1793 struct nc_rpc_commit *rpc_com;
1794 struct nc_rpc_cancel *rpc_can;
1795 struct nc_rpc_validate *rpc_val;
1796 struct nc_rpc_getschema *rpc_gs;
1797 struct nc_rpc_subscribe *rpc_sub;
1798 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001799 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001800 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001801 uint64_t cur_msgid;
1802
Michal Vasko45e53ae2016-04-07 11:46:03 +02001803 if (!session) {
1804 ERRARG("session");
1805 return NC_MSG_ERROR;
1806 } else if (!rpc) {
1807 ERRARG("rpc");
1808 return NC_MSG_ERROR;
1809 } else if (!msgid) {
1810 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001811 return NC_MSG_ERROR;
1812 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001813 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001814 return NC_MSG_ERROR;
1815 }
1816
Michal Vasko90e8e692016-07-13 12:27:57 +02001817 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001818 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001819 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01001820 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001821 return NC_MSG_ERROR;
1822 }
1823 }
1824
1825 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001826 case NC_RPC_ACT_GENERIC:
1827 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001828
1829 if (rpc_gen->has_data) {
1830 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01001831 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001832 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01001833 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 +01001834 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01001835 if (!data) {
1836 return NC_MSG_ERROR;
1837 }
Michal Vasko086311b2016-01-08 09:53:11 +01001838 }
1839 break;
1840
1841 case NC_RPC_GETCONFIG:
1842 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1843
1844 data = lyd_new(NULL, ietfnc, "get-config");
1845 node = lyd_new(data, ietfnc, "source");
1846 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1847 if (!node) {
1848 lyd_free(data);
1849 return NC_MSG_ERROR;
1850 }
1851 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001852 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001853 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001854 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001855 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001856 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001857 lyd_insert_attr(node, NULL, "type", "xpath");
1858 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001859 }
1860 if (!node) {
1861 lyd_free(data);
1862 return NC_MSG_ERROR;
1863 }
1864 }
1865
1866 if (rpc_gc->wd_mode) {
1867 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001868 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001869 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001870 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001871 return NC_MSG_ERROR;
1872 }
1873 }
1874 switch (rpc_gc->wd_mode) {
1875 case NC_WD_UNKNOWN:
1876 /* cannot get here */
1877 break;
1878 case NC_WD_ALL:
1879 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1880 break;
1881 case NC_WD_ALL_TAG:
1882 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1883 break;
1884 case NC_WD_TRIM:
1885 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1886 break;
1887 case NC_WD_EXPLICIT:
1888 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1889 break;
1890 }
1891 if (!node) {
1892 lyd_free(data);
1893 return NC_MSG_ERROR;
1894 }
1895 }
1896 break;
1897
1898 case NC_RPC_EDIT:
1899 rpc_e = (struct nc_rpc_edit *)rpc;
1900
1901 data = lyd_new(NULL, ietfnc, "edit-config");
1902 node = lyd_new(data, ietfnc, "target");
1903 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1904 if (!node) {
1905 lyd_free(data);
1906 return NC_MSG_ERROR;
1907 }
1908
1909 if (rpc_e->default_op) {
1910 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1911 if (!node) {
1912 lyd_free(data);
1913 return NC_MSG_ERROR;
1914 }
1915 }
1916
1917 if (rpc_e->test_opt) {
1918 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1919 if (!node) {
1920 lyd_free(data);
1921 return NC_MSG_ERROR;
1922 }
1923 }
1924
1925 if (rpc_e->error_opt) {
1926 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1927 if (!node) {
1928 lyd_free(data);
1929 return NC_MSG_ERROR;
1930 }
1931 }
1932
Michal Vasko7793bc62016-09-16 11:58:41 +02001933 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001934 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001935 } else {
1936 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1937 }
1938 if (!node) {
1939 lyd_free(data);
1940 return NC_MSG_ERROR;
1941 }
1942 break;
1943
1944 case NC_RPC_COPY:
1945 rpc_cp = (struct nc_rpc_copy *)rpc;
1946
1947 data = lyd_new(NULL, ietfnc, "copy-config");
1948 node = lyd_new(data, ietfnc, "target");
1949 if (rpc_cp->url_trg) {
1950 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1951 } else {
1952 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1953 }
1954 if (!node) {
1955 lyd_free(data);
1956 return NC_MSG_ERROR;
1957 }
1958
1959 node = lyd_new(data, ietfnc, "source");
1960 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001961 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001962 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001963 } else {
1964 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1965 }
1966 } else {
1967 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1968 }
1969 if (!node) {
1970 lyd_free(data);
1971 return NC_MSG_ERROR;
1972 }
1973
1974 if (rpc_cp->wd_mode) {
1975 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001976 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001977 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001978 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001979 return NC_MSG_ERROR;
1980 }
1981 }
1982 switch (rpc_cp->wd_mode) {
1983 case NC_WD_UNKNOWN:
1984 /* cannot get here */
1985 break;
1986 case NC_WD_ALL:
1987 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1988 break;
1989 case NC_WD_ALL_TAG:
1990 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1991 break;
1992 case NC_WD_TRIM:
1993 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1994 break;
1995 case NC_WD_EXPLICIT:
1996 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1997 break;
1998 }
1999 if (!node) {
2000 lyd_free(data);
2001 return NC_MSG_ERROR;
2002 }
2003 }
2004 break;
2005
2006 case NC_RPC_DELETE:
2007 rpc_del = (struct nc_rpc_delete *)rpc;
2008
2009 data = lyd_new(NULL, ietfnc, "delete-config");
2010 node = lyd_new(data, ietfnc, "target");
2011 if (rpc_del->url) {
2012 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2013 } else {
2014 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2015 }
2016 if (!node) {
2017 lyd_free(data);
2018 return NC_MSG_ERROR;
2019 }
2020 break;
2021
2022 case NC_RPC_LOCK:
2023 rpc_lock = (struct nc_rpc_lock *)rpc;
2024
2025 data = lyd_new(NULL, ietfnc, "lock");
2026 node = lyd_new(data, ietfnc, "target");
2027 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2028 if (!node) {
2029 lyd_free(data);
2030 return NC_MSG_ERROR;
2031 }
2032 break;
2033
2034 case NC_RPC_UNLOCK:
2035 rpc_lock = (struct nc_rpc_lock *)rpc;
2036
2037 data = lyd_new(NULL, ietfnc, "unlock");
2038 node = lyd_new(data, ietfnc, "target");
2039 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2040 if (!node) {
2041 lyd_free(data);
2042 return NC_MSG_ERROR;
2043 }
2044 break;
2045
2046 case NC_RPC_GET:
2047 rpc_g = (struct nc_rpc_get *)rpc;
2048
2049 data = lyd_new(NULL, ietfnc, "get");
2050 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002051 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002052 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002053 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002054 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002055 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002056 lyd_insert_attr(node, NULL, "type", "xpath");
2057 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002058 }
2059 if (!node) {
2060 lyd_free(data);
2061 return NC_MSG_ERROR;
2062 }
2063 }
2064
2065 if (rpc_g->wd_mode) {
2066 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002067 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002068 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002069 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002070 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002071 return NC_MSG_ERROR;
2072 }
2073 }
2074 switch (rpc_g->wd_mode) {
2075 case NC_WD_UNKNOWN:
2076 /* cannot get here */
2077 break;
2078 case NC_WD_ALL:
2079 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2080 break;
2081 case NC_WD_ALL_TAG:
2082 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2083 break;
2084 case NC_WD_TRIM:
2085 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2086 break;
2087 case NC_WD_EXPLICIT:
2088 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2089 break;
2090 }
2091 if (!node) {
2092 lyd_free(data);
2093 return NC_MSG_ERROR;
2094 }
2095 }
2096 break;
2097
2098 case NC_RPC_KILL:
2099 rpc_k = (struct nc_rpc_kill *)rpc;
2100
2101 data = lyd_new(NULL, ietfnc, "kill-session");
2102 sprintf(str, "%u", rpc_k->sid);
2103 lyd_new_leaf(data, ietfnc, "session-id", str);
2104 break;
2105
2106 case NC_RPC_COMMIT:
2107 rpc_com = (struct nc_rpc_commit *)rpc;
2108
2109 data = lyd_new(NULL, ietfnc, "commit");
2110 if (rpc_com->confirmed) {
2111 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2112 }
2113
2114 if (rpc_com->confirm_timeout) {
2115 sprintf(str, "%u", rpc_com->confirm_timeout);
2116 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2117 }
2118
2119 if (rpc_com->persist) {
2120 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2121 if (!node) {
2122 lyd_free(data);
2123 return NC_MSG_ERROR;
2124 }
2125 }
2126
2127 if (rpc_com->persist_id) {
2128 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2129 if (!node) {
2130 lyd_free(data);
2131 return NC_MSG_ERROR;
2132 }
2133 }
2134 break;
2135
2136 case NC_RPC_DISCARD:
2137 data = lyd_new(NULL, ietfnc, "discard-changes");
2138 break;
2139
2140 case NC_RPC_CANCEL:
2141 rpc_can = (struct nc_rpc_cancel *)rpc;
2142
2143 data = lyd_new(NULL, ietfnc, "cancel-commit");
2144 if (rpc_can->persist_id) {
2145 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2146 if (!node) {
2147 lyd_free(data);
2148 return NC_MSG_ERROR;
2149 }
2150 }
2151 break;
2152
2153 case NC_RPC_VALIDATE:
2154 rpc_val = (struct nc_rpc_validate *)rpc;
2155
2156 data = lyd_new(NULL, ietfnc, "validate");
2157 node = lyd_new(data, ietfnc, "source");
2158 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002159 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002160 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002161 } else {
2162 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2163 }
2164 } else {
2165 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2166 }
2167 if (!node) {
2168 lyd_free(data);
2169 return NC_MSG_ERROR;
2170 }
2171 break;
2172
2173 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002174 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002175 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002176 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002177 return NC_MSG_ERROR;
2178 }
2179
2180 rpc_gs = (struct nc_rpc_getschema *)rpc;
2181
2182 data = lyd_new(NULL, ietfncmon, "get-schema");
2183 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2184 if (!node) {
2185 lyd_free(data);
2186 return NC_MSG_ERROR;
2187 }
2188 if (rpc_gs->version) {
2189 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2190 if (!node) {
2191 lyd_free(data);
2192 return NC_MSG_ERROR;
2193 }
2194 }
2195 if (rpc_gs->format) {
2196 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2197 if (!node) {
2198 lyd_free(data);
2199 return NC_MSG_ERROR;
2200 }
2201 }
2202 break;
2203
2204 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002205 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002206 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002207 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002208 return NC_MSG_ERROR;
2209 }
2210
2211 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2212
2213 data = lyd_new(NULL, notifs, "create-subscription");
2214 if (rpc_sub->stream) {
2215 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2216 if (!node) {
2217 lyd_free(data);
2218 return NC_MSG_ERROR;
2219 }
2220 }
2221
2222 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002223 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002224 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002225 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002226 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002227 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002228 lyd_insert_attr(node, NULL, "type", "xpath");
2229 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002230 }
2231 if (!node) {
2232 lyd_free(data);
2233 return NC_MSG_ERROR;
2234 }
2235 }
2236
2237 if (rpc_sub->start) {
2238 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2239 if (!node) {
2240 lyd_free(data);
2241 return NC_MSG_ERROR;
2242 }
2243 }
2244
2245 if (rpc_sub->stop) {
2246 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2247 if (!node) {
2248 lyd_free(data);
2249 return NC_MSG_ERROR;
2250 }
2251 }
2252 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002253 default:
2254 ERRINT;
2255 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002256 }
2257
Michal Vasko41adf392017-01-17 10:39:04 +01002258 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2259 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002260 if (dofree) {
2261 lyd_free(data);
2262 }
Michal Vasko086311b2016-01-08 09:53:11 +01002263 return NC_MSG_ERROR;
2264 }
2265
Michal Vaskoade892d2017-02-22 13:40:35 +01002266 ret = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002267 if (ret == -1) {
2268 /* error */
2269 r = NC_MSG_ERROR;
2270 } else if (!ret) {
2271 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01002272 r = NC_MSG_WOULDBLOCK;
2273 } else {
2274 /* send RPC, store its message ID */
2275 r = nc_send_msg(session, data);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002276 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002277 }
Michal Vaskoade892d2017-02-22 13:40:35 +01002278 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01002279
Radek Krejcib4b19062018-02-07 16:33:06 +01002280 if (dofree) {
2281 lyd_free(data);
2282 }
Michal Vasko086311b2016-01-08 09:53:11 +01002283
2284 if (r != NC_MSG_RPC) {
2285 return r;
2286 }
2287
2288 *msgid = cur_msgid;
2289 return NC_MSG_RPC;
2290}
Michal Vaskode2946c2017-01-12 12:19:26 +01002291
2292API void
2293nc_client_session_set_not_strict(struct nc_session *session)
2294{
2295 if (session->side != NC_CLIENT) {
2296 ERRARG("session");
2297 return;
2298 }
2299
2300 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2301}