blob: 0db10cae2411a5f93b27b0f550518d6af178cbc5 [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
Michal Vasko742a5b12022-02-24 16:07:27 +010023#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010025#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010028#include "ly_common.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 Vasko7a266772024-01-23 11:02:38 +010078 LOG_LOCSET(sparent, NULL);
Michal Vasko85be65e2023-06-13 09:44:17 +020079
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 Vasko7a266772024-01-23 11:02:38 +0100168 LOG_LOCBACK(1, 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;
Michal Vasko21eaa392024-02-20 15:48:42 +0100460 if ((num < INT32_MIN) || (num > (long)UINT32_MAX)) {
Michal Vaskocea58712022-04-01 14:37:08 +0200461 /* 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) &&
zhangtaogb348a8b2024-02-21 15:43:30 +0800484 ((ns && opaq->name.module_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 Vaskoa878a892023-08-18 12:22:07 +0200573 if (!parent && lydctx->ext) {
574 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
575 } else {
576 /* try to find parent schema node even if it is an opaque node (not connected to the parent) */
577 *snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts);
578 }
579 if (!*snode) {
580 /* check for extension data */
581 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 */
585 return r;
Radek Krejcif16e2542021-02-17 15:39:23 +0100586 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100587
Michal Vaskoa878a892023-08-18 12:22:07 +0200588 /* unknown data node */
589 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
590 if (parent) {
591 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
592 (int)name_len, name, LYD_NAME(parent));
593 } else if (lydctx->ext) {
594 if (lydctx->ext->argument) {
595 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);
Michal Vaskoe0665742021-02-11 11:08:44 +0100597 } else {
Michal Vaskoa878a892023-08-18 12:22:07 +0200598 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
599 (int)name_len, name, lydctx->ext->def->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100600 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200601 } else {
602 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
603 (int)name_len, name, mod->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200604 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200605 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200606 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200607 return LY_SUCCESS;
608 } else {
609 /* check that schema node is valid and can be used */
610 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
Michal Vasko8cc3f662022-03-29 11:25:51 +0200614 return LY_SUCCESS;
615}
616
617/**
Michal Vaskod027f382023-02-10 09:13:25 +0100618 * @brief Parse an XML opque node.
619 *
620 * @param[in] lydctx XML YANG data parser context.
621 * @param[in] sibling Existing sibling node, if any.
622 * @param[in] prefix Parsed node prefix.
623 * @param[in] prefix_len Length of @p prefix.
624 * @param[in] name Parsed node name.
625 * @param[in] name_len Length of @p name.
626 * @param[out] insert_anchor Optional anchor node for inserting this node.
627 * @param[out] node Created node.
628 * @return LY_ERR value.
629 */
630static LY_ERR
631lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len,
632 const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node)
633{
634 LY_ERR rc = LY_SUCCESS;
635 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200636 struct lyd_node_opaq *opaq;
637 const char *ns_uri, *value = NULL;
638 size_t value_len;
639 ly_bool ws_only, dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100640 const struct lyxml_ns *ns;
641 uint32_t hints;
642 void *val_prefix_data = NULL;
643 LY_VALUE_FORMAT format;
644
645 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
646
647 *node = NULL;
648
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200649 /* remember the value */
650 value = xmlctx->value;
651 value_len = xmlctx->value_len;
652 ws_only = xmlctx->ws_only;
653 dynamic = xmlctx->dynamic;
654 if (dynamic) {
Michal Vaskod027f382023-02-10 09:13:25 +0100655 xmlctx->dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100656 }
657
Michal Vasko535d21c2023-08-09 10:41:44 +0200658 /* get value prefixes, if any */
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200659 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 +0200660 LY_CHECK_GOTO(rc, cleanup);
661
Michal Vaskod027f382023-02-10 09:13:25 +0100662 /* get NS again, it may have been backed up and restored */
663 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
664 ns_uri = ns ? ns->uri : NULL;
665
666 /* get best-effort node hints */
667 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor);
668
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200669 /* create the node without value */
670 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, NULL, 0,
671 NULL, format, NULL, hints, node);
Michal Vaskod027f382023-02-10 09:13:25 +0100672 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100673
Michal Vaskoa878a892023-08-18 12:22:07 +0200674 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100675 LOG_LOCSET(NULL, *node);
Michal Vaskoa878a892023-08-18 12:22:07 +0200676
Michal Vaskod027f382023-02-10 09:13:25 +0100677 /* parser next */
678 rc = lyxml_ctx_next(xmlctx);
679 LY_CHECK_GOTO(rc, cleanup);
680
681 /* process children */
682 while (xmlctx->status == LYXML_ELEMENT) {
683 rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
684 LY_CHECK_GOTO(rc, cleanup);
685 }
686
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200687 /* update the value */
688 opaq = (struct lyd_node_opaq *)*node;
689 if (opaq->child) {
690 if (!ws_only) {
691 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(opaq));
692 rc = LY_EVALID;
693 goto cleanup;
694 }
695 } else if (value_len) {
696 lydict_remove(xmlctx->ctx, opaq->value);
697 if (dynamic) {
698 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
699 dynamic = 0;
700 } else {
701 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
702 }
703 }
704
705 /* always store val_prefix_data because the format requires them */
706 assert(!opaq->val_prefix_data);
707 opaq->val_prefix_data = val_prefix_data;
708 val_prefix_data = NULL;
709
Michal Vaskod027f382023-02-10 09:13:25 +0100710cleanup:
Michal Vaskoa878a892023-08-18 12:22:07 +0200711 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100712 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +0200713 }
Michal Vaskod027f382023-02-10 09:13:25 +0100714 ly_free_prefix_data(format, val_prefix_data);
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200715 if (dynamic) {
716 free((char *)value);
717 }
Michal Vaskod027f382023-02-10 09:13:25 +0100718 if (rc) {
719 lyd_free_tree(*node);
720 *node = NULL;
721 }
722 return rc;
723}
724
725/**
726 * @brief Parse an XML leaf/leaf-list node.
727 *
728 * @param[in] lydctx XML YANG data parser context.
729 * @param[in] parent Parent node, if any.
730 * @param[in] snode Schema node of the new node.
731 * @param[out] node Created node.
732 * @return LY_ERR value.
733 */
734static LY_ERR
735lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode,
736 struct lyd_node **node)
737{
738 LY_ERR r, rc = LY_SUCCESS;
739 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
740 struct lyd_node *anchor;
741
742 *node = NULL;
743
744 /* create node */
745 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
746 LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node);
747 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
748
749 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100750 LOG_LOCSET(NULL, *node);
Michal Vaskod027f382023-02-10 09:13:25 +0100751 }
752
Michal Vasko58a1a702023-03-01 13:45:38 +0100753 if (*node && parent && (snode->flags & LYS_KEY)) {
Michal Vaskod027f382023-02-10 09:13:25 +0100754 /* check the key order, the anchor must never be a key */
755 anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node);
756 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
757 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
758 LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name);
759 r = LY_EVALID;
760 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
761 } else {
762 LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name);
763 }
764 }
765 }
766
767 /* parser next */
768 r = lyxml_ctx_next(xmlctx);
769 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
770
771 /* no children expected */
772 if (xmlctx->status == LYXML_ELEMENT) {
773 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
774 (int)xmlctx->name_len, xmlctx->name, snode->name);
775 r = LY_EVALID;
776 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
777 }
778
779cleanup:
780 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100781 LOG_LOCBACK(0, 1);
Michal Vaskod027f382023-02-10 09:13:25 +0100782 }
783 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
784 lyd_free_tree(*node);
785 *node = NULL;
786 }
787 return rc;
788}
789
790/**
791 * @brief Parse an XML inner node.
792 *
793 * @param[in] lydctx XML YANG data parser context.
794 * @param[in] snode Schema node of the new node.
795 * @param[in] ext Extension instance of @p snode, if any.
796 * @param[out] node Created node.
797 * @return LY_ERR value.
798 */
799static LY_ERR
800lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
801 struct lyd_node **node)
802{
803 LY_ERR r, rc = LY_SUCCESS;
804 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
805 uint32_t prev_parse_opts = lydctx->parse_opts;
806
807 *node = NULL;
808
809 if (!xmlctx->ws_only) {
810 /* value in inner node */
811 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
812 (int)xmlctx->value_len, xmlctx->value, snode->name);
813 r = LY_EVALID;
814 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
815 }
816
817 /* create node */
818 rc = lyd_create_inner(snode, node);
819 LY_CHECK_GOTO(rc, cleanup);
820
Michal Vasko58a1a702023-03-01 13:45:38 +0100821 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100822 LOG_LOCSET(NULL, *node);
Michal Vaskod027f382023-02-10 09:13:25 +0100823
824 /* parser next */
825 rc = lyxml_ctx_next(xmlctx);
826 LY_CHECK_GOTO(rc, cleanup);
827
828 if (ext) {
829 /* only parse these extension data and validate afterwards */
830 lydctx->parse_opts |= LYD_PARSE_ONLY;
831 }
832
833 /* process children */
834 while (xmlctx->status == LYXML_ELEMENT) {
835 r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
836 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
837 }
838
839 /* restore options */
840 lydctx->parse_opts = prev_parse_opts;
841
842 if (snode->nodetype == LYS_LIST) {
843 /* check all keys exist */
844 r = lyd_parse_check_keys(*node);
Michal Vasko202d8162023-03-01 14:42:19 +0100845 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100846 }
847
Michal Vasko40015732023-11-20 13:48:15 +0100848 if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) {
849 /* new node validation, autodelete CANNOT occur (it can if multi-error), all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +0100850 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
851 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
852
853 /* add any missing default children */
854 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
855 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
856 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
857 }
858
859 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
860 /* rememeber the RPC/action/notification */
861 lydctx->op_node = *node;
862 }
863
864cleanup:
865 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100866 LOG_LOCBACK(0, 1);
Michal Vaskod027f382023-02-10 09:13:25 +0100867 }
868 lydctx->parse_opts = prev_parse_opts;
Michal Vasko202d8162023-03-01 14:42:19 +0100869 if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
870 /* list without keys is unusable or an error */
Michal Vaskod027f382023-02-10 09:13:25 +0100871 lyd_free_tree(*node);
872 *node = NULL;
873 }
874 return rc;
875}
876
877/**
878 * @brief Parse an XML anyxml/anydata node.
879 *
880 * @param[in] lydctx XML YANG data parser context.
881 * @param[in] snode Schema node of the new node.
882 * @param[in] ext Extension instance of @p snode, if any.
883 * @param[out] node Created node.
884 * @return LY_ERR value.
885 */
886static LY_ERR
887lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
888 struct lyd_node **node)
889{
890 LY_ERR r, rc = LY_SUCCESS;
891 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
892 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
893 struct lyd_node *child = NULL;
894 char *val = NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +0200895 ly_bool log_node = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100896
897 *node = NULL;
898
899 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
900 /* value in anydata node, we expect a tree */
901 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200902 xmlctx->value_len < 20 ? (int)xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskod027f382023-02-10 09:13:25 +0100903 r = LY_EVALID;
904 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
905 }
906
907 if (!xmlctx->ws_only) {
908 /* use an arbitrary text value for anyxml */
909 val = strndup(xmlctx->value, xmlctx->value_len);
910 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
911
912 /* parser next */
913 r = lyxml_ctx_next(xmlctx);
914 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
915
916 /* create node */
917 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
918 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
919 val = NULL;
920 } else {
Michal Vasko85be65e2023-06-13 09:44:17 +0200921 /* create node */
922 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
923 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
924
925 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100926 LOG_LOCSET(NULL, *node);
Michal Vasko85be65e2023-06-13 09:44:17 +0200927 log_node = 1;
928
Michal Vaskod027f382023-02-10 09:13:25 +0100929 /* parser next */
930 r = lyxml_ctx_next(xmlctx);
931 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
932
933 /* update options so that generic data can be parsed */
934 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
935 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
936 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
937
938 /* parse any data tree */
939 while (xmlctx->status == LYXML_ELEMENT) {
940 r = lydxml_subtree_r(lydctx, NULL, &child, NULL);
941 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
942 }
943
Michal Vasko85be65e2023-06-13 09:44:17 +0200944 /* assign the data tree */
945 ((struct lyd_node_any *)*node)->value.tree = child;
Michal Vaskod027f382023-02-10 09:13:25 +0100946 child = NULL;
947 }
948
949cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +0200950 if (log_node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100951 LOG_LOCBACK(0, 1);
Michal Vasko85be65e2023-06-13 09:44:17 +0200952 }
Michal Vaskod027f382023-02-10 09:13:25 +0100953 lydctx->parse_opts = prev_parse_opts;
954 lydctx->int_opts = prev_int_opts;
955 free(val);
956 lyd_free_tree(child);
957 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
958 lyd_free_tree(*node);
959 *node = NULL;
960 }
961 return rc;
962}
963
964/**
965 * @brief Parse an XML subtree, recursively.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200966 *
967 * @param[in] lydctx XML YANG data parser context.
968 * @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 +0100969 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200970 * @param[in,out] parsed Optional set to add all the parsed siblings into.
971 * @return LY_ERR value.
972 */
973static LY_ERR
974lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
975{
Michal Vaskod027f382023-02-10 09:13:25 +0100976 LY_ERR r, rc = LY_SUCCESS;
977 const char *prefix, *name;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200978 size_t prefix_len, name_len;
979 struct lyxml_ctx *xmlctx;
980 const struct ly_ctx *ctx;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200981 struct lyd_meta *meta = NULL;
982 struct lyd_attr *attr = NULL;
Michal Vasko4a1e3e82023-09-05 08:45:01 +0200983 const struct lysc_node *snode = NULL;
984 struct lysc_ext_instance *ext = NULL;
Michal Vaskod027f382023-02-10 09:13:25 +0100985 uint32_t orig_parse_opts;
986 struct lyd_node *node = NULL, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200987 ly_bool parse_subtree;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200988
989 assert(parent || first_p);
990
991 xmlctx = lydctx->xmlctx;
992 ctx = xmlctx->ctx;
993
994 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
995 /* all descendants should be parsed */
996 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
997 orig_parse_opts = lydctx->parse_opts;
998
999 assert(xmlctx->status == LYXML_ELEMENT);
1000
1001 /* remember element prefix and name */
1002 prefix = xmlctx->prefix;
1003 prefix_len = xmlctx->prefix_len;
1004 name = xmlctx->name;
1005 name_len = xmlctx->name_len;
1006
1007 /* parser next */
Michal Vaskod027f382023-02-10 09:13:25 +01001008 rc = lyxml_ctx_next(xmlctx);
1009 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001010
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001011 if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && name_len && !prefix_len &&
1012 !ly_strncmp("eventTime", name, name_len)) {
1013 /* parse eventTime, create node */
1014 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1015 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len,
1016 "urn:ietf:params:xml:ns:netconf:notification:1.0", 47, xmlctx->value,
1017 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA, &node);
1018 LY_CHECK_GOTO(rc, cleanup);
1019
1020 /* validate the value */
1021 r = lyd_parser_notif_eventtime_validate(node);
1022 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
1023
1024 /* parser next */
1025 r = lyxml_ctx_next(xmlctx);
1026 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
1027 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1028 LOGVAL(ctx, LYVE_DATA, "Unexpected notification \"eventTime\" node children.");
1029 rc = LY_EVALID;
1030 lyd_free_tree(node);
1031 goto cleanup;
1032 }
1033
1034 goto node_parsed;
1035 }
1036
Michal Vasko8cc3f662022-03-29 11:25:51 +02001037 /* get the schema node */
Michal Vasko202d8162023-03-01 14:42:19 +01001038 r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext);
1039 if (r) {
1040 rc = r;
1041 if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1042 /* skip the invalid data */
1043 if ((r = lydxml_data_skip(xmlctx))) {
1044 rc = r;
1045 }
1046 }
1047 goto cleanup;
1048 } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001049 LOGVRB("Skipping parsing of unknown node \"%.*s\".", (int)name_len, name);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001050
1051 /* skip element with children */
Michal Vaskod027f382023-02-10 09:13:25 +01001052 rc = lydxml_data_skip(xmlctx);
1053 goto cleanup;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001054 }
1055
Michal Vaskoa5da3292020-08-12 13:10:50 +02001056 /* create metadata/attributes */
1057 if (xmlctx->status == LYXML_ATTRIBUTE) {
1058 if (snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001059 rc = lydxml_metadata(lydctx, snode, &meta);
1060 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001061 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001062 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskod027f382023-02-10 09:13:25 +01001063 rc = lydxml_attrs(xmlctx, &attr);
1064 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001065 }
1066 }
1067
1068 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1069 if (!snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001070 /* opaque */
1071 r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len,
1072 &insert_anchor, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001073 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskod027f382023-02-10 09:13:25 +01001074 /* term */
1075 r = lydxml_subtree_term(lydctx, parent, snode, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001076 } else if (snode->nodetype & LYD_NODE_INNER) {
Michal Vaskod027f382023-02-10 09:13:25 +01001077 /* inner */
1078 r = lydxml_subtree_inner(lydctx, snode, ext, &node);
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001079 } else {
Michal Vaskod027f382023-02-10 09:13:25 +01001080 /* any */
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001081 assert(snode->nodetype & LYD_NODE_ANY);
Michal Vaskod027f382023-02-10 09:13:25 +01001082 r = lydxml_subtree_any(lydctx, snode, ext, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001083 }
Michal Vaskod027f382023-02-10 09:13:25 +01001084 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko1524f1a2023-03-01 09:36:34 +01001085
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001086node_parsed:
Michal Vasko58a1a702023-03-01 13:45:38 +01001087 if (node && snode) {
Michal Vasko135719f2022-08-25 12:18:17 +02001088 /* add/correct flags */
Michal Vaskod027f382023-02-10 09:13:25 +01001089 r = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko567d7032023-02-13 08:53:31 +01001090 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001091
Michal Vaskoeba23112022-08-26 08:35:41 +02001092 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1093 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001094 r = lyd_validate_node_ext(node, &lydctx->ext_node);
1095 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001096 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001097 }
1098
1099 /* parser next */
1100 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +01001101 if (!parse_subtree) {
Michal Vaskod027f382023-02-10 09:13:25 +01001102 r = lyxml_ctx_next(xmlctx);
Michal Vasko567d7032023-02-13 08:53:31 +01001103 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001104 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001105
Michal Vasko58a1a702023-03-01 13:45:38 +01001106 LY_CHECK_GOTO(!node, cleanup);
1107
Michal Vaskoa5da3292020-08-12 13:10:50 +02001108 /* add metadata/attributes */
1109 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +01001110 lyd_insert_meta(node, meta, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001111 meta = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001112 } else {
1113 lyd_insert_attr(node, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001114 attr = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001115 }
1116
1117 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +02001118 if (insert_anchor) {
1119 lyd_insert_after(insert_anchor, node);
1120 } else if (ext) {
Michal Vaskod027f382023-02-10 09:13:25 +01001121 r = lyplg_ext_insert(parent, node);
Michal Vasko567d7032023-02-13 08:53:31 +01001122 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001123 } else {
aPiecek1462ab12024-02-07 09:13:29 +01001124 lyd_insert_node(parent, first_p, node,
1125 lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001126 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001127 while (!parent && (*first_p)->prev->next) {
1128 *first_p = (*first_p)->prev;
1129 }
1130
Michal Vaskoe0665742021-02-11 11:08:44 +01001131 /* rememeber a successfully parsed node */
1132 if (parsed) {
1133 ly_set_add(parsed, node, 1, NULL);
1134 }
1135
Michal Vaskod027f382023-02-10 09:13:25 +01001136cleanup:
Michal Vasko8cc3f662022-03-29 11:25:51 +02001137 lydctx->parse_opts = orig_parse_opts;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001138 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001139 lyd_free_attr_siblings(ctx, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001140 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +02001141}
1142
Michal Vaskoe0665742021-02-11 11:08:44 +01001143/**
1144 * @brief Parse a specific XML element into an opaque node.
1145 *
1146 * @param[in] xmlctx XML parser context.
1147 * @param[in] name Name of the element.
1148 * @param[in] uri URI of the element.
1149 * @param[in] value Whether a value is expected in the element.
1150 * @param[out] evnp Parsed envelope (opaque node).
1151 * @return LY_SUCCESS on success.
1152 * @return LY_ENOT if the specified element did not match.
1153 * @return LY_ERR value on error.
1154 */
Michal Vasko1bf09392020-03-27 12:38:10 +01001155static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001156lydxml_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 +01001157{
Michal Vaskoe0665742021-02-11 11:08:44 +01001158 LY_ERR rc = LY_SUCCESS;
1159 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +02001160 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +01001161 const char *prefix;
1162 size_t prefix_len;
1163
Michal Vaskof048ba52023-06-13 10:40:43 +02001164 if (xmlctx->status != LYXML_ELEMENT) {
1165 /* nothing to parse */
1166 return LY_ENOT;
1167 }
1168
Michal Vasko1bf09392020-03-27 12:38:10 +01001169 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
1170 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +01001171 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001172 }
1173
1174 prefix = xmlctx->prefix;
1175 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001176 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001177 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001178 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001179 return LY_EVALID;
1180 } else if (strcmp(ns->uri, uri)) {
1181 /* different namespace */
1182 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001183 }
1184
1185 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
1186
1187 /* create attributes */
1188 if (xmlctx->status == LYXML_ATTRIBUTE) {
1189 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1190 }
1191
Michal Vaskoe0665742021-02-11 11:08:44 +01001192 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1193 if (!value && !xmlctx->ws_only) {
1194 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +01001195 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001196 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001197 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001198 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001199
1200 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001201 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001202 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001203 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001204
1205 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001206 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001207 attr = NULL;
1208
Michal Vaskoe0665742021-02-11 11:08:44 +01001209 /* parser next element */
1210 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001211
Michal Vaskoe0665742021-02-11 11:08:44 +01001212cleanup:
1213 lyd_free_attr_siblings(xmlctx->ctx, attr);
1214 if (rc) {
1215 lyd_free_tree(*envp);
1216 *envp = NULL;
1217 }
1218 return rc;
1219}
1220
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001221LY_ERR
1222lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1223 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
1224 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
1225{
Michal Vaskod027f382023-02-10 09:13:25 +01001226 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001227 struct lyd_xml_ctx *lydctx;
Michal Vasko64592692023-06-12 13:50:11 +02001228 ly_bool parsed_data_nodes = 0, close_elem = 0;
1229 struct lyd_node *act = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001230 enum LYXML_PARSER_STATUS status;
1231
1232 assert(ctx && in && lydctx_p);
1233 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1234 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1235
1236 /* init context */
1237 lydctx = calloc(1, sizeof *lydctx);
1238 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1239 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1240 lydctx->parse_opts = parse_opts;
1241 lydctx->val_opts = val_opts;
1242 lydctx->int_opts = int_opts;
1243 lydctx->free = lyd_xml_ctx_free;
1244 lydctx->ext = ext;
1245
1246 /* find the operation node if it exists already */
1247 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1248
Michal Vasko64592692023-06-12 13:50:11 +02001249 if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) {
1250 /* can be either, try to parse "action" */
1251 if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) {
1252 close_elem = 1;
1253 int_opts &= ~LYD_INTOPT_RPC;
1254 }
1255 }
1256
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001257 /* parse XML data */
1258 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
Michal Vaskod027f382023-02-10 09:13:25 +01001259 r = lydxml_subtree_r(lydctx, parent, first_p, parsed);
1260 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1261
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001262 parsed_data_nodes = 1;
1263
1264 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1265 break;
1266 }
1267 }
1268
Michal Vasko64592692023-06-12 13:50:11 +02001269 /* close an opened element */
1270 if (close_elem) {
1271 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1272 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
1273 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1274 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
1275 rc = LY_EVALID;
1276 goto cleanup;
1277 }
1278
1279 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
1280 }
1281
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001282 /* check final state */
1283 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1284 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001285 r = LY_EVALID;
1286 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001287 }
1288 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1289 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001290 r = LY_EVALID;
1291 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001292 }
1293
1294 if (!parsed_data_nodes) {
1295 /* no data nodes were parsed */
1296 lydctx->op_node = NULL;
1297 }
1298
1299 if (parse_opts & LYD_PARSE_SUBTREE) {
1300 /* check for a sibling element */
1301 assert(subtree_sibling);
1302 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1303 *subtree_sibling = 1;
1304 } else {
1305 *subtree_sibling = 0;
1306 }
1307 }
1308
1309cleanup:
1310 /* there should be no unres stored if validation should be skipped */
1311 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1312 !lydctx->node_when.count));
1313
Michal Vasko64592692023-06-12 13:50:11 +02001314 lyd_free_tree(act);
Michal Vaskod027f382023-02-10 09:13:25 +01001315 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001316 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1317 } else {
1318 *lydctx_p = (struct lyd_ctx *)lydctx;
1319
1320 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1321 lyxml_ctx_free(lydctx->xmlctx);
1322 lydctx->xmlctx = NULL;
1323 }
1324 return rc;
1325}
1326
Michal Vaskoe0665742021-02-11 11:08:44 +01001327/**
1328 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
1329 *
1330 * @param[in] xmlctx XML parser context.
1331 * @param[out] evnp Parsed envelope(s) (opaque node).
1332 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1333 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1334 * @return LY_SUCCESS on success.
1335 * @return LY_ERR value on error.
1336 */
1337static LY_ERR
1338lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1339{
1340 LY_ERR rc = LY_SUCCESS, r;
1341 struct lyd_node *child;
1342
1343 assert(envp && !*envp);
1344
1345 /* parse "rpc" */
1346 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001347 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1348
1349 /* parse "action", if any */
1350 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
1351 if (r == LY_SUCCESS) {
1352 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001353 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_DEFAULT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001354
1355 /* NETCONF action */
1356 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1357 *close_elem = 2;
1358 } else if (r == LY_ENOT) {
1359 /* NETCONF RPC */
1360 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1361 *close_elem = 1;
1362 } else {
1363 rc = r;
1364 goto cleanup;
1365 }
1366
1367cleanup:
1368 if (rc) {
1369 lyd_free_tree(*envp);
1370 *envp = NULL;
1371 }
1372 return rc;
1373}
1374
1375/**
Michal Vaskoe0665742021-02-11 11:08:44 +01001376 * @brief Parse an XML element as an opaque node subtree.
1377 *
1378 * @param[in] xmlctx XML parser context.
1379 * @param[in] parent Parent to append nodes to.
1380 * @return LY_ERR value.
1381 */
1382static LY_ERR
1383lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001384{
Michal Vaskoe0665742021-02-11 11:08:44 +01001385 LY_ERR rc = LY_SUCCESS;
1386 const struct lyxml_ns *ns;
1387 struct lyd_attr *attr = NULL;
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001388 struct lyd_node *node = NULL;
1389 struct lyd_node_opaq *opaq;
1390 const char *name, *prefix, *value = NULL;
1391 size_t name_len, prefix_len, value_len;
1392 ly_bool ws_only, dynamic = 0;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001393
Michal Vaskoe0665742021-02-11 11:08:44 +01001394 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001395
Michal Vaskoe0665742021-02-11 11:08:44 +01001396 name = xmlctx->name;
1397 name_len = xmlctx->name_len;
1398 prefix = xmlctx->prefix;
1399 prefix_len = xmlctx->prefix_len;
1400 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1401 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001402 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001403 return LY_EVALID;
1404 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001405
Michal Vaskoe0665742021-02-11 11:08:44 +01001406 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001407
Michal Vaskoe0665742021-02-11 11:08:44 +01001408 /* create attributes */
1409 if (xmlctx->status == LYXML_ATTRIBUTE) {
1410 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1411 }
1412
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001413 /* remember the value */
Michal Vaskoe0665742021-02-11 11:08:44 +01001414 assert(xmlctx->status == LYXML_ELEM_CONTENT);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001415 value = xmlctx->value;
1416 value_len = xmlctx->value_len;
1417 ws_only = xmlctx->ws_only;
1418 dynamic = xmlctx->dynamic;
1419 if (dynamic) {
1420 xmlctx->dynamic = 0;
1421 }
1422
1423 /* create the node without value */
1424 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), NULL, 0, NULL,
1425 LY_VALUE_XML, NULL, 0, &node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001426 LY_CHECK_GOTO(rc, cleanup);
1427
1428 /* assign atributes */
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001429 ((struct lyd_node_opaq *)node)->attr = attr;
Michal Vaskoe0665742021-02-11 11:08:44 +01001430 attr = NULL;
1431
1432 /* parser next element */
1433 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1434
1435 /* parse all the descendants */
1436 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001437 rc = lydxml_opaq_r(xmlctx, node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001438 LY_CHECK_GOTO(rc, cleanup);
1439 }
1440
1441 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001442 lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_LAST);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001443
1444 /* update the value */
1445 opaq = (struct lyd_node_opaq *)node;
1446 if (opaq->child) {
1447 if (!ws_only) {
1448 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(node));
1449 rc = LY_EVALID;
1450 goto cleanup;
1451 }
1452 } else if (value_len) {
1453 lydict_remove(xmlctx->ctx, opaq->value);
1454 if (dynamic) {
1455 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
1456 dynamic = 0;
1457 } else {
1458 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
1459 }
1460 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001461
1462cleanup:
1463 lyd_free_attr_siblings(xmlctx->ctx, attr);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001464 if (dynamic) {
1465 free((char *)value);
1466 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001467 if (rc) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001468 lyd_free_tree(node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001469 }
1470 return rc;
1471}
1472
1473/**
1474 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1475 *
1476 * @param[in] xmlctx XML parser context.
1477 * @param[in] parent Parent to append nodes to.
1478 * @return LY_ERR value.
1479 */
1480static LY_ERR
1481lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1482{
1483 LY_ERR r;
1484 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001485 ly_bool no_dup;
1486
1487 /* there must be some child */
1488 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1489 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1490 return LY_EVALID;
1491 }
1492
1493 while (xmlctx->status == LYXML_ELEMENT) {
1494 child = NULL;
1495
1496 /*
1497 * session-id
1498 */
1499 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1500 if (r == LY_SUCCESS) {
1501 no_dup = 1;
1502 goto check_child;
1503 } else if (r != LY_ENOT) {
1504 goto error;
1505 }
1506
1507 /*
1508 * bad-attribute
1509 */
1510 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1511 if (r == LY_SUCCESS) {
1512 no_dup = 1;
1513 goto check_child;
1514 } else if (r != LY_ENOT) {
1515 goto error;
1516 }
1517
1518 /*
1519 * bad-element
1520 */
1521 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1522 if (r == LY_SUCCESS) {
1523 no_dup = 1;
1524 goto check_child;
1525 } else if (r != LY_ENOT) {
1526 goto error;
1527 }
1528
1529 /*
1530 * bad-namespace
1531 */
1532 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1533 if (r == LY_SUCCESS) {
1534 no_dup = 1;
1535 goto check_child;
1536 } else if (r != LY_ENOT) {
1537 goto error;
1538 }
1539
1540 if (r == LY_ENOT) {
1541 assert(xmlctx->status == LYXML_ELEMENT);
1542
Michal Vasko845ba392022-06-16 07:52:13 +02001543 /* custom elements, parse all the siblings */
1544 while (xmlctx->status == LYXML_ELEMENT) {
1545 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1546 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1547 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001548 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001549 }
1550
1551check_child:
1552 /* check for duplicates */
1553 if (no_dup) {
1554 LY_LIST_FOR(lyd_child(parent), iter) {
1555 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1556 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1557 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1558 ((struct lyd_node_opaq *)child)->name.name);
1559 r = LY_EVALID;
1560 goto error;
1561 }
1562 }
1563 }
1564
1565 /* finish child parsing */
1566 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1567 assert(xmlctx->status == LYXML_ELEMENT);
1568 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001569 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001570 r = LY_EVALID;
1571 goto error;
1572 }
1573 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1574
1575 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001576 lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001577 }
1578
1579 return LY_SUCCESS;
1580
1581error:
1582 lyd_free_tree(child);
1583 return r;
1584}
1585
1586/**
1587 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1588 *
1589 * @param[in] xmlctx XML parser context.
1590 * @param[in] parent Parent to append nodes to.
1591 * @return LY_ERR value.
1592 */
1593static LY_ERR
1594lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1595{
1596 LY_ERR r;
1597 struct lyd_node *child, *iter;
1598 const char *val;
1599 ly_bool no_dup;
1600
1601 /* there must be some child */
1602 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1603 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1604 return LY_EVALID;
1605 }
1606
1607 while (xmlctx->status == LYXML_ELEMENT) {
1608 child = NULL;
1609
1610 /*
1611 * error-type
1612 */
1613 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1614 if (r == LY_SUCCESS) {
1615 val = ((struct lyd_node_opaq *)child)->value;
1616 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1617 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1618 ((struct lyd_node_opaq *)child)->name.name);
1619 r = LY_EVALID;
1620 goto error;
1621 }
1622
1623 no_dup = 1;
1624 goto check_child;
1625 } else if (r != LY_ENOT) {
1626 goto error;
1627 }
1628
1629 /*
1630 * error-tag
1631 */
1632 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1633 if (r == LY_SUCCESS) {
1634 val = ((struct lyd_node_opaq *)child)->value;
1635 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1636 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1637 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1638 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1639 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1640 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1641 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1642 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1643 ((struct lyd_node_opaq *)child)->name.name);
1644 r = LY_EVALID;
1645 goto error;
1646 }
1647
1648 no_dup = 1;
1649 goto check_child;
1650 } else if (r != LY_ENOT) {
1651 goto error;
1652 }
1653
1654 /*
1655 * error-severity
1656 */
1657 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1658 if (r == LY_SUCCESS) {
1659 val = ((struct lyd_node_opaq *)child)->value;
1660 if (strcmp(val, "error") && strcmp(val, "warning")) {
1661 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1662 ((struct lyd_node_opaq *)child)->name.name);
1663 r = LY_EVALID;
1664 goto error;
1665 }
1666
1667 no_dup = 1;
1668 goto check_child;
1669 } else if (r != LY_ENOT) {
1670 goto error;
1671 }
1672
1673 /*
1674 * error-app-tag
1675 */
1676 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1677 if (r == LY_SUCCESS) {
1678 no_dup = 1;
1679 goto check_child;
1680 } else if (r != LY_ENOT) {
1681 goto error;
1682 }
1683
1684 /*
1685 * error-path
1686 */
1687 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1688 if (r == LY_SUCCESS) {
1689 no_dup = 1;
1690 goto check_child;
1691 } else if (r != LY_ENOT) {
1692 goto error;
1693 }
1694
1695 /*
1696 * error-message
1697 */
1698 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1699 if (r == LY_SUCCESS) {
1700 no_dup = 1;
1701 goto check_child;
1702 } else if (r != LY_ENOT) {
1703 goto error;
1704 }
1705
1706 /* error-info */
1707 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1708 if (r == LY_SUCCESS) {
1709 /* parse all the descendants */
1710 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1711
1712 no_dup = 0;
1713 goto check_child;
1714 } else if (r != LY_ENOT) {
1715 goto error;
1716 }
1717
1718 if (r == LY_ENOT) {
1719 assert(xmlctx->status == LYXML_ELEMENT);
1720 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001721 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001722 r = LY_EVALID;
1723 goto error;
1724 }
1725
1726check_child:
1727 /* check for duplicates */
1728 if (no_dup) {
1729 LY_LIST_FOR(lyd_child(parent), iter) {
1730 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1731 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1732 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1733 ((struct lyd_node_opaq *)child)->name.name);
1734 r = LY_EVALID;
1735 goto error;
1736 }
1737 }
1738 }
1739
1740 /* finish child parsing */
1741 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1742 assert(xmlctx->status == LYXML_ELEMENT);
1743 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001744 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001745 r = LY_EVALID;
1746 goto error;
1747 }
1748 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1749
1750 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001751 lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001752 }
1753
1754 return LY_SUCCESS;
1755
1756error:
1757 lyd_free_tree(child);
1758 return r;
1759}
1760
1761/**
1762 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1763 *
1764 * @param[in] xmlctx XML parser context.
1765 * @param[out] evnp Parsed envelope(s) (opaque node).
1766 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1767 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1768 * @return LY_SUCCESS on success.
1769 * @return LY_ERR value on error.
1770 */
1771static LY_ERR
1772lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1773{
1774 LY_ERR rc = LY_SUCCESS, r;
1775 struct lyd_node *child = NULL;
1776 const char *parsed_elem = NULL;
1777
1778 assert(envp && !*envp);
1779
1780 /* parse "rpc-reply" */
1781 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001782 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1783
1784 /* there must be some child */
1785 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1786 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1787 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001788 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001789 }
1790
Michal Vaskoe0665742021-02-11 11:08:44 +01001791 /* try to parse "ok" */
1792 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1793 if (r == LY_SUCCESS) {
1794 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001795 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001796
1797 /* finish child parsing */
1798 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1799 assert(xmlctx->status == LYXML_ELEMENT);
1800 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001801 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001802 rc = LY_EVALID;
1803 goto cleanup;
1804 }
1805 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1806
1807 /* success */
1808 parsed_elem = "ok";
1809 goto finish;
1810 } else if (r != LY_ENOT) {
1811 rc = r;
1812 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001813 }
1814
Michal Vaskoe0665742021-02-11 11:08:44 +01001815 /* try to parse all "rpc-error" elements */
1816 while (xmlctx->status == LYXML_ELEMENT) {
1817 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1818 if (r == LY_ENOT) {
1819 break;
1820 } else if (r) {
1821 rc = r;
1822 goto cleanup;
1823 }
1824
1825 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001826 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001827
1828 /* parse all children of "rpc-error" */
1829 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1830
1831 /* finish child parsing */
1832 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1833 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1834
1835 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001836 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001837
1838finish:
1839 if (parsed_elem) {
1840 /* NETCONF rpc-reply with no data */
1841 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1842 assert(xmlctx->status == LYXML_ELEMENT);
1843 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001844 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001845 rc = LY_EVALID;
1846 goto cleanup;
1847 }
1848 }
1849
1850 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001851 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001852 *close_elem = 1;
1853
1854cleanup:
1855 if (rc) {
1856 lyd_free_tree(*envp);
1857 *envp = NULL;
1858 }
1859 return rc;
1860}
1861
Michal Vasko2552ea32020-12-08 15:32:34 +01001862LY_ERR
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001863lyd_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 +01001864 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 +01001865 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001866{
Michal Vaskoe0665742021-02-11 11:08:44 +01001867 LY_ERR rc = LY_SUCCESS;
1868 struct lyd_xml_ctx *lydctx;
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001869 struct lyd_node *node;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001870 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001871 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001872
Michal Vaskoe0665742021-02-11 11:08:44 +01001873 assert(ctx && in && lydctx_p);
1874 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1875 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001876 assert(!(parse_opts & LYD_PARSE_SUBTREE));
1877
Michal Vaskoe0665742021-02-11 11:08:44 +01001878 /* init context */
1879 lydctx = calloc(1, sizeof *lydctx);
1880 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1881 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1882 lydctx->parse_opts = parse_opts;
1883 lydctx->val_opts = val_opts;
1884 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001885 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001886
Michal Vaskoe0665742021-02-11 11:08:44 +01001887 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001888 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001889 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001890 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1891 if (rc == LY_ENOT) {
1892 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1893 }
1894 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001895 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001896 case LYD_TYPE_NOTIF_NETCONF:
1897 assert(!parent);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001898
1899 /* parse "notification" */
1900 rc = lydxml_envelope(lydctx->xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001901 if (rc == LY_ENOT) {
1902 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1903 }
1904 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001905
1906 /* NETCONF notification */
1907 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME;
1908 close_elem = 1;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001909 break;
1910 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001911 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001912 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1913 if (rc == LY_ENOT) {
1914 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1915 }
1916 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001917 break;
Michal Vasko820efe82023-05-12 15:47:43 +02001918 case LYD_TYPE_RPC_RESTCONF:
1919 assert(parent);
1920
1921 /* parse "input" */
Michal Vasko420cc252023-08-24 08:14:24 +02001922 rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_node_module(parent)->ns, 0, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001923 if (rc == LY_ENOT) {
1924 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace.");
1925 }
1926 LY_CHECK_GOTO(rc, cleanup);
1927
1928 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
1929 close_elem = 1;
1930 break;
1931 case LYD_TYPE_REPLY_RESTCONF:
1932 assert(parent);
1933
1934 /* parse "output" */
Michal Vasko420cc252023-08-24 08:14:24 +02001935 rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_node_module(parent)->ns, 0, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001936 if (rc == LY_ENOT) {
1937 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace.");
1938 }
1939 LY_CHECK_GOTO(rc, cleanup);
1940
1941 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
1942 close_elem = 1;
1943 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001944 default:
1945 LOGINT(ctx);
1946 rc = LY_EINT;
1947 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001948 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001949
Michal Vaskoe0665742021-02-11 11:08:44 +01001950 lydctx->int_opts = int_opts;
1951
1952 /* find the operation node if it exists already */
1953 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1954
1955 /* parse XML data */
1956 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1957 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1958 parsed_data_nodes = 1;
1959
1960 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1961 break;
1962 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001963 }
1964
Michal Vaskoe0665742021-02-11 11:08:44 +01001965 /* close all opened elements */
1966 for (i = 0; i < close_elem; ++i) {
1967 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1968 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001969 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1970 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001971 rc = LY_EVALID;
1972 goto cleanup;
1973 }
1974
1975 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001976 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001977
1978 /* check final state */
1979 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1980 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1981 rc = LY_EVALID;
1982 goto cleanup;
1983 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01001984 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 +01001985 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1986 rc = LY_EVALID;
1987 goto cleanup;
1988 }
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001989 if (int_opts & LYD_INTOPT_EVENTTIME) {
1990 /* parse as a child of the envelope */
1991 node = (*first_p)->prev;
1992 if (node->schema) {
1993 LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node.");
1994 rc = LY_EVALID;
1995 goto cleanup;
1996 } else {
1997 /* can be the only opaque node and an operation had to be parsed */
1998 assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
Michal Vasko2e784f82024-01-11 09:51:22 +01001999 lyd_unlink(node);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002000 assert(*envp);
2001 lyd_insert_child(*envp, node);
2002 }
2003 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002004
2005 if (!parsed_data_nodes) {
2006 /* no data nodes were parsed */
2007 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01002008 }
2009
2010cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01002011 /* there should be no unres stored if validation should be skipped */
2012 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01002013 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01002014
2015 if (rc) {
2016 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
2017 } else {
2018 *lydctx_p = (struct lyd_ctx *)lydctx;
2019
2020 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
2021 lyxml_ctx_free(lydctx->xmlctx);
2022 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002023 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002024 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002025}