blob: ee45c56cb4b7d4ae70dd900f90105033e75743bb [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"
Radek Krejci77114102021-03-10 15:21:57 +010022#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
26#include "parser_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "set.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "tree_data_internal.h"
31#include "tree_schema.h"
Radek Krejci77114102021-03-10 15:21:57 +010032#include "tree_schema_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010033#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020035
36/**
Michal Vaskob36053d2020-03-26 15:49:30 +010037 * @brief Internal context for XML YANG data parser.
Radek Krejci1798aae2020-07-14 13:26:06 +020038 *
Radek Krejci4f2e3e52021-03-30 14:20:28 +020039 * Note that the structure maps to the ::lyd_ctx which is common for all the data parsers
Radek Krejcie7b95092019-05-15 11:03:07 +020040 */
41struct lyd_xml_ctx {
Radek Krejcif16e2542021-02-17 15:39:23 +010042 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 +010043 uint32_t parse_opts; /**< various @ref dataparseroptions. */
44 uint32_t val_opts; /**< various @ref datavalidationoptions. */
Radek Krejci1798aae2020-07-14 13:26:06 +020045 uint32_t int_opts; /**< internal data parser options */
46 uint32_t path_len; /**< used bytes in the path buffer */
47 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +010048 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci4f2e3e52021-03-30 14:20:28 +020049 struct ly_set node_exts; /**< set of nodes and extensions connected with a plugin providing own validation callback */
Michal Vasko49c39d82020-11-06 17:20:27 +010050 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
51 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
Radek Krejci1798aae2020-07-14 13:26:06 +020052 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 +020053
Radek Krejci1798aae2020-07-14 13:26:06 +020054 /* callbacks */
55 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020056
57 struct lyxml_ctx *xmlctx; /**< XML context */
Radek Krejcie7b95092019-05-15 11:03:07 +020058};
59
Radek Krejci1798aae2020-07-14 13:26:06 +020060void
61lyd_xml_ctx_free(struct lyd_ctx *lydctx)
62{
63 struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
64
65 lyd_ctx_free(lydctx);
66 lyxml_ctx_free(ctx->xmlctx);
67 free(ctx);
68}
69
Radek Krejcie7b95092019-05-15 11:03:07 +020070static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020071lydxml_metadata(struct lyd_xml_ctx *lydctx, struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020072{
aPiecek1c4da362021-04-29 14:26:34 +020073 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020074 const struct lyxml_ns *ns;
75 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010076 const char *name;
77 size_t name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +020078 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Radek Krejci28681fa2019-09-06 13:08:45 +020079
Michal Vaskob36053d2020-03-26 15:49:30 +010080 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +020081
Michal Vaskob36053d2020-03-26 15:49:30 +010082 while (xmlctx->status == LYXML_ATTRIBUTE) {
83 if (!xmlctx->prefix_len) {
Radek Krejci28681fa2019-09-06 13:08:45 +020084 /* in XML, all attributes must be prefixed
85 * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */
Michal Vaskoe0665742021-02-11 11:08:44 +010086 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
aPiecek1c4da362021-04-29 14:26:34 +020087 ret = LY_EVALID;
Radek Krejci2efc45b2020-12-22 16:25:44 +010088 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +010089 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +010090 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +020091 }
Michal Vaskob36053d2020-03-26 15:49:30 +010092
Radek Krejci28681fa2019-09-06 13:08:45 +020093skip_attr:
Michal Vaskob36053d2020-03-26 15:49:30 +010094 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
95 assert(xmlctx->status == LYXML_ATTR_CONTENT);
96 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +020097 continue;
98 }
99
100 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200101 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200102 if (!ns) {
aPiecek1c4da362021-04-29 14:26:34 +0200103 ret = LY_ENOTFOUND;
Michal Vasko52927e22020-03-16 17:26:14 +0100104 /* unknown namespace, XML error */
Radek Krejci422afb12021-03-04 16:38:16 +0100105 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Radek Krejci28681fa2019-09-06 13:08:45 +0200106 goto cleanup;
107 }
Michal Vasko52927e22020-03-16 17:26:14 +0100108 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200109 if (!mod) {
110 /* module is not implemented or not present in the schema */
Michal Vaskoe0665742021-02-11 11:08:44 +0100111 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
aPiecek1c4da362021-04-29 14:26:34 +0200112 ret = LY_ENOTFOUND;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100113 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200114 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100115 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
116 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100117 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200118 }
119 goto skip_attr;
120 }
121
Michal Vasko60ea6352020-06-29 13:39:39 +0200122 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100123 name = xmlctx->name;
124 name_len = xmlctx->name_len;
125 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
126 assert(xmlctx->status == LYXML_ATTR_CONTENT);
127
128 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200129 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200130 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200131 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100132
133 /* next attribute */
134 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200135 }
Michal Vasko52927e22020-03-16 17:26:14 +0100136
Radek Krejcie7b95092019-05-15 11:03:07 +0200137cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100138 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200139 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100140 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200141 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200142 return ret;
143}
144
Michal Vasko52927e22020-03-16 17:26:14 +0100145static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200146lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100147{
148 LY_ERR ret = LY_SUCCESS;
149 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100150 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200151 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200152 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100153 const char *name, *prefix;
154 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100155
156 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100157 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100158
Michal Vaskob36053d2020-03-26 15:49:30 +0100159 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100160 ns = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +0100161 if (xmlctx->prefix_len) {
Michal Vasko52927e22020-03-16 17:26:14 +0100162 /* get namespace of the attribute */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200163 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100164 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100165 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko52927e22020-03-16 17:26:14 +0100166 ret = LY_EVALID;
167 goto cleanup;
168 }
169 }
170
171 if (*attr) {
172 attr2 = *attr;
173 } else {
174 attr2 = NULL;
175 }
176
Michal Vaskob36053d2020-03-26 15:49:30 +0100177 /* remember attr prefix, name, and get its content */
178 prefix = xmlctx->prefix;
179 prefix_len = xmlctx->prefix_len;
180 name = xmlctx->name;
181 name_len = xmlctx->name_len;
182 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
183 assert(xmlctx->status == LYXML_ATTR_CONTENT);
184
Michal Vasko52927e22020-03-16 17:26:14 +0100185 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100186 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200187 LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100188 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100189
190 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100191 ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
192 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100193 LY_CHECK_GOTO(ret, cleanup);
194
195 if (!*attr) {
196 *attr = attr2;
197 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100198
199 /* next attribute */
200 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100201 }
202
203cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100204 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200205 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100206 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100207 }
Michal Vasko52927e22020-03-16 17:26:14 +0100208 return ret;
209}
210
Michal Vasko44685da2020-03-17 15:38:06 +0100211static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100212lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100213{
Michal Vaskob36053d2020-03-26 15:49:30 +0100214 LY_ERR ret = LY_SUCCESS, r;
215 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100216 struct ly_set key_set = {0};
217 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100218 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100219
220 assert(list && (list->nodetype == LYS_LIST));
221
222 /* get all keys into a set (keys do not have if-features or anything) */
223 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100224 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200225 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200226 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100227 }
228
Michal Vasko12d809c2021-03-03 16:34:32 +0100229 /* remember parent count */
230 parents_count = xmlctx->elements.count;
231
Michal Vaskob36053d2020-03-26 15:49:30 +0100232 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100233 /* find key definition */
234 for (i = 0; i < key_set.count; ++i) {
235 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100236 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100237 break;
238 }
239 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100240 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100241
242 /* skip attributes */
243 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100244 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
245 assert(xmlctx->status == LYXML_ATTR_CONTENT);
246 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100247 }
248
Michal Vaskob36053d2020-03-26 15:49:30 +0100249 assert(xmlctx->status == LYXML_ELEM_CONTENT);
250 if (i < key_set.count) {
251 /* validate the value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200252 r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns);
Michal Vaskob36053d2020-03-26 15:49:30 +0100253 if (!r) {
254 /* key with a valid value, remove from the set */
255 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100256 }
257 }
258
Michal Vaskob36053d2020-03-26 15:49:30 +0100259 /* parser next */
260 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100261
Michal Vaskob36053d2020-03-26 15:49:30 +0100262 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100263 while (xmlctx->status == LYXML_ELEMENT) {
264 while (parents_count < xmlctx->elements.count) {
265 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
266 }
267 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100268 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
269 }
270
271 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
272 assert(xmlctx->status == LYXML_ELEM_CLOSE);
273 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
274 if (next != LYXML_ELEM_CLOSE) {
275 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
276 }
Michal Vasko44685da2020-03-17 15:38:06 +0100277 }
278
279 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100280 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100281 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100282 }
283
284cleanup:
285 ly_set_erase(&key_set, NULL);
286 return ret;
287}
288
Michal Vasko5c24ed12021-06-09 09:27:32 +0200289/**
290 * @brief Skip an element with all its descendants.
291 *
292 * @param[in] xmlctx XML parser context.
293 * @return LY_ERR value.
294 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100295static LY_ERR
296lydxml_data_skip(struct lyxml_ctx *xmlctx)
297{
298 uint32_t parents_count;
299
300 /* remember current number of parents */
301 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200302 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100303
304 /* skip after the content */
305 while (xmlctx->status != LYXML_ELEM_CONTENT) {
306 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
307 }
308 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
309
310 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200311 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100312 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
313 }
314
315 /* close element */
316 assert(xmlctx->status == LYXML_ELEM_CLOSE);
317 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
318
319 return LY_SUCCESS;
320}
321
Michal Vasko5c24ed12021-06-09 09:27:32 +0200322/**
323 * @brief Check that the current element can be parsed as a data node.
324 *
325 * @param[in] lydctx XML data parser context.
326 * @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
327 * @return LY_ERR value.
328 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100329static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200330lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100331{
332 LY_ERR ret = LY_SUCCESS;
333 enum LYXML_PARSER_STATUS prev_status;
Michal Vasko63f3d842020-07-08 10:10:14 +0200334 const char *prev_current, *pname, *pprefix;
Michal Vasko1bf09392020-03-27 12:38:10 +0100335 size_t pprefix_len, pname_len;
336 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
337
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100338 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
339 /* only checks specific to opaque nodes */
340 return LY_SUCCESS;
341 }
342
343 if ((*snode)->nodetype & (LYD_NODE_TERM | LYS_LIST)) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100344 /* backup parser */
345 prev_status = xmlctx->status;
346 pprefix = xmlctx->prefix;
347 pprefix_len = xmlctx->prefix_len;
348 pname = xmlctx->name;
349 pname_len = xmlctx->name_len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200350 prev_current = xmlctx->in->current;
Michal Vasko1bf09392020-03-27 12:38:10 +0100351 if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) {
352 /* it was backed up, do not free */
353 xmlctx->dynamic = 0;
354 }
355
356 /* skip attributes */
357 while (xmlctx->status == LYXML_ATTRIBUTE) {
358 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
359 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
360 }
361
362 if ((*snode)->nodetype & LYD_NODE_TERM) {
363 /* value may not be valid in which case we parse it as an opaque node */
Radek Krejci8df109d2021-04-23 12:19:08 +0200364 if (lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns)) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100365 *snode = NULL;
366 }
367 } else {
368 /* skip content */
369 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
370
371 if (lydxml_check_list(xmlctx, *snode)) {
372 /* invalid list, parse as opaque if it missing/has invalid some keys */
373 *snode = NULL;
374 }
375 }
376
377restore:
378 /* restore parser */
379 if (xmlctx->dynamic) {
380 free((char *)xmlctx->value);
381 }
382 xmlctx->status = prev_status;
383 xmlctx->prefix = pprefix;
384 xmlctx->prefix_len = pprefix_len;
385 xmlctx->name = pname;
386 xmlctx->name_len = pname_len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200387 xmlctx->in->current = prev_current;
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100388 } else if ((*snode)->nodetype & LYD_NODE_INNER) {
Michal Vasko90fa5682021-06-03 08:32:10 +0200389 /* skip attributes */
390 while (xmlctx->status == LYXML_ATTRIBUTE) {
391 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
392 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
393 }
394
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100395 /* if there is a non-WS value, it cannot be parsed as an inner node */
396 assert(xmlctx->status == LYXML_ELEM_CONTENT);
397 if (!xmlctx->ws_only) {
398 *snode = NULL;
399 }
400
Michal Vasko1bf09392020-03-27 12:38:10 +0100401 }
402
403 return ret;
404}
405
Radek Krejcie7b95092019-05-15 11:03:07 +0200406/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200407 * @brief Parse XML subtree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200408 *
Michal Vaskob36053d2020-03-26 15:49:30 +0100409 * @param[in] lydctx XML YANG data parser context.
Michal Vaskoe0665742021-02-11 11:08:44 +0100410 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
411 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. In case there were already some siblings,
412 * this may point to a previously existing node.
413 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Michal Vasko9b368d32020-02-14 13:53:31 +0100414 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200415 */
416static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100417lydxml_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 +0200418{
Michal Vaskob36053d2020-03-26 15:49:30 +0100419 LY_ERR ret = LY_SUCCESS;
Michal Vasko27c4dce2021-03-04 15:50:50 +0100420 const char *prefix, *name, *val;
Michal Vasko1bf09392020-03-27 12:38:10 +0100421 size_t prefix_len, name_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100422 struct lyxml_ctx *xmlctx;
423 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200424 const struct lyxml_ns *ns;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200425 struct lyd_meta *meta = NULL;
426 struct lyd_attr *attr = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200427 const struct lysc_node *snode;
428 struct lys_module *mod;
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200429 uint32_t prev_parse_opts, prev_int_opts;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200430 struct lyd_node *node = NULL, *anchor;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100431 void *val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200432 LY_VALUE_FORMAT format;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200433 uint32_t getnext_opts;
Radek Krejcie7b95092019-05-15 11:03:07 +0200434
Michal Vaskoe0665742021-02-11 11:08:44 +0100435 assert(parent || first_p);
436
Michal Vaskob36053d2020-03-26 15:49:30 +0100437 xmlctx = lydctx->xmlctx;
438 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100439 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100440
Michal Vaskoa5da3292020-08-12 13:10:50 +0200441 assert(xmlctx->status == LYXML_ELEMENT);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200442
Michal Vaskoa5da3292020-08-12 13:10:50 +0200443 /* remember element prefix and name */
444 prefix = xmlctx->prefix;
445 prefix_len = xmlctx->prefix_len;
446 name = xmlctx->name;
447 name_len = xmlctx->name_len;
448
449 /* get the element module */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200450 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200451 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100452 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200453 ret = LY_EVALID;
454 goto error;
455 }
456 mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri);
457 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100458 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100459 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko90932a92020-02-12 14:33:03 +0100460 ret = LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200461 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200462 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100463 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200464 /* skip element with children */
465 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
466 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200467 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200468 }
469
Michal Vaskoa5da3292020-08-12 13:10:50 +0200470 /* parser next */
471 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
Michal Vasko90932a92020-02-12 14:33:03 +0100472
Michal Vaskoa5da3292020-08-12 13:10:50 +0200473 /* get the schema node */
474 snode = NULL;
475 if (mod && (!parent || parent->schema)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100476 if (!parent && lydctx->ext) {
Radek Krejciba05eab2021-03-10 13:19:29 +0100477 snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100478 } else {
479 snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
480 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200481 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100482 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
483 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100484 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
485 (int)name_len, name, parent->schema->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100486 } else if (lydctx->ext) {
487 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100488 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
489 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100490 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100491 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
492 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100493 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100494 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100495 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
496 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100497 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200498 ret = LY_EVALID;
499 goto error;
Michal Vaskoe0665742021-02-11 11:08:44 +0100500 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200501 /* skip element with children */
502 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
503 return LY_SUCCESS;
504 }
505 } else {
506 /* check that schema node is valid and can be used */
507 LY_CHECK_GOTO(ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, snode), error);
508 LY_CHECK_GOTO(ret = lydxml_data_check_opaq(lydctx, &snode), error);
509 }
510 }
511
512 /* create metadata/attributes */
513 if (xmlctx->status == LYXML_ATTRIBUTE) {
514 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200515 ret = lydxml_metadata(lydctx, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200516 LY_CHECK_GOTO(ret, error);
517 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100518 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200519 ret = lydxml_attrs(xmlctx, &attr);
520 LY_CHECK_GOTO(ret, error);
521 }
522 }
523
524 assert(xmlctx->status == LYXML_ELEM_CONTENT);
525 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100526 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200527
528 if (xmlctx->ws_only) {
529 /* ignore WS-only value */
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100530 if (xmlctx->dynamic) {
531 free((char *) xmlctx->value);
532 }
533 xmlctx->dynamic = 0;
534 xmlctx->value = "";
Michal Vaskoa5da3292020-08-12 13:10:50 +0200535 xmlctx->value_len = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200536 format = LY_VALUE_XML;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200537 } else {
538 /* get value prefixes */
Radek Krejci8df109d2021-04-23 12:19:08 +0200539 ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100540 &xmlctx->ns, &format, &val_prefix_data);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200541 LY_CHECK_GOTO(ret, error);
542 }
543
544 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100545 ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
546 xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, LYD_HINT_DATA, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200547 LY_CHECK_GOTO(ret, error);
548
549 /* parser next */
550 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
551
552 /* process children */
553 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100554 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200555 LY_CHECK_GOTO(ret, error);
556 }
557 } else if (snode->nodetype & LYD_NODE_TERM) {
558 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200559 LY_CHECK_GOTO(ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len,
Radek Krejci8df109d2021-04-23 12:19:08 +0200560 &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error);
Radek Krejciddace2c2021-01-08 11:30:56 +0100561 LOG_LOCSET(snode, node, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200562
563 if (parent && (node->schema->flags & LYS_KEY)) {
564 /* check the key order, the anchor must never be a key */
Michal Vaskoe0665742021-02-11 11:08:44 +0100565 anchor = lyd_insert_get_next_anchor(lyd_child(parent), node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200566 if (anchor && (anchor->schema->flags & LYS_KEY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100567 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100568 LOGVAL(ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", node->schema->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200569 ret = LY_EVALID;
570 goto error;
571 } else {
572 LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name);
573 }
574 }
575 }
576
577 /* parser next */
578 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
579
580 /* no children expected */
581 if (xmlctx->status == LYXML_ELEMENT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100582 LOGVAL(ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100583 (int)xmlctx->name_len, xmlctx->name, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200584 ret = LY_EVALID;
585 goto error;
586 }
587 } else if (snode->nodetype & LYD_NODE_INNER) {
588 if (!xmlctx->ws_only) {
589 /* value in inner node */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100590 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100591 (int)xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200592 ret = LY_EVALID;
593 goto error;
594 }
595
596 /* create node */
597 ret = lyd_create_inner(snode, &node);
598 LY_CHECK_GOTO(ret, error);
599
Radek Krejciddace2c2021-01-08 11:30:56 +0100600 LOG_LOCSET(snode, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100601
Michal Vaskoa5da3292020-08-12 13:10:50 +0200602 /* parser next */
603 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
604
605 /* process children */
606 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100607 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200608 LY_CHECK_GOTO(ret, error);
609 }
610
611 if (snode->nodetype == LYS_LIST) {
612 /* check all keys exist */
613 LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error);
614 }
615
Michal Vaskoe0665742021-02-11 11:08:44 +0100616 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200617 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100618 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200619 LY_CHECK_GOTO(ret, error);
620
621 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200622 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts,
623 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200624 LY_CHECK_GOTO(ret, error);
625 }
626
627 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
628 /* rememeber the RPC/action/notification */
629 lydctx->op_node = node;
630 }
631 } else if (snode->nodetype & LYD_NODE_ANY) {
Michal Vasko27c4dce2021-03-04 15:50:50 +0100632 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
633 /* value in anydata node, we expect a tree */
634 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100635 (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200636 ret = LY_EVALID;
637 goto error;
638 }
639
Michal Vasko27c4dce2021-03-04 15:50:50 +0100640 if (!xmlctx->ws_only) {
641 /* use an arbitrary text value for anyxml */
642 lydict_insert(xmlctx->ctx, xmlctx->value, xmlctx->value_len, &val);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200643
Michal Vasko27c4dce2021-03-04 15:50:50 +0100644 /* parser next */
645 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
646
647 /* create node */
648 ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, &node);
649 LY_CHECK_GOTO(ret, error);
650 } else {
651 /* parser next */
652 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
653
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200654 /* update options so that generic data can be parsed */
655 prev_parse_opts = lydctx->parse_opts;
Michal Vasko27c4dce2021-03-04 15:50:50 +0100656 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
657 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200658 prev_int_opts = lydctx->int_opts;
659 lydctx->int_opts |= LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF;
660
661 /* parse any data tree */
Michal Vasko27c4dce2021-03-04 15:50:50 +0100662 anchor = NULL;
663 while (xmlctx->status == LYXML_ELEMENT) {
664 ret = lydxml_subtree_r(lydctx, NULL, &anchor, NULL);
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200665 if (ret) {
666 break;
667 }
Michal Vasko27c4dce2021-03-04 15:50:50 +0100668 }
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200669
670 /* restore options */
671 lydctx->parse_opts = prev_parse_opts;
672 lydctx->int_opts = prev_int_opts;
673
674 LY_CHECK_GOTO(ret, error);
Michal Vasko27c4dce2021-03-04 15:50:50 +0100675
676 /* create node */
677 ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node);
678 LY_CHECK_GOTO(ret, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200679 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200680 }
681 assert(node);
682
683 /* add/correct flags */
684 if (snode) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200685 lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200686 }
687
688 /* parser next */
689 assert(xmlctx->status == LYXML_ELEM_CLOSE);
690 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
691
692 /* add metadata/attributes */
693 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +0100694 lyd_insert_meta(node, meta, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200695 } else {
696 lyd_insert_attr(node, attr);
697 }
698
699 /* insert, keep first pointer correct */
Michal Vaskoe0665742021-02-11 11:08:44 +0100700 lyd_insert_node(parent, first_p, node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200701 while (!parent && (*first_p)->prev->next) {
702 *first_p = (*first_p)->prev;
703 }
704
Michal Vaskoe0665742021-02-11 11:08:44 +0100705 /* rememeber a successfully parsed node */
706 if (parsed) {
707 ly_set_add(parsed, node, 1, NULL);
708 }
709
Radek Krejciddace2c2021-01-08 11:30:56 +0100710 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200711 return LY_SUCCESS;
712
713error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100714 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200715 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200716 lyd_free_attr_siblings(ctx, attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200717 lyd_free_tree(node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200718 return ret;
719}
720
Michal Vaskoe0665742021-02-11 11:08:44 +0100721/**
722 * @brief Parse a specific XML element into an opaque node.
723 *
724 * @param[in] xmlctx XML parser context.
725 * @param[in] name Name of the element.
726 * @param[in] uri URI of the element.
727 * @param[in] value Whether a value is expected in the element.
728 * @param[out] evnp Parsed envelope (opaque node).
729 * @return LY_SUCCESS on success.
730 * @return LY_ENOT if the specified element did not match.
731 * @return LY_ERR value on error.
732 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100733static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100734lydxml_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 +0100735{
Michal Vaskoe0665742021-02-11 11:08:44 +0100736 LY_ERR rc = LY_SUCCESS;
737 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +0200738 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100739 const char *prefix;
740 size_t prefix_len;
741
Michal Vasko1bf09392020-03-27 12:38:10 +0100742 assert(xmlctx->status == LYXML_ELEMENT);
743 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
744 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +0100745 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100746 }
747
748 prefix = xmlctx->prefix;
749 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200750 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100751 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100752 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100753 return LY_EVALID;
754 } else if (strcmp(ns->uri, uri)) {
755 /* different namespace */
756 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100757 }
758
759 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
760
761 /* create attributes */
762 if (xmlctx->status == LYXML_ATTRIBUTE) {
763 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
764 }
765
Michal Vaskoe0665742021-02-11 11:08:44 +0100766 assert(xmlctx->status == LYXML_ELEM_CONTENT);
767 if (!value && !xmlctx->ws_only) {
768 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +0100769 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100770 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100771 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100772 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100773
774 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100775 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200776 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100777 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100778
779 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +0100780 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100781 attr = NULL;
782
Michal Vaskoe0665742021-02-11 11:08:44 +0100783 /* parser next element */
784 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100785
Michal Vaskoe0665742021-02-11 11:08:44 +0100786cleanup:
787 lyd_free_attr_siblings(xmlctx->ctx, attr);
788 if (rc) {
789 lyd_free_tree(*envp);
790 *envp = NULL;
791 }
792 return rc;
793}
794
795/**
796 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
797 *
798 * @param[in] xmlctx XML parser context.
799 * @param[out] evnp Parsed envelope(s) (opaque node).
800 * @param[out] int_opts Internal options for parsing the rest of YANG data.
801 * @param[out] close_elem Number of parsed opened elements that need to be closed.
802 * @return LY_SUCCESS on success.
803 * @return LY_ERR value on error.
804 */
805static LY_ERR
806lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
807{
808 LY_ERR rc = LY_SUCCESS, r;
809 struct lyd_node *child;
810
811 assert(envp && !*envp);
812
813 /* parse "rpc" */
814 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100815 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
816
817 /* parse "action", if any */
818 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
819 if (r == LY_SUCCESS) {
820 /* insert */
821 lyd_insert_node(*envp, NULL, child);
822
823 /* NETCONF action */
824 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
825 *close_elem = 2;
826 } else if (r == LY_ENOT) {
827 /* NETCONF RPC */
828 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
829 *close_elem = 1;
830 } else {
831 rc = r;
832 goto cleanup;
833 }
834
835cleanup:
836 if (rc) {
837 lyd_free_tree(*envp);
838 *envp = NULL;
839 }
840 return rc;
841}
842
843/**
844 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
845 *
846 * @param[in] xmlctx XML parser context.
847 * @param[out] evnp Parsed envelope(s) (opaque node).
848 * @param[out] int_opts Internal options for parsing the rest of YANG data.
849 * @param[out] close_elem Number of parsed opened elements that need to be closed.
850 * @return LY_SUCCESS on success.
851 * @return LY_ERR value on error.
852 */
853static LY_ERR
854lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
855{
856 LY_ERR rc = LY_SUCCESS, r;
857 struct lyd_node *child;
858
859 assert(envp && !*envp);
860
861 /* parse "notification" */
862 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100863 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
864
865 /* parse "eventTime" */
866 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
867 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +0100868 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
869 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100870 r = LY_EVALID;
871 }
872 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
873
874 /* insert */
875 lyd_insert_node(*envp, NULL, child);
876
877 /* validate value */
878 /* TODO validate child->value as yang:date-and-time */
879
880 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100881 if (xmlctx->status != LYXML_ELEM_CLOSE) {
882 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +0100883 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100884 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100885 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100886 goto cleanup;
887 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100888 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
889
890 /* NETCONF notification */
891 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
892 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100893
894cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100895 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +0100896 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100897 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100898 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100899 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100900}
Michal Vasko79135ae2020-12-16 10:08:35 +0100901
Michal Vaskoe0665742021-02-11 11:08:44 +0100902/**
903 * @brief Parse an XML element as an opaque node subtree.
904 *
905 * @param[in] xmlctx XML parser context.
906 * @param[in] parent Parent to append nodes to.
907 * @return LY_ERR value.
908 */
909static LY_ERR
910lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +0100911{
Michal Vaskoe0665742021-02-11 11:08:44 +0100912 LY_ERR rc = LY_SUCCESS;
913 const struct lyxml_ns *ns;
914 struct lyd_attr *attr = NULL;
915 struct lyd_node *child = NULL;
916 const char *name, *prefix;
917 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100918
Michal Vaskoe0665742021-02-11 11:08:44 +0100919 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100920
Michal Vaskoe0665742021-02-11 11:08:44 +0100921 name = xmlctx->name;
922 name_len = xmlctx->name_len;
923 prefix = xmlctx->prefix;
924 prefix_len = xmlctx->prefix_len;
925 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
926 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100927 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100928 return LY_EVALID;
929 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100930
Michal Vaskoe0665742021-02-11 11:08:44 +0100931 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +0100932
Michal Vaskoe0665742021-02-11 11:08:44 +0100933 /* create attributes */
934 if (xmlctx->status == LYXML_ATTRIBUTE) {
935 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
936 }
937
938 /* create node */
939 assert(xmlctx->status == LYXML_ELEM_CONTENT);
940 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200941 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child);
Michal Vaskoe0665742021-02-11 11:08:44 +0100942 LY_CHECK_GOTO(rc, cleanup);
943
944 /* assign atributes */
945 ((struct lyd_node_opaq *)child)->attr = attr;
946 attr = NULL;
947
948 /* parser next element */
949 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
950
951 /* parse all the descendants */
952 while (xmlctx->status == LYXML_ELEMENT) {
953 rc = lydxml_opaq_r(xmlctx, child);
954 LY_CHECK_GOTO(rc, cleanup);
955 }
956
957 /* insert */
958 lyd_insert_node(parent, NULL, child);
959
960cleanup:
961 lyd_free_attr_siblings(xmlctx->ctx, attr);
962 if (rc) {
963 lyd_free_tree(child);
964 }
965 return rc;
966}
967
968/**
969 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
970 *
971 * @param[in] xmlctx XML parser context.
972 * @param[in] parent Parent to append nodes to.
973 * @return LY_ERR value.
974 */
975static LY_ERR
976lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
977{
978 LY_ERR r;
979 struct lyd_node *child, *iter;
980 const struct lyxml_ns *ns;
981 ly_bool no_dup;
982
983 /* there must be some child */
984 if (xmlctx->status == LYXML_ELEM_CLOSE) {
985 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
986 return LY_EVALID;
987 }
988
989 while (xmlctx->status == LYXML_ELEMENT) {
990 child = NULL;
991
992 /*
993 * session-id
994 */
995 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
996 if (r == LY_SUCCESS) {
997 no_dup = 1;
998 goto check_child;
999 } else if (r != LY_ENOT) {
1000 goto error;
1001 }
1002
1003 /*
1004 * bad-attribute
1005 */
1006 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1007 if (r == LY_SUCCESS) {
1008 no_dup = 1;
1009 goto check_child;
1010 } else if (r != LY_ENOT) {
1011 goto error;
1012 }
1013
1014 /*
1015 * bad-element
1016 */
1017 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1018 if (r == LY_SUCCESS) {
1019 no_dup = 1;
1020 goto check_child;
1021 } else if (r != LY_ENOT) {
1022 goto error;
1023 }
1024
1025 /*
1026 * bad-namespace
1027 */
1028 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1029 if (r == LY_SUCCESS) {
1030 no_dup = 1;
1031 goto check_child;
1032 } else if (r != LY_ENOT) {
1033 goto error;
1034 }
1035
1036 if (r == LY_ENOT) {
1037 assert(xmlctx->status == LYXML_ELEMENT);
1038
1039 /* learn namespace */
1040 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
1041 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001042 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001043 r = LY_EVALID;
1044 goto error;
1045 } else if (!strcmp(ns->uri, "urn:ietf:params:xml:ns:netconf:base:1.0")) {
1046 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001047 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001048 r = LY_EVALID;
1049 goto error;
1050 }
1051
1052 /* custom elements */
1053 r = lydxml_opaq_r(xmlctx, parent);
1054 LY_CHECK_GOTO(r, error);
1055
1056 no_dup = 0;
1057 }
1058
1059check_child:
1060 /* check for duplicates */
1061 if (no_dup) {
1062 LY_LIST_FOR(lyd_child(parent), iter) {
1063 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1064 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1065 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1066 ((struct lyd_node_opaq *)child)->name.name);
1067 r = LY_EVALID;
1068 goto error;
1069 }
1070 }
1071 }
1072
1073 /* finish child parsing */
1074 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1075 assert(xmlctx->status == LYXML_ELEMENT);
1076 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001077 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001078 r = LY_EVALID;
1079 goto error;
1080 }
1081 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1082
1083 /* insert */
1084 lyd_insert_node(parent, NULL, child);
1085 }
1086
1087 return LY_SUCCESS;
1088
1089error:
1090 lyd_free_tree(child);
1091 return r;
1092}
1093
1094/**
1095 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1096 *
1097 * @param[in] xmlctx XML parser context.
1098 * @param[in] parent Parent to append nodes to.
1099 * @return LY_ERR value.
1100 */
1101static LY_ERR
1102lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1103{
1104 LY_ERR r;
1105 struct lyd_node *child, *iter;
1106 const char *val;
1107 ly_bool no_dup;
1108
1109 /* there must be some child */
1110 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1111 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1112 return LY_EVALID;
1113 }
1114
1115 while (xmlctx->status == LYXML_ELEMENT) {
1116 child = NULL;
1117
1118 /*
1119 * error-type
1120 */
1121 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1122 if (r == LY_SUCCESS) {
1123 val = ((struct lyd_node_opaq *)child)->value;
1124 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1125 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1126 ((struct lyd_node_opaq *)child)->name.name);
1127 r = LY_EVALID;
1128 goto error;
1129 }
1130
1131 no_dup = 1;
1132 goto check_child;
1133 } else if (r != LY_ENOT) {
1134 goto error;
1135 }
1136
1137 /*
1138 * error-tag
1139 */
1140 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1141 if (r == LY_SUCCESS) {
1142 val = ((struct lyd_node_opaq *)child)->value;
1143 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1144 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1145 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1146 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1147 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1148 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1149 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1150 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1151 ((struct lyd_node_opaq *)child)->name.name);
1152 r = LY_EVALID;
1153 goto error;
1154 }
1155
1156 no_dup = 1;
1157 goto check_child;
1158 } else if (r != LY_ENOT) {
1159 goto error;
1160 }
1161
1162 /*
1163 * error-severity
1164 */
1165 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1166 if (r == LY_SUCCESS) {
1167 val = ((struct lyd_node_opaq *)child)->value;
1168 if (strcmp(val, "error") && strcmp(val, "warning")) {
1169 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1170 ((struct lyd_node_opaq *)child)->name.name);
1171 r = LY_EVALID;
1172 goto error;
1173 }
1174
1175 no_dup = 1;
1176 goto check_child;
1177 } else if (r != LY_ENOT) {
1178 goto error;
1179 }
1180
1181 /*
1182 * error-app-tag
1183 */
1184 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1185 if (r == LY_SUCCESS) {
1186 no_dup = 1;
1187 goto check_child;
1188 } else if (r != LY_ENOT) {
1189 goto error;
1190 }
1191
1192 /*
1193 * error-path
1194 */
1195 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1196 if (r == LY_SUCCESS) {
1197 no_dup = 1;
1198 goto check_child;
1199 } else if (r != LY_ENOT) {
1200 goto error;
1201 }
1202
1203 /*
1204 * error-message
1205 */
1206 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1207 if (r == LY_SUCCESS) {
1208 no_dup = 1;
1209 goto check_child;
1210 } else if (r != LY_ENOT) {
1211 goto error;
1212 }
1213
1214 /* error-info */
1215 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1216 if (r == LY_SUCCESS) {
1217 /* parse all the descendants */
1218 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1219
1220 no_dup = 0;
1221 goto check_child;
1222 } else if (r != LY_ENOT) {
1223 goto error;
1224 }
1225
1226 if (r == LY_ENOT) {
1227 assert(xmlctx->status == LYXML_ELEMENT);
1228 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001229 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001230 r = LY_EVALID;
1231 goto error;
1232 }
1233
1234check_child:
1235 /* check for duplicates */
1236 if (no_dup) {
1237 LY_LIST_FOR(lyd_child(parent), iter) {
1238 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1239 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1240 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1241 ((struct lyd_node_opaq *)child)->name.name);
1242 r = LY_EVALID;
1243 goto error;
1244 }
1245 }
1246 }
1247
1248 /* finish child parsing */
1249 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1250 assert(xmlctx->status == LYXML_ELEMENT);
1251 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001252 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001253 r = LY_EVALID;
1254 goto error;
1255 }
1256 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1257
1258 /* insert */
1259 lyd_insert_node(parent, NULL, child);
1260 }
1261
1262 return LY_SUCCESS;
1263
1264error:
1265 lyd_free_tree(child);
1266 return r;
1267}
1268
1269/**
1270 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1271 *
1272 * @param[in] xmlctx XML parser context.
1273 * @param[out] evnp Parsed envelope(s) (opaque node).
1274 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1275 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1276 * @return LY_SUCCESS on success.
1277 * @return LY_ERR value on error.
1278 */
1279static LY_ERR
1280lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1281{
1282 LY_ERR rc = LY_SUCCESS, r;
1283 struct lyd_node *child = NULL;
1284 const char *parsed_elem = NULL;
1285
1286 assert(envp && !*envp);
1287
1288 /* parse "rpc-reply" */
1289 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001290 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1291
1292 /* there must be some child */
1293 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1294 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1295 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001296 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001297 }
1298
Michal Vaskoe0665742021-02-11 11:08:44 +01001299 /* try to parse "ok" */
1300 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1301 if (r == LY_SUCCESS) {
1302 /* insert */
1303 lyd_insert_node(*envp, NULL, child);
1304
1305 /* finish child parsing */
1306 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1307 assert(xmlctx->status == LYXML_ELEMENT);
1308 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001309 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001310 rc = LY_EVALID;
1311 goto cleanup;
1312 }
1313 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1314
1315 /* success */
1316 parsed_elem = "ok";
1317 goto finish;
1318 } else if (r != LY_ENOT) {
1319 rc = r;
1320 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001321 }
1322
Michal Vaskoe0665742021-02-11 11:08:44 +01001323 /* try to parse all "rpc-error" elements */
1324 while (xmlctx->status == LYXML_ELEMENT) {
1325 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1326 if (r == LY_ENOT) {
1327 break;
1328 } else if (r) {
1329 rc = r;
1330 goto cleanup;
1331 }
1332
1333 /* insert */
1334 lyd_insert_node(*envp, NULL, child);
1335
1336 /* parse all children of "rpc-error" */
1337 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1338
1339 /* finish child parsing */
1340 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1341 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1342
1343 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001344 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001345
1346finish:
1347 if (parsed_elem) {
1348 /* NETCONF rpc-reply with no data */
1349 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1350 assert(xmlctx->status == LYXML_ELEMENT);
1351 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001352 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001353 rc = LY_EVALID;
1354 goto cleanup;
1355 }
1356 }
1357
1358 /* NETCONF rpc-reply */
1359 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_REPLY;
1360 *close_elem = 1;
1361
1362cleanup:
1363 if (rc) {
1364 lyd_free_tree(*envp);
1365 *envp = NULL;
1366 }
1367 return rc;
1368}
1369
Michal Vasko2552ea32020-12-08 15:32:34 +01001370LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001371lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1372 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1373 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001374{
Michal Vaskoe0665742021-02-11 11:08:44 +01001375 LY_ERR rc = LY_SUCCESS;
1376 struct lyd_xml_ctx *lydctx;
1377 uint32_t i, int_opts, close_elem = 0;
1378 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001379
Michal Vaskoe0665742021-02-11 11:08:44 +01001380 assert(ctx && in && lydctx_p);
1381 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1382 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vasko2552ea32020-12-08 15:32:34 +01001383
Michal Vaskoe0665742021-02-11 11:08:44 +01001384 /* init context */
1385 lydctx = calloc(1, sizeof *lydctx);
1386 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1387 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1388 lydctx->parse_opts = parse_opts;
1389 lydctx->val_opts = val_opts;
1390 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001391 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001392
Michal Vaskoe0665742021-02-11 11:08:44 +01001393 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001394 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001395 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1396 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001397 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001398 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1399 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001400 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001401 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1402 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001403 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001404 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1405 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001406 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001407 assert(!parent);
1408 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1409 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001410 case LYD_TYPE_NOTIF_NETCONF:
1411 assert(!parent);
1412 LY_CHECK_GOTO(rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1413 break;
1414 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001415 assert(parent);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001416 LY_CHECK_GOTO(rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001417 break;
1418 }
1419 lydctx->int_opts = int_opts;
1420
1421 /* find the operation node if it exists already */
1422 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1423
1424 /* parse XML data */
1425 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1426 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1427 parsed_data_nodes = 1;
1428
1429 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1430 break;
1431 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001432 }
1433
Michal Vaskoe0665742021-02-11 11:08:44 +01001434 /* close all opened elements */
1435 for (i = 0; i < close_elem; ++i) {
1436 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1437 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001438 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1439 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001440 rc = LY_EVALID;
1441 goto cleanup;
1442 }
1443
1444 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001445 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001446
1447 /* check final state */
1448 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1449 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1450 rc = LY_EVALID;
1451 goto cleanup;
1452 }
1453 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1454 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1455 rc = LY_EVALID;
1456 goto cleanup;
1457 }
1458
1459 if (!parsed_data_nodes) {
1460 /* no data nodes were parsed */
1461 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001462 }
1463
1464cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001465 /* there should be no unres stored if validation should be skipped */
1466 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001467 !lydctx->node_when.count && !lydctx->node_exts.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01001468
1469 if (rc) {
1470 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1471 } else {
1472 *lydctx_p = (struct lyd_ctx *)lydctx;
1473
1474 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1475 lyxml_ctx_free(lydctx->xmlctx);
1476 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001477 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001478 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001479}