blob: b24e9fac2c7916eda174713592b3c6ba80be4195 [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 Vasko7b3a00e2023-08-09 11:58:03 +0200399 LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, (int)xmlctx->value_len,
Michal Vaskod0237d42021-07-12 14:49:46 +0200400 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) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200560 if (ns) {
561 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
562 } else if (prefix_len) {
563 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%.*s\" in the context.", (int)prefix_len, prefix);
564 } else {
565 LOGVAL(ctx, LYVE_REFERENCE, "No default namespace in the context.");
566 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200567 return LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200568 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200569 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200570 }
571
Michal Vaskoa5da3292020-08-12 13:10:50 +0200572 /* get the schema node */
Michal Vasko81008a52021-07-21 16:06:12 +0200573 if (mod) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100574 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200575 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100576 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200577 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100578 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200579 if (!*snode) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100580 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200581 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
582 name_len, snode, ext);
583 if (r != LY_ENOT) {
584 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100585 return r;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100586 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100587
588 /* unknown data node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100589 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
590 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100591 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100592 (int)name_len, name, LYD_NAME(parent));
Radek Krejcif16e2542021-02-17 15:39:23 +0100593 } else if (lydctx->ext) {
594 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100595 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
596 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100597 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100598 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
599 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100600 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100601 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100602 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
603 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100604 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200605 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200606 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200607 return LY_SUCCESS;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200608 } else {
609 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200610 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
611 LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
Michal Vaskoa5da3292020-08-12 13:10:50 +0200612 }
613 }
614
Michal Vasko8cc3f662022-03-29 11:25:51 +0200615 return LY_SUCCESS;
616}
617
618/**
Michal Vaskod027f382023-02-10 09:13:25 +0100619 * @brief Parse an XML opque node.
620 *
621 * @param[in] lydctx XML YANG data parser context.
622 * @param[in] sibling Existing sibling node, if any.
623 * @param[in] prefix Parsed node prefix.
624 * @param[in] prefix_len Length of @p prefix.
625 * @param[in] name Parsed node name.
626 * @param[in] name_len Length of @p name.
627 * @param[out] insert_anchor Optional anchor node for inserting this node.
628 * @param[out] node Created node.
629 * @return LY_ERR value.
630 */
631static LY_ERR
632lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len,
633 const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node)
634{
635 LY_ERR rc = LY_SUCCESS;
636 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200637 struct lyd_node_opaq *opaq;
638 const char *ns_uri, *value = NULL;
639 size_t value_len;
640 ly_bool ws_only, dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100641 const struct lyxml_ns *ns;
642 uint32_t hints;
643 void *val_prefix_data = NULL;
644 LY_VALUE_FORMAT format;
645
646 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
647
648 *node = NULL;
649
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200650 /* remember the value */
651 value = xmlctx->value;
652 value_len = xmlctx->value_len;
653 ws_only = xmlctx->ws_only;
654 dynamic = xmlctx->dynamic;
655 if (dynamic) {
Michal Vaskod027f382023-02-10 09:13:25 +0100656 xmlctx->dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100657 }
658
Michal Vasko535d21c2023-08-09 10:41:44 +0200659 /* get value prefixes, if any */
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200660 rc = ly_store_prefix_data(xmlctx->ctx, value, value_len, LY_VALUE_XML, &xmlctx->ns, &format, &val_prefix_data);
Michal Vasko535d21c2023-08-09 10:41:44 +0200661 LY_CHECK_GOTO(rc, cleanup);
662
Michal Vaskod027f382023-02-10 09:13:25 +0100663 /* get NS again, it may have been backed up and restored */
664 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
665 ns_uri = ns ? ns->uri : NULL;
666
667 /* get best-effort node hints */
668 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor);
669
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200670 /* create the node without value */
671 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, NULL, 0,
672 NULL, format, NULL, hints, node);
Michal Vaskod027f382023-02-10 09:13:25 +0100673 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100674
675 /* parser next */
676 rc = lyxml_ctx_next(xmlctx);
677 LY_CHECK_GOTO(rc, cleanup);
678
679 /* process children */
680 while (xmlctx->status == LYXML_ELEMENT) {
681 rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
682 LY_CHECK_GOTO(rc, cleanup);
683 }
684
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200685 /* update the value */
686 opaq = (struct lyd_node_opaq *)*node;
687 if (opaq->child) {
688 if (!ws_only) {
689 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(opaq));
690 rc = LY_EVALID;
691 goto cleanup;
692 }
693 } else if (value_len) {
694 lydict_remove(xmlctx->ctx, opaq->value);
695 if (dynamic) {
696 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
697 dynamic = 0;
698 } else {
699 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
700 }
701 }
702
703 /* always store val_prefix_data because the format requires them */
704 assert(!opaq->val_prefix_data);
705 opaq->val_prefix_data = val_prefix_data;
706 val_prefix_data = NULL;
707
Michal Vaskod027f382023-02-10 09:13:25 +0100708cleanup:
709 ly_free_prefix_data(format, val_prefix_data);
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200710 if (dynamic) {
711 free((char *)value);
712 }
Michal Vaskod027f382023-02-10 09:13:25 +0100713 if (rc) {
714 lyd_free_tree(*node);
715 *node = NULL;
716 }
717 return rc;
718}
719
720/**
721 * @brief Parse an XML leaf/leaf-list node.
722 *
723 * @param[in] lydctx XML YANG data parser context.
724 * @param[in] parent Parent node, if any.
725 * @param[in] snode Schema node of the new node.
726 * @param[out] node Created node.
727 * @return LY_ERR value.
728 */
729static LY_ERR
730lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode,
731 struct lyd_node **node)
732{
733 LY_ERR r, rc = LY_SUCCESS;
734 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
735 struct lyd_node *anchor;
736
737 *node = NULL;
738
739 /* create node */
740 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
741 LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node);
742 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
743
744 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100745 LOG_LOCSET(NULL, *node, NULL, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +0100746 }
747
Michal Vasko58a1a702023-03-01 13:45:38 +0100748 if (*node && parent && (snode->flags & LYS_KEY)) {
Michal Vaskod027f382023-02-10 09:13:25 +0100749 /* check the key order, the anchor must never be a key */
750 anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node);
751 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
752 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
753 LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name);
754 r = LY_EVALID;
755 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
756 } else {
757 LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name);
758 }
759 }
760 }
761
762 /* parser next */
763 r = lyxml_ctx_next(xmlctx);
764 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
765
766 /* no children expected */
767 if (xmlctx->status == LYXML_ELEMENT) {
768 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
769 (int)xmlctx->name_len, xmlctx->name, snode->name);
770 r = LY_EVALID;
771 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
772 }
773
774cleanup:
775 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100776 LOG_LOCBACK(0, 1, 0, 0);
Michal Vaskod027f382023-02-10 09:13:25 +0100777 }
778 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
779 lyd_free_tree(*node);
780 *node = NULL;
781 }
782 return rc;
783}
784
785/**
786 * @brief Parse an XML inner node.
787 *
788 * @param[in] lydctx XML YANG data parser context.
789 * @param[in] snode Schema node of the new node.
790 * @param[in] ext Extension instance of @p snode, if any.
791 * @param[out] node Created node.
792 * @return LY_ERR value.
793 */
794static LY_ERR
795lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
796 struct lyd_node **node)
797{
798 LY_ERR r, rc = LY_SUCCESS;
799 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
800 uint32_t prev_parse_opts = lydctx->parse_opts;
801
802 *node = NULL;
803
804 if (!xmlctx->ws_only) {
805 /* value in inner node */
806 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
807 (int)xmlctx->value_len, xmlctx->value, snode->name);
808 r = LY_EVALID;
809 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
810 }
811
812 /* create node */
813 rc = lyd_create_inner(snode, node);
814 LY_CHECK_GOTO(rc, cleanup);
815
Michal Vasko58a1a702023-03-01 13:45:38 +0100816 assert(*node);
817 LOG_LOCSET(NULL, *node, NULL, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +0100818
819 /* parser next */
820 rc = lyxml_ctx_next(xmlctx);
821 LY_CHECK_GOTO(rc, cleanup);
822
823 if (ext) {
824 /* only parse these extension data and validate afterwards */
825 lydctx->parse_opts |= LYD_PARSE_ONLY;
826 }
827
828 /* process children */
829 while (xmlctx->status == LYXML_ELEMENT) {
830 r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
831 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
832 }
833
834 /* restore options */
835 lydctx->parse_opts = prev_parse_opts;
836
837 if (snode->nodetype == LYS_LIST) {
838 /* check all keys exist */
839 r = lyd_parse_check_keys(*node);
Michal Vasko202d8162023-03-01 14:42:19 +0100840 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100841 }
842
843 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
844 /* new node validation, autodelete CANNOT occur, all nodes are new */
845 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
846 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
847
848 /* add any missing default children */
849 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
850 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
851 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
852 }
853
854 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
855 /* rememeber the RPC/action/notification */
856 lydctx->op_node = *node;
857 }
858
859cleanup:
860 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100861 LOG_LOCBACK(0, 1, 0, 0);
Michal Vaskod027f382023-02-10 09:13:25 +0100862 }
863 lydctx->parse_opts = prev_parse_opts;
Michal Vasko202d8162023-03-01 14:42:19 +0100864 if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
865 /* list without keys is unusable or an error */
Michal Vaskod027f382023-02-10 09:13:25 +0100866 lyd_free_tree(*node);
867 *node = NULL;
868 }
869 return rc;
870}
871
872/**
873 * @brief Parse an XML anyxml/anydata node.
874 *
875 * @param[in] lydctx XML YANG data parser context.
876 * @param[in] snode Schema node of the new node.
877 * @param[in] ext Extension instance of @p snode, if any.
878 * @param[out] node Created node.
879 * @return LY_ERR value.
880 */
881static LY_ERR
882lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
883 struct lyd_node **node)
884{
885 LY_ERR r, rc = LY_SUCCESS;
886 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
887 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
888 struct lyd_node *child = NULL;
889 char *val = NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +0200890 ly_bool log_node = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100891
892 *node = NULL;
893
894 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
895 /* value in anydata node, we expect a tree */
896 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200897 xmlctx->value_len < 20 ? (int)xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskod027f382023-02-10 09:13:25 +0100898 r = LY_EVALID;
899 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
900 }
901
902 if (!xmlctx->ws_only) {
903 /* use an arbitrary text value for anyxml */
904 val = strndup(xmlctx->value, xmlctx->value_len);
905 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
906
907 /* parser next */
908 r = lyxml_ctx_next(xmlctx);
909 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
910
911 /* create node */
912 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
913 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
914 val = NULL;
915 } else {
Michal Vasko85be65e2023-06-13 09:44:17 +0200916 /* create node */
917 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
918 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
919
920 assert(*node);
921 LOG_LOCSET(NULL, *node, NULL, NULL);
922 log_node = 1;
923
Michal Vaskod027f382023-02-10 09:13:25 +0100924 /* parser next */
925 r = lyxml_ctx_next(xmlctx);
926 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
927
928 /* update options so that generic data can be parsed */
929 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
930 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
931 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
932
933 /* parse any data tree */
934 while (xmlctx->status == LYXML_ELEMENT) {
935 r = lydxml_subtree_r(lydctx, NULL, &child, NULL);
936 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
937 }
938
Michal Vasko85be65e2023-06-13 09:44:17 +0200939 /* assign the data tree */
940 ((struct lyd_node_any *)*node)->value.tree = child;
Michal Vaskod027f382023-02-10 09:13:25 +0100941 child = NULL;
942 }
943
944cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +0200945 if (log_node) {
946 LOG_LOCBACK(0, 1, 0, 0);
947 }
Michal Vaskod027f382023-02-10 09:13:25 +0100948 lydctx->parse_opts = prev_parse_opts;
949 lydctx->int_opts = prev_int_opts;
950 free(val);
951 lyd_free_tree(child);
952 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
953 lyd_free_tree(*node);
954 *node = NULL;
955 }
956 return rc;
957}
958
959/**
960 * @brief Parse an XML subtree, recursively.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200961 *
962 * @param[in] lydctx XML YANG data parser context.
963 * @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 +0100964 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200965 * @param[in,out] parsed Optional set to add all the parsed siblings into.
966 * @return LY_ERR value.
967 */
968static LY_ERR
969lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
970{
Michal Vaskod027f382023-02-10 09:13:25 +0100971 LY_ERR r, rc = LY_SUCCESS;
972 const char *prefix, *name;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200973 size_t prefix_len, name_len;
974 struct lyxml_ctx *xmlctx;
975 const struct ly_ctx *ctx;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200976 struct lyd_meta *meta = NULL;
977 struct lyd_attr *attr = NULL;
978 const struct lysc_node *snode;
979 struct lysc_ext_instance *ext;
Michal Vaskod027f382023-02-10 09:13:25 +0100980 uint32_t orig_parse_opts;
981 struct lyd_node *node = NULL, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200982 ly_bool parse_subtree;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200983
984 assert(parent || first_p);
985
986 xmlctx = lydctx->xmlctx;
987 ctx = xmlctx->ctx;
988
989 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
990 /* all descendants should be parsed */
991 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
992 orig_parse_opts = lydctx->parse_opts;
993
994 assert(xmlctx->status == LYXML_ELEMENT);
995
996 /* remember element prefix and name */
997 prefix = xmlctx->prefix;
998 prefix_len = xmlctx->prefix_len;
999 name = xmlctx->name;
1000 name_len = xmlctx->name_len;
1001
1002 /* parser next */
Michal Vaskod027f382023-02-10 09:13:25 +01001003 rc = lyxml_ctx_next(xmlctx);
1004 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001005
1006 /* get the schema node */
Michal Vasko202d8162023-03-01 14:42:19 +01001007 r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext);
1008 if (r) {
1009 rc = r;
1010 if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1011 /* skip the invalid data */
1012 if ((r = lydxml_data_skip(xmlctx))) {
1013 rc = r;
1014 }
1015 }
1016 goto cleanup;
1017 } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001018 LOGVRB("Skipping parsing of unknown node \"%.*s\".", (int)name_len, name);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001019
1020 /* skip element with children */
Michal Vaskod027f382023-02-10 09:13:25 +01001021 rc = lydxml_data_skip(xmlctx);
1022 goto cleanup;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001023 }
1024
Michal Vaskoa5da3292020-08-12 13:10:50 +02001025 /* create metadata/attributes */
1026 if (xmlctx->status == LYXML_ATTRIBUTE) {
1027 if (snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001028 rc = lydxml_metadata(lydctx, snode, &meta);
1029 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001030 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001031 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskod027f382023-02-10 09:13:25 +01001032 rc = lydxml_attrs(xmlctx, &attr);
1033 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001034 }
1035 }
1036
1037 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1038 if (!snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001039 /* opaque */
1040 r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len,
1041 &insert_anchor, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001042 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskod027f382023-02-10 09:13:25 +01001043 /* term */
1044 r = lydxml_subtree_term(lydctx, parent, snode, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001045 } else if (snode->nodetype & LYD_NODE_INNER) {
Michal Vaskod027f382023-02-10 09:13:25 +01001046 /* inner */
1047 r = lydxml_subtree_inner(lydctx, snode, ext, &node);
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001048 } else {
Michal Vaskod027f382023-02-10 09:13:25 +01001049 /* any */
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001050 assert(snode->nodetype & LYD_NODE_ANY);
Michal Vaskod027f382023-02-10 09:13:25 +01001051 r = lydxml_subtree_any(lydctx, snode, ext, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001052 }
Michal Vaskod027f382023-02-10 09:13:25 +01001053 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko1524f1a2023-03-01 09:36:34 +01001054
Michal Vasko58a1a702023-03-01 13:45:38 +01001055 if (node && snode) {
Michal Vasko135719f2022-08-25 12:18:17 +02001056 /* add/correct flags */
Michal Vaskod027f382023-02-10 09:13:25 +01001057 r = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko567d7032023-02-13 08:53:31 +01001058 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001059
Michal Vaskoeba23112022-08-26 08:35:41 +02001060 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1061 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001062 r = lyd_validate_node_ext(node, &lydctx->ext_node);
1063 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001064 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001065 }
1066
1067 /* parser next */
1068 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +01001069 if (!parse_subtree) {
Michal Vaskod027f382023-02-10 09:13:25 +01001070 r = lyxml_ctx_next(xmlctx);
Michal Vasko567d7032023-02-13 08:53:31 +01001071 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001072 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001073
Michal Vasko58a1a702023-03-01 13:45:38 +01001074 LY_CHECK_GOTO(!node, cleanup);
1075
Michal Vaskoa5da3292020-08-12 13:10:50 +02001076 /* add metadata/attributes */
1077 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +01001078 lyd_insert_meta(node, meta, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001079 meta = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001080 } else {
1081 lyd_insert_attr(node, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001082 attr = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001083 }
1084
1085 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +02001086 if (insert_anchor) {
1087 lyd_insert_after(insert_anchor, node);
1088 } else if (ext) {
Michal Vaskod027f382023-02-10 09:13:25 +01001089 r = lyplg_ext_insert(parent, node);
Michal Vasko567d7032023-02-13 08:53:31 +01001090 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001091 } else {
1092 lyd_insert_node(parent, first_p, node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
1093 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001094 while (!parent && (*first_p)->prev->next) {
1095 *first_p = (*first_p)->prev;
1096 }
1097
Michal Vaskoe0665742021-02-11 11:08:44 +01001098 /* rememeber a successfully parsed node */
1099 if (parsed) {
1100 ly_set_add(parsed, node, 1, NULL);
1101 }
1102
Michal Vaskod027f382023-02-10 09:13:25 +01001103cleanup:
Michal Vasko8cc3f662022-03-29 11:25:51 +02001104 lydctx->parse_opts = orig_parse_opts;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001105 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001106 lyd_free_attr_siblings(ctx, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001107 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +02001108}
1109
Michal Vaskoe0665742021-02-11 11:08:44 +01001110/**
1111 * @brief Parse a specific XML element into an opaque node.
1112 *
1113 * @param[in] xmlctx XML parser context.
1114 * @param[in] name Name of the element.
1115 * @param[in] uri URI of the element.
1116 * @param[in] value Whether a value is expected in the element.
1117 * @param[out] evnp Parsed envelope (opaque node).
1118 * @return LY_SUCCESS on success.
1119 * @return LY_ENOT if the specified element did not match.
1120 * @return LY_ERR value on error.
1121 */
Michal Vasko1bf09392020-03-27 12:38:10 +01001122static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001123lydxml_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 +01001124{
Michal Vaskoe0665742021-02-11 11:08:44 +01001125 LY_ERR rc = LY_SUCCESS;
1126 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +02001127 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +01001128 const char *prefix;
1129 size_t prefix_len;
1130
Michal Vaskof048ba52023-06-13 10:40:43 +02001131 if (xmlctx->status != LYXML_ELEMENT) {
1132 /* nothing to parse */
1133 return LY_ENOT;
1134 }
1135
Michal Vasko1bf09392020-03-27 12:38:10 +01001136 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
1137 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +01001138 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001139 }
1140
1141 prefix = xmlctx->prefix;
1142 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001143 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001144 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001145 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001146 return LY_EVALID;
1147 } else if (strcmp(ns->uri, uri)) {
1148 /* different namespace */
1149 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001150 }
1151
1152 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
1153
1154 /* create attributes */
1155 if (xmlctx->status == LYXML_ATTRIBUTE) {
1156 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1157 }
1158
Michal Vaskoe0665742021-02-11 11:08:44 +01001159 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1160 if (!value && !xmlctx->ws_only) {
1161 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +01001162 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001163 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001164 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001165 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001166
1167 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001168 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001169 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001170 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001171
1172 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001173 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001174 attr = NULL;
1175
Michal Vaskoe0665742021-02-11 11:08:44 +01001176 /* parser next element */
1177 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001178
Michal Vaskoe0665742021-02-11 11:08:44 +01001179cleanup:
1180 lyd_free_attr_siblings(xmlctx->ctx, attr);
1181 if (rc) {
1182 lyd_free_tree(*envp);
1183 *envp = NULL;
1184 }
1185 return rc;
1186}
1187
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001188LY_ERR
1189lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1190 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
1191 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
1192{
Michal Vaskod027f382023-02-10 09:13:25 +01001193 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001194 struct lyd_xml_ctx *lydctx;
Michal Vasko64592692023-06-12 13:50:11 +02001195 ly_bool parsed_data_nodes = 0, close_elem = 0;
1196 struct lyd_node *act = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001197 enum LYXML_PARSER_STATUS status;
1198
1199 assert(ctx && in && lydctx_p);
1200 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1201 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1202
1203 /* init context */
1204 lydctx = calloc(1, sizeof *lydctx);
1205 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1206 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1207 lydctx->parse_opts = parse_opts;
1208 lydctx->val_opts = val_opts;
1209 lydctx->int_opts = int_opts;
1210 lydctx->free = lyd_xml_ctx_free;
1211 lydctx->ext = ext;
1212
1213 /* find the operation node if it exists already */
1214 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1215
Michal Vasko64592692023-06-12 13:50:11 +02001216 if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) {
1217 /* can be either, try to parse "action" */
1218 if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) {
1219 close_elem = 1;
1220 int_opts &= ~LYD_INTOPT_RPC;
1221 }
1222 }
1223
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001224 /* parse XML data */
1225 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
Michal Vaskod027f382023-02-10 09:13:25 +01001226 r = lydxml_subtree_r(lydctx, parent, first_p, parsed);
1227 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1228
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001229 parsed_data_nodes = 1;
1230
1231 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1232 break;
1233 }
1234 }
1235
Michal Vasko64592692023-06-12 13:50:11 +02001236 /* close an opened element */
1237 if (close_elem) {
1238 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1239 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
1240 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1241 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
1242 rc = LY_EVALID;
1243 goto cleanup;
1244 }
1245
1246 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
1247 }
1248
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001249 /* check final state */
1250 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1251 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001252 r = LY_EVALID;
1253 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001254 }
1255 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1256 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001257 r = LY_EVALID;
1258 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001259 }
1260
1261 if (!parsed_data_nodes) {
1262 /* no data nodes were parsed */
1263 lydctx->op_node = NULL;
1264 }
1265
1266 if (parse_opts & LYD_PARSE_SUBTREE) {
1267 /* check for a sibling element */
1268 assert(subtree_sibling);
1269 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1270 *subtree_sibling = 1;
1271 } else {
1272 *subtree_sibling = 0;
1273 }
1274 }
1275
1276cleanup:
1277 /* there should be no unres stored if validation should be skipped */
1278 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1279 !lydctx->node_when.count));
1280
Michal Vasko64592692023-06-12 13:50:11 +02001281 lyd_free_tree(act);
Michal Vaskod027f382023-02-10 09:13:25 +01001282 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001283 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1284 } else {
1285 *lydctx_p = (struct lyd_ctx *)lydctx;
1286
1287 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1288 lyxml_ctx_free(lydctx->xmlctx);
1289 lydctx->xmlctx = NULL;
1290 }
1291 return rc;
1292}
1293
Michal Vaskoe0665742021-02-11 11:08:44 +01001294/**
1295 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
1296 *
1297 * @param[in] xmlctx XML parser context.
1298 * @param[out] evnp Parsed envelope(s) (opaque node).
1299 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1300 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1301 * @return LY_SUCCESS on success.
1302 * @return LY_ERR value on error.
1303 */
1304static LY_ERR
1305lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1306{
1307 LY_ERR rc = LY_SUCCESS, r;
1308 struct lyd_node *child;
1309
1310 assert(envp && !*envp);
1311
1312 /* parse "rpc" */
1313 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001314 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1315
1316 /* parse "action", if any */
1317 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
1318 if (r == LY_SUCCESS) {
1319 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001320 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001321
1322 /* NETCONF action */
1323 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1324 *close_elem = 2;
1325 } else if (r == LY_ENOT) {
1326 /* NETCONF RPC */
1327 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1328 *close_elem = 1;
1329 } else {
1330 rc = r;
1331 goto cleanup;
1332 }
1333
1334cleanup:
1335 if (rc) {
1336 lyd_free_tree(*envp);
1337 *envp = NULL;
1338 }
1339 return rc;
1340}
1341
1342/**
1343 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
1344 *
1345 * @param[in] xmlctx XML parser context.
1346 * @param[out] evnp Parsed envelope(s) (opaque node).
1347 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1348 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1349 * @return LY_SUCCESS on success.
1350 * @return LY_ERR value on error.
1351 */
1352static LY_ERR
1353lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1354{
1355 LY_ERR rc = LY_SUCCESS, r;
1356 struct lyd_node *child;
1357
1358 assert(envp && !*envp);
1359
1360 /* parse "notification" */
1361 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001362 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1363
1364 /* parse "eventTime" */
1365 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
1366 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +01001367 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
1368 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001369 r = LY_EVALID;
1370 }
1371 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1372
1373 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001374 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001375
1376 /* validate value */
Michal Vasko820efe82023-05-12 15:47:43 +02001377 r = lyd_parser_notif_eventtime_validate(child);
Michal Vasko51de7b72022-04-29 09:50:22 +02001378 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001379
1380 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +01001381 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1382 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001383 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001384 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001385 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001386 goto cleanup;
1387 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001388 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1389
1390 /* NETCONF notification */
1391 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
1392 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001393
1394cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001395 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +01001396 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001397 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001398 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001399 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001400}
Michal Vasko79135ae2020-12-16 10:08:35 +01001401
Michal Vaskoe0665742021-02-11 11:08:44 +01001402/**
1403 * @brief Parse an XML element as an opaque node subtree.
1404 *
1405 * @param[in] xmlctx XML parser context.
1406 * @param[in] parent Parent to append nodes to.
1407 * @return LY_ERR value.
1408 */
1409static LY_ERR
1410lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001411{
Michal Vaskoe0665742021-02-11 11:08:44 +01001412 LY_ERR rc = LY_SUCCESS;
1413 const struct lyxml_ns *ns;
1414 struct lyd_attr *attr = NULL;
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001415 struct lyd_node *node = NULL;
1416 struct lyd_node_opaq *opaq;
1417 const char *name, *prefix, *value = NULL;
1418 size_t name_len, prefix_len, value_len;
1419 ly_bool ws_only, dynamic = 0;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001420
Michal Vaskoe0665742021-02-11 11:08:44 +01001421 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001422
Michal Vaskoe0665742021-02-11 11:08:44 +01001423 name = xmlctx->name;
1424 name_len = xmlctx->name_len;
1425 prefix = xmlctx->prefix;
1426 prefix_len = xmlctx->prefix_len;
1427 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1428 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001429 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001430 return LY_EVALID;
1431 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001432
Michal Vaskoe0665742021-02-11 11:08:44 +01001433 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001434
Michal Vaskoe0665742021-02-11 11:08:44 +01001435 /* create attributes */
1436 if (xmlctx->status == LYXML_ATTRIBUTE) {
1437 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1438 }
1439
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001440 /* remember the value */
Michal Vaskoe0665742021-02-11 11:08:44 +01001441 assert(xmlctx->status == LYXML_ELEM_CONTENT);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001442 value = xmlctx->value;
1443 value_len = xmlctx->value_len;
1444 ws_only = xmlctx->ws_only;
1445 dynamic = xmlctx->dynamic;
1446 if (dynamic) {
1447 xmlctx->dynamic = 0;
1448 }
1449
1450 /* create the node without value */
1451 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), NULL, 0, NULL,
1452 LY_VALUE_XML, NULL, 0, &node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001453 LY_CHECK_GOTO(rc, cleanup);
1454
1455 /* assign atributes */
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001456 ((struct lyd_node_opaq *)node)->attr = attr;
Michal Vaskoe0665742021-02-11 11:08:44 +01001457 attr = NULL;
1458
1459 /* parser next element */
1460 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1461
1462 /* parse all the descendants */
1463 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001464 rc = lydxml_opaq_r(xmlctx, node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001465 LY_CHECK_GOTO(rc, cleanup);
1466 }
1467
1468 /* insert */
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001469 lyd_insert_node(parent, NULL, node, 1);
1470
1471 /* update the value */
1472 opaq = (struct lyd_node_opaq *)node;
1473 if (opaq->child) {
1474 if (!ws_only) {
1475 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(node));
1476 rc = LY_EVALID;
1477 goto cleanup;
1478 }
1479 } else if (value_len) {
1480 lydict_remove(xmlctx->ctx, opaq->value);
1481 if (dynamic) {
1482 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
1483 dynamic = 0;
1484 } else {
1485 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
1486 }
1487 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001488
1489cleanup:
1490 lyd_free_attr_siblings(xmlctx->ctx, attr);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001491 if (dynamic) {
1492 free((char *)value);
1493 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001494 if (rc) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001495 lyd_free_tree(node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001496 }
1497 return rc;
1498}
1499
1500/**
1501 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1502 *
1503 * @param[in] xmlctx XML parser context.
1504 * @param[in] parent Parent to append nodes to.
1505 * @return LY_ERR value.
1506 */
1507static LY_ERR
1508lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1509{
1510 LY_ERR r;
1511 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001512 ly_bool no_dup;
1513
1514 /* there must be some child */
1515 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1516 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1517 return LY_EVALID;
1518 }
1519
1520 while (xmlctx->status == LYXML_ELEMENT) {
1521 child = NULL;
1522
1523 /*
1524 * session-id
1525 */
1526 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1527 if (r == LY_SUCCESS) {
1528 no_dup = 1;
1529 goto check_child;
1530 } else if (r != LY_ENOT) {
1531 goto error;
1532 }
1533
1534 /*
1535 * bad-attribute
1536 */
1537 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1538 if (r == LY_SUCCESS) {
1539 no_dup = 1;
1540 goto check_child;
1541 } else if (r != LY_ENOT) {
1542 goto error;
1543 }
1544
1545 /*
1546 * bad-element
1547 */
1548 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1549 if (r == LY_SUCCESS) {
1550 no_dup = 1;
1551 goto check_child;
1552 } else if (r != LY_ENOT) {
1553 goto error;
1554 }
1555
1556 /*
1557 * bad-namespace
1558 */
1559 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1560 if (r == LY_SUCCESS) {
1561 no_dup = 1;
1562 goto check_child;
1563 } else if (r != LY_ENOT) {
1564 goto error;
1565 }
1566
1567 if (r == LY_ENOT) {
1568 assert(xmlctx->status == LYXML_ELEMENT);
1569
Michal Vasko845ba392022-06-16 07:52:13 +02001570 /* custom elements, parse all the siblings */
1571 while (xmlctx->status == LYXML_ELEMENT) {
1572 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1573 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1574 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001575 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001576 }
1577
1578check_child:
1579 /* check for duplicates */
1580 if (no_dup) {
1581 LY_LIST_FOR(lyd_child(parent), iter) {
1582 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1583 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1584 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1585 ((struct lyd_node_opaq *)child)->name.name);
1586 r = LY_EVALID;
1587 goto error;
1588 }
1589 }
1590 }
1591
1592 /* finish child parsing */
1593 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1594 assert(xmlctx->status == LYXML_ELEMENT);
1595 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001596 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001597 r = LY_EVALID;
1598 goto error;
1599 }
1600 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1601
1602 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001603 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001604 }
1605
1606 return LY_SUCCESS;
1607
1608error:
1609 lyd_free_tree(child);
1610 return r;
1611}
1612
1613/**
1614 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1615 *
1616 * @param[in] xmlctx XML parser context.
1617 * @param[in] parent Parent to append nodes to.
1618 * @return LY_ERR value.
1619 */
1620static LY_ERR
1621lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1622{
1623 LY_ERR r;
1624 struct lyd_node *child, *iter;
1625 const char *val;
1626 ly_bool no_dup;
1627
1628 /* there must be some child */
1629 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1630 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1631 return LY_EVALID;
1632 }
1633
1634 while (xmlctx->status == LYXML_ELEMENT) {
1635 child = NULL;
1636
1637 /*
1638 * error-type
1639 */
1640 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1641 if (r == LY_SUCCESS) {
1642 val = ((struct lyd_node_opaq *)child)->value;
1643 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1644 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1645 ((struct lyd_node_opaq *)child)->name.name);
1646 r = LY_EVALID;
1647 goto error;
1648 }
1649
1650 no_dup = 1;
1651 goto check_child;
1652 } else if (r != LY_ENOT) {
1653 goto error;
1654 }
1655
1656 /*
1657 * error-tag
1658 */
1659 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1660 if (r == LY_SUCCESS) {
1661 val = ((struct lyd_node_opaq *)child)->value;
1662 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1663 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1664 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1665 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1666 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1667 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1668 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1669 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1670 ((struct lyd_node_opaq *)child)->name.name);
1671 r = LY_EVALID;
1672 goto error;
1673 }
1674
1675 no_dup = 1;
1676 goto check_child;
1677 } else if (r != LY_ENOT) {
1678 goto error;
1679 }
1680
1681 /*
1682 * error-severity
1683 */
1684 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1685 if (r == LY_SUCCESS) {
1686 val = ((struct lyd_node_opaq *)child)->value;
1687 if (strcmp(val, "error") && strcmp(val, "warning")) {
1688 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1689 ((struct lyd_node_opaq *)child)->name.name);
1690 r = LY_EVALID;
1691 goto error;
1692 }
1693
1694 no_dup = 1;
1695 goto check_child;
1696 } else if (r != LY_ENOT) {
1697 goto error;
1698 }
1699
1700 /*
1701 * error-app-tag
1702 */
1703 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1704 if (r == LY_SUCCESS) {
1705 no_dup = 1;
1706 goto check_child;
1707 } else if (r != LY_ENOT) {
1708 goto error;
1709 }
1710
1711 /*
1712 * error-path
1713 */
1714 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1715 if (r == LY_SUCCESS) {
1716 no_dup = 1;
1717 goto check_child;
1718 } else if (r != LY_ENOT) {
1719 goto error;
1720 }
1721
1722 /*
1723 * error-message
1724 */
1725 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1726 if (r == LY_SUCCESS) {
1727 no_dup = 1;
1728 goto check_child;
1729 } else if (r != LY_ENOT) {
1730 goto error;
1731 }
1732
1733 /* error-info */
1734 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1735 if (r == LY_SUCCESS) {
1736 /* parse all the descendants */
1737 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1738
1739 no_dup = 0;
1740 goto check_child;
1741 } else if (r != LY_ENOT) {
1742 goto error;
1743 }
1744
1745 if (r == LY_ENOT) {
1746 assert(xmlctx->status == LYXML_ELEMENT);
1747 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001748 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001749 r = LY_EVALID;
1750 goto error;
1751 }
1752
1753check_child:
1754 /* check for duplicates */
1755 if (no_dup) {
1756 LY_LIST_FOR(lyd_child(parent), iter) {
1757 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1758 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1759 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1760 ((struct lyd_node_opaq *)child)->name.name);
1761 r = LY_EVALID;
1762 goto error;
1763 }
1764 }
1765 }
1766
1767 /* finish child parsing */
1768 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1769 assert(xmlctx->status == LYXML_ELEMENT);
1770 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001771 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001772 r = LY_EVALID;
1773 goto error;
1774 }
1775 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1776
1777 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001778 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001779 }
1780
1781 return LY_SUCCESS;
1782
1783error:
1784 lyd_free_tree(child);
1785 return r;
1786}
1787
1788/**
1789 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1790 *
1791 * @param[in] xmlctx XML parser context.
1792 * @param[out] evnp Parsed envelope(s) (opaque node).
1793 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1794 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1795 * @return LY_SUCCESS on success.
1796 * @return LY_ERR value on error.
1797 */
1798static LY_ERR
1799lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1800{
1801 LY_ERR rc = LY_SUCCESS, r;
1802 struct lyd_node *child = NULL;
1803 const char *parsed_elem = NULL;
1804
1805 assert(envp && !*envp);
1806
1807 /* parse "rpc-reply" */
1808 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001809 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1810
1811 /* there must be some child */
1812 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1813 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1814 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001815 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001816 }
1817
Michal Vaskoe0665742021-02-11 11:08:44 +01001818 /* try to parse "ok" */
1819 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1820 if (r == LY_SUCCESS) {
1821 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001822 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001823
1824 /* finish child parsing */
1825 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1826 assert(xmlctx->status == LYXML_ELEMENT);
1827 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001828 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001829 rc = LY_EVALID;
1830 goto cleanup;
1831 }
1832 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1833
1834 /* success */
1835 parsed_elem = "ok";
1836 goto finish;
1837 } else if (r != LY_ENOT) {
1838 rc = r;
1839 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001840 }
1841
Michal Vaskoe0665742021-02-11 11:08:44 +01001842 /* try to parse all "rpc-error" elements */
1843 while (xmlctx->status == LYXML_ELEMENT) {
1844 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1845 if (r == LY_ENOT) {
1846 break;
1847 } else if (r) {
1848 rc = r;
1849 goto cleanup;
1850 }
1851
1852 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001853 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001854
1855 /* parse all children of "rpc-error" */
1856 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1857
1858 /* finish child parsing */
1859 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1860 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1861
1862 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001863 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001864
1865finish:
1866 if (parsed_elem) {
1867 /* NETCONF rpc-reply with no data */
1868 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1869 assert(xmlctx->status == LYXML_ELEMENT);
1870 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001871 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001872 rc = LY_EVALID;
1873 goto cleanup;
1874 }
1875 }
1876
1877 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001878 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001879 *close_elem = 1;
1880
1881cleanup:
1882 if (rc) {
1883 lyd_free_tree(*envp);
1884 *envp = NULL;
1885 }
1886 return rc;
1887}
1888
Michal Vasko2552ea32020-12-08 15:32:34 +01001889LY_ERR
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001890lyd_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 +01001891 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 +01001892 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001893{
Michal Vaskoe0665742021-02-11 11:08:44 +01001894 LY_ERR rc = LY_SUCCESS;
1895 struct lyd_xml_ctx *lydctx;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001896 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001897 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001898
Michal Vaskoe0665742021-02-11 11:08:44 +01001899 assert(ctx && in && lydctx_p);
1900 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1901 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001902 assert(!(parse_opts & LYD_PARSE_SUBTREE));
1903
Michal Vaskoe0665742021-02-11 11:08:44 +01001904 /* init context */
1905 lydctx = calloc(1, sizeof *lydctx);
1906 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1907 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1908 lydctx->parse_opts = parse_opts;
1909 lydctx->val_opts = val_opts;
1910 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001911 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001912
Michal Vaskoe0665742021-02-11 11:08:44 +01001913 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001914 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001915 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001916 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1917 if (rc == LY_ENOT) {
1918 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1919 }
1920 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001921 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001922 case LYD_TYPE_NOTIF_NETCONF:
1923 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001924 rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem);
1925 if (rc == LY_ENOT) {
1926 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1927 }
1928 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001929 break;
1930 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001931 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001932 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1933 if (rc == LY_ENOT) {
1934 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1935 }
1936 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001937 break;
Michal Vasko820efe82023-05-12 15:47:43 +02001938 case LYD_TYPE_RPC_RESTCONF:
1939 assert(parent);
1940
1941 /* parse "input" */
1942 rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_owner_module(parent)->ns, 0, envp);
1943 if (rc == LY_ENOT) {
1944 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace.");
1945 }
1946 LY_CHECK_GOTO(rc, cleanup);
1947
1948 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
1949 close_elem = 1;
1950 break;
1951 case LYD_TYPE_REPLY_RESTCONF:
1952 assert(parent);
1953
1954 /* parse "output" */
1955 rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_owner_module(parent)->ns, 0, envp);
1956 if (rc == LY_ENOT) {
1957 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace.");
1958 }
1959 LY_CHECK_GOTO(rc, cleanup);
1960
1961 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
1962 close_elem = 1;
1963 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001964 default:
1965 LOGINT(ctx);
1966 rc = LY_EINT;
1967 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001968 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001969
Michal Vaskoe0665742021-02-11 11:08:44 +01001970 lydctx->int_opts = int_opts;
1971
1972 /* find the operation node if it exists already */
1973 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1974
1975 /* parse XML data */
1976 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1977 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1978 parsed_data_nodes = 1;
1979
1980 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1981 break;
1982 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001983 }
1984
Michal Vaskoe0665742021-02-11 11:08:44 +01001985 /* close all opened elements */
1986 for (i = 0; i < close_elem; ++i) {
1987 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1988 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001989 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1990 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001991 rc = LY_EVALID;
1992 goto cleanup;
1993 }
1994
1995 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001996 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001997
1998 /* check final state */
1999 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
2000 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
2001 rc = LY_EVALID;
2002 goto cleanup;
2003 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01002004 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 +01002005 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
2006 rc = LY_EVALID;
2007 goto cleanup;
2008 }
2009
2010 if (!parsed_data_nodes) {
2011 /* no data nodes were parsed */
2012 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01002013 }
2014
2015cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01002016 /* there should be no unres stored if validation should be skipped */
2017 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01002018 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01002019
2020 if (rc) {
2021 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
2022 } else {
2023 *lydctx_p = (struct lyd_ctx *)lydctx;
2024
2025 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
2026 lyxml_ctx_free(lydctx->xmlctx);
2027 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002028 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002029 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002030}