blob: 1284b4c33d4b936eb795cacbc7e73164a09a160a [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file parser_xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief XML data parser for libyang
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
8 * 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
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Michal Vasko69730152020-10-09 16:30:07 +020015#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <stdint.h>
17#include <stdlib.h>
18#include <string.h>
19
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020022#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020024#include "parser_data.h"
25#include "parser_internal.h"
Radek Krejcif16e2542021-02-17 15:39:23 +010026#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "set.h"
Radek Krejci47fab892020-11-05 17:02:41 +010028#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "tree_data_internal.h"
30#include "tree_schema.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010031#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033
34/**
Michal Vaskob36053d2020-03-26 15:49:30 +010035 * @brief Internal context for XML YANG data parser.
Radek Krejci1798aae2020-07-14 13:26:06 +020036 *
37 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
Radek Krejcie7b95092019-05-15 11:03:07 +020038 */
39struct lyd_xml_ctx {
Radek Krejcif16e2542021-02-17 15:39:23 +010040 const struct lysc_ext_instance *ext; /**< extension instance possibly changing document root context of the data being parsed */
Michal Vaskoe0665742021-02-11 11:08:44 +010041 uint32_t parse_opts; /**< various @ref dataparseroptions. */
42 uint32_t val_opts; /**< various @ref datavalidationoptions. */
Radek Krejci1798aae2020-07-14 13:26:06 +020043 uint32_t int_opts; /**< internal data parser options */
44 uint32_t path_len; /**< used bytes in the path buffer */
45 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vasko49c39d82020-11-06 17:20:27 +010046 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
47 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
48 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci1798aae2020-07-14 13:26:06 +020049 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
Radek Krejcie7b95092019-05-15 11:03:07 +020050
Radek Krejci1798aae2020-07-14 13:26:06 +020051 /* callbacks */
52 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020053
54 struct lyxml_ctx *xmlctx; /**< XML context */
Radek Krejcie7b95092019-05-15 11:03:07 +020055};
56
Radek Krejci1798aae2020-07-14 13:26:06 +020057void
58lyd_xml_ctx_free(struct lyd_ctx *lydctx)
59{
60 struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
61
62 lyd_ctx_free(lydctx);
63 lyxml_ctx_free(ctx->xmlctx);
64 free(ctx);
65}
66
Radek Krejcie7b95092019-05-15 11:03:07 +020067static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020068lydxml_metadata(struct lyd_xml_ctx *lydctx, struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020069{
Michal Vaskob36053d2020-03-26 15:49:30 +010070 LY_ERR ret = LY_EVALID;
Radek Krejci28681fa2019-09-06 13:08:45 +020071 const struct lyxml_ns *ns;
72 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010073 const char *name;
74 size_t name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +020075 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Radek Krejci28681fa2019-09-06 13:08:45 +020076
Michal Vaskob36053d2020-03-26 15:49:30 +010077 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +020078
Michal Vaskob36053d2020-03-26 15:49:30 +010079 while (xmlctx->status == LYXML_ATTRIBUTE) {
80 if (!xmlctx->prefix_len) {
Radek Krejci28681fa2019-09-06 13:08:45 +020081 /* in XML, all attributes must be prefixed
82 * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */
Michal Vaskoe0665742021-02-11 11:08:44 +010083 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010084 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Michal Vasko69730152020-10-09 16:30:07 +020085 xmlctx->name_len, xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +010086 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +020087 }
Michal Vaskob36053d2020-03-26 15:49:30 +010088
Radek Krejci28681fa2019-09-06 13:08:45 +020089skip_attr:
Michal Vaskob36053d2020-03-26 15:49:30 +010090 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
91 assert(xmlctx->status == LYXML_ATTR_CONTENT);
92 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +020093 continue;
94 }
95
96 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +020097 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +020098 if (!ns) {
Michal Vasko52927e22020-03-16 17:26:14 +010099 /* unknown namespace, XML error */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100100 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", xmlctx->prefix_len, xmlctx->prefix);
Radek Krejci28681fa2019-09-06 13:08:45 +0200101 goto cleanup;
102 }
Michal Vasko52927e22020-03-16 17:26:14 +0100103 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200104 if (!mod) {
105 /* module is not implemented or not present in the schema */
Michal Vaskoe0665742021-02-11 11:08:44 +0100106 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100107 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200108 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
109 ns->uri, xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", xmlctx->name_len,
110 xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100111 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200112 }
113 goto skip_attr;
114 }
115
Michal Vasko60ea6352020-06-29 13:39:39 +0200116 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100117 name = xmlctx->name;
118 name_len = xmlctx->name_len;
119 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
120 assert(xmlctx->status == LYXML_ATTR_CONTENT);
121
122 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200123 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200124 xmlctx->value_len, &xmlctx->dynamic, LY_PREF_XML, &xmlctx->ns, LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200125 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100126
127 /* next attribute */
128 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200129 }
Michal Vasko52927e22020-03-16 17:26:14 +0100130
Radek Krejci28681fa2019-09-06 13:08:45 +0200131 ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200132
133cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100134 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200135 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100136 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200137 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200138 return ret;
139}
140
Michal Vasko52927e22020-03-16 17:26:14 +0100141static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200142lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100143{
144 LY_ERR ret = LY_SUCCESS;
145 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100146 void *val_prefix_data;
147 LY_PREFIX_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200148 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100149 const char *name, *prefix;
150 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100151
152 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100153 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100154
Michal Vaskob36053d2020-03-26 15:49:30 +0100155 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100156 ns = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +0100157 if (xmlctx->prefix_len) {
Michal Vasko52927e22020-03-16 17:26:14 +0100158 /* get namespace of the attribute */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200159 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100160 if (!ns) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100161 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko52927e22020-03-16 17:26:14 +0100162 ret = LY_EVALID;
163 goto cleanup;
164 }
165 }
166
167 if (*attr) {
168 attr2 = *attr;
169 } else {
170 attr2 = NULL;
171 }
172
Michal Vaskob36053d2020-03-26 15:49:30 +0100173 /* remember attr prefix, name, and get its content */
174 prefix = xmlctx->prefix;
175 prefix_len = xmlctx->prefix_len;
176 name = xmlctx->name;
177 name_len = xmlctx->name_len;
178 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
179 assert(xmlctx->status == LYXML_ATTR_CONTENT);
180
Michal Vasko52927e22020-03-16 17:26:14 +0100181 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100182 val_prefix_data = NULL;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100183 LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_PREF_XML,
184 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100185
186 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100187 ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
188 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100189 LY_CHECK_GOTO(ret, cleanup);
190
191 if (!*attr) {
192 *attr = attr2;
193 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100194
195 /* next attribute */
196 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100197 }
198
199cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100200 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200201 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100202 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100203 }
Michal Vasko52927e22020-03-16 17:26:14 +0100204 return ret;
205}
206
Michal Vasko44685da2020-03-17 15:38:06 +0100207static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100208lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100209{
Michal Vaskob36053d2020-03-26 15:49:30 +0100210 LY_ERR ret = LY_SUCCESS, r;
211 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100212 struct ly_set key_set = {0};
213 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100214 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100215
216 assert(list && (list->nodetype == LYS_LIST));
217
218 /* get all keys into a set (keys do not have if-features or anything) */
219 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100220 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200221 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200222 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100223 }
224
Michal Vasko12d809c2021-03-03 16:34:32 +0100225 /* remember parent count */
226 parents_count = xmlctx->elements.count;
227
Michal Vaskob36053d2020-03-26 15:49:30 +0100228 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100229 /* find key definition */
230 for (i = 0; i < key_set.count; ++i) {
231 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100232 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100233 break;
234 }
235 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100236 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100237
238 /* skip attributes */
239 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100240 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
241 assert(xmlctx->status == LYXML_ATTR_CONTENT);
242 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100243 }
244
Michal Vaskob36053d2020-03-26 15:49:30 +0100245 assert(xmlctx->status == LYXML_ELEM_CONTENT);
246 if (i < key_set.count) {
247 /* validate the value */
Michal Vasko713148b2020-11-12 13:10:13 +0100248 r = _lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_PREF_XML, &xmlctx->ns);
Michal Vaskob36053d2020-03-26 15:49:30 +0100249 if (!r) {
250 /* key with a valid value, remove from the set */
251 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100252 }
253 }
254
Michal Vaskob36053d2020-03-26 15:49:30 +0100255 /* parser next */
256 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100257
Michal Vaskob36053d2020-03-26 15:49:30 +0100258 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100259 while (xmlctx->status == LYXML_ELEMENT) {
260 while (parents_count < xmlctx->elements.count) {
261 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
262 }
263 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100264 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
265 }
266
267 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
268 assert(xmlctx->status == LYXML_ELEM_CLOSE);
269 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
270 if (next != LYXML_ELEM_CLOSE) {
271 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
272 }
Michal Vasko44685da2020-03-17 15:38:06 +0100273 }
274
275 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100276 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100277 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100278 }
279
280cleanup:
281 ly_set_erase(&key_set, NULL);
282 return ret;
283}
284
Michal Vasko1bf09392020-03-27 12:38:10 +0100285static LY_ERR
286lydxml_data_skip(struct lyxml_ctx *xmlctx)
287{
288 uint32_t parents_count;
289
290 /* remember current number of parents */
291 parents_count = xmlctx->elements.count;
292
293 /* skip after the content */
294 while (xmlctx->status != LYXML_ELEM_CONTENT) {
295 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
296 }
297 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
298
299 /* skip all children elements, recursively, if any */
300 while (parents_count < xmlctx->elements.count) {
301 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
302 }
303
304 /* close element */
305 assert(xmlctx->status == LYXML_ELEM_CLOSE);
306 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
307
308 return LY_SUCCESS;
309}
310
311static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200312lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100313{
314 LY_ERR ret = LY_SUCCESS;
315 enum LYXML_PARSER_STATUS prev_status;
Michal Vasko63f3d842020-07-08 10:10:14 +0200316 const char *prev_current, *pname, *pprefix;
Michal Vasko1bf09392020-03-27 12:38:10 +0100317 size_t pprefix_len, pname_len;
318 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
319
Michal Vaskoe0665742021-02-11 11:08:44 +0100320 if ((lydctx->parse_opts & LYD_PARSE_OPAQ) && ((*snode)->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100321 /* backup parser */
322 prev_status = xmlctx->status;
323 pprefix = xmlctx->prefix;
324 pprefix_len = xmlctx->prefix_len;
325 pname = xmlctx->name;
326 pname_len = xmlctx->name_len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200327 prev_current = xmlctx->in->current;
Michal Vasko1bf09392020-03-27 12:38:10 +0100328 if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) {
329 /* it was backed up, do not free */
330 xmlctx->dynamic = 0;
331 }
332
333 /* skip attributes */
334 while (xmlctx->status == LYXML_ATTRIBUTE) {
335 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
336 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
337 }
338
339 if ((*snode)->nodetype & LYD_NODE_TERM) {
340 /* value may not be valid in which case we parse it as an opaque node */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200341 if (_lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_PREF_XML, &xmlctx->ns)) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100342 *snode = NULL;
343 }
344 } else {
345 /* skip content */
346 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
347
348 if (lydxml_check_list(xmlctx, *snode)) {
349 /* invalid list, parse as opaque if it missing/has invalid some keys */
350 *snode = NULL;
351 }
352 }
353
354restore:
355 /* restore parser */
356 if (xmlctx->dynamic) {
357 free((char *)xmlctx->value);
358 }
359 xmlctx->status = prev_status;
360 xmlctx->prefix = pprefix;
361 xmlctx->prefix_len = pprefix_len;
362 xmlctx->name = pname;
363 xmlctx->name_len = pname_len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200364 xmlctx->in->current = prev_current;
Michal Vasko1bf09392020-03-27 12:38:10 +0100365 }
366
367 return ret;
368}
369
Radek Krejcie7b95092019-05-15 11:03:07 +0200370/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200371 * @brief Parse XML subtree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200372 *
Michal Vaskob36053d2020-03-26 15:49:30 +0100373 * @param[in] lydctx XML YANG data parser context.
Michal Vaskoe0665742021-02-11 11:08:44 +0100374 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
375 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. In case there were already some siblings,
376 * this may point to a previously existing node.
377 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Michal Vasko9b368d32020-02-14 13:53:31 +0100378 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200379 */
380static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100381lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
Radek Krejcie7b95092019-05-15 11:03:07 +0200382{
Michal Vaskob36053d2020-03-26 15:49:30 +0100383 LY_ERR ret = LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +0100384 const char *prefix, *name;
385 size_t prefix_len, name_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100386 struct lyxml_ctx *xmlctx;
387 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200388 const struct lyxml_ns *ns;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200389 struct lyd_meta *meta = NULL;
390 struct lyd_attr *attr = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200391 const struct lysc_node *snode;
392 struct lys_module *mod;
Michal Vasko1bf09392020-03-27 12:38:10 +0100393 uint32_t prev_opts;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200394 struct lyd_node *node = NULL, *anchor;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100395 void *val_prefix_data = NULL;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100396 LY_PREFIX_FORMAT format;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200397 uint32_t getnext_opts;
Radek Krejcie7b95092019-05-15 11:03:07 +0200398
Michal Vaskoe0665742021-02-11 11:08:44 +0100399 assert(parent || first_p);
400
Michal Vaskob36053d2020-03-26 15:49:30 +0100401 xmlctx = lydctx->xmlctx;
402 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100403 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100404
Michal Vaskoa5da3292020-08-12 13:10:50 +0200405 assert(xmlctx->status == LYXML_ELEMENT);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200406
Michal Vaskoa5da3292020-08-12 13:10:50 +0200407 /* remember element prefix and name */
408 prefix = xmlctx->prefix;
409 prefix_len = xmlctx->prefix_len;
410 name = xmlctx->name;
411 name_len = xmlctx->name_len;
412
413 /* get the element module */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200414 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200415 if (!ns) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100416 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200417 ret = LY_EVALID;
418 goto error;
419 }
420 mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri);
421 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100422 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100423 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko90932a92020-02-12 14:33:03 +0100424 ret = LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200425 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200426 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100427 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200428 /* skip element with children */
429 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
430 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200431 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200432 }
433
Michal Vaskoa5da3292020-08-12 13:10:50 +0200434 /* parser next */
435 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
Michal Vasko90932a92020-02-12 14:33:03 +0100436
Michal Vaskoa5da3292020-08-12 13:10:50 +0200437 /* get the schema node */
438 snode = NULL;
439 if (mod && (!parent || parent->schema)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100440 if (!parent && lydctx->ext) {
441 snode = lys_find_ext_instance_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
442 } else {
443 snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
444 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200445 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100446 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
447 if (parent) {
448 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.", name_len, name,
449 parent->schema->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100450 } else if (lydctx->ext) {
451 if (lydctx->ext->argument) {
452 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.", name_len, name,
453 lydctx->ext->argument, lydctx->ext->def->name);
454 } else {
455 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.", name_len, name,
456 lydctx->ext->def->name);
457 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100458 } else {
459 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", name_len, name,
460 mod->name);
461 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200462 ret = LY_EVALID;
463 goto error;
Michal Vaskoe0665742021-02-11 11:08:44 +0100464 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200465 /* skip element with children */
466 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
467 return LY_SUCCESS;
468 }
469 } else {
470 /* check that schema node is valid and can be used */
471 LY_CHECK_GOTO(ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, snode), error);
472 LY_CHECK_GOTO(ret = lydxml_data_check_opaq(lydctx, &snode), error);
473 }
474 }
475
476 /* create metadata/attributes */
477 if (xmlctx->status == LYXML_ATTRIBUTE) {
478 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200479 ret = lydxml_metadata(lydctx, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200480 LY_CHECK_GOTO(ret, error);
481 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100482 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200483 ret = lydxml_attrs(xmlctx, &attr);
484 LY_CHECK_GOTO(ret, error);
485 }
486 }
487
488 assert(xmlctx->status == LYXML_ELEM_CONTENT);
489 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100490 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200491
492 if (xmlctx->ws_only) {
493 /* ignore WS-only value */
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100494 if (xmlctx->dynamic) {
495 free((char *) xmlctx->value);
496 }
497 xmlctx->dynamic = 0;
498 xmlctx->value = "";
Michal Vaskoa5da3292020-08-12 13:10:50 +0200499 xmlctx->value_len = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100500 format = LY_PREF_XML;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200501 } else {
502 /* get value prefixes */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100503 ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_PREF_XML,
504 &xmlctx->ns, &format, &val_prefix_data);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200505 LY_CHECK_GOTO(ret, error);
506 }
507
508 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100509 ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
510 xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, LYD_HINT_DATA, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200511 LY_CHECK_GOTO(ret, error);
512
513 /* parser next */
514 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
515
516 /* process children */
517 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100518 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200519 LY_CHECK_GOTO(ret, error);
520 }
521 } else if (snode->nodetype & LYD_NODE_TERM) {
522 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200523 LY_CHECK_GOTO(ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len,
Michal Vasko69730152020-10-09 16:30:07 +0200524 &xmlctx->dynamic, LY_PREF_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error);
Radek Krejciddace2c2021-01-08 11:30:56 +0100525 LOG_LOCSET(snode, node, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200526
527 if (parent && (node->schema->flags & LYS_KEY)) {
528 /* check the key order, the anchor must never be a key */
Michal Vaskoe0665742021-02-11 11:08:44 +0100529 anchor = lyd_insert_get_next_anchor(lyd_child(parent), node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200530 if (anchor && (anchor->schema->flags & LYS_KEY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100531 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100532 LOGVAL(ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", node->schema->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200533 ret = LY_EVALID;
534 goto error;
535 } else {
536 LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name);
537 }
538 }
539 }
540
541 /* parser next */
542 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
543
544 /* no children expected */
545 if (xmlctx->status == LYXML_ELEMENT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100546 LOGVAL(ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
Michal Vasko69730152020-10-09 16:30:07 +0200547 xmlctx->name_len, xmlctx->name, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200548 ret = LY_EVALID;
549 goto error;
550 }
551 } else if (snode->nodetype & LYD_NODE_INNER) {
552 if (!xmlctx->ws_only) {
553 /* value in inner node */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100554 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
Michal Vasko69730152020-10-09 16:30:07 +0200555 xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200556 ret = LY_EVALID;
557 goto error;
558 }
559
560 /* create node */
561 ret = lyd_create_inner(snode, &node);
562 LY_CHECK_GOTO(ret, error);
563
Radek Krejciddace2c2021-01-08 11:30:56 +0100564 LOG_LOCSET(snode, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100565
Michal Vaskoa5da3292020-08-12 13:10:50 +0200566 /* parser next */
567 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
568
569 /* process children */
570 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100571 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200572 LY_CHECK_GOTO(ret, error);
573 }
574
575 if (snode->nodetype == LYS_LIST) {
576 /* check all keys exist */
577 LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error);
578 }
579
Michal Vaskoe0665742021-02-11 11:08:44 +0100580 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200581 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100582 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200583 LY_CHECK_GOTO(ret, error);
584
585 /* add any missing default children */
Michal Vaskoe0665742021-02-11 11:08:44 +0100586 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lydctx->node_types, &lydctx->node_when,
587 (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200588 LY_CHECK_GOTO(ret, error);
589 }
590
591 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
592 /* rememeber the RPC/action/notification */
593 lydctx->op_node = node;
594 }
595 } else if (snode->nodetype & LYD_NODE_ANY) {
596 if (!xmlctx->ws_only) {
597 /* value in inner node */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100598 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an any node \"%s\" found.",
Michal Vasko69730152020-10-09 16:30:07 +0200599 xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200600 ret = LY_EVALID;
601 goto error;
602 }
603
604 /* parser next */
605 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
606
607 /* parse any data tree with correct options */
Michal Vaskoe0665742021-02-11 11:08:44 +0100608 prev_opts = lydctx->parse_opts;
609 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
610 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200611 anchor = NULL;
612 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100613 ret = lydxml_subtree_r(lydctx, NULL, &anchor, NULL);
614 LY_CHECK_ERR_GOTO(ret, lydctx->parse_opts = prev_opts, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200615 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100616 lydctx->parse_opts = prev_opts;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200617
618 /* create node */
Michal Vasko366a4a12020-12-04 16:23:57 +0100619 ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200620 LY_CHECK_GOTO(ret, error);
621 }
622 assert(node);
623
624 /* add/correct flags */
625 if (snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100626 lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_opts);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200627 }
628
629 /* parser next */
630 assert(xmlctx->status == LYXML_ELEM_CLOSE);
631 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
632
633 /* add metadata/attributes */
634 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +0100635 lyd_insert_meta(node, meta, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200636 } else {
637 lyd_insert_attr(node, attr);
638 }
639
640 /* insert, keep first pointer correct */
Michal Vaskoe0665742021-02-11 11:08:44 +0100641 lyd_insert_node(parent, first_p, node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200642 while (!parent && (*first_p)->prev->next) {
643 *first_p = (*first_p)->prev;
644 }
645
Michal Vaskoe0665742021-02-11 11:08:44 +0100646 /* rememeber a successfully parsed node */
647 if (parsed) {
648 ly_set_add(parsed, node, 1, NULL);
649 }
650
Radek Krejciddace2c2021-01-08 11:30:56 +0100651 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200652 return LY_SUCCESS;
653
654error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100655 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200656 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200657 lyd_free_attr_siblings(ctx, attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200658 lyd_free_tree(node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200659 return ret;
660}
661
Michal Vaskoe0665742021-02-11 11:08:44 +0100662/**
663 * @brief Parse a specific XML element into an opaque node.
664 *
665 * @param[in] xmlctx XML parser context.
666 * @param[in] name Name of the element.
667 * @param[in] uri URI of the element.
668 * @param[in] value Whether a value is expected in the element.
669 * @param[out] evnp Parsed envelope (opaque node).
670 * @return LY_SUCCESS on success.
671 * @return LY_ENOT if the specified element did not match.
672 * @return LY_ERR value on error.
673 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100674static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100675lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, ly_bool value, struct lyd_node **envp)
Michal Vasko1bf09392020-03-27 12:38:10 +0100676{
Michal Vaskoe0665742021-02-11 11:08:44 +0100677 LY_ERR rc = LY_SUCCESS;
678 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +0200679 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100680 const char *prefix;
681 size_t prefix_len;
682
Michal Vasko1bf09392020-03-27 12:38:10 +0100683 assert(xmlctx->status == LYXML_ELEMENT);
684 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
685 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +0100686 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100687 }
688
689 prefix = xmlctx->prefix;
690 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200691 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100692 if (!ns) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100693 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100694 return LY_EVALID;
695 } else if (strcmp(ns->uri, uri)) {
696 /* different namespace */
697 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100698 }
699
700 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
701
702 /* create attributes */
703 if (xmlctx->status == LYXML_ATTRIBUTE) {
704 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
705 }
706
Michal Vaskoe0665742021-02-11 11:08:44 +0100707 assert(xmlctx->status == LYXML_ELEM_CONTENT);
708 if (!value && !xmlctx->ws_only) {
709 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
710 xmlctx->value_len, xmlctx->value, name);
711 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100712 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100713 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100714
715 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100716 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
717 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_PREF_XML, NULL, 0, envp);
718 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100719
720 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +0100721 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100722 attr = NULL;
723
Michal Vaskoe0665742021-02-11 11:08:44 +0100724 /* parser next element */
725 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100726
Michal Vaskoe0665742021-02-11 11:08:44 +0100727cleanup:
728 lyd_free_attr_siblings(xmlctx->ctx, attr);
729 if (rc) {
730 lyd_free_tree(*envp);
731 *envp = NULL;
732 }
733 return rc;
734}
735
736/**
737 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
738 *
739 * @param[in] xmlctx XML parser context.
740 * @param[out] evnp Parsed envelope(s) (opaque node).
741 * @param[out] int_opts Internal options for parsing the rest of YANG data.
742 * @param[out] close_elem Number of parsed opened elements that need to be closed.
743 * @return LY_SUCCESS on success.
744 * @return LY_ERR value on error.
745 */
746static LY_ERR
747lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
748{
749 LY_ERR rc = LY_SUCCESS, r;
750 struct lyd_node *child;
751
752 assert(envp && !*envp);
753
754 /* parse "rpc" */
755 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100756 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
757
758 /* parse "action", if any */
759 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
760 if (r == LY_SUCCESS) {
761 /* insert */
762 lyd_insert_node(*envp, NULL, child);
763
764 /* NETCONF action */
765 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
766 *close_elem = 2;
767 } else if (r == LY_ENOT) {
768 /* NETCONF RPC */
769 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
770 *close_elem = 1;
771 } else {
772 rc = r;
773 goto cleanup;
774 }
775
776cleanup:
777 if (rc) {
778 lyd_free_tree(*envp);
779 *envp = NULL;
780 }
781 return rc;
782}
783
784/**
785 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
786 *
787 * @param[in] xmlctx XML parser context.
788 * @param[out] evnp Parsed envelope(s) (opaque node).
789 * @param[out] int_opts Internal options for parsing the rest of YANG data.
790 * @param[out] close_elem Number of parsed opened elements that need to be closed.
791 * @return LY_SUCCESS on success.
792 * @return LY_ERR value on error.
793 */
794static LY_ERR
795lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
796{
797 LY_ERR rc = LY_SUCCESS, r;
798 struct lyd_node *child;
799
800 assert(envp && !*envp);
801
802 /* parse "notification" */
803 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100804 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
805
806 /* parse "eventTime" */
807 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
808 if (r == LY_ENOT) {
809 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".", xmlctx->name_len,
810 xmlctx->name);
811 r = LY_EVALID;
812 }
813 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
814
815 /* insert */
816 lyd_insert_node(*envp, NULL, child);
817
818 /* validate value */
819 /* TODO validate child->value as yang:date-and-time */
820
821 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100822 if (xmlctx->status != LYXML_ELEM_CLOSE) {
823 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +0100824 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Michal Vasko69730152020-10-09 16:30:07 +0200825 xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100826 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100827 goto cleanup;
828 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100829 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
830
831 /* NETCONF notification */
832 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
833 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100834
835cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100836 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +0100837 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100838 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100839 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100840 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100841}
Michal Vasko79135ae2020-12-16 10:08:35 +0100842
Michal Vaskoe0665742021-02-11 11:08:44 +0100843/**
844 * @brief Parse an XML element as an opaque node subtree.
845 *
846 * @param[in] xmlctx XML parser context.
847 * @param[in] parent Parent to append nodes to.
848 * @return LY_ERR value.
849 */
850static LY_ERR
851lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +0100852{
Michal Vaskoe0665742021-02-11 11:08:44 +0100853 LY_ERR rc = LY_SUCCESS;
854 const struct lyxml_ns *ns;
855 struct lyd_attr *attr = NULL;
856 struct lyd_node *child = NULL;
857 const char *name, *prefix;
858 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100859
Michal Vaskoe0665742021-02-11 11:08:44 +0100860 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100861
Michal Vaskoe0665742021-02-11 11:08:44 +0100862 name = xmlctx->name;
863 name_len = xmlctx->name_len;
864 prefix = xmlctx->prefix;
865 prefix_len = xmlctx->prefix_len;
866 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
867 if (!ns) {
868 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix);
869 return LY_EVALID;
870 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100871
Michal Vaskoe0665742021-02-11 11:08:44 +0100872 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +0100873
Michal Vaskoe0665742021-02-11 11:08:44 +0100874 /* create attributes */
875 if (xmlctx->status == LYXML_ATTRIBUTE) {
876 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
877 }
878
879 /* create node */
880 assert(xmlctx->status == LYXML_ELEM_CONTENT);
881 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
882 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_PREF_XML, NULL, 0, &child);
883 LY_CHECK_GOTO(rc, cleanup);
884
885 /* assign atributes */
886 ((struct lyd_node_opaq *)child)->attr = attr;
887 attr = NULL;
888
889 /* parser next element */
890 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
891
892 /* parse all the descendants */
893 while (xmlctx->status == LYXML_ELEMENT) {
894 rc = lydxml_opaq_r(xmlctx, child);
895 LY_CHECK_GOTO(rc, cleanup);
896 }
897
898 /* insert */
899 lyd_insert_node(parent, NULL, child);
900
901cleanup:
902 lyd_free_attr_siblings(xmlctx->ctx, attr);
903 if (rc) {
904 lyd_free_tree(child);
905 }
906 return rc;
907}
908
909/**
910 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
911 *
912 * @param[in] xmlctx XML parser context.
913 * @param[in] parent Parent to append nodes to.
914 * @return LY_ERR value.
915 */
916static LY_ERR
917lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
918{
919 LY_ERR r;
920 struct lyd_node *child, *iter;
921 const struct lyxml_ns *ns;
922 ly_bool no_dup;
923
924 /* there must be some child */
925 if (xmlctx->status == LYXML_ELEM_CLOSE) {
926 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
927 return LY_EVALID;
928 }
929
930 while (xmlctx->status == LYXML_ELEMENT) {
931 child = NULL;
932
933 /*
934 * session-id
935 */
936 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
937 if (r == LY_SUCCESS) {
938 no_dup = 1;
939 goto check_child;
940 } else if (r != LY_ENOT) {
941 goto error;
942 }
943
944 /*
945 * bad-attribute
946 */
947 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
948 if (r == LY_SUCCESS) {
949 no_dup = 1;
950 goto check_child;
951 } else if (r != LY_ENOT) {
952 goto error;
953 }
954
955 /*
956 * bad-element
957 */
958 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
959 if (r == LY_SUCCESS) {
960 no_dup = 1;
961 goto check_child;
962 } else if (r != LY_ENOT) {
963 goto error;
964 }
965
966 /*
967 * bad-namespace
968 */
969 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
970 if (r == LY_SUCCESS) {
971 no_dup = 1;
972 goto check_child;
973 } else if (r != LY_ENOT) {
974 goto error;
975 }
976
977 if (r == LY_ENOT) {
978 assert(xmlctx->status == LYXML_ELEMENT);
979
980 /* learn namespace */
981 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
982 if (!ns) {
983 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", xmlctx->prefix_len, xmlctx->prefix);
984 r = LY_EVALID;
985 goto error;
986 } else if (!strcmp(ns->uri, "urn:ietf:params:xml:ns:netconf:base:1.0")) {
987 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci53875452021-02-16 13:44:51 +0100988 xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100989 r = LY_EVALID;
990 goto error;
991 }
992
993 /* custom elements */
994 r = lydxml_opaq_r(xmlctx, parent);
995 LY_CHECK_GOTO(r, error);
996
997 no_dup = 0;
998 }
999
1000check_child:
1001 /* check for duplicates */
1002 if (no_dup) {
1003 LY_LIST_FOR(lyd_child(parent), iter) {
1004 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1005 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1006 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1007 ((struct lyd_node_opaq *)child)->name.name);
1008 r = LY_EVALID;
1009 goto error;
1010 }
1011 }
1012 }
1013
1014 /* finish child parsing */
1015 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1016 assert(xmlctx->status == LYXML_ELEMENT);
1017 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
1018 xmlctx->name_len, xmlctx->name);
1019 r = LY_EVALID;
1020 goto error;
1021 }
1022 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1023
1024 /* insert */
1025 lyd_insert_node(parent, NULL, child);
1026 }
1027
1028 return LY_SUCCESS;
1029
1030error:
1031 lyd_free_tree(child);
1032 return r;
1033}
1034
1035/**
1036 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1037 *
1038 * @param[in] xmlctx XML parser context.
1039 * @param[in] parent Parent to append nodes to.
1040 * @return LY_ERR value.
1041 */
1042static LY_ERR
1043lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1044{
1045 LY_ERR r;
1046 struct lyd_node *child, *iter;
1047 const char *val;
1048 ly_bool no_dup;
1049
1050 /* there must be some child */
1051 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1052 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1053 return LY_EVALID;
1054 }
1055
1056 while (xmlctx->status == LYXML_ELEMENT) {
1057 child = NULL;
1058
1059 /*
1060 * error-type
1061 */
1062 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1063 if (r == LY_SUCCESS) {
1064 val = ((struct lyd_node_opaq *)child)->value;
1065 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1066 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1067 ((struct lyd_node_opaq *)child)->name.name);
1068 r = LY_EVALID;
1069 goto error;
1070 }
1071
1072 no_dup = 1;
1073 goto check_child;
1074 } else if (r != LY_ENOT) {
1075 goto error;
1076 }
1077
1078 /*
1079 * error-tag
1080 */
1081 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1082 if (r == LY_SUCCESS) {
1083 val = ((struct lyd_node_opaq *)child)->value;
1084 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1085 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1086 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1087 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1088 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1089 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1090 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1091 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1092 ((struct lyd_node_opaq *)child)->name.name);
1093 r = LY_EVALID;
1094 goto error;
1095 }
1096
1097 no_dup = 1;
1098 goto check_child;
1099 } else if (r != LY_ENOT) {
1100 goto error;
1101 }
1102
1103 /*
1104 * error-severity
1105 */
1106 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1107 if (r == LY_SUCCESS) {
1108 val = ((struct lyd_node_opaq *)child)->value;
1109 if (strcmp(val, "error") && strcmp(val, "warning")) {
1110 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1111 ((struct lyd_node_opaq *)child)->name.name);
1112 r = LY_EVALID;
1113 goto error;
1114 }
1115
1116 no_dup = 1;
1117 goto check_child;
1118 } else if (r != LY_ENOT) {
1119 goto error;
1120 }
1121
1122 /*
1123 * error-app-tag
1124 */
1125 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1126 if (r == LY_SUCCESS) {
1127 no_dup = 1;
1128 goto check_child;
1129 } else if (r != LY_ENOT) {
1130 goto error;
1131 }
1132
1133 /*
1134 * error-path
1135 */
1136 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1137 if (r == LY_SUCCESS) {
1138 no_dup = 1;
1139 goto check_child;
1140 } else if (r != LY_ENOT) {
1141 goto error;
1142 }
1143
1144 /*
1145 * error-message
1146 */
1147 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1148 if (r == LY_SUCCESS) {
1149 no_dup = 1;
1150 goto check_child;
1151 } else if (r != LY_ENOT) {
1152 goto error;
1153 }
1154
1155 /* error-info */
1156 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1157 if (r == LY_SUCCESS) {
1158 /* parse all the descendants */
1159 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1160
1161 no_dup = 0;
1162 goto check_child;
1163 } else if (r != LY_ENOT) {
1164 goto error;
1165 }
1166
1167 if (r == LY_ENOT) {
1168 assert(xmlctx->status == LYXML_ELEMENT);
1169 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
1170 xmlctx->name_len, xmlctx->name);
1171 r = LY_EVALID;
1172 goto error;
1173 }
1174
1175check_child:
1176 /* check for duplicates */
1177 if (no_dup) {
1178 LY_LIST_FOR(lyd_child(parent), iter) {
1179 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1180 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1181 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1182 ((struct lyd_node_opaq *)child)->name.name);
1183 r = LY_EVALID;
1184 goto error;
1185 }
1186 }
1187 }
1188
1189 /* finish child parsing */
1190 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1191 assert(xmlctx->status == LYXML_ELEMENT);
1192 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
1193 xmlctx->name_len, xmlctx->name);
1194 r = LY_EVALID;
1195 goto error;
1196 }
1197 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1198
1199 /* insert */
1200 lyd_insert_node(parent, NULL, child);
1201 }
1202
1203 return LY_SUCCESS;
1204
1205error:
1206 lyd_free_tree(child);
1207 return r;
1208}
1209
1210/**
1211 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1212 *
1213 * @param[in] xmlctx XML parser context.
1214 * @param[out] evnp Parsed envelope(s) (opaque node).
1215 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1216 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1217 * @return LY_SUCCESS on success.
1218 * @return LY_ERR value on error.
1219 */
1220static LY_ERR
1221lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1222{
1223 LY_ERR rc = LY_SUCCESS, r;
1224 struct lyd_node *child = NULL;
1225 const char *parsed_elem = NULL;
1226
1227 assert(envp && !*envp);
1228
1229 /* parse "rpc-reply" */
1230 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001231 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1232
1233 /* there must be some child */
1234 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1235 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1236 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001237 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001238 }
1239
Michal Vaskoe0665742021-02-11 11:08:44 +01001240 /* try to parse "ok" */
1241 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1242 if (r == LY_SUCCESS) {
1243 /* insert */
1244 lyd_insert_node(*envp, NULL, child);
1245
1246 /* finish child parsing */
1247 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1248 assert(xmlctx->status == LYXML_ELEMENT);
1249 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
1250 xmlctx->name_len, xmlctx->name);
1251 rc = LY_EVALID;
1252 goto cleanup;
1253 }
1254 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1255
1256 /* success */
1257 parsed_elem = "ok";
1258 goto finish;
1259 } else if (r != LY_ENOT) {
1260 rc = r;
1261 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001262 }
1263
Michal Vaskoe0665742021-02-11 11:08:44 +01001264 /* try to parse all "rpc-error" elements */
1265 while (xmlctx->status == LYXML_ELEMENT) {
1266 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1267 if (r == LY_ENOT) {
1268 break;
1269 } else if (r) {
1270 rc = r;
1271 goto cleanup;
1272 }
1273
1274 /* insert */
1275 lyd_insert_node(*envp, NULL, child);
1276
1277 /* parse all children of "rpc-error" */
1278 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1279
1280 /* finish child parsing */
1281 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1282 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1283
1284 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001285 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001286
1287finish:
1288 if (parsed_elem) {
1289 /* NETCONF rpc-reply with no data */
1290 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1291 assert(xmlctx->status == LYXML_ELEMENT);
1292 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
1293 xmlctx->name_len, xmlctx->name, parsed_elem);
1294 rc = LY_EVALID;
1295 goto cleanup;
1296 }
1297 }
1298
1299 /* NETCONF rpc-reply */
1300 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_REPLY;
1301 *close_elem = 1;
1302
1303cleanup:
1304 if (rc) {
1305 lyd_free_tree(*envp);
1306 *envp = NULL;
1307 }
1308 return rc;
1309}
1310
Michal Vasko2552ea32020-12-08 15:32:34 +01001311LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001312lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1313 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1314 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001315{
Michal Vaskoe0665742021-02-11 11:08:44 +01001316 LY_ERR rc = LY_SUCCESS;
1317 struct lyd_xml_ctx *lydctx;
1318 uint32_t i, int_opts, close_elem = 0;
1319 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001320
Michal Vaskoe0665742021-02-11 11:08:44 +01001321 assert(ctx && in && lydctx_p);
1322 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1323 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vasko2552ea32020-12-08 15:32:34 +01001324
Michal Vaskoe0665742021-02-11 11:08:44 +01001325 /* init context */
1326 lydctx = calloc(1, sizeof *lydctx);
1327 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1328 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1329 lydctx->parse_opts = parse_opts;
1330 lydctx->val_opts = val_opts;
1331 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001332 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001333
Michal Vaskoe0665742021-02-11 11:08:44 +01001334 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001335 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001336 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1337 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001338 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001339 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1340 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001341 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001342 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1343 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001344 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001345 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1346 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001347 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001348 assert(!parent);
1349 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1350 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001351 case LYD_TYPE_NOTIF_NETCONF:
1352 assert(!parent);
1353 LY_CHECK_GOTO(rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1354 break;
1355 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001356 assert(parent);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001357 LY_CHECK_GOTO(rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001358 break;
1359 }
1360 lydctx->int_opts = int_opts;
1361
1362 /* find the operation node if it exists already */
1363 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1364
1365 /* parse XML data */
1366 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1367 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1368 parsed_data_nodes = 1;
1369
1370 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1371 break;
1372 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001373 }
1374
Michal Vaskoe0665742021-02-11 11:08:44 +01001375 /* close all opened elements */
1376 for (i = 0; i < close_elem; ++i) {
1377 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1378 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
1379 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".", lydctx->xmlctx->name_len,
1380 lydctx->xmlctx->name);
1381 rc = LY_EVALID;
1382 goto cleanup;
1383 }
1384
1385 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001386 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001387
1388 /* check final state */
1389 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1390 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1391 rc = LY_EVALID;
1392 goto cleanup;
1393 }
1394 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1395 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1396 rc = LY_EVALID;
1397 goto cleanup;
1398 }
1399
1400 if (!parsed_data_nodes) {
1401 /* no data nodes were parsed */
1402 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001403 }
1404
1405cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001406 /* there should be no unres stored if validation should be skipped */
1407 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1408 !lydctx->node_when.count));
1409
1410 if (rc) {
1411 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1412 } else {
1413 *lydctx_p = (struct lyd_ctx *)lydctx;
1414
1415 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1416 lyxml_ctx_free(lydctx->xmlctx);
1417 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001418 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001419 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001420}