blob: fb1b50e93620bf547c5f5d6df1a37826757486d6 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file parser_xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko8cc3f662022-03-29 11:25:51 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02005 * @brief XML data parser for libyang
6 *
Michal Vasko8cc3f662022-03-29 11:25:51 +02007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Michal Vasko742a5b12022-02-24 16:07:27 +010016#define _GNU_SOURCE
17
Michal Vasko69730152020-10-09 16:30:07 +020018#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vasko742a5b12022-02-24 16:07:27 +010024#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020029#include "parser_data.h"
30#include "parser_internal.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010031#include "plugins_exts.h"
Michal Vasko51de7b72022-04-29 09:50:22 +020032#include "plugins_internal.h"
33#include "schema_compile_node.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "set.h"
Radek Krejci77114102021-03-10 15:21:57 +010035#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree_data_internal.h"
38#include "tree_schema.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_schema_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010040#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020042
Michal Vaskod027f382023-02-10 09:13:25 +010043static LY_ERR lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
44 struct ly_set *parsed);
45
Radek Krejci1798aae2020-07-14 13:26:06 +020046void
47lyd_xml_ctx_free(struct lyd_ctx *lydctx)
48{
49 struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
50
51 lyd_ctx_free(lydctx);
52 lyxml_ctx_free(ctx->xmlctx);
53 free(ctx);
54}
55
Michal Vasko45791ad2021-06-17 08:45:03 +020056/**
57 * @brief Parse and create XML metadata.
58 *
59 * @param[in] lydctx XML data parser context.
Michal Vaskoddd76592022-01-17 13:34:48 +010060 * @param[in] sparent Schema node of the parent.
Michal Vasko45791ad2021-06-17 08:45:03 +020061 * @param[out] meta List of created metadata instances.
62 * @return LY_ERR value.
63 */
Radek Krejcie7b95092019-05-15 11:03:07 +020064static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +010065lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020066{
aPiecek1c4da362021-04-29 14:26:34 +020067 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020068 const struct lyxml_ns *ns;
69 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010070 const char *name;
71 size_t name_len;
Michal Vasko45791ad2021-06-17 08:45:03 +020072 LY_ARRAY_COUNT_TYPE u;
73 ly_bool filter_attrs = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020074 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Radek Krejci28681fa2019-09-06 13:08:45 +020075
Michal Vaskob36053d2020-03-26 15:49:30 +010076 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +020077
Michal Vasko85be65e2023-06-13 09:44:17 +020078 LOG_LOCSET(sparent, NULL, NULL, NULL);
79
Michal Vasko45791ad2021-06-17 08:45:03 +020080 /* check for NETCONF filter unqualified attributes */
Michal Vasko1b2a3f42022-12-20 09:38:28 +010081 if (!strcmp(sparent->module->name, "notifications")) {
82 /* ancient module that does not even use the extension */
83 filter_attrs = 1;
84 } else {
85 LY_ARRAY_FOR(sparent->exts, u) {
86 if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") &&
87 !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) {
88 filter_attrs = 1;
89 break;
90 }
Michal Vasko45791ad2021-06-17 08:45:03 +020091 }
92 }
93
Michal Vaskob36053d2020-03-26 15:49:30 +010094 while (xmlctx->status == LYXML_ATTRIBUTE) {
95 if (!xmlctx->prefix_len) {
Michal Vasko45791ad2021-06-17 08:45:03 +020096 /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */
97 if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) ||
98 !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) {
99 mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf");
100 if (!mod) {
101 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
102 "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes.");
103 ret = LY_ENOTFOUND;
104 goto cleanup;
105 }
106 goto create_meta;
107 }
108
Michal Vaskoe0665742021-02-11 11:08:44 +0100109 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100110 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100111 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200112 ret = LY_EVALID;
Michal Vaskob36053d2020-03-26 15:49:30 +0100113 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200114 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100115
Michal Vasko45791ad2021-06-17 08:45:03 +0200116 /* skip attr */
Michal Vaskob36053d2020-03-26 15:49:30 +0100117 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
118 assert(xmlctx->status == LYXML_ATTR_CONTENT);
119 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200120 continue;
121 }
122
123 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200124 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200125 if (!ns) {
Michal Vasko52927e22020-03-16 17:26:14 +0100126 /* unknown namespace, XML error */
Radek Krejci422afb12021-03-04 16:38:16 +0100127 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko45791ad2021-06-17 08:45:03 +0200128 ret = LY_ENOTFOUND;
Radek Krejci28681fa2019-09-06 13:08:45 +0200129 goto cleanup;
130 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200131
132 /* get the module with metadata definition */
Michal Vasko52927e22020-03-16 17:26:14 +0100133 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200134 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100135 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100136 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200137 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100138 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
139 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200140 ret = LY_ENOTFOUND;
Michal Vaskob36053d2020-03-26 15:49:30 +0100141 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200142 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200143
144 /* skip attr */
145 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
146 assert(xmlctx->status == LYXML_ATTR_CONTENT);
147 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
148 continue;
Radek Krejci28681fa2019-09-06 13:08:45 +0200149 }
150
Michal Vasko45791ad2021-06-17 08:45:03 +0200151create_meta:
Michal Vasko60ea6352020-06-29 13:39:39 +0200152 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100153 name = xmlctx->name;
154 name_len = xmlctx->name_len;
155 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
156 assert(xmlctx->status == LYXML_ATTR_CONTENT);
157
158 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200159 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100160 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200161 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100162
163 /* next attribute */
164 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200165 }
Michal Vasko52927e22020-03-16 17:26:14 +0100166
Radek Krejcie7b95092019-05-15 11:03:07 +0200167cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +0200168 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100169 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200170 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200172 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200173 return ret;
174}
175
Michal Vasko52927e22020-03-16 17:26:14 +0100176static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200177lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100178{
179 LY_ERR ret = LY_SUCCESS;
180 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100181 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200182 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200183 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100184 const char *name, *prefix;
185 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100186
187 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100188 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100189
Michal Vaskob36053d2020-03-26 15:49:30 +0100190 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100191 if (*attr) {
192 attr2 = *attr;
193 } else {
194 attr2 = NULL;
195 }
196
Michal Vaskob36053d2020-03-26 15:49:30 +0100197 /* remember attr prefix, name, and get its content */
198 prefix = xmlctx->prefix;
199 prefix_len = xmlctx->prefix_len;
200 name = xmlctx->name;
201 name_len = xmlctx->name_len;
202 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
203 assert(xmlctx->status == LYXML_ATTR_CONTENT);
204
Michal Vaskoe137fc42021-07-22 11:53:13 +0200205 /* handle special "xml" attribute prefix */
206 if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) {
207 name = prefix;
208 name_len += 1 + prefix_len;
209 prefix = NULL;
210 prefix_len = 0;
211 }
212
213 /* find namespace of the attribute, if any */
214 ns = NULL;
215 if (prefix_len) {
216 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
217 if (!ns) {
218 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
219 ret = LY_EVALID;
220 goto cleanup;
221 }
222 }
223
Michal Vasko52927e22020-03-16 17:26:14 +0100224 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100225 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200226 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 +0100227 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100228
229 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100230 ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
Michal Vaskoe5e49e92022-02-01 13:15:08 +0100231 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data,
232 LYD_HINT_DATA);
Michal Vasko52927e22020-03-16 17:26:14 +0100233 LY_CHECK_GOTO(ret, cleanup);
234
235 if (!*attr) {
236 *attr = attr2;
237 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100238
239 /* next attribute */
240 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100241 }
242
243cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100244 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200245 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100246 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100247 }
Michal Vasko52927e22020-03-16 17:26:14 +0100248 return ret;
249}
250
Michal Vasko44685da2020-03-17 15:38:06 +0100251static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100252lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100253{
Michal Vaskob36053d2020-03-26 15:49:30 +0100254 LY_ERR ret = LY_SUCCESS, r;
255 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100256 struct ly_set key_set = {0};
257 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100258 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100259
260 assert(list && (list->nodetype == LYS_LIST));
261
262 /* get all keys into a set (keys do not have if-features or anything) */
263 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100264 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200265 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200266 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100267 }
268
Michal Vasko12d809c2021-03-03 16:34:32 +0100269 /* remember parent count */
270 parents_count = xmlctx->elements.count;
271
Michal Vaskob36053d2020-03-26 15:49:30 +0100272 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100273 /* find key definition */
274 for (i = 0; i < key_set.count; ++i) {
275 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100276 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100277 break;
278 }
279 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100280 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100281
282 /* skip attributes */
283 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100284 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
285 assert(xmlctx->status == LYXML_ATTR_CONTENT);
286 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100287 }
288
Michal Vaskob36053d2020-03-26 15:49:30 +0100289 assert(xmlctx->status == LYXML_ELEM_CONTENT);
290 if (i < key_set.count) {
291 /* validate the value */
Michal Vasko583b4642023-05-25 10:39:34 +0200292 r = ly_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA);
Michal Vaskob36053d2020-03-26 15:49:30 +0100293 if (!r) {
294 /* key with a valid value, remove from the set */
295 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100296 }
297 }
298
Michal Vaskob36053d2020-03-26 15:49:30 +0100299 /* parser next */
300 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100301
Michal Vaskob36053d2020-03-26 15:49:30 +0100302 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100303 while (xmlctx->status == LYXML_ELEMENT) {
304 while (parents_count < xmlctx->elements.count) {
305 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
306 }
307 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100308 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
309 }
310
311 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
312 assert(xmlctx->status == LYXML_ELEM_CLOSE);
313 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
314 if (next != LYXML_ELEM_CLOSE) {
315 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
316 }
Michal Vasko44685da2020-03-17 15:38:06 +0100317 }
318
319 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100320 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100321 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100322 }
323
324cleanup:
325 ly_set_erase(&key_set, NULL);
326 return ret;
327}
328
Michal Vasko5c24ed12021-06-09 09:27:32 +0200329/**
330 * @brief Skip an element with all its descendants.
331 *
332 * @param[in] xmlctx XML parser context.
333 * @return LY_ERR value.
334 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100335static LY_ERR
336lydxml_data_skip(struct lyxml_ctx *xmlctx)
337{
338 uint32_t parents_count;
339
340 /* remember current number of parents */
341 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200342 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100343
344 /* skip after the content */
345 while (xmlctx->status != LYXML_ELEM_CONTENT) {
346 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
347 }
348 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
349
350 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200351 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100352 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
353 }
354
355 /* close element */
356 assert(xmlctx->status == LYXML_ELEM_CLOSE);
357 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
358
359 return LY_SUCCESS;
360}
361
Michal Vasko5c24ed12021-06-09 09:27:32 +0200362/**
363 * @brief Check that the current element can be parsed as a data node.
364 *
365 * @param[in] lydctx XML data parser context.
366 * @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
367 * @return LY_ERR value.
368 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100369static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200370lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100371{
372 LY_ERR ret = LY_SUCCESS;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200373 struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx;
Michal Vasko1bf09392020-03-27 12:38:10 +0100374
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100375 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
376 /* only checks specific to opaque nodes */
377 return LY_SUCCESS;
378 }
379
Michal Vasko13854662021-06-09 09:27:50 +0200380 if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) {
381 /* nothing to check */
382 return LY_SUCCESS;
383 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100384
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200385 assert(xmlctx->elements.count);
386
Michal Vasko13854662021-06-09 09:27:50 +0200387 /* backup parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200388 LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx));
Michal Vasko1bf09392020-03-27 12:38:10 +0100389
Michal Vasko13854662021-06-09 09:27:50 +0200390 /* skip attributes */
391 while (xmlctx->status == LYXML_ATTRIBUTE) {
392 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
393 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
394 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100395
Michal Vasko13854662021-06-09 09:27:50 +0200396 if ((*snode)->nodetype & LYD_NODE_TERM) {
397 /* value may not be valid in which case we parse it as an opaque node */
Michal Vasko583b4642023-05-25 10:39:34 +0200398 if (ly_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA)) {
Michal Vaskod0237d42021-07-12 14:49:46 +0200399 LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, xmlctx->value_len,
400 xmlctx->value);
Michal Vasko13854662021-06-09 09:27:50 +0200401 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100402 }
Michal Vasko13854662021-06-09 09:27:50 +0200403 } else if ((*snode)->nodetype == LYS_LIST) {
404 /* skip content */
405 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
Michal Vasko1bf09392020-03-27 12:38:10 +0100406
Michal Vasko13854662021-06-09 09:27:50 +0200407 if (lydxml_check_list(xmlctx, *snode)) {
408 /* invalid list, parse as opaque if it missing/has invalid some keys */
Michal Vaskod0237d42021-07-12 14:49:46 +0200409 LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name);
Michal Vasko13854662021-06-09 09:27:50 +0200410 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100411 }
Michal Vasko13854662021-06-09 09:27:50 +0200412 } else {
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100413 /* if there is a non-WS value, it cannot be parsed as an inner node */
414 assert(xmlctx->status == LYXML_ELEM_CONTENT);
415 if (!xmlctx->ws_only) {
416 *snode = NULL;
417 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100418 }
419
Michal Vasko13854662021-06-09 09:27:50 +0200420restore:
421 /* restore parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200422 lyxml_ctx_restore(xmlctx, &pxmlctx);
Michal Vasko1bf09392020-03-27 12:38:10 +0100423 return ret;
424}
425
Radek Krejcie7b95092019-05-15 11:03:07 +0200426/**
Michal Vaskocea58712022-04-01 14:37:08 +0200427 * @brief Get sensible data hints for an opaque node.
428 *
429 * @param[in] name Node name.
430 * @param[in] name_len Length of @p name.
431 * @param[in] value Node value.
432 * @param[in] value_len Length of @p value.
433 * @param[in] first Node first sibling.
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100434 * @param[in] ns Node module namespace, NULL for no namespace.
Michal Vaskocea58712022-04-01 14:37:08 +0200435 * @param[out] hints Data hints to use.
436 * @param[out] anchor Anchor to insert after in case of a list.
437 */
438static void
Michal Vaskod027f382023-02-10 09:13:25 +0100439lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, const struct lyd_node *first,
Michal Vaskocea58712022-04-01 14:37:08 +0200440 const char *ns, uint32_t *hints, struct lyd_node **anchor)
441{
442 struct lyd_node_opaq *opaq;
443 char *ptr;
444 long num;
445
446 *hints = 0;
447 *anchor = NULL;
448
449 if (!value_len) {
Michal Vasko6235e152023-08-07 13:39:13 +0200450 /* no value but it may also be zero-length string */
451 *hints |= LYD_VALHINT_EMPTY | LYD_VALHINT_STRING;
Michal Vaskocea58712022-04-01 14:37:08 +0200452 } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) {
453 /* boolean value */
454 *hints |= LYD_VALHINT_BOOLEAN;
455 } else {
456 num = strtol(value, &ptr, 10);
457 if ((unsigned)(ptr - value) == value_len) {
458 /* number value */
459 *hints |= LYD_VALHINT_DECNUM;
460 if ((num < INT32_MIN) || (num > UINT32_MAX)) {
461 /* large number */
462 *hints |= LYD_VALHINT_NUM64;
463 }
464 } else {
465 /* string value */
466 *hints |= LYD_VALHINT_STRING;
467 }
468 }
469
470 if (!first) {
471 return;
472 }
473
474 /* search backwards to find the last instance */
475 do {
476 first = first->prev;
477 if (first->schema) {
478 continue;
479 }
480
481 opaq = (struct lyd_node_opaq *)first;
482 assert(opaq->format == LY_VALUE_XML);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100483 if (!ly_strncmp(opaq->name.name, name, name_len) &&
484 ((ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) {
Michal Vaskocea58712022-04-01 14:37:08 +0200485 if (opaq->value && opaq->value[0]) {
486 /* leaf-list nodes */
487 opaq->hints |= LYD_NODEHINT_LEAFLIST;
488 *hints |= LYD_NODEHINT_LEAFLIST;
489 } else {
490 /* list nodes */
491 opaq->hints |= LYD_NODEHINT_LIST;
492 *hints |= LYD_NODEHINT_LIST;
493 }
Michal Vaskod027f382023-02-10 09:13:25 +0100494 *anchor = (struct lyd_node *)first;
Michal Vaskocea58712022-04-01 14:37:08 +0200495 break;
496 }
497 } while (first->prev->next);
498}
499
500/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200501 * @brief Get schema node for the current element.
Michal Vaskoddd76592022-01-17 13:34:48 +0100502 *
503 * @param[in] lydctx XML data parser context.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200504 * @param[in] parent Parsed parent data node, if any.
505 * @param[in] prefix Element prefix, if any.
506 * @param[in] prefix_len Length of @p prefix.
507 * @param[in] name Element name.
508 * @param[in] name_len Length of @p name.
509 * @param[out] snode Found schema node, NULL if no suitable was found.
510 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vaskoddd76592022-01-17 13:34:48 +0100511 * @return LY_SUCCESS on success;
Michal Vaskoddd76592022-01-17 13:34:48 +0100512 * @return LY_ERR on error.
513 */
514static LY_ERR
Michal Vaskod027f382023-02-10 09:13:25 +0100515lydxml_subtree_get_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200516 const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vaskoddd76592022-01-17 13:34:48 +0100517{
518 LY_ERR r;
Michal Vaskob36053d2020-03-26 15:49:30 +0100519 struct lyxml_ctx *xmlctx;
520 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200521 const struct lyxml_ns *ns;
Radek Krejcie7b95092019-05-15 11:03:07 +0200522 struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200523 uint32_t getnext_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +0100524
Michal Vaskob36053d2020-03-26 15:49:30 +0100525 xmlctx = lydctx->xmlctx;
526 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100527 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100528
Michal Vasko8cc3f662022-03-29 11:25:51 +0200529 *snode = NULL;
530 *ext = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100531
Michal Vasko8cc3f662022-03-29 11:25:51 +0200532 /* get current namespace */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200533 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200534 if (!ns) {
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100535 if (lydctx->int_opts & LYD_INTOPT_ANY) {
536 goto unknown_module;
537 }
538
539 if (prefix_len) {
540 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
541 } else {
542 LOGVAL(ctx, LYVE_REFERENCE, "Missing XML namespace.");
543 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200544 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200545 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200546
547 /* get the element module, use parent context if possible because of extensions */
548 mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200549 if (!mod) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100550 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200551 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len,
552 snode, ext);
553 if (r != LY_ENOT) {
554 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100555 return r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100556 }
557
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100558unknown_module:
Michal Vaskoe0665742021-02-11 11:08:44 +0100559 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100560 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200561 return LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200562 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200563 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200564 }
565
Michal Vaskoa5da3292020-08-12 13:10:50 +0200566 /* get the schema node */
Michal Vasko81008a52021-07-21 16:06:12 +0200567 if (mod) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100568 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200569 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100570 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200571 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100572 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200573 if (!*snode) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100574 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200575 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
576 name_len, snode, ext);
577 if (r != LY_ENOT) {
578 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100579 return r;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100580 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100581
582 /* unknown data node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100583 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
584 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100585 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100586 (int)name_len, name, LYD_NAME(parent));
Radek Krejcif16e2542021-02-17 15:39:23 +0100587 } else if (lydctx->ext) {
588 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100589 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
590 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100591 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100592 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
593 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100594 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100595 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100596 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
597 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100598 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200599 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200600 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200601 return LY_SUCCESS;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200602 } else {
603 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200604 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
605 LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
Michal Vaskoa5da3292020-08-12 13:10:50 +0200606 }
607 }
608
Michal Vasko8cc3f662022-03-29 11:25:51 +0200609 return LY_SUCCESS;
610}
611
612/**
Michal Vaskod027f382023-02-10 09:13:25 +0100613 * @brief Parse an XML opque node.
614 *
615 * @param[in] lydctx XML YANG data parser context.
616 * @param[in] sibling Existing sibling node, if any.
617 * @param[in] prefix Parsed node prefix.
618 * @param[in] prefix_len Length of @p prefix.
619 * @param[in] name Parsed node name.
620 * @param[in] name_len Length of @p name.
621 * @param[out] insert_anchor Optional anchor node for inserting this node.
622 * @param[out] node Created node.
623 * @return LY_ERR value.
624 */
625static LY_ERR
626lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len,
627 const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node)
628{
629 LY_ERR rc = LY_SUCCESS;
630 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
631 const char *ns_uri;
632 const struct lyxml_ns *ns;
633 uint32_t hints;
634 void *val_prefix_data = NULL;
635 LY_VALUE_FORMAT format;
636
637 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
638
639 *node = NULL;
640
641 if (xmlctx->ws_only) {
642 /* ignore WS-only value */
643 if (xmlctx->dynamic) {
644 free((char *)xmlctx->value);
645 }
646 xmlctx->dynamic = 0;
647 xmlctx->value = "";
648 xmlctx->value_len = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100649 }
650
Michal Vasko535d21c2023-08-09 10:41:44 +0200651 /* get value prefixes, if any */
652 rc = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, &format,
653 &val_prefix_data);
654 LY_CHECK_GOTO(rc, cleanup);
655
Michal Vaskod027f382023-02-10 09:13:25 +0100656 /* get NS again, it may have been backed up and restored */
657 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
658 ns_uri = ns ? ns->uri : NULL;
659
660 /* get best-effort node hints */
661 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor);
662
663 /* create node */
664 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0,
665 xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, hints, node);
666 LY_CHECK_GOTO(rc, cleanup);
667 val_prefix_data = NULL;
668
669 /* parser next */
670 rc = lyxml_ctx_next(xmlctx);
671 LY_CHECK_GOTO(rc, cleanup);
672
673 /* process children */
674 while (xmlctx->status == LYXML_ELEMENT) {
675 rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
676 LY_CHECK_GOTO(rc, cleanup);
677 }
678
679cleanup:
680 ly_free_prefix_data(format, val_prefix_data);
681 if (rc) {
682 lyd_free_tree(*node);
683 *node = NULL;
684 }
685 return rc;
686}
687
688/**
689 * @brief Parse an XML leaf/leaf-list node.
690 *
691 * @param[in] lydctx XML YANG data parser context.
692 * @param[in] parent Parent node, if any.
693 * @param[in] snode Schema node of the new node.
694 * @param[out] node Created node.
695 * @return LY_ERR value.
696 */
697static LY_ERR
698lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode,
699 struct lyd_node **node)
700{
701 LY_ERR r, rc = LY_SUCCESS;
702 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
703 struct lyd_node *anchor;
704
705 *node = NULL;
706
707 /* create node */
708 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
709 LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node);
710 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
711
712 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100713 LOG_LOCSET(NULL, *node, NULL, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +0100714 }
715
Michal Vasko58a1a702023-03-01 13:45:38 +0100716 if (*node && parent && (snode->flags & LYS_KEY)) {
Michal Vaskod027f382023-02-10 09:13:25 +0100717 /* check the key order, the anchor must never be a key */
718 anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node);
719 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
720 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
721 LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name);
722 r = LY_EVALID;
723 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
724 } else {
725 LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name);
726 }
727 }
728 }
729
730 /* parser next */
731 r = lyxml_ctx_next(xmlctx);
732 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
733
734 /* no children expected */
735 if (xmlctx->status == LYXML_ELEMENT) {
736 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
737 (int)xmlctx->name_len, xmlctx->name, snode->name);
738 r = LY_EVALID;
739 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
740 }
741
742cleanup:
743 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100744 LOG_LOCBACK(0, 1, 0, 0);
Michal Vaskod027f382023-02-10 09:13:25 +0100745 }
746 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
747 lyd_free_tree(*node);
748 *node = NULL;
749 }
750 return rc;
751}
752
753/**
754 * @brief Parse an XML inner node.
755 *
756 * @param[in] lydctx XML YANG data parser context.
757 * @param[in] snode Schema node of the new node.
758 * @param[in] ext Extension instance of @p snode, if any.
759 * @param[out] node Created node.
760 * @return LY_ERR value.
761 */
762static LY_ERR
763lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
764 struct lyd_node **node)
765{
766 LY_ERR r, rc = LY_SUCCESS;
767 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
768 uint32_t prev_parse_opts = lydctx->parse_opts;
769
770 *node = NULL;
771
772 if (!xmlctx->ws_only) {
773 /* value in inner node */
774 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
775 (int)xmlctx->value_len, xmlctx->value, snode->name);
776 r = LY_EVALID;
777 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
778 }
779
780 /* create node */
781 rc = lyd_create_inner(snode, node);
782 LY_CHECK_GOTO(rc, cleanup);
783
Michal Vasko58a1a702023-03-01 13:45:38 +0100784 assert(*node);
785 LOG_LOCSET(NULL, *node, NULL, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +0100786
787 /* parser next */
788 rc = lyxml_ctx_next(xmlctx);
789 LY_CHECK_GOTO(rc, cleanup);
790
791 if (ext) {
792 /* only parse these extension data and validate afterwards */
793 lydctx->parse_opts |= LYD_PARSE_ONLY;
794 }
795
796 /* process children */
797 while (xmlctx->status == LYXML_ELEMENT) {
798 r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
799 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
800 }
801
802 /* restore options */
803 lydctx->parse_opts = prev_parse_opts;
804
805 if (snode->nodetype == LYS_LIST) {
806 /* check all keys exist */
807 r = lyd_parse_check_keys(*node);
Michal Vasko202d8162023-03-01 14:42:19 +0100808 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100809 }
810
811 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
812 /* new node validation, autodelete CANNOT occur, all nodes are new */
813 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
814 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
815
816 /* add any missing default children */
817 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
818 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
819 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
820 }
821
822 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
823 /* rememeber the RPC/action/notification */
824 lydctx->op_node = *node;
825 }
826
827cleanup:
828 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100829 LOG_LOCBACK(0, 1, 0, 0);
Michal Vaskod027f382023-02-10 09:13:25 +0100830 }
831 lydctx->parse_opts = prev_parse_opts;
Michal Vasko202d8162023-03-01 14:42:19 +0100832 if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
833 /* list without keys is unusable or an error */
Michal Vaskod027f382023-02-10 09:13:25 +0100834 lyd_free_tree(*node);
835 *node = NULL;
836 }
837 return rc;
838}
839
840/**
841 * @brief Parse an XML anyxml/anydata node.
842 *
843 * @param[in] lydctx XML YANG data parser context.
844 * @param[in] snode Schema node of the new node.
845 * @param[in] ext Extension instance of @p snode, if any.
846 * @param[out] node Created node.
847 * @return LY_ERR value.
848 */
849static LY_ERR
850lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
851 struct lyd_node **node)
852{
853 LY_ERR r, rc = LY_SUCCESS;
854 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
855 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
856 struct lyd_node *child = NULL;
857 char *val = NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +0200858 ly_bool log_node = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100859
860 *node = NULL;
861
862 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
863 /* value in anydata node, we expect a tree */
864 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
865 (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name);
866 r = LY_EVALID;
867 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
868 }
869
870 if (!xmlctx->ws_only) {
871 /* use an arbitrary text value for anyxml */
872 val = strndup(xmlctx->value, xmlctx->value_len);
873 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
874
875 /* parser next */
876 r = lyxml_ctx_next(xmlctx);
877 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
878
879 /* create node */
880 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
881 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
882 val = NULL;
883 } else {
Michal Vasko85be65e2023-06-13 09:44:17 +0200884 /* create node */
885 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
886 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
887
888 assert(*node);
889 LOG_LOCSET(NULL, *node, NULL, NULL);
890 log_node = 1;
891
Michal Vaskod027f382023-02-10 09:13:25 +0100892 /* parser next */
893 r = lyxml_ctx_next(xmlctx);
894 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
895
896 /* update options so that generic data can be parsed */
897 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
898 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
899 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
900
901 /* parse any data tree */
902 while (xmlctx->status == LYXML_ELEMENT) {
903 r = lydxml_subtree_r(lydctx, NULL, &child, NULL);
904 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
905 }
906
Michal Vasko85be65e2023-06-13 09:44:17 +0200907 /* assign the data tree */
908 ((struct lyd_node_any *)*node)->value.tree = child;
Michal Vaskod027f382023-02-10 09:13:25 +0100909 child = NULL;
910 }
911
912cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +0200913 if (log_node) {
914 LOG_LOCBACK(0, 1, 0, 0);
915 }
Michal Vaskod027f382023-02-10 09:13:25 +0100916 lydctx->parse_opts = prev_parse_opts;
917 lydctx->int_opts = prev_int_opts;
918 free(val);
919 lyd_free_tree(child);
920 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
921 lyd_free_tree(*node);
922 *node = NULL;
923 }
924 return rc;
925}
926
927/**
928 * @brief Parse an XML subtree, recursively.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200929 *
930 * @param[in] lydctx XML YANG data parser context.
931 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
Michal Vaskod027f382023-02-10 09:13:25 +0100932 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200933 * @param[in,out] parsed Optional set to add all the parsed siblings into.
934 * @return LY_ERR value.
935 */
936static LY_ERR
937lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
938{
Michal Vaskod027f382023-02-10 09:13:25 +0100939 LY_ERR r, rc = LY_SUCCESS;
940 const char *prefix, *name;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200941 size_t prefix_len, name_len;
942 struct lyxml_ctx *xmlctx;
943 const struct ly_ctx *ctx;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200944 struct lyd_meta *meta = NULL;
945 struct lyd_attr *attr = NULL;
946 const struct lysc_node *snode;
947 struct lysc_ext_instance *ext;
Michal Vaskod027f382023-02-10 09:13:25 +0100948 uint32_t orig_parse_opts;
949 struct lyd_node *node = NULL, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200950 ly_bool parse_subtree;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200951
952 assert(parent || first_p);
953
954 xmlctx = lydctx->xmlctx;
955 ctx = xmlctx->ctx;
956
957 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
958 /* all descendants should be parsed */
959 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
960 orig_parse_opts = lydctx->parse_opts;
961
962 assert(xmlctx->status == LYXML_ELEMENT);
963
964 /* remember element prefix and name */
965 prefix = xmlctx->prefix;
966 prefix_len = xmlctx->prefix_len;
967 name = xmlctx->name;
968 name_len = xmlctx->name_len;
969
970 /* parser next */
Michal Vaskod027f382023-02-10 09:13:25 +0100971 rc = lyxml_ctx_next(xmlctx);
972 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200973
974 /* get the schema node */
Michal Vasko202d8162023-03-01 14:42:19 +0100975 r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext);
976 if (r) {
977 rc = r;
978 if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
979 /* skip the invalid data */
980 if ((r = lydxml_data_skip(xmlctx))) {
981 rc = r;
982 }
983 }
984 goto cleanup;
985 } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200986 LOGVRB("Skipping parsing of unknown node \"%.*s\".", name_len, name);
987
988 /* skip element with children */
Michal Vaskod027f382023-02-10 09:13:25 +0100989 rc = lydxml_data_skip(xmlctx);
990 goto cleanup;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200991 }
992
Michal Vaskoa5da3292020-08-12 13:10:50 +0200993 /* create metadata/attributes */
994 if (xmlctx->status == LYXML_ATTRIBUTE) {
995 if (snode) {
Michal Vaskod027f382023-02-10 09:13:25 +0100996 rc = lydxml_metadata(lydctx, snode, &meta);
997 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200998 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100999 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskod027f382023-02-10 09:13:25 +01001000 rc = lydxml_attrs(xmlctx, &attr);
1001 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001002 }
1003 }
1004
1005 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1006 if (!snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001007 /* opaque */
1008 r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len,
1009 &insert_anchor, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001010 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskod027f382023-02-10 09:13:25 +01001011 /* term */
1012 r = lydxml_subtree_term(lydctx, parent, snode, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001013 } else if (snode->nodetype & LYD_NODE_INNER) {
Michal Vaskod027f382023-02-10 09:13:25 +01001014 /* inner */
1015 r = lydxml_subtree_inner(lydctx, snode, ext, &node);
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001016 } else {
Michal Vaskod027f382023-02-10 09:13:25 +01001017 /* any */
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001018 assert(snode->nodetype & LYD_NODE_ANY);
Michal Vaskod027f382023-02-10 09:13:25 +01001019 r = lydxml_subtree_any(lydctx, snode, ext, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001020 }
Michal Vaskod027f382023-02-10 09:13:25 +01001021 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko1524f1a2023-03-01 09:36:34 +01001022
Michal Vasko58a1a702023-03-01 13:45:38 +01001023 if (node && snode) {
Michal Vasko135719f2022-08-25 12:18:17 +02001024 /* add/correct flags */
Michal Vaskod027f382023-02-10 09:13:25 +01001025 r = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko567d7032023-02-13 08:53:31 +01001026 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001027
Michal Vaskoeba23112022-08-26 08:35:41 +02001028 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1029 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001030 r = lyd_validate_node_ext(node, &lydctx->ext_node);
1031 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001032 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001033 }
1034
1035 /* parser next */
1036 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +01001037 if (!parse_subtree) {
Michal Vaskod027f382023-02-10 09:13:25 +01001038 r = lyxml_ctx_next(xmlctx);
Michal Vasko567d7032023-02-13 08:53:31 +01001039 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001040 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001041
Michal Vasko58a1a702023-03-01 13:45:38 +01001042 LY_CHECK_GOTO(!node, cleanup);
1043
Michal Vaskoa5da3292020-08-12 13:10:50 +02001044 /* add metadata/attributes */
1045 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +01001046 lyd_insert_meta(node, meta, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001047 meta = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001048 } else {
1049 lyd_insert_attr(node, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001050 attr = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001051 }
1052
1053 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +02001054 if (insert_anchor) {
1055 lyd_insert_after(insert_anchor, node);
1056 } else if (ext) {
Michal Vaskod027f382023-02-10 09:13:25 +01001057 r = lyplg_ext_insert(parent, node);
Michal Vasko567d7032023-02-13 08:53:31 +01001058 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001059 } else {
1060 lyd_insert_node(parent, first_p, node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
1061 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001062 while (!parent && (*first_p)->prev->next) {
1063 *first_p = (*first_p)->prev;
1064 }
1065
Michal Vaskoe0665742021-02-11 11:08:44 +01001066 /* rememeber a successfully parsed node */
1067 if (parsed) {
1068 ly_set_add(parsed, node, 1, NULL);
1069 }
1070
Michal Vaskod027f382023-02-10 09:13:25 +01001071cleanup:
Michal Vasko8cc3f662022-03-29 11:25:51 +02001072 lydctx->parse_opts = orig_parse_opts;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001073 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001074 lyd_free_attr_siblings(ctx, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001075 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +02001076}
1077
Michal Vaskoe0665742021-02-11 11:08:44 +01001078/**
1079 * @brief Parse a specific XML element into an opaque node.
1080 *
1081 * @param[in] xmlctx XML parser context.
1082 * @param[in] name Name of the element.
1083 * @param[in] uri URI of the element.
1084 * @param[in] value Whether a value is expected in the element.
1085 * @param[out] evnp Parsed envelope (opaque node).
1086 * @return LY_SUCCESS on success.
1087 * @return LY_ENOT if the specified element did not match.
1088 * @return LY_ERR value on error.
1089 */
Michal Vasko1bf09392020-03-27 12:38:10 +01001090static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001091lydxml_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 +01001092{
Michal Vaskoe0665742021-02-11 11:08:44 +01001093 LY_ERR rc = LY_SUCCESS;
1094 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +02001095 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +01001096 const char *prefix;
1097 size_t prefix_len;
1098
Michal Vaskof048ba52023-06-13 10:40:43 +02001099 if (xmlctx->status != LYXML_ELEMENT) {
1100 /* nothing to parse */
1101 return LY_ENOT;
1102 }
1103
Michal Vasko1bf09392020-03-27 12:38:10 +01001104 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
1105 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +01001106 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001107 }
1108
1109 prefix = xmlctx->prefix;
1110 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001111 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001112 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001113 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001114 return LY_EVALID;
1115 } else if (strcmp(ns->uri, uri)) {
1116 /* different namespace */
1117 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001118 }
1119
1120 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
1121
1122 /* create attributes */
1123 if (xmlctx->status == LYXML_ATTRIBUTE) {
1124 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1125 }
1126
Michal Vaskoe0665742021-02-11 11:08:44 +01001127 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1128 if (!value && !xmlctx->ws_only) {
1129 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +01001130 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001131 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001132 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001133 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001134
1135 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001136 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001137 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001138 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001139
1140 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001141 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001142 attr = NULL;
1143
Michal Vaskoe0665742021-02-11 11:08:44 +01001144 /* parser next element */
1145 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001146
Michal Vaskoe0665742021-02-11 11:08:44 +01001147cleanup:
1148 lyd_free_attr_siblings(xmlctx->ctx, attr);
1149 if (rc) {
1150 lyd_free_tree(*envp);
1151 *envp = NULL;
1152 }
1153 return rc;
1154}
1155
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001156LY_ERR
1157lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1158 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
1159 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
1160{
Michal Vaskod027f382023-02-10 09:13:25 +01001161 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001162 struct lyd_xml_ctx *lydctx;
Michal Vasko64592692023-06-12 13:50:11 +02001163 ly_bool parsed_data_nodes = 0, close_elem = 0;
1164 struct lyd_node *act = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001165 enum LYXML_PARSER_STATUS status;
1166
1167 assert(ctx && in && lydctx_p);
1168 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1169 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1170
1171 /* init context */
1172 lydctx = calloc(1, sizeof *lydctx);
1173 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1174 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1175 lydctx->parse_opts = parse_opts;
1176 lydctx->val_opts = val_opts;
1177 lydctx->int_opts = int_opts;
1178 lydctx->free = lyd_xml_ctx_free;
1179 lydctx->ext = ext;
1180
1181 /* find the operation node if it exists already */
1182 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1183
Michal Vasko64592692023-06-12 13:50:11 +02001184 if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) {
1185 /* can be either, try to parse "action" */
1186 if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) {
1187 close_elem = 1;
1188 int_opts &= ~LYD_INTOPT_RPC;
1189 }
1190 }
1191
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001192 /* parse XML data */
1193 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
Michal Vaskod027f382023-02-10 09:13:25 +01001194 r = lydxml_subtree_r(lydctx, parent, first_p, parsed);
1195 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1196
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001197 parsed_data_nodes = 1;
1198
1199 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1200 break;
1201 }
1202 }
1203
Michal Vasko64592692023-06-12 13:50:11 +02001204 /* close an opened element */
1205 if (close_elem) {
1206 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1207 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
1208 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1209 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
1210 rc = LY_EVALID;
1211 goto cleanup;
1212 }
1213
1214 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
1215 }
1216
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001217 /* check final state */
1218 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1219 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001220 r = LY_EVALID;
1221 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001222 }
1223 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1224 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001225 r = LY_EVALID;
1226 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001227 }
1228
1229 if (!parsed_data_nodes) {
1230 /* no data nodes were parsed */
1231 lydctx->op_node = NULL;
1232 }
1233
1234 if (parse_opts & LYD_PARSE_SUBTREE) {
1235 /* check for a sibling element */
1236 assert(subtree_sibling);
1237 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1238 *subtree_sibling = 1;
1239 } else {
1240 *subtree_sibling = 0;
1241 }
1242 }
1243
1244cleanup:
1245 /* there should be no unres stored if validation should be skipped */
1246 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1247 !lydctx->node_when.count));
1248
Michal Vasko64592692023-06-12 13:50:11 +02001249 lyd_free_tree(act);
Michal Vaskod027f382023-02-10 09:13:25 +01001250 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001251 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1252 } else {
1253 *lydctx_p = (struct lyd_ctx *)lydctx;
1254
1255 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1256 lyxml_ctx_free(lydctx->xmlctx);
1257 lydctx->xmlctx = NULL;
1258 }
1259 return rc;
1260}
1261
Michal Vaskoe0665742021-02-11 11:08:44 +01001262/**
1263 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
1264 *
1265 * @param[in] xmlctx XML parser context.
1266 * @param[out] evnp Parsed envelope(s) (opaque node).
1267 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1268 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1269 * @return LY_SUCCESS on success.
1270 * @return LY_ERR value on error.
1271 */
1272static LY_ERR
1273lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1274{
1275 LY_ERR rc = LY_SUCCESS, r;
1276 struct lyd_node *child;
1277
1278 assert(envp && !*envp);
1279
1280 /* parse "rpc" */
1281 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001282 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1283
1284 /* parse "action", if any */
1285 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
1286 if (r == LY_SUCCESS) {
1287 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001288 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001289
1290 /* NETCONF action */
1291 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1292 *close_elem = 2;
1293 } else if (r == LY_ENOT) {
1294 /* NETCONF RPC */
1295 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1296 *close_elem = 1;
1297 } else {
1298 rc = r;
1299 goto cleanup;
1300 }
1301
1302cleanup:
1303 if (rc) {
1304 lyd_free_tree(*envp);
1305 *envp = NULL;
1306 }
1307 return rc;
1308}
1309
1310/**
1311 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
1312 *
1313 * @param[in] xmlctx XML parser context.
1314 * @param[out] evnp Parsed envelope(s) (opaque node).
1315 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1316 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1317 * @return LY_SUCCESS on success.
1318 * @return LY_ERR value on error.
1319 */
1320static LY_ERR
1321lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1322{
1323 LY_ERR rc = LY_SUCCESS, r;
1324 struct lyd_node *child;
1325
1326 assert(envp && !*envp);
1327
1328 /* parse "notification" */
1329 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001330 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1331
1332 /* parse "eventTime" */
1333 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
1334 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +01001335 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
1336 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001337 r = LY_EVALID;
1338 }
1339 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1340
1341 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001342 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001343
1344 /* validate value */
Michal Vasko820efe82023-05-12 15:47:43 +02001345 r = lyd_parser_notif_eventtime_validate(child);
Michal Vasko51de7b72022-04-29 09:50:22 +02001346 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001347
1348 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +01001349 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1350 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001351 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001352 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001353 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001354 goto cleanup;
1355 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001356 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1357
1358 /* NETCONF notification */
1359 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
1360 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001361
1362cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001363 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +01001364 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001365 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001366 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001367 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001368}
Michal Vasko79135ae2020-12-16 10:08:35 +01001369
Michal Vaskoe0665742021-02-11 11:08:44 +01001370/**
1371 * @brief Parse an XML element as an opaque node subtree.
1372 *
1373 * @param[in] xmlctx XML parser context.
1374 * @param[in] parent Parent to append nodes to.
1375 * @return LY_ERR value.
1376 */
1377static LY_ERR
1378lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001379{
Michal Vaskoe0665742021-02-11 11:08:44 +01001380 LY_ERR rc = LY_SUCCESS;
1381 const struct lyxml_ns *ns;
1382 struct lyd_attr *attr = NULL;
1383 struct lyd_node *child = NULL;
1384 const char *name, *prefix;
1385 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001386
Michal Vaskoe0665742021-02-11 11:08:44 +01001387 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001388
Michal Vaskoe0665742021-02-11 11:08:44 +01001389 name = xmlctx->name;
1390 name_len = xmlctx->name_len;
1391 prefix = xmlctx->prefix;
1392 prefix_len = xmlctx->prefix_len;
1393 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1394 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001395 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001396 return LY_EVALID;
1397 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001398
Michal Vaskoe0665742021-02-11 11:08:44 +01001399 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001400
Michal Vaskoe0665742021-02-11 11:08:44 +01001401 /* create attributes */
1402 if (xmlctx->status == LYXML_ATTRIBUTE) {
1403 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1404 }
1405
1406 /* create node */
1407 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1408 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 +02001409 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child);
Michal Vaskoe0665742021-02-11 11:08:44 +01001410 LY_CHECK_GOTO(rc, cleanup);
1411
1412 /* assign atributes */
1413 ((struct lyd_node_opaq *)child)->attr = attr;
1414 attr = NULL;
1415
1416 /* parser next element */
1417 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1418
1419 /* parse all the descendants */
1420 while (xmlctx->status == LYXML_ELEMENT) {
1421 rc = lydxml_opaq_r(xmlctx, child);
1422 LY_CHECK_GOTO(rc, cleanup);
1423 }
1424
1425 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001426 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001427
1428cleanup:
1429 lyd_free_attr_siblings(xmlctx->ctx, attr);
1430 if (rc) {
1431 lyd_free_tree(child);
1432 }
1433 return rc;
1434}
1435
1436/**
1437 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1438 *
1439 * @param[in] xmlctx XML parser context.
1440 * @param[in] parent Parent to append nodes to.
1441 * @return LY_ERR value.
1442 */
1443static LY_ERR
1444lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1445{
1446 LY_ERR r;
1447 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001448 ly_bool no_dup;
1449
1450 /* there must be some child */
1451 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1452 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1453 return LY_EVALID;
1454 }
1455
1456 while (xmlctx->status == LYXML_ELEMENT) {
1457 child = NULL;
1458
1459 /*
1460 * session-id
1461 */
1462 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1463 if (r == LY_SUCCESS) {
1464 no_dup = 1;
1465 goto check_child;
1466 } else if (r != LY_ENOT) {
1467 goto error;
1468 }
1469
1470 /*
1471 * bad-attribute
1472 */
1473 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1474 if (r == LY_SUCCESS) {
1475 no_dup = 1;
1476 goto check_child;
1477 } else if (r != LY_ENOT) {
1478 goto error;
1479 }
1480
1481 /*
1482 * bad-element
1483 */
1484 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1485 if (r == LY_SUCCESS) {
1486 no_dup = 1;
1487 goto check_child;
1488 } else if (r != LY_ENOT) {
1489 goto error;
1490 }
1491
1492 /*
1493 * bad-namespace
1494 */
1495 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1496 if (r == LY_SUCCESS) {
1497 no_dup = 1;
1498 goto check_child;
1499 } else if (r != LY_ENOT) {
1500 goto error;
1501 }
1502
1503 if (r == LY_ENOT) {
1504 assert(xmlctx->status == LYXML_ELEMENT);
1505
Michal Vasko845ba392022-06-16 07:52:13 +02001506 /* custom elements, parse all the siblings */
1507 while (xmlctx->status == LYXML_ELEMENT) {
1508 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1509 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1510 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001511 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001512 }
1513
1514check_child:
1515 /* check for duplicates */
1516 if (no_dup) {
1517 LY_LIST_FOR(lyd_child(parent), iter) {
1518 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1519 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1520 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1521 ((struct lyd_node_opaq *)child)->name.name);
1522 r = LY_EVALID;
1523 goto error;
1524 }
1525 }
1526 }
1527
1528 /* finish child parsing */
1529 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1530 assert(xmlctx->status == LYXML_ELEMENT);
1531 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001532 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001533 r = LY_EVALID;
1534 goto error;
1535 }
1536 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1537
1538 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001539 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001540 }
1541
1542 return LY_SUCCESS;
1543
1544error:
1545 lyd_free_tree(child);
1546 return r;
1547}
1548
1549/**
1550 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1551 *
1552 * @param[in] xmlctx XML parser context.
1553 * @param[in] parent Parent to append nodes to.
1554 * @return LY_ERR value.
1555 */
1556static LY_ERR
1557lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1558{
1559 LY_ERR r;
1560 struct lyd_node *child, *iter;
1561 const char *val;
1562 ly_bool no_dup;
1563
1564 /* there must be some child */
1565 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1566 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1567 return LY_EVALID;
1568 }
1569
1570 while (xmlctx->status == LYXML_ELEMENT) {
1571 child = NULL;
1572
1573 /*
1574 * error-type
1575 */
1576 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1577 if (r == LY_SUCCESS) {
1578 val = ((struct lyd_node_opaq *)child)->value;
1579 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1580 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1581 ((struct lyd_node_opaq *)child)->name.name);
1582 r = LY_EVALID;
1583 goto error;
1584 }
1585
1586 no_dup = 1;
1587 goto check_child;
1588 } else if (r != LY_ENOT) {
1589 goto error;
1590 }
1591
1592 /*
1593 * error-tag
1594 */
1595 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1596 if (r == LY_SUCCESS) {
1597 val = ((struct lyd_node_opaq *)child)->value;
1598 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1599 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1600 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1601 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1602 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1603 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1604 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1605 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1606 ((struct lyd_node_opaq *)child)->name.name);
1607 r = LY_EVALID;
1608 goto error;
1609 }
1610
1611 no_dup = 1;
1612 goto check_child;
1613 } else if (r != LY_ENOT) {
1614 goto error;
1615 }
1616
1617 /*
1618 * error-severity
1619 */
1620 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1621 if (r == LY_SUCCESS) {
1622 val = ((struct lyd_node_opaq *)child)->value;
1623 if (strcmp(val, "error") && strcmp(val, "warning")) {
1624 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1625 ((struct lyd_node_opaq *)child)->name.name);
1626 r = LY_EVALID;
1627 goto error;
1628 }
1629
1630 no_dup = 1;
1631 goto check_child;
1632 } else if (r != LY_ENOT) {
1633 goto error;
1634 }
1635
1636 /*
1637 * error-app-tag
1638 */
1639 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1640 if (r == LY_SUCCESS) {
1641 no_dup = 1;
1642 goto check_child;
1643 } else if (r != LY_ENOT) {
1644 goto error;
1645 }
1646
1647 /*
1648 * error-path
1649 */
1650 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1651 if (r == LY_SUCCESS) {
1652 no_dup = 1;
1653 goto check_child;
1654 } else if (r != LY_ENOT) {
1655 goto error;
1656 }
1657
1658 /*
1659 * error-message
1660 */
1661 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1662 if (r == LY_SUCCESS) {
1663 no_dup = 1;
1664 goto check_child;
1665 } else if (r != LY_ENOT) {
1666 goto error;
1667 }
1668
1669 /* error-info */
1670 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1671 if (r == LY_SUCCESS) {
1672 /* parse all the descendants */
1673 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1674
1675 no_dup = 0;
1676 goto check_child;
1677 } else if (r != LY_ENOT) {
1678 goto error;
1679 }
1680
1681 if (r == LY_ENOT) {
1682 assert(xmlctx->status == LYXML_ELEMENT);
1683 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001684 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001685 r = LY_EVALID;
1686 goto error;
1687 }
1688
1689check_child:
1690 /* check for duplicates */
1691 if (no_dup) {
1692 LY_LIST_FOR(lyd_child(parent), iter) {
1693 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1694 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1695 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1696 ((struct lyd_node_opaq *)child)->name.name);
1697 r = LY_EVALID;
1698 goto error;
1699 }
1700 }
1701 }
1702
1703 /* finish child parsing */
1704 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1705 assert(xmlctx->status == LYXML_ELEMENT);
1706 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001707 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001708 r = LY_EVALID;
1709 goto error;
1710 }
1711 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1712
1713 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001714 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001715 }
1716
1717 return LY_SUCCESS;
1718
1719error:
1720 lyd_free_tree(child);
1721 return r;
1722}
1723
1724/**
1725 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1726 *
1727 * @param[in] xmlctx XML parser context.
1728 * @param[out] evnp Parsed envelope(s) (opaque node).
1729 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1730 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1731 * @return LY_SUCCESS on success.
1732 * @return LY_ERR value on error.
1733 */
1734static LY_ERR
1735lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1736{
1737 LY_ERR rc = LY_SUCCESS, r;
1738 struct lyd_node *child = NULL;
1739 const char *parsed_elem = NULL;
1740
1741 assert(envp && !*envp);
1742
1743 /* parse "rpc-reply" */
1744 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001745 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1746
1747 /* there must be some child */
1748 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1749 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1750 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001751 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001752 }
1753
Michal Vaskoe0665742021-02-11 11:08:44 +01001754 /* try to parse "ok" */
1755 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1756 if (r == LY_SUCCESS) {
1757 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001758 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001759
1760 /* finish child parsing */
1761 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1762 assert(xmlctx->status == LYXML_ELEMENT);
1763 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001764 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001765 rc = LY_EVALID;
1766 goto cleanup;
1767 }
1768 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1769
1770 /* success */
1771 parsed_elem = "ok";
1772 goto finish;
1773 } else if (r != LY_ENOT) {
1774 rc = r;
1775 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001776 }
1777
Michal Vaskoe0665742021-02-11 11:08:44 +01001778 /* try to parse all "rpc-error" elements */
1779 while (xmlctx->status == LYXML_ELEMENT) {
1780 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1781 if (r == LY_ENOT) {
1782 break;
1783 } else if (r) {
1784 rc = r;
1785 goto cleanup;
1786 }
1787
1788 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001789 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001790
1791 /* parse all children of "rpc-error" */
1792 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1793
1794 /* finish child parsing */
1795 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1796 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1797
1798 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001799 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001800
1801finish:
1802 if (parsed_elem) {
1803 /* NETCONF rpc-reply with no data */
1804 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1805 assert(xmlctx->status == LYXML_ELEMENT);
1806 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001807 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001808 rc = LY_EVALID;
1809 goto cleanup;
1810 }
1811 }
1812
1813 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001814 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001815 *close_elem = 1;
1816
1817cleanup:
1818 if (rc) {
1819 lyd_free_tree(*envp);
1820 *envp = NULL;
1821 }
1822 return rc;
1823}
1824
Michal Vasko2552ea32020-12-08 15:32:34 +01001825LY_ERR
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001826lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Radek Krejcif16e2542021-02-17 15:39:23 +01001827 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001828 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001829{
Michal Vaskoe0665742021-02-11 11:08:44 +01001830 LY_ERR rc = LY_SUCCESS;
1831 struct lyd_xml_ctx *lydctx;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001832 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001833 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001834
Michal Vaskoe0665742021-02-11 11:08:44 +01001835 assert(ctx && in && lydctx_p);
1836 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1837 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001838 assert(!(parse_opts & LYD_PARSE_SUBTREE));
1839
Michal Vaskoe0665742021-02-11 11:08:44 +01001840 /* init context */
1841 lydctx = calloc(1, sizeof *lydctx);
1842 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1843 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1844 lydctx->parse_opts = parse_opts;
1845 lydctx->val_opts = val_opts;
1846 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001847 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001848
Michal Vaskoe0665742021-02-11 11:08:44 +01001849 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001850 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001851 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001852 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1853 if (rc == LY_ENOT) {
1854 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1855 }
1856 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001857 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001858 case LYD_TYPE_NOTIF_NETCONF:
1859 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001860 rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem);
1861 if (rc == LY_ENOT) {
1862 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1863 }
1864 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001865 break;
1866 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001867 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001868 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1869 if (rc == LY_ENOT) {
1870 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1871 }
1872 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001873 break;
Michal Vasko820efe82023-05-12 15:47:43 +02001874 case LYD_TYPE_RPC_RESTCONF:
1875 assert(parent);
1876
1877 /* parse "input" */
1878 rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_owner_module(parent)->ns, 0, envp);
1879 if (rc == LY_ENOT) {
1880 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace.");
1881 }
1882 LY_CHECK_GOTO(rc, cleanup);
1883
1884 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
1885 close_elem = 1;
1886 break;
1887 case LYD_TYPE_REPLY_RESTCONF:
1888 assert(parent);
1889
1890 /* parse "output" */
1891 rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_owner_module(parent)->ns, 0, envp);
1892 if (rc == LY_ENOT) {
1893 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace.");
1894 }
1895 LY_CHECK_GOTO(rc, cleanup);
1896
1897 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
1898 close_elem = 1;
1899 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001900 default:
1901 LOGINT(ctx);
1902 rc = LY_EINT;
1903 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001904 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001905
Michal Vaskoe0665742021-02-11 11:08:44 +01001906 lydctx->int_opts = int_opts;
1907
1908 /* find the operation node if it exists already */
1909 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1910
1911 /* parse XML data */
1912 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1913 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1914 parsed_data_nodes = 1;
1915
1916 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1917 break;
1918 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001919 }
1920
Michal Vaskoe0665742021-02-11 11:08:44 +01001921 /* close all opened elements */
1922 for (i = 0; i < close_elem; ++i) {
1923 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1924 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001925 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1926 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001927 rc = LY_EVALID;
1928 goto cleanup;
1929 }
1930
1931 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001932 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001933
1934 /* check final state */
1935 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1936 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1937 rc = LY_EVALID;
1938 goto cleanup;
1939 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01001940 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001941 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1942 rc = LY_EVALID;
1943 goto cleanup;
1944 }
1945
1946 if (!parsed_data_nodes) {
1947 /* no data nodes were parsed */
1948 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001949 }
1950
1951cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001952 /* there should be no unres stored if validation should be skipped */
1953 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01001954 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01001955
1956 if (rc) {
1957 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1958 } else {
1959 *lydctx_p = (struct lyd_ctx *)lydctx;
1960
1961 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1962 lyxml_ctx_free(lydctx->xmlctx);
1963 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001964 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001965 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001966}