blob: e15f20fe56387c461eea2f75b71ce276292c1d55 [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 }
Michal Vaskoff7e3562018-02-15 13:41:22 +0100205 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200206
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 Vaskoff7e3562018-02-15 13:41:22 +0100266/* NC_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 Vaskoff7e3562018-02-15 13:41:22 +0100277 ietfnc = lys_parse_path(ctx, NC_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);
Michal Vaskof945da52018-02-15 08:45:13 +0100412 if (asprintf(&filename, "%s/%s%s%s.yang", searchdirs ? searchdirs[0] : ".", mod_name,
413 mod_rev ? "@" : "", mod_rev ? mod_rev : "") == -1) {
414 ERRMEM;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200415 } else {
Michal Vaskof945da52018-02-15 08:45:13 +0100416 output = fopen(filename, "w");
417 if (!output) {
418 WRN("Unable to store \"%s\" as a local copy of schema retreived via <get-schema> (%s).",
419 filename, strerror(errno));
420 } else {
421 fputs(model_data, output);
422 fclose(output);
423 }
424 free(filename);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200425 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200426 }
427
Michal Vasko086311b2016-01-08 09:53:11 +0100428 return model_data;
429}
430
Michal Vaskoceae0152018-02-14 16:03:59 +0100431static int
432nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, int implement,
433 ly_module_imp_clb user_clb, void *user_data, const struct lys_module **mod)
434{
435 int ret = 0;
436 struct ly_err_item *eitem;
437
438 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
439 if (*mod) {
440 if (implement && !(*mod)->implemented) {
441 /* make the present module implemented */
442 if (lys_set_implemented(*mod)) {
443 ERR("Failed to implement model \"%s\".", (*mod)->name);
444 ret = -1;
445 }
446 }
447 } else if (!(*mod) && implement) {
448 /* missing implemented module, load it ... */
449
450 /* clear all the errors and just collect them for now */
451 ly_err_clean(session->ctx, NULL);
452 ly_log_options(LY_LOSTORE);
453
454 /* 1) using only searchpaths */
455 *mod = ly_ctx_load_module(session->ctx, name, revision);
456
457 /* 2) using user callback */
458 if (!(*mod) && user_clb) {
459 ly_ctx_set_module_imp_clb(session->ctx, user_clb, user_data);
460 *mod = ly_ctx_load_module(session->ctx, name, revision);
461 }
462
463 /* 3) using get-schema callback */
464 if (!(*mod)) {
465 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
466 *mod = ly_ctx_load_module(session->ctx, name, revision);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100467 if (*mod) {
468 /* print get-schema warning */
469 eitem = ly_err_first(session->ctx);
470 if (eitem && (eitem->prev->level == LY_LLWRN)) {
471 ly_log_options(LY_LOLOG);
472 ly_err_print(eitem->prev);
473 }
474 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100475 }
476
477 /* unset callback back to use searchpath */
478 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
479
Michal Vaskodbf7c272018-02-19 09:07:59 +0100480 /* restore logging options, then print errors on definite failure */
481 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100482 if (!(*mod)) {
483 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
484 ly_err_print(eitem);
485 }
486 ret = -1;
487 }
488
Michal Vaskodbf7c272018-02-19 09:07:59 +0100489 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100490 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100491 }
492
493 return ret;
494}
495
Michal Vaskoff7e3562018-02-15 13:41:22 +0100496/* NC_SCHEMAS_DIR not used (implicitly) */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200497static int
498nc_ctx_fill_cpblts(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
499{
500 int ret = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200501 const struct lys_module *mod;
502 char *ptr, *ptr2;
503 const char *module_cpblt;
504 char *name = NULL, *revision = NULL, *features = NULL;
505 unsigned int u;
506
507 for (u = 0; session->opts.client.cpblts[u]; ++u) {
508 module_cpblt = strstr(session->opts.client.cpblts[u], "module=");
509 /* this capability requires a module */
510 if (!module_cpblt) {
511 continue;
512 }
513
514 /* get module name */
515 ptr = (char *)module_cpblt + 7;
516 ptr2 = strchr(ptr, '&');
517 if (!ptr2) {
518 ptr2 = ptr + strlen(ptr);
519 }
520 free(name);
521 name = strndup(ptr, ptr2 - ptr);
522
523 /* get module revision */
524 free(revision); revision = NULL;
525 ptr = strstr(module_cpblt, "revision=");
526 if (ptr) {
527 ptr += 9;
528 ptr2 = strchr(ptr, '&');
529 if (!ptr2) {
530 ptr2 = ptr + strlen(ptr);
531 }
532 revision = strndup(ptr, ptr2 - ptr);
533 }
534
Michal Vasko78cd61f2018-02-22 08:32:24 +0100535 /* we can continue even if it fails */
536 nc_ctx_load_module(session, name, revision, 1, user_clb, user_data, &mod);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200537
538 if (!mod) {
Michal Vasko60f66602017-10-17 13:52:18 +0200539 if (session->status != NC_STATUS_RUNNING) {
540 /* something bad heppened, discard the session */
541 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
542 ret = 1;
543 goto cleanup;
544 }
545
Radek Krejcifd5b6682017-06-13 15:52:53 +0200546 /* all loading ways failed, the schema will be ignored in the received data */
547 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
548 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200549 } else {
550 /* set features - first disable all to enable specified then */
551 lys_features_disable(mod, "*");
552
553 ptr = strstr(module_cpblt, "features=");
554 if (ptr) {
555 ptr += 9;
556 ptr2 = strchr(ptr, '&');
557 if (!ptr2) {
558 ptr2 = ptr + strlen(ptr);
559 }
560 free(features);
561 features = strndup(ptr, ptr2 - ptr);
562
563 /* basically manual strtok_r (to avoid macro) */
564 ptr2 = features;
565 for (ptr = features; *ptr; ++ptr) {
566 if (*ptr == ',') {
567 *ptr = '\0';
568 /* remember last feature */
569 ptr2 = ptr + 1;
570 }
571 }
572
573 ptr = features;
574 while (1) {
575 lys_features_enable(mod, ptr);
576 if (ptr != ptr2) {
577 ptr += strlen(ptr) + 1;
578 } else {
579 break;
580 }
581 }
582 }
583 }
584 }
585
586 ret = 0;
587
588cleanup:
589 free(name);
590 free(revision);
591 free(features);
592
593 return ret;
594}
595
596static int
597nc_ctx_fill_yl(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
598{
599 int ret = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200600 struct nc_rpc *rpc = NULL;
601 struct nc_reply *reply = NULL;
602 struct nc_reply_error *error_rpl;
603 struct nc_reply_data *data_rpl;
604 NC_MSG_TYPE msg;
605 uint64_t msgid;
606 struct lyd_node *data = NULL, *iter;
607 struct ly_set *modules = NULL, *imports = NULL, *features = NULL;
608 unsigned int u, v;
609 const char *name, *revision;
610 int implemented, imports_flag = 0;
611 const struct lys_module *mod;
612
613 /* get yang-library data from the server */
614 rpc = nc_rpc_get("/ietf-yang-library:*//.", 0, NC_PARAMTYPE_CONST);
615 if (!rpc) {
616 goto cleanup;
617 }
618
619 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
620 usleep(1000);
621 }
622 if (msg == NC_MSG_ERROR) {
Michal Vasko60f66602017-10-17 13:52:18 +0200623 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200624 session->id);
625 goto cleanup;
626 }
627
628 do {
629 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
630 } while (msg == NC_MSG_NOTIF);
631 if (msg == NC_MSG_WOULDBLOCK) {
632 ERR("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
633 goto cleanup;
634 } else if (msg == NC_MSG_ERROR) {
635 ERR("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
636 goto cleanup;
637 }
638
639 switch (reply->type) {
640 case NC_RPL_OK:
641 ERR("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
642 goto cleanup;
643 case NC_RPL_DATA:
644 /* fine */
645 break;
646 case NC_RPL_ERROR:
647 error_rpl = (struct nc_reply_error *)reply;
648 if (error_rpl->count) {
649 ERR("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
650 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
651 } else {
652 ERR("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
653 }
654 goto cleanup;
655 case NC_RPL_NOTIF:
656 ERR("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
657 goto cleanup;
658 }
659
660 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko09319322017-10-02 09:26:54 +0200661 if (!data_rpl->data || strcmp(data_rpl->data->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200662 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
663 goto cleanup;
664 }
665
Michal Vaskof4c8b792017-07-28 14:57:22 +0200666 modules = lyd_find_path(data_rpl->data, "/ietf-yang-library:modules-state/module");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200667 if (!modules || !modules->number) {
668 ERR("No yang-library modules information for session %u.", session->id);
669 goto cleanup;
670 }
671
672 features = ly_set_new();
673 imports = ly_set_new();
674
675parse:
676 for (u = modules->number - 1; u < modules->number; u--) {
677 name = revision = NULL;
678 ly_set_clean(features);
679 implemented = 0;
680
681 /* store the data */
682 LY_TREE_FOR(modules->set.d[u]->child, iter) {
683 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
684 /* ignore empty nodes */
685 continue;
686 }
687 if (!strcmp(iter->schema->name, "name")) {
688 name = ((struct lyd_node_leaf_list *)iter)->value_str;
689 } else if (!strcmp(iter->schema->name, "revision")) {
690 revision = ((struct lyd_node_leaf_list *)iter)->value_str;
691 } else if (!strcmp(iter->schema->name, "conformance-type")) {
692 implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
693 } else if (!strcmp(iter->schema->name, "feature")) {
694 ly_set_add(features, (void*)((struct lyd_node_leaf_list *)iter)->value_str, LY_SET_OPT_USEASLIST);
695 }
696 }
697
Michal Vasko78cd61f2018-02-22 08:32:24 +0100698 /* continue even on fail */
699 nc_ctx_load_module(session, name, revision, implemented, user_clb, user_data, &mod);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200700
Michal Vaskoceae0152018-02-14 16:03:59 +0100701 if (!mod) { /* !mod && !implemented - will be loaded automatically, but remember to set features in the end */
702 assert(!implemented);
Michal Vasko3c556372017-08-09 10:17:34 +0200703 if (imports_flag) {
704 ERR("Module \"%s@%s\" is supposed to be imported, but no other module imports it.",
705 name, revision ? revision : "<latest>");
706 ret = -1;
707 goto cleanup;
708 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200709 ly_set_add(imports, modules->set.d[u], LY_SET_OPT_USEASLIST);
710 continue;
711 }
712
713 if (!mod) {
714 /* all loading ways failed, the schema will be ignored in the received data */
715 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
716 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200717 } else {
718 /* set features - first disable all to enable specified then */
719 lys_features_disable(mod, "*");
720 for (v = 0; v < features->number; v++) {
721 lys_features_enable(mod, (const char*)features->set.g[v]);
722 }
723 }
724 }
725
726 if (!imports_flag && imports->number) {
727 /* even imported modules should be now loaded as dependency, so just go through
728 * the parsing again and just set the features */
729 ly_set_free(modules);
730 modules = imports;
731 imports = NULL;
732 imports_flag = 1;
733 goto parse;
734 }
735
736 /* done */
737 ret = 0;
738
739cleanup:
740 nc_rpc_free(rpc);
741 nc_reply_free(reply);
742 lyd_free_withsiblings(data);
743
744 ly_set_free(modules);
745 ly_set_free(imports);
746 ly_set_free(features);
747
Michal Vasko60f66602017-10-17 13:52:18 +0200748 if (session->status != NC_STATUS_RUNNING) {
749 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
750 ret = -1;
751 }
752
Radek Krejcifd5b6682017-06-13 15:52:53 +0200753 return ret;
754}
755
Michal Vasko086311b2016-01-08 09:53:11 +0100756int
757nc_ctx_check_and_fill(struct nc_session *session)
758{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200759 int i, get_schema_support = 0, yanglib_support = 0, ret = -1, r;
Michal Vaskocdeee432017-01-13 13:51:01 +0100760 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100761 void *old_data = NULL;
762
Michal Vasko2e6defd2016-10-07 15:48:15 +0200763 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100764
Radek Krejcifd5b6682017-06-13 15:52:53 +0200765 /* store the original user's callback, here we will be switching between searchpath, user callback
766 * and get-schema callback */
767 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
768 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); /* unset callback, so we prefer local searchpath */
769
Michal Vasko086311b2016-01-08 09:53:11 +0100770 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200771 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200772 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100773 get_schema_support = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200774 if (yanglib_support) {
775 break;
776 }
777 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-yang-library?", 46)) {
778 yanglib_support = 1;
779 if (get_schema_support) {
780 break;
781 }
Michal Vasko086311b2016-01-08 09:53:11 +0100782 }
783 }
784
785 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200786 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100787 if (!lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100788 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200789 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100790 }
791 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200792 /* yang-library present does not need to be checked, it is one of the libyang's internal modules,
793 * so it is always present */
Michal Vasko086311b2016-01-08 09:53:11 +0100794
795 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200796 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200797 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100798 }
799
Radek Krejcifd5b6682017-06-13 15:52:53 +0200800 if (yanglib_support && get_schema_support) {
801 /* load schemas according to the ietf-yang-library data, which are more precise than capabilities list */
802 r = nc_ctx_fill_yl(session, old_clb, old_data);
803 if (r == -1) {
804 goto cleanup;
805 } else if (r == 1) {
Michal Vasko60f66602017-10-17 13:52:18 +0200806 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 +0200807 /* try to use standard capabilities */
808 goto capabilities;
809 }
810 } else {
811capabilities:
Michal Vaskoef578332016-01-25 13:20:09 +0100812
Michal Vasko60f66602017-10-17 13:52:18 +0200813 if (nc_ctx_fill_cpblts(session, old_clb, old_data)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200814 goto cleanup;
Michal Vaskoef578332016-01-25 13:20:09 +0100815 }
Michal Vasko086311b2016-01-08 09:53:11 +0100816 }
817
Radek Krejcifd5b6682017-06-13 15:52:53 +0200818 /* succsess */
819 ret = 0;
820
Michal Vaskoeee99412016-11-21 10:19:43 +0100821 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
822 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 +0100823 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200824
825cleanup:
826 /* set user callback back */
827 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
828
Michal Vaskoef578332016-01-25 13:20:09 +0100829 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100830}
831
832API struct nc_session *
833nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
834{
Michal Vaskod083db62016-01-19 10:31:29 +0100835 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100836
Michal Vasko45e53ae2016-04-07 11:46:03 +0200837 if (fdin < 0) {
838 ERRARG("fdin");
839 return NULL;
840 } else if (fdout < 0) {
841 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100842 return NULL;
843 }
844
845 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +0100846 session = nc_new_session(0);
Michal Vasko086311b2016-01-08 09:53:11 +0100847 if (!session) {
848 ERRMEM;
849 return NULL;
850 }
851 session->status = NC_STATUS_STARTING;
852 session->side = NC_CLIENT;
853
854 /* transport specific data */
855 session->ti_type = NC_TI_FD;
Michal Vaskoade892d2017-02-22 13:40:35 +0100856 pthread_mutex_init(session->ti_lock, NULL);
857 pthread_cond_init(session->ti_cond, NULL);
858 *session->ti_inuse = 0;
859
Michal Vasko086311b2016-01-08 09:53:11 +0100860 session->ti.fd.in = fdin;
861 session->ti.fd.out = fdout;
862
863 /* assign context (dicionary needed for handshake) */
864 if (!ctx) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100865 ctx = ly_ctx_new(NC_SCHEMAS_DIR, 0);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100866 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200867 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100868 /* that's just it */
869 goto fail;
870 }
Michal Vasko086311b2016-01-08 09:53:11 +0100871 } else {
872 session->flags |= NC_SESSION_SHAREDCTX;
873 }
874 session->ctx = ctx;
875
876 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200877 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100878 goto fail;
879 }
880 session->status = NC_STATUS_RUNNING;
881
Michal Vaskoef578332016-01-25 13:20:09 +0100882 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100883 goto fail;
884 }
885
886 return session;
887
888fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100889 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100890 return NULL;
891}
892
893int
Michal Vaskof05562c2016-01-20 12:06:43 +0100894nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100895{
Michal Vasko0190bc32016-03-02 15:47:49 +0100896 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100897 struct addrinfo hints, *res_list, *res;
898 char port_s[6]; /* length of string representation of short int */
899
900 snprintf(port_s, 6, "%u", port);
901
902 /* Connect to a server */
903 memset(&hints, 0, sizeof hints);
904 hints.ai_family = AF_UNSPEC;
905 hints.ai_socktype = SOCK_STREAM;
906 hints.ai_protocol = IPPROTO_TCP;
907 i = getaddrinfo(host, port_s, &hints, &res_list);
908 if (i != 0) {
909 ERR("Unable to translate the host address (%s).", gai_strerror(i));
910 return -1;
911 }
912
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200913 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100914 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
915 if (sock == -1) {
916 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200917 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100918 }
919
920 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
921 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100922 close(sock);
923 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200924 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100925 }
926
Michal Vasko0190bc32016-03-02 15:47:49 +0100927 /* make the socket non-blocking */
928 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
929 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100930 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200931 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100932 return -1;
933 }
934
Michal Vasko086311b2016-01-08 09:53:11 +0100935 /* we're done, network connection established */
936 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100937 }
938
Michal Vasko29af44b2016-10-13 10:59:55 +0200939 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100940 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 +0100941 }
942 freeaddrinfo(res_list);
943
944 return sock;
945}
946
Michal Vasko086311b2016-01-08 09:53:11 +0100947static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100948get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100949{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100950 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100951 char *ptr;
952 const char *str_msgid;
953 uint64_t cur_msgid;
954 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100955 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100956 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
957
Michal Vaskoade892d2017-02-22 13:40:35 +0100958 r = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100959 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100960 /* error */
961 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100962 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100963 /* timeout */
964 return NC_MSG_WOULDBLOCK;
965 }
966
967 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200968 if (!msgid && session->opts.client.notifs) {
969 cont = session->opts.client.notifs;
970 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100971
Michal Vasko71ba2da2016-05-04 10:53:16 +0200972 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100973 free(cont);
974
Michal Vasko71ba2da2016-05-04 10:53:16 +0200975 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100976 }
977
978 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200979 if (msgid && session->opts.client.replies) {
980 cont = session->opts.client.replies;
981 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100982
Michal Vasko71ba2da2016-05-04 10:53:16 +0200983 xml = cont->msg;
984 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100985
Michal Vasko71ba2da2016-05-04 10:53:16 +0200986 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100987 }
988
Michal Vasko71ba2da2016-05-04 10:53:16 +0200989 if (!msgtype) {
990 /* read message from wire */
991 msgtype = nc_read_msg_poll(session, timeout, &xml);
992 }
Michal Vasko086311b2016-01-08 09:53:11 +0100993
994 /* we read rpc-reply, want a notif */
995 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200996 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +0100997 while (*cont_ptr) {
998 cont_ptr = &((*cont_ptr)->next);
999 }
1000 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001001 if (!*cont_ptr) {
1002 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001003 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001004 lyxml_free(session->ctx, xml);
1005 return NC_MSG_ERROR;
1006 }
Michal Vasko086311b2016-01-08 09:53:11 +01001007 (*cont_ptr)->msg = xml;
1008 (*cont_ptr)->next = NULL;
1009 }
1010
1011 /* we read notif, want a rpc-reply */
1012 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001013 if (!session->opts.client.ntf_tid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001014 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +01001015 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001016 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001017 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001018 }
Michal Vasko086311b2016-01-08 09:53:11 +01001019
Michal Vasko2e6defd2016-10-07 15:48:15 +02001020 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001021 while (*cont_ptr) {
1022 cont_ptr = &((*cont_ptr)->next);
1023 }
1024 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001025 if (!cont_ptr) {
1026 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001027 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001028 lyxml_free(session->ctx, xml);
1029 return NC_MSG_ERROR;
1030 }
Michal Vasko086311b2016-01-08 09:53:11 +01001031 (*cont_ptr)->msg = xml;
1032 (*cont_ptr)->next = NULL;
1033 }
1034
Michal Vaskoade892d2017-02-22 13:40:35 +01001035 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01001036
1037 switch (msgtype) {
1038 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001039 if (!msgid) {
1040 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001041 }
Michal Vasko086311b2016-01-08 09:53:11 +01001042 break;
1043
1044 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001045 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001046 /* check message-id */
1047 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1048 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001049 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001050 } else {
1051 cur_msgid = strtoul(str_msgid, &ptr, 10);
1052 if (cur_msgid != msgid) {
1053 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1054 session->id, str_msgid);
1055 msgtype = NC_MSG_REPLY_ERR_MSGID;
1056 }
1057 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001058 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001059 }
Michal Vasko086311b2016-01-08 09:53:11 +01001060 break;
1061
1062 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001063 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001064 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001065 msgtype = NC_MSG_ERROR;
1066 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001067
1068 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001069 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001070 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001071 msgtype = NC_MSG_ERROR;
1072 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001073
1074 default:
1075 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1076 * NC_MSG_NONE is not returned by nc_read_msg()
1077 */
1078 break;
1079 }
1080
1081 return msgtype;
1082}
1083
1084/* cannot strictly fail, but does not need to fill any error parameter at all */
1085static void
1086parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1087{
1088 struct lyxml_elem *iter, *next, *info;
1089
1090 LY_TREE_FOR(xml->child, iter) {
1091 if (!iter->ns) {
1092 if (iter->content) {
1093 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1094 } else {
1095 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1096 }
1097 continue;
1098 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1099 if (iter->content) {
1100 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1101 iter->name, iter->content, iter->ns->value);
1102 } else {
1103 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1104 }
1105 continue;
1106 }
1107
1108 if (!strcmp(iter->name, "error-type")) {
1109 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1110 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1111 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1112 } else if (err->type) {
1113 WRN("<rpc-error> <error-type> duplicated.");
1114 } else {
1115 err->type = lydict_insert(ctx, iter->content, 0);
1116 }
1117 } else if (!strcmp(iter->name, "error-tag")) {
1118 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1119 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1120 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1121 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1122 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1123 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1124 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1125 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1126 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1127 && strcmp(iter->content, "malformed-message"))) {
1128 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1129 } else if (err->tag) {
1130 WRN("<rpc-error> <error-tag> duplicated.");
1131 } else {
1132 err->tag = lydict_insert(ctx, iter->content, 0);
1133 }
1134 } else if (!strcmp(iter->name, "error-severity")) {
1135 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1136 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1137 } else if (err->severity) {
1138 WRN("<rpc-error> <error-severity> duplicated.");
1139 } else {
1140 err->severity = lydict_insert(ctx, iter->content, 0);
1141 }
1142 } else if (!strcmp(iter->name, "error-app-tag")) {
1143 if (err->apptag) {
1144 WRN("<rpc-error> <error-app-tag> duplicated.");
1145 } else {
1146 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1147 }
1148 } else if (!strcmp(iter->name, "error-path")) {
1149 if (err->path) {
1150 WRN("<rpc-error> <error-path> duplicated.");
1151 } else {
1152 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1153 }
1154 } else if (!strcmp(iter->name, "error-message")) {
1155 if (err->message) {
1156 WRN("<rpc-error> <error-message> duplicated.");
1157 } else {
1158 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001159 if (err->message_lang) {
1160 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1161 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001162 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1163 }
1164 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1165 }
1166 } else if (!strcmp(iter->name, "error-info")) {
1167 LY_TREE_FOR_SAFE(iter->child, next, info) {
1168 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1169 if (!strcmp(info->name, "session-id")) {
1170 if (err->sid) {
1171 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1172 } else {
1173 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1174 }
Michal Vasko630485f2018-01-03 14:28:32 +01001175 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001176 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001177 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1178 if (!err->attr) {
1179 ERRMEM;
1180 return;
1181 }
Michal Vasko086311b2016-01-08 09:53:11 +01001182 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1183 } else if (!strcmp(info->name, "bad-element")) {
1184 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001185 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1186 if (!err->elem) {
1187 ERRMEM;
1188 return;
1189 }
Michal Vasko086311b2016-01-08 09:53:11 +01001190 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1191 } else if (!strcmp(info->name, "bad-namespace")) {
1192 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001193 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1194 if (!err->ns) {
1195 ERRMEM;
1196 return;
1197 }
Michal Vasko086311b2016-01-08 09:53:11 +01001198 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1199 } else {
1200 if (info->content) {
1201 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1202 info->name, info->content);
1203 } else {
1204 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1205 }
1206 }
1207 } else {
1208 lyxml_unlink(ctx, info);
1209 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001210 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1211 if (!err->other) {
1212 ERRMEM;
1213 return;
1214 }
Michal Vasko086311b2016-01-08 09:53:11 +01001215 err->other[err->other_count - 1] = info;
1216 }
1217 }
1218 } else {
1219 if (iter->content) {
1220 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1221 } else {
1222 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1223 }
1224 }
1225 }
1226}
1227
1228static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001229parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001230{
1231 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001232 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001233 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001234 struct nc_reply_data *data_rpl;
1235 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001236 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001237 int i;
1238
1239 if (!xml->child) {
1240 ERR("An empty <rpc-reply>.");
1241 return NULL;
1242 }
1243
1244 /* rpc-error */
1245 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1246 /* count and check elements */
1247 i = 0;
1248 LY_TREE_FOR(xml->child, iter) {
1249 if (strcmp(iter->name, "rpc-error")) {
1250 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1251 return NULL;
1252 } else if (!iter->ns) {
1253 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1254 return NULL;
1255 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1256 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1257 return NULL;
1258 }
1259 ++i;
1260 }
1261
1262 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001263 if (!error_rpl) {
1264 ERRMEM;
1265 return NULL;
1266 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001267 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001268 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001269 if (!error_rpl->err) {
1270 ERRMEM;
1271 free(error_rpl);
1272 return NULL;
1273 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001274 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001275 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001276 reply = (struct nc_reply *)error_rpl;
1277
1278 i = 0;
1279 LY_TREE_FOR(xml->child, iter) {
1280 parse_rpc_error(ctx, iter, error_rpl->err + i);
1281 ++i;
1282 }
1283
1284 /* ok */
1285 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1286 if (xml->child->next) {
1287 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1288 return NULL;
1289 }
1290 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001291 if (!reply) {
1292 ERRMEM;
1293 return NULL;
1294 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001295 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001296
1297 /* some RPC output */
1298 } else {
1299 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001300 case NC_RPC_ACT_GENERIC:
1301 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001302
1303 if (rpc_gen->has_data) {
Michal Vaskoe1708602016-10-18 12:17:22 +02001304 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +01001305 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001306 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 +02001307 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001308 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001309 return NULL;
1310 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001311 }
Michal Vasko086311b2016-01-08 09:53:11 +01001312 break;
1313
1314 case NC_RPC_GETCONFIG:
1315 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001316 if (!xml->child->child) {
1317 /* we did not receive any data */
1318 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001319 if (!data_rpl) {
1320 ERRMEM;
1321 return NULL;
1322 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001323 data_rpl->type = NC_RPL_DATA;
1324 data_rpl->data = NULL;
1325 return (struct nc_reply *)data_rpl;
1326 }
1327
Michal Vasko086311b2016-01-08 09:53:11 +01001328 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001329 data = lyd_parse_xml(ctx, &xml->child->child,
1330 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001331 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001332 if (!data) {
1333 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1334 return NULL;
1335 }
1336 break;
1337
1338 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001339 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001340 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001341 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001342 return NULL;
1343 }
1344 break;
1345
1346 case NC_RPC_EDIT:
1347 case NC_RPC_COPY:
1348 case NC_RPC_DELETE:
1349 case NC_RPC_LOCK:
1350 case NC_RPC_UNLOCK:
1351 case NC_RPC_KILL:
1352 case NC_RPC_COMMIT:
1353 case NC_RPC_DISCARD:
1354 case NC_RPC_CANCEL:
1355 case NC_RPC_VALIDATE:
1356 case NC_RPC_SUBSCRIBE:
1357 /* there is no output defined */
1358 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1359 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001360 default:
1361 ERRINT;
1362 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001363 }
1364
1365 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001366 if (!data_rpl) {
1367 ERRMEM;
1368 return NULL;
1369 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001370 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001371 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001372 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001373 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001374 } else {
1375 /* <get>, <get-config> */
1376 data_rpl->data = data;
1377 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001378 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001379 if (!data_rpl->data) {
1380 ERR("Failed to parse <rpc-reply>.");
1381 free(data_rpl);
1382 return NULL;
1383 }
1384 reply = (struct nc_reply *)data_rpl;
1385 }
1386
1387 return reply;
1388}
1389
Radek Krejci53691be2016-02-22 13:58:37 +01001390#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001391
Michal Vasko3031aae2016-01-27 16:07:18 +01001392int
1393nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1394{
1395 int sock;
1396
Michal Vasko45e53ae2016-04-07 11:46:03 +02001397 if (!address) {
1398 ERRARG("address");
1399 return -1;
1400 } else if (!port) {
1401 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001402 return -1;
1403 }
1404
1405 sock = nc_sock_listen(address, port);
1406 if (sock == -1) {
1407 return -1;
1408 }
1409
1410 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001411 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1412 if (!client_opts.ch_binds) {
1413 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001414 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001415 return -1;
1416 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001417
Michal Vasko2e6defd2016-10-07 15:48:15 +02001418 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1419 if (!client_opts.ch_bind_ti) {
1420 ERRMEM;
1421 close(sock);
1422 return -1;
1423 }
1424 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1425
Michal Vasko3031aae2016-01-27 16:07:18 +01001426 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001427 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1428 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001429 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001430 return -1;
1431 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001432 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1433 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001434 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001435
1436 return 0;
1437}
1438
1439int
1440nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1441{
1442 uint32_t i;
1443 int ret = -1;
1444
1445 if (!address && !port && !ti) {
1446 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1447 close(client_opts.ch_binds[i].sock);
1448 free((char *)client_opts.ch_binds[i].address);
1449
1450 ret = 0;
1451 }
1452 free(client_opts.ch_binds);
1453 client_opts.ch_binds = NULL;
1454 client_opts.ch_bind_count = 0;
1455 } else {
1456 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1457 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1458 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001459 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001460 close(client_opts.ch_binds[i].sock);
1461 free((char *)client_opts.ch_binds[i].address);
1462
1463 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001464 if (!client_opts.ch_bind_count) {
1465 free(client_opts.ch_binds);
1466 client_opts.ch_binds = NULL;
1467 } else if (i < client_opts.ch_bind_count) {
1468 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1469 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1470 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001471
1472 ret = 0;
1473 }
1474 }
1475 }
1476
1477 return ret;
1478}
1479
1480API int
1481nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1482{
1483 int sock;
1484 char *host = NULL;
1485 uint16_t port, idx;
1486
Michal Vasko45e53ae2016-04-07 11:46:03 +02001487 if (!client_opts.ch_binds) {
1488 ERRINIT;
1489 return -1;
1490 } else if (!session) {
1491 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001492 return -1;
1493 }
1494
1495 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1496
Michal Vasko50456e82016-02-02 12:16:08 +01001497 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001498 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001499 return sock;
1500 }
1501
Radek Krejci53691be2016-02-22 13:58:37 +01001502#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001503 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001504 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001505 } else
1506#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001507#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001508 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001509 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001510 } else
1511#endif
1512 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001513 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001514 *session = NULL;
1515 }
1516
1517 free(host);
1518
1519 if (!(*session)) {
1520 return -1;
1521 }
1522
1523 return 1;
1524}
1525
Radek Krejci53691be2016-02-22 13:58:37 +01001526#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001527
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001528API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001529nc_session_get_cpblts(const struct nc_session *session)
1530{
1531 if (!session) {
1532 ERRARG("session");
1533 return NULL;
1534 }
1535
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001536 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001537}
1538
1539API const char *
1540nc_session_cpblt(const struct nc_session *session, const char *capab)
1541{
1542 int i, len;
1543
1544 if (!session) {
1545 ERRARG("session");
1546 return NULL;
1547 } else if (!capab) {
1548 ERRARG("capab");
1549 return NULL;
1550 }
1551
1552 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001553 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1554 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1555 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001556 }
1557 }
1558
1559 return NULL;
1560}
1561
Michal Vasko9cd26a82016-05-31 08:58:48 +02001562API int
1563nc_session_ntf_thread_running(const struct nc_session *session)
1564{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001565 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001566 ERRARG("session");
1567 return 0;
1568 }
1569
Michal Vasko2e6defd2016-10-07 15:48:15 +02001570 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001571}
1572
Michal Vaskob7558c52016-02-26 15:04:19 +01001573API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001574nc_client_init(void)
1575{
1576 nc_init();
1577}
1578
1579API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001580nc_client_destroy(void)
1581{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001582 nc_client_set_schema_searchpath(NULL);
1583#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1584 nc_client_ch_del_bind(NULL, 0, 0);
1585#endif
1586#ifdef NC_ENABLED_SSH
1587 nc_client_ssh_destroy_opts();
1588#endif
1589#ifdef NC_ENABLED_TLS
1590 nc_client_tls_destroy_opts();
1591#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001592 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001593}
1594
Michal Vasko086311b2016-01-08 09:53:11 +01001595API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001596nc_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 +01001597{
1598 struct lyxml_elem *xml;
1599 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1600
Michal Vasko45e53ae2016-04-07 11:46:03 +02001601 if (!session) {
1602 ERRARG("session");
1603 return NC_MSG_ERROR;
1604 } else if (!rpc) {
1605 ERRARG("rpc");
1606 return NC_MSG_ERROR;
1607 } else if (!reply) {
1608 ERRARG("reply");
1609 return NC_MSG_ERROR;
1610 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1611 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001612 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001613 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001614 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001615 return NC_MSG_ERROR;
1616 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001617 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001618 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1619 parseroptions &= LYD_OPT_STRICT;
1620 }
Michal Vasko41adf392017-01-17 10:39:04 +01001621 /* no mechanism to check external dependencies is provided */
1622 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001623 *reply = NULL;
1624
1625 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001626
Michal Vasko71ba2da2016-05-04 10:53:16 +02001627 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001628 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001629 lyxml_free(session->ctx, xml);
1630 if (!(*reply)) {
1631 return NC_MSG_ERROR;
1632 }
1633 }
1634
1635 return msgtype;
1636}
1637
1638API NC_MSG_TYPE
1639nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1640{
1641 struct lyxml_elem *xml, *ev_time;
1642 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1643
Michal Vasko45e53ae2016-04-07 11:46:03 +02001644 if (!session) {
1645 ERRARG("session");
1646 return NC_MSG_ERROR;
1647 } else if (!notif) {
1648 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001649 return NC_MSG_ERROR;
1650 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001651 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001652 return NC_MSG_ERROR;
1653 }
1654
1655 msgtype = get_msg(session, timeout, 0, &xml);
1656
1657 if (msgtype == NC_MSG_NOTIF) {
1658 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001659 if (!*notif) {
1660 ERRMEM;
1661 lyxml_free(session->ctx, xml);
1662 return NC_MSG_ERROR;
1663 }
Michal Vasko086311b2016-01-08 09:53:11 +01001664
1665 /* eventTime */
1666 LY_TREE_FOR(xml->child, ev_time) {
1667 if (!strcmp(ev_time->name, "eventTime")) {
1668 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1669 /* lyd_parse does not know this element */
1670 lyxml_free(session->ctx, ev_time);
1671 break;
1672 }
1673 }
1674 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001675 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001676 goto fail;
1677 }
1678
1679 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001680 (*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 +01001681 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001682 lyxml_free(session->ctx, xml);
1683 xml = NULL;
1684 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001685 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001686 goto fail;
1687 }
1688 }
1689
1690 return msgtype;
1691
1692fail:
1693 lydict_remove(session->ctx, (*notif)->datetime);
1694 lyd_free((*notif)->tree);
1695 free(*notif);
1696 *notif = NULL;
1697 lyxml_free(session->ctx, xml);
1698
1699 return NC_MSG_ERROR;
1700}
1701
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001702static void *
1703nc_recv_notif_thread(void *arg)
1704{
1705 struct nc_ntf_thread_arg *ntarg;
1706 struct nc_session *session;
1707 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1708 struct nc_notif *notif;
1709 NC_MSG_TYPE msgtype;
1710
1711 ntarg = (struct nc_ntf_thread_arg *)arg;
1712 session = ntarg->session;
1713 notif_clb = ntarg->notif_clb;
1714 free(ntarg);
1715
Michal Vasko2e6defd2016-10-07 15:48:15 +02001716 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001717 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001718 if (msgtype == NC_MSG_NOTIF) {
1719 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001720 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1721 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1722 nc_notif_free(notif);
1723 break;
1724 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001725 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001726 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001727 /* quit this thread once the session is broken */
1728 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001729 }
1730
1731 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1732 }
1733
Michal Vasko0651c902016-05-19 15:55:42 +02001734 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001735 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001736 return NULL;
1737}
1738
1739API int
1740nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1741{
1742 struct nc_ntf_thread_arg *ntarg;
1743 int ret;
1744
Michal Vasko45e53ae2016-04-07 11:46:03 +02001745 if (!session) {
1746 ERRARG("session");
1747 return -1;
1748 } else if (!notif_clb) {
1749 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001750 return -1;
1751 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1752 ERR("Session %u: invalid session to receive Notifications.", session->id);
1753 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001754 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001755 ERR("Session %u: separate notification thread is already running.", session->id);
1756 return -1;
1757 }
1758
1759 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001760 if (!ntarg) {
1761 ERRMEM;
1762 return -1;
1763 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001764 ntarg->session = session;
1765 ntarg->notif_clb = notif_clb;
1766
1767 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001768 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1769 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001770 ERRMEM;
1771 free(ntarg);
1772 return -1;
1773 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001774
Michal Vasko2e6defd2016-10-07 15:48:15 +02001775 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001776 if (ret) {
1777 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1778 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001779 free((pthread_t *)session->opts.client.ntf_tid);
1780 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001781 return -1;
1782 }
1783
1784 return 0;
1785}
1786
Michal Vasko086311b2016-01-08 09:53:11 +01001787API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001788nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001789{
1790 NC_MSG_TYPE r;
Radek Krejcib4b19062018-02-07 16:33:06 +01001791 int ret, dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02001792 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001793 struct nc_rpc_getconfig *rpc_gc;
1794 struct nc_rpc_edit *rpc_e;
1795 struct nc_rpc_copy *rpc_cp;
1796 struct nc_rpc_delete *rpc_del;
1797 struct nc_rpc_lock *rpc_lock;
1798 struct nc_rpc_get *rpc_g;
1799 struct nc_rpc_kill *rpc_k;
1800 struct nc_rpc_commit *rpc_com;
1801 struct nc_rpc_cancel *rpc_can;
1802 struct nc_rpc_validate *rpc_val;
1803 struct nc_rpc_getschema *rpc_gs;
1804 struct nc_rpc_subscribe *rpc_sub;
1805 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001806 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001807 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001808 uint64_t cur_msgid;
1809
Michal Vasko45e53ae2016-04-07 11:46:03 +02001810 if (!session) {
1811 ERRARG("session");
1812 return NC_MSG_ERROR;
1813 } else if (!rpc) {
1814 ERRARG("rpc");
1815 return NC_MSG_ERROR;
1816 } else if (!msgid) {
1817 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001818 return NC_MSG_ERROR;
1819 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001820 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001821 return NC_MSG_ERROR;
1822 }
1823
Michal Vasko90e8e692016-07-13 12:27:57 +02001824 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001825 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001826 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01001827 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001828 return NC_MSG_ERROR;
1829 }
1830 }
1831
1832 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001833 case NC_RPC_ACT_GENERIC:
1834 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001835
1836 if (rpc_gen->has_data) {
1837 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01001838 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001839 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01001840 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 +01001841 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01001842 if (!data) {
1843 return NC_MSG_ERROR;
1844 }
Michal Vasko086311b2016-01-08 09:53:11 +01001845 }
1846 break;
1847
1848 case NC_RPC_GETCONFIG:
1849 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1850
1851 data = lyd_new(NULL, ietfnc, "get-config");
1852 node = lyd_new(data, ietfnc, "source");
1853 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1854 if (!node) {
1855 lyd_free(data);
1856 return NC_MSG_ERROR;
1857 }
1858 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001859 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001860 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001861 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001862 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001863 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001864 lyd_insert_attr(node, NULL, "type", "xpath");
1865 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001866 }
1867 if (!node) {
1868 lyd_free(data);
1869 return NC_MSG_ERROR;
1870 }
1871 }
1872
1873 if (rpc_gc->wd_mode) {
1874 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001875 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001876 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001877 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001878 return NC_MSG_ERROR;
1879 }
1880 }
1881 switch (rpc_gc->wd_mode) {
1882 case NC_WD_UNKNOWN:
1883 /* cannot get here */
1884 break;
1885 case NC_WD_ALL:
1886 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1887 break;
1888 case NC_WD_ALL_TAG:
1889 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1890 break;
1891 case NC_WD_TRIM:
1892 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1893 break;
1894 case NC_WD_EXPLICIT:
1895 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1896 break;
1897 }
1898 if (!node) {
1899 lyd_free(data);
1900 return NC_MSG_ERROR;
1901 }
1902 }
1903 break;
1904
1905 case NC_RPC_EDIT:
1906 rpc_e = (struct nc_rpc_edit *)rpc;
1907
1908 data = lyd_new(NULL, ietfnc, "edit-config");
1909 node = lyd_new(data, ietfnc, "target");
1910 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1911 if (!node) {
1912 lyd_free(data);
1913 return NC_MSG_ERROR;
1914 }
1915
1916 if (rpc_e->default_op) {
1917 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1918 if (!node) {
1919 lyd_free(data);
1920 return NC_MSG_ERROR;
1921 }
1922 }
1923
1924 if (rpc_e->test_opt) {
1925 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1926 if (!node) {
1927 lyd_free(data);
1928 return NC_MSG_ERROR;
1929 }
1930 }
1931
1932 if (rpc_e->error_opt) {
1933 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1934 if (!node) {
1935 lyd_free(data);
1936 return NC_MSG_ERROR;
1937 }
1938 }
1939
Michal Vasko7793bc62016-09-16 11:58:41 +02001940 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001941 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001942 } else {
1943 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1944 }
1945 if (!node) {
1946 lyd_free(data);
1947 return NC_MSG_ERROR;
1948 }
1949 break;
1950
1951 case NC_RPC_COPY:
1952 rpc_cp = (struct nc_rpc_copy *)rpc;
1953
1954 data = lyd_new(NULL, ietfnc, "copy-config");
1955 node = lyd_new(data, ietfnc, "target");
1956 if (rpc_cp->url_trg) {
1957 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1958 } else {
1959 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1960 }
1961 if (!node) {
1962 lyd_free(data);
1963 return NC_MSG_ERROR;
1964 }
1965
1966 node = lyd_new(data, ietfnc, "source");
1967 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001968 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001969 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001970 } else {
1971 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1972 }
1973 } else {
1974 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1975 }
1976 if (!node) {
1977 lyd_free(data);
1978 return NC_MSG_ERROR;
1979 }
1980
1981 if (rpc_cp->wd_mode) {
1982 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001983 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001984 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001985 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001986 return NC_MSG_ERROR;
1987 }
1988 }
1989 switch (rpc_cp->wd_mode) {
1990 case NC_WD_UNKNOWN:
1991 /* cannot get here */
1992 break;
1993 case NC_WD_ALL:
1994 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1995 break;
1996 case NC_WD_ALL_TAG:
1997 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1998 break;
1999 case NC_WD_TRIM:
2000 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2001 break;
2002 case NC_WD_EXPLICIT:
2003 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2004 break;
2005 }
2006 if (!node) {
2007 lyd_free(data);
2008 return NC_MSG_ERROR;
2009 }
2010 }
2011 break;
2012
2013 case NC_RPC_DELETE:
2014 rpc_del = (struct nc_rpc_delete *)rpc;
2015
2016 data = lyd_new(NULL, ietfnc, "delete-config");
2017 node = lyd_new(data, ietfnc, "target");
2018 if (rpc_del->url) {
2019 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2020 } else {
2021 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2022 }
2023 if (!node) {
2024 lyd_free(data);
2025 return NC_MSG_ERROR;
2026 }
2027 break;
2028
2029 case NC_RPC_LOCK:
2030 rpc_lock = (struct nc_rpc_lock *)rpc;
2031
2032 data = lyd_new(NULL, ietfnc, "lock");
2033 node = lyd_new(data, ietfnc, "target");
2034 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2035 if (!node) {
2036 lyd_free(data);
2037 return NC_MSG_ERROR;
2038 }
2039 break;
2040
2041 case NC_RPC_UNLOCK:
2042 rpc_lock = (struct nc_rpc_lock *)rpc;
2043
2044 data = lyd_new(NULL, ietfnc, "unlock");
2045 node = lyd_new(data, ietfnc, "target");
2046 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2047 if (!node) {
2048 lyd_free(data);
2049 return NC_MSG_ERROR;
2050 }
2051 break;
2052
2053 case NC_RPC_GET:
2054 rpc_g = (struct nc_rpc_get *)rpc;
2055
2056 data = lyd_new(NULL, ietfnc, "get");
2057 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002058 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002059 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002060 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002061 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002062 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002063 lyd_insert_attr(node, NULL, "type", "xpath");
2064 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002065 }
2066 if (!node) {
2067 lyd_free(data);
2068 return NC_MSG_ERROR;
2069 }
2070 }
2071
2072 if (rpc_g->wd_mode) {
2073 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002074 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002075 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002076 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002077 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002078 return NC_MSG_ERROR;
2079 }
2080 }
2081 switch (rpc_g->wd_mode) {
2082 case NC_WD_UNKNOWN:
2083 /* cannot get here */
2084 break;
2085 case NC_WD_ALL:
2086 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2087 break;
2088 case NC_WD_ALL_TAG:
2089 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2090 break;
2091 case NC_WD_TRIM:
2092 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2093 break;
2094 case NC_WD_EXPLICIT:
2095 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2096 break;
2097 }
2098 if (!node) {
2099 lyd_free(data);
2100 return NC_MSG_ERROR;
2101 }
2102 }
2103 break;
2104
2105 case NC_RPC_KILL:
2106 rpc_k = (struct nc_rpc_kill *)rpc;
2107
2108 data = lyd_new(NULL, ietfnc, "kill-session");
2109 sprintf(str, "%u", rpc_k->sid);
2110 lyd_new_leaf(data, ietfnc, "session-id", str);
2111 break;
2112
2113 case NC_RPC_COMMIT:
2114 rpc_com = (struct nc_rpc_commit *)rpc;
2115
2116 data = lyd_new(NULL, ietfnc, "commit");
2117 if (rpc_com->confirmed) {
2118 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2119 }
2120
2121 if (rpc_com->confirm_timeout) {
2122 sprintf(str, "%u", rpc_com->confirm_timeout);
2123 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2124 }
2125
2126 if (rpc_com->persist) {
2127 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2128 if (!node) {
2129 lyd_free(data);
2130 return NC_MSG_ERROR;
2131 }
2132 }
2133
2134 if (rpc_com->persist_id) {
2135 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2136 if (!node) {
2137 lyd_free(data);
2138 return NC_MSG_ERROR;
2139 }
2140 }
2141 break;
2142
2143 case NC_RPC_DISCARD:
2144 data = lyd_new(NULL, ietfnc, "discard-changes");
2145 break;
2146
2147 case NC_RPC_CANCEL:
2148 rpc_can = (struct nc_rpc_cancel *)rpc;
2149
2150 data = lyd_new(NULL, ietfnc, "cancel-commit");
2151 if (rpc_can->persist_id) {
2152 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2153 if (!node) {
2154 lyd_free(data);
2155 return NC_MSG_ERROR;
2156 }
2157 }
2158 break;
2159
2160 case NC_RPC_VALIDATE:
2161 rpc_val = (struct nc_rpc_validate *)rpc;
2162
2163 data = lyd_new(NULL, ietfnc, "validate");
2164 node = lyd_new(data, ietfnc, "source");
2165 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002166 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002167 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002168 } else {
2169 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2170 }
2171 } else {
2172 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2173 }
2174 if (!node) {
2175 lyd_free(data);
2176 return NC_MSG_ERROR;
2177 }
2178 break;
2179
2180 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002181 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002182 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002183 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002184 return NC_MSG_ERROR;
2185 }
2186
2187 rpc_gs = (struct nc_rpc_getschema *)rpc;
2188
2189 data = lyd_new(NULL, ietfncmon, "get-schema");
2190 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2191 if (!node) {
2192 lyd_free(data);
2193 return NC_MSG_ERROR;
2194 }
2195 if (rpc_gs->version) {
2196 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2197 if (!node) {
2198 lyd_free(data);
2199 return NC_MSG_ERROR;
2200 }
2201 }
2202 if (rpc_gs->format) {
2203 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2204 if (!node) {
2205 lyd_free(data);
2206 return NC_MSG_ERROR;
2207 }
2208 }
2209 break;
2210
2211 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002212 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002213 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002214 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002215 return NC_MSG_ERROR;
2216 }
2217
2218 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2219
2220 data = lyd_new(NULL, notifs, "create-subscription");
2221 if (rpc_sub->stream) {
2222 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2223 if (!node) {
2224 lyd_free(data);
2225 return NC_MSG_ERROR;
2226 }
2227 }
2228
2229 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002230 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002231 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002232 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002233 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002234 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002235 lyd_insert_attr(node, NULL, "type", "xpath");
2236 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002237 }
2238 if (!node) {
2239 lyd_free(data);
2240 return NC_MSG_ERROR;
2241 }
2242 }
2243
2244 if (rpc_sub->start) {
2245 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2246 if (!node) {
2247 lyd_free(data);
2248 return NC_MSG_ERROR;
2249 }
2250 }
2251
2252 if (rpc_sub->stop) {
2253 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2254 if (!node) {
2255 lyd_free(data);
2256 return NC_MSG_ERROR;
2257 }
2258 }
2259 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002260 default:
2261 ERRINT;
2262 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002263 }
2264
Michal Vasko41adf392017-01-17 10:39:04 +01002265 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2266 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002267 if (dofree) {
2268 lyd_free(data);
2269 }
Michal Vasko086311b2016-01-08 09:53:11 +01002270 return NC_MSG_ERROR;
2271 }
2272
Michal Vaskoade892d2017-02-22 13:40:35 +01002273 ret = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002274 if (ret == -1) {
2275 /* error */
2276 r = NC_MSG_ERROR;
2277 } else if (!ret) {
2278 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01002279 r = NC_MSG_WOULDBLOCK;
2280 } else {
2281 /* send RPC, store its message ID */
2282 r = nc_send_msg(session, data);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002283 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002284 }
Michal Vaskoade892d2017-02-22 13:40:35 +01002285 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01002286
Radek Krejcib4b19062018-02-07 16:33:06 +01002287 if (dofree) {
2288 lyd_free(data);
2289 }
Michal Vasko086311b2016-01-08 09:53:11 +01002290
2291 if (r != NC_MSG_RPC) {
2292 return r;
2293 }
2294
2295 *msgid = cur_msgid;
2296 return NC_MSG_RPC;
2297}
Michal Vaskode2946c2017-01-12 12:19:26 +01002298
2299API void
2300nc_client_session_set_not_strict(struct nc_session *session)
2301{
2302 if (session->side != NC_CLIENT) {
2303 ERRARG("session");
2304 return;
2305 }
2306
2307 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2308}