blob: 0f86a684bdb42784505778ab13d6b298efdc4b79 [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 Vasko45791ad2021-06-17 08:45:03 +020078 /* check for NETCONF filter unqualified attributes */
Michal Vasko1b2a3f42022-12-20 09:38:28 +010079 if (!strcmp(sparent->module->name, "notifications")) {
80 /* ancient module that does not even use the extension */
81 filter_attrs = 1;
82 } else {
83 LY_ARRAY_FOR(sparent->exts, u) {
84 if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") &&
85 !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) {
86 filter_attrs = 1;
87 break;
88 }
Michal Vasko45791ad2021-06-17 08:45:03 +020089 }
90 }
91
Michal Vaskob36053d2020-03-26 15:49:30 +010092 while (xmlctx->status == LYXML_ATTRIBUTE) {
93 if (!xmlctx->prefix_len) {
Michal Vasko45791ad2021-06-17 08:45:03 +020094 /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */
95 if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) ||
96 !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) {
97 mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf");
98 if (!mod) {
99 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
100 "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes.");
101 ret = LY_ENOTFOUND;
102 goto cleanup;
103 }
104 goto create_meta;
105 }
106
Michal Vaskoe0665742021-02-11 11:08:44 +0100107 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100108 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100109 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200110 ret = LY_EVALID;
Michal Vaskob36053d2020-03-26 15:49:30 +0100111 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200112 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100113
Michal Vasko45791ad2021-06-17 08:45:03 +0200114 /* skip attr */
Michal Vaskob36053d2020-03-26 15:49:30 +0100115 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
116 assert(xmlctx->status == LYXML_ATTR_CONTENT);
117 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200118 continue;
119 }
120
121 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200122 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200123 if (!ns) {
Michal Vasko52927e22020-03-16 17:26:14 +0100124 /* unknown namespace, XML error */
Radek Krejci422afb12021-03-04 16:38:16 +0100125 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko45791ad2021-06-17 08:45:03 +0200126 ret = LY_ENOTFOUND;
Radek Krejci28681fa2019-09-06 13:08:45 +0200127 goto cleanup;
128 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200129
130 /* get the module with metadata definition */
Michal Vasko52927e22020-03-16 17:26:14 +0100131 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200132 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100133 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100134 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200135 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100136 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
137 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200138 ret = LY_ENOTFOUND;
Michal Vaskob36053d2020-03-26 15:49:30 +0100139 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200140 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200141
142 /* skip attr */
143 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
144 assert(xmlctx->status == LYXML_ATTR_CONTENT);
145 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
146 continue;
Radek Krejci28681fa2019-09-06 13:08:45 +0200147 }
148
Michal Vasko45791ad2021-06-17 08:45:03 +0200149create_meta:
Michal Vasko60ea6352020-06-29 13:39:39 +0200150 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100151 name = xmlctx->name;
152 name_len = xmlctx->name_len;
153 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
154 assert(xmlctx->status == LYXML_ATTR_CONTENT);
155
156 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200157 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100158 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200159 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100160
161 /* next attribute */
162 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200163 }
Michal Vasko52927e22020-03-16 17:26:14 +0100164
Radek Krejcie7b95092019-05-15 11:03:07 +0200165cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100166 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200167 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100168 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200169 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200170 return ret;
171}
172
Michal Vasko52927e22020-03-16 17:26:14 +0100173static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200174lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100175{
176 LY_ERR ret = LY_SUCCESS;
177 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100178 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200179 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200180 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100181 const char *name, *prefix;
182 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100183
184 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100185 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100186
Michal Vaskob36053d2020-03-26 15:49:30 +0100187 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100188 if (*attr) {
189 attr2 = *attr;
190 } else {
191 attr2 = NULL;
192 }
193
Michal Vaskob36053d2020-03-26 15:49:30 +0100194 /* remember attr prefix, name, and get its content */
195 prefix = xmlctx->prefix;
196 prefix_len = xmlctx->prefix_len;
197 name = xmlctx->name;
198 name_len = xmlctx->name_len;
199 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
200 assert(xmlctx->status == LYXML_ATTR_CONTENT);
201
Michal Vaskoe137fc42021-07-22 11:53:13 +0200202 /* handle special "xml" attribute prefix */
203 if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) {
204 name = prefix;
205 name_len += 1 + prefix_len;
206 prefix = NULL;
207 prefix_len = 0;
208 }
209
210 /* find namespace of the attribute, if any */
211 ns = NULL;
212 if (prefix_len) {
213 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
214 if (!ns) {
215 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
216 ret = LY_EVALID;
217 goto cleanup;
218 }
219 }
220
Michal Vasko52927e22020-03-16 17:26:14 +0100221 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100222 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200223 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 +0100224 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100225
226 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100227 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 +0100228 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data,
229 LYD_HINT_DATA);
Michal Vasko52927e22020-03-16 17:26:14 +0100230 LY_CHECK_GOTO(ret, cleanup);
231
232 if (!*attr) {
233 *attr = attr2;
234 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100235
236 /* next attribute */
237 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100238 }
239
240cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100241 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200242 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100243 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100244 }
Michal Vasko52927e22020-03-16 17:26:14 +0100245 return ret;
246}
247
Michal Vasko44685da2020-03-17 15:38:06 +0100248static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100249lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100250{
Michal Vaskob36053d2020-03-26 15:49:30 +0100251 LY_ERR ret = LY_SUCCESS, r;
252 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100253 struct ly_set key_set = {0};
254 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100255 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100256
257 assert(list && (list->nodetype == LYS_LIST));
258
259 /* get all keys into a set (keys do not have if-features or anything) */
260 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100261 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200262 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200263 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100264 }
265
Michal Vasko12d809c2021-03-03 16:34:32 +0100266 /* remember parent count */
267 parents_count = xmlctx->elements.count;
268
Michal Vaskob36053d2020-03-26 15:49:30 +0100269 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100270 /* find key definition */
271 for (i = 0; i < key_set.count; ++i) {
272 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100273 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100274 break;
275 }
276 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100277 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100278
279 /* skip attributes */
280 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100281 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
282 assert(xmlctx->status == LYXML_ATTR_CONTENT);
283 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100284 }
285
Michal Vaskob36053d2020-03-26 15:49:30 +0100286 assert(xmlctx->status == LYXML_ELEM_CONTENT);
287 if (i < key_set.count) {
288 /* validate the value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200289 r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns);
Michal Vaskob36053d2020-03-26 15:49:30 +0100290 if (!r) {
291 /* key with a valid value, remove from the set */
292 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100293 }
294 }
295
Michal Vaskob36053d2020-03-26 15:49:30 +0100296 /* parser next */
297 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100298
Michal Vaskob36053d2020-03-26 15:49:30 +0100299 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100300 while (xmlctx->status == LYXML_ELEMENT) {
301 while (parents_count < xmlctx->elements.count) {
302 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
303 }
304 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100305 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
306 }
307
308 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
309 assert(xmlctx->status == LYXML_ELEM_CLOSE);
310 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
311 if (next != LYXML_ELEM_CLOSE) {
312 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
313 }
Michal Vasko44685da2020-03-17 15:38:06 +0100314 }
315
316 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100317 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100318 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100319 }
320
321cleanup:
322 ly_set_erase(&key_set, NULL);
323 return ret;
324}
325
Michal Vasko5c24ed12021-06-09 09:27:32 +0200326/**
327 * @brief Skip an element with all its descendants.
328 *
329 * @param[in] xmlctx XML parser context.
330 * @return LY_ERR value.
331 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100332static LY_ERR
333lydxml_data_skip(struct lyxml_ctx *xmlctx)
334{
335 uint32_t parents_count;
336
337 /* remember current number of parents */
338 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200339 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100340
341 /* skip after the content */
342 while (xmlctx->status != LYXML_ELEM_CONTENT) {
343 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
344 }
345 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
346
347 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200348 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100349 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
350 }
351
352 /* close element */
353 assert(xmlctx->status == LYXML_ELEM_CLOSE);
354 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
355
356 return LY_SUCCESS;
357}
358
Michal Vasko5c24ed12021-06-09 09:27:32 +0200359/**
360 * @brief Check that the current element can be parsed as a data node.
361 *
362 * @param[in] lydctx XML data parser context.
363 * @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
364 * @return LY_ERR value.
365 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100366static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200367lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100368{
369 LY_ERR ret = LY_SUCCESS;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200370 struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx;
Michal Vasko1bf09392020-03-27 12:38:10 +0100371
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100372 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
373 /* only checks specific to opaque nodes */
374 return LY_SUCCESS;
375 }
376
Michal Vasko13854662021-06-09 09:27:50 +0200377 if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) {
378 /* nothing to check */
379 return LY_SUCCESS;
380 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100381
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200382 assert(xmlctx->elements.count);
383
Michal Vasko13854662021-06-09 09:27:50 +0200384 /* backup parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200385 LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx));
Michal Vasko1bf09392020-03-27 12:38:10 +0100386
Michal Vasko13854662021-06-09 09:27:50 +0200387 /* skip attributes */
388 while (xmlctx->status == LYXML_ATTRIBUTE) {
389 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
390 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
391 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100392
Michal Vasko13854662021-06-09 09:27:50 +0200393 if ((*snode)->nodetype & LYD_NODE_TERM) {
394 /* value may not be valid in which case we parse it as an opaque node */
395 if (lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns)) {
Michal Vaskod0237d42021-07-12 14:49:46 +0200396 LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, xmlctx->value_len,
397 xmlctx->value);
Michal Vasko13854662021-06-09 09:27:50 +0200398 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100399 }
Michal Vasko13854662021-06-09 09:27:50 +0200400 } else if ((*snode)->nodetype == LYS_LIST) {
401 /* skip content */
402 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
Michal Vasko1bf09392020-03-27 12:38:10 +0100403
Michal Vasko13854662021-06-09 09:27:50 +0200404 if (lydxml_check_list(xmlctx, *snode)) {
405 /* invalid list, parse as opaque if it missing/has invalid some keys */
Michal Vaskod0237d42021-07-12 14:49:46 +0200406 LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name);
Michal Vasko13854662021-06-09 09:27:50 +0200407 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100408 }
Michal Vasko13854662021-06-09 09:27:50 +0200409 } else {
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100410 /* if there is a non-WS value, it cannot be parsed as an inner node */
411 assert(xmlctx->status == LYXML_ELEM_CONTENT);
412 if (!xmlctx->ws_only) {
413 *snode = NULL;
414 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100415 }
416
Michal Vasko13854662021-06-09 09:27:50 +0200417restore:
418 /* restore parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200419 lyxml_ctx_restore(xmlctx, &pxmlctx);
Michal Vasko1bf09392020-03-27 12:38:10 +0100420 return ret;
421}
422
Radek Krejcie7b95092019-05-15 11:03:07 +0200423/**
Michal Vaskocea58712022-04-01 14:37:08 +0200424 * @brief Get sensible data hints for an opaque node.
425 *
426 * @param[in] name Node name.
427 * @param[in] name_len Length of @p name.
428 * @param[in] value Node value.
429 * @param[in] value_len Length of @p value.
430 * @param[in] first Node first sibling.
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100431 * @param[in] ns Node module namespace, NULL for no namespace.
Michal Vaskocea58712022-04-01 14:37:08 +0200432 * @param[out] hints Data hints to use.
433 * @param[out] anchor Anchor to insert after in case of a list.
434 */
435static void
Michal Vaskod027f382023-02-10 09:13:25 +0100436lydxml_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 +0200437 const char *ns, uint32_t *hints, struct lyd_node **anchor)
438{
439 struct lyd_node_opaq *opaq;
440 char *ptr;
441 long num;
442
443 *hints = 0;
444 *anchor = NULL;
445
446 if (!value_len) {
447 /* no value */
448 *hints |= LYD_VALHINT_EMPTY;
449 } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) {
450 /* boolean value */
451 *hints |= LYD_VALHINT_BOOLEAN;
452 } else {
453 num = strtol(value, &ptr, 10);
454 if ((unsigned)(ptr - value) == value_len) {
455 /* number value */
456 *hints |= LYD_VALHINT_DECNUM;
457 if ((num < INT32_MIN) || (num > UINT32_MAX)) {
458 /* large number */
459 *hints |= LYD_VALHINT_NUM64;
460 }
461 } else {
462 /* string value */
463 *hints |= LYD_VALHINT_STRING;
464 }
465 }
466
467 if (!first) {
468 return;
469 }
470
471 /* search backwards to find the last instance */
472 do {
473 first = first->prev;
474 if (first->schema) {
475 continue;
476 }
477
478 opaq = (struct lyd_node_opaq *)first;
479 assert(opaq->format == LY_VALUE_XML);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100480 if (!ly_strncmp(opaq->name.name, name, name_len) &&
481 ((ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) {
Michal Vaskocea58712022-04-01 14:37:08 +0200482 if (opaq->value && opaq->value[0]) {
483 /* leaf-list nodes */
484 opaq->hints |= LYD_NODEHINT_LEAFLIST;
485 *hints |= LYD_NODEHINT_LEAFLIST;
486 } else {
487 /* list nodes */
488 opaq->hints |= LYD_NODEHINT_LIST;
489 *hints |= LYD_NODEHINT_LIST;
490 }
Michal Vaskod027f382023-02-10 09:13:25 +0100491 *anchor = (struct lyd_node *)first;
Michal Vaskocea58712022-04-01 14:37:08 +0200492 break;
493 }
494 } while (first->prev->next);
495}
496
497/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200498 * @brief Get schema node for the current element.
Michal Vaskoddd76592022-01-17 13:34:48 +0100499 *
500 * @param[in] lydctx XML data parser context.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200501 * @param[in] parent Parsed parent data node, if any.
502 * @param[in] prefix Element prefix, if any.
503 * @param[in] prefix_len Length of @p prefix.
504 * @param[in] name Element name.
505 * @param[in] name_len Length of @p name.
506 * @param[out] snode Found schema node, NULL if no suitable was found.
507 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vaskoddd76592022-01-17 13:34:48 +0100508 * @return LY_SUCCESS on success;
Michal Vaskoddd76592022-01-17 13:34:48 +0100509 * @return LY_ERR on error.
510 */
511static LY_ERR
Michal Vaskod027f382023-02-10 09:13:25 +0100512lydxml_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 +0200513 const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vaskoddd76592022-01-17 13:34:48 +0100514{
515 LY_ERR r;
Michal Vaskob36053d2020-03-26 15:49:30 +0100516 struct lyxml_ctx *xmlctx;
517 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200518 const struct lyxml_ns *ns;
Radek Krejcie7b95092019-05-15 11:03:07 +0200519 struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200520 uint32_t getnext_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +0100521
Michal Vaskob36053d2020-03-26 15:49:30 +0100522 xmlctx = lydctx->xmlctx;
523 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100524 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100525
Michal Vasko8cc3f662022-03-29 11:25:51 +0200526 *snode = NULL;
527 *ext = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100528
Michal Vasko8cc3f662022-03-29 11:25:51 +0200529 /* get current namespace */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200530 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200531 if (!ns) {
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100532 if (lydctx->int_opts & LYD_INTOPT_ANY) {
533 goto unknown_module;
534 }
535
536 if (prefix_len) {
537 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
538 } else {
539 LOGVAL(ctx, LYVE_REFERENCE, "Missing XML namespace.");
540 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200541 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200542 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200543
544 /* get the element module, use parent context if possible because of extensions */
545 mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200546 if (!mod) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100547 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200548 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len,
549 snode, ext);
550 if (r != LY_ENOT) {
551 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100552 return r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100553 }
554
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100555unknown_module:
Michal Vaskoe0665742021-02-11 11:08:44 +0100556 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100557 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200558 return LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200559 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200560 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200561 }
562
Michal Vaskoa5da3292020-08-12 13:10:50 +0200563 /* get the schema node */
Michal Vasko81008a52021-07-21 16:06:12 +0200564 if (mod) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100565 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200566 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100567 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200568 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100569 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200570 if (!*snode) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100571 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200572 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
573 name_len, snode, ext);
574 if (r != LY_ENOT) {
575 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100576 return r;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100577 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100578
579 /* unknown data node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100580 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
581 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100582 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100583 (int)name_len, name, LYD_NAME(parent));
Radek Krejcif16e2542021-02-17 15:39:23 +0100584 } else if (lydctx->ext) {
585 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100586 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
587 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100588 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100589 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
590 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100591 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100592 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100593 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
594 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100595 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200596 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200597 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200598 return LY_SUCCESS;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200599 } else {
600 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200601 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
602 LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
Michal Vaskoa5da3292020-08-12 13:10:50 +0200603 }
604 }
605
Michal Vasko8cc3f662022-03-29 11:25:51 +0200606 return LY_SUCCESS;
607}
608
609/**
Michal Vaskod027f382023-02-10 09:13:25 +0100610 * @brief Parse an XML opque node.
611 *
612 * @param[in] lydctx XML YANG data parser context.
613 * @param[in] sibling Existing sibling node, if any.
614 * @param[in] prefix Parsed node prefix.
615 * @param[in] prefix_len Length of @p prefix.
616 * @param[in] name Parsed node name.
617 * @param[in] name_len Length of @p name.
618 * @param[out] insert_anchor Optional anchor node for inserting this node.
619 * @param[out] node Created node.
620 * @return LY_ERR value.
621 */
622static LY_ERR
623lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len,
624 const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node)
625{
626 LY_ERR rc = LY_SUCCESS;
627 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
628 const char *ns_uri;
629 const struct lyxml_ns *ns;
630 uint32_t hints;
631 void *val_prefix_data = NULL;
632 LY_VALUE_FORMAT format;
633
634 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
635
636 *node = NULL;
637
638 if (xmlctx->ws_only) {
639 /* ignore WS-only value */
640 if (xmlctx->dynamic) {
641 free((char *)xmlctx->value);
642 }
643 xmlctx->dynamic = 0;
644 xmlctx->value = "";
645 xmlctx->value_len = 0;
646 format = LY_VALUE_XML;
647 } else {
648 /* get value prefixes */
649 rc = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, &format,
650 &val_prefix_data);
651 LY_CHECK_GOTO(rc, cleanup);
652 }
653
654 /* get NS again, it may have been backed up and restored */
655 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
656 ns_uri = ns ? ns->uri : NULL;
657
658 /* get best-effort node hints */
659 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor);
660
661 /* create node */
662 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0,
663 xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, hints, node);
664 LY_CHECK_GOTO(rc, cleanup);
665 val_prefix_data = NULL;
666
667 /* parser next */
668 rc = lyxml_ctx_next(xmlctx);
669 LY_CHECK_GOTO(rc, cleanup);
670
671 /* process children */
672 while (xmlctx->status == LYXML_ELEMENT) {
673 rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
674 LY_CHECK_GOTO(rc, cleanup);
675 }
676
677cleanup:
678 ly_free_prefix_data(format, val_prefix_data);
679 if (rc) {
680 lyd_free_tree(*node);
681 *node = NULL;
682 }
683 return rc;
684}
685
686/**
687 * @brief Parse an XML leaf/leaf-list node.
688 *
689 * @param[in] lydctx XML YANG data parser context.
690 * @param[in] parent Parent node, if any.
691 * @param[in] snode Schema node of the new node.
692 * @param[out] node Created node.
693 * @return LY_ERR value.
694 */
695static LY_ERR
696lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode,
697 struct lyd_node **node)
698{
699 LY_ERR r, rc = LY_SUCCESS;
700 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
701 struct lyd_node *anchor;
702
703 *node = NULL;
704
705 /* create node */
706 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
707 LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node);
708 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
709
710 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100711 LOG_LOCSET(NULL, *node, NULL, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +0100712 }
713
Michal Vasko58a1a702023-03-01 13:45:38 +0100714 if (*node && parent && (snode->flags & LYS_KEY)) {
Michal Vaskod027f382023-02-10 09:13:25 +0100715 /* check the key order, the anchor must never be a key */
716 anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node);
717 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
718 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
719 LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name);
720 r = LY_EVALID;
721 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
722 } else {
723 LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name);
724 }
725 }
726 }
727
728 /* parser next */
729 r = lyxml_ctx_next(xmlctx);
730 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
731
732 /* no children expected */
733 if (xmlctx->status == LYXML_ELEMENT) {
734 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
735 (int)xmlctx->name_len, xmlctx->name, snode->name);
736 r = LY_EVALID;
737 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
738 }
739
740cleanup:
741 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100742 LOG_LOCBACK(0, 1, 0, 0);
Michal Vaskod027f382023-02-10 09:13:25 +0100743 }
744 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
745 lyd_free_tree(*node);
746 *node = NULL;
747 }
748 return rc;
749}
750
751/**
752 * @brief Parse an XML inner node.
753 *
754 * @param[in] lydctx XML YANG data parser context.
755 * @param[in] snode Schema node of the new node.
756 * @param[in] ext Extension instance of @p snode, if any.
757 * @param[out] node Created node.
758 * @return LY_ERR value.
759 */
760static LY_ERR
761lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
762 struct lyd_node **node)
763{
764 LY_ERR r, rc = LY_SUCCESS;
765 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
766 uint32_t prev_parse_opts = lydctx->parse_opts;
767
768 *node = NULL;
769
770 if (!xmlctx->ws_only) {
771 /* value in inner node */
772 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
773 (int)xmlctx->value_len, xmlctx->value, snode->name);
774 r = LY_EVALID;
775 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
776 }
777
778 /* create node */
779 rc = lyd_create_inner(snode, node);
780 LY_CHECK_GOTO(rc, cleanup);
781
Michal Vasko58a1a702023-03-01 13:45:38 +0100782 assert(*node);
783 LOG_LOCSET(NULL, *node, NULL, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +0100784
785 /* parser next */
786 rc = lyxml_ctx_next(xmlctx);
787 LY_CHECK_GOTO(rc, cleanup);
788
789 if (ext) {
790 /* only parse these extension data and validate afterwards */
791 lydctx->parse_opts |= LYD_PARSE_ONLY;
792 }
793
794 /* process children */
795 while (xmlctx->status == LYXML_ELEMENT) {
796 r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
797 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
798 }
799
800 /* restore options */
801 lydctx->parse_opts = prev_parse_opts;
802
803 if (snode->nodetype == LYS_LIST) {
804 /* check all keys exist */
805 r = lyd_parse_check_keys(*node);
Michal Vasko202d8162023-03-01 14:42:19 +0100806 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100807 }
808
809 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
810 /* new node validation, autodelete CANNOT occur, all nodes are new */
811 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
812 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
813
814 /* add any missing default children */
815 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
816 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
817 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
818 }
819
820 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
821 /* rememeber the RPC/action/notification */
822 lydctx->op_node = *node;
823 }
824
825cleanup:
826 if (*node) {
Michal Vasko58a1a702023-03-01 13:45:38 +0100827 LOG_LOCBACK(0, 1, 0, 0);
Michal Vaskod027f382023-02-10 09:13:25 +0100828 }
829 lydctx->parse_opts = prev_parse_opts;
Michal Vasko202d8162023-03-01 14:42:19 +0100830 if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
831 /* list without keys is unusable or an error */
Michal Vaskod027f382023-02-10 09:13:25 +0100832 lyd_free_tree(*node);
833 *node = NULL;
834 }
835 return rc;
836}
837
838/**
839 * @brief Parse an XML anyxml/anydata node.
840 *
841 * @param[in] lydctx XML YANG data parser context.
842 * @param[in] snode Schema node of the new node.
843 * @param[in] ext Extension instance of @p snode, if any.
844 * @param[out] node Created node.
845 * @return LY_ERR value.
846 */
847static LY_ERR
848lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
849 struct lyd_node **node)
850{
851 LY_ERR r, rc = LY_SUCCESS;
852 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
853 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
854 struct lyd_node *child = NULL;
855 char *val = NULL;
856
857 *node = NULL;
858
859 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
860 /* value in anydata node, we expect a tree */
861 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
862 (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name);
863 r = LY_EVALID;
864 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
865 }
866
867 if (!xmlctx->ws_only) {
868 /* use an arbitrary text value for anyxml */
869 val = strndup(xmlctx->value, xmlctx->value_len);
870 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
871
872 /* parser next */
873 r = lyxml_ctx_next(xmlctx);
874 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
875
876 /* create node */
877 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
878 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
879 val = NULL;
880 } else {
881 /* parser next */
882 r = lyxml_ctx_next(xmlctx);
883 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
884
885 /* update options so that generic data can be parsed */
886 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
887 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
888 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
889
890 /* parse any data tree */
891 while (xmlctx->status == LYXML_ELEMENT) {
892 r = lydxml_subtree_r(lydctx, NULL, &child, NULL);
893 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
894 }
895
896 /* restore options */
897 lydctx->parse_opts = prev_parse_opts;
898 lydctx->int_opts = prev_int_opts;
899
900 /* create node */
901 r = lyd_create_any(snode, child, LYD_ANYDATA_DATATREE, 1, node);
902 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
903 child = NULL;
904 }
905
906cleanup:
907 lydctx->parse_opts = prev_parse_opts;
908 lydctx->int_opts = prev_int_opts;
909 free(val);
910 lyd_free_tree(child);
911 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
912 lyd_free_tree(*node);
913 *node = NULL;
914 }
915 return rc;
916}
917
918/**
919 * @brief Parse an XML subtree, recursively.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200920 *
921 * @param[in] lydctx XML YANG data parser context.
922 * @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 +0100923 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200924 * @param[in,out] parsed Optional set to add all the parsed siblings into.
925 * @return LY_ERR value.
926 */
927static LY_ERR
928lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
929{
Michal Vaskod027f382023-02-10 09:13:25 +0100930 LY_ERR r, rc = LY_SUCCESS;
931 const char *prefix, *name;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200932 size_t prefix_len, name_len;
933 struct lyxml_ctx *xmlctx;
934 const struct ly_ctx *ctx;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200935 struct lyd_meta *meta = NULL;
936 struct lyd_attr *attr = NULL;
937 const struct lysc_node *snode;
938 struct lysc_ext_instance *ext;
Michal Vaskod027f382023-02-10 09:13:25 +0100939 uint32_t orig_parse_opts;
940 struct lyd_node *node = NULL, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200941 ly_bool parse_subtree;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200942
943 assert(parent || first_p);
944
945 xmlctx = lydctx->xmlctx;
946 ctx = xmlctx->ctx;
947
948 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
949 /* all descendants should be parsed */
950 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
951 orig_parse_opts = lydctx->parse_opts;
952
953 assert(xmlctx->status == LYXML_ELEMENT);
954
955 /* remember element prefix and name */
956 prefix = xmlctx->prefix;
957 prefix_len = xmlctx->prefix_len;
958 name = xmlctx->name;
959 name_len = xmlctx->name_len;
960
961 /* parser next */
Michal Vaskod027f382023-02-10 09:13:25 +0100962 rc = lyxml_ctx_next(xmlctx);
963 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200964
965 /* get the schema node */
Michal Vasko202d8162023-03-01 14:42:19 +0100966 r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext);
967 if (r) {
968 rc = r;
969 if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
970 /* skip the invalid data */
971 if ((r = lydxml_data_skip(xmlctx))) {
972 rc = r;
973 }
974 }
975 goto cleanup;
976 } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200977 LOGVRB("Skipping parsing of unknown node \"%.*s\".", name_len, name);
978
979 /* skip element with children */
Michal Vaskod027f382023-02-10 09:13:25 +0100980 rc = lydxml_data_skip(xmlctx);
981 goto cleanup;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200982 }
983
Michal Vaskoa5da3292020-08-12 13:10:50 +0200984 /* create metadata/attributes */
985 if (xmlctx->status == LYXML_ATTRIBUTE) {
986 if (snode) {
Michal Vaskod027f382023-02-10 09:13:25 +0100987 rc = lydxml_metadata(lydctx, snode, &meta);
988 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200989 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100990 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskod027f382023-02-10 09:13:25 +0100991 rc = lydxml_attrs(xmlctx, &attr);
992 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200993 }
994 }
995
996 assert(xmlctx->status == LYXML_ELEM_CONTENT);
997 if (!snode) {
Michal Vaskod027f382023-02-10 09:13:25 +0100998 /* opaque */
999 r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len,
1000 &insert_anchor, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001001 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskod027f382023-02-10 09:13:25 +01001002 /* term */
1003 r = lydxml_subtree_term(lydctx, parent, snode, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001004 } else if (snode->nodetype & LYD_NODE_INNER) {
Michal Vaskod027f382023-02-10 09:13:25 +01001005 /* inner */
1006 r = lydxml_subtree_inner(lydctx, snode, ext, &node);
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001007 } else {
Michal Vaskod027f382023-02-10 09:13:25 +01001008 /* any */
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001009 assert(snode->nodetype & LYD_NODE_ANY);
Michal Vaskod027f382023-02-10 09:13:25 +01001010 r = lydxml_subtree_any(lydctx, snode, ext, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001011 }
Michal Vaskod027f382023-02-10 09:13:25 +01001012 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko1524f1a2023-03-01 09:36:34 +01001013
Michal Vasko58a1a702023-03-01 13:45:38 +01001014 if (node && snode) {
Michal Vasko135719f2022-08-25 12:18:17 +02001015 /* add/correct flags */
Michal Vaskod027f382023-02-10 09:13:25 +01001016 r = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko567d7032023-02-13 08:53:31 +01001017 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001018
Michal Vaskoeba23112022-08-26 08:35:41 +02001019 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1020 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001021 r = lyd_validate_node_ext(node, &lydctx->ext_node);
1022 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001023 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001024 }
1025
1026 /* parser next */
1027 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +01001028 if (!parse_subtree) {
Michal Vaskod027f382023-02-10 09:13:25 +01001029 r = lyxml_ctx_next(xmlctx);
Michal Vasko567d7032023-02-13 08:53:31 +01001030 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001031 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001032
Michal Vasko58a1a702023-03-01 13:45:38 +01001033 LY_CHECK_GOTO(!node, cleanup);
1034
Michal Vaskoa5da3292020-08-12 13:10:50 +02001035 /* add metadata/attributes */
1036 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +01001037 lyd_insert_meta(node, meta, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001038 meta = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001039 } else {
1040 lyd_insert_attr(node, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001041 attr = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001042 }
1043
1044 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +02001045 if (insert_anchor) {
1046 lyd_insert_after(insert_anchor, node);
1047 } else if (ext) {
Michal Vaskod027f382023-02-10 09:13:25 +01001048 r = lyplg_ext_insert(parent, node);
Michal Vasko567d7032023-02-13 08:53:31 +01001049 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001050 } else {
1051 lyd_insert_node(parent, first_p, node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
1052 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001053 while (!parent && (*first_p)->prev->next) {
1054 *first_p = (*first_p)->prev;
1055 }
1056
Michal Vaskoe0665742021-02-11 11:08:44 +01001057 /* rememeber a successfully parsed node */
1058 if (parsed) {
1059 ly_set_add(parsed, node, 1, NULL);
1060 }
1061
Michal Vaskod027f382023-02-10 09:13:25 +01001062cleanup:
Michal Vasko8cc3f662022-03-29 11:25:51 +02001063 lydctx->parse_opts = orig_parse_opts;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001064 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001065 lyd_free_attr_siblings(ctx, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001066 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +02001067}
1068
Michal Vaskoe0665742021-02-11 11:08:44 +01001069/**
1070 * @brief Parse a specific XML element into an opaque node.
1071 *
1072 * @param[in] xmlctx XML parser context.
1073 * @param[in] name Name of the element.
1074 * @param[in] uri URI of the element.
1075 * @param[in] value Whether a value is expected in the element.
1076 * @param[out] evnp Parsed envelope (opaque node).
1077 * @return LY_SUCCESS on success.
1078 * @return LY_ENOT if the specified element did not match.
1079 * @return LY_ERR value on error.
1080 */
Michal Vasko1bf09392020-03-27 12:38:10 +01001081static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001082lydxml_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 +01001083{
Michal Vaskoe0665742021-02-11 11:08:44 +01001084 LY_ERR rc = LY_SUCCESS;
1085 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +02001086 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +01001087 const char *prefix;
1088 size_t prefix_len;
1089
Michal Vasko1bf09392020-03-27 12:38:10 +01001090 assert(xmlctx->status == LYXML_ELEMENT);
1091 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
1092 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +01001093 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001094 }
1095
1096 prefix = xmlctx->prefix;
1097 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001098 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001099 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001100 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001101 return LY_EVALID;
1102 } else if (strcmp(ns->uri, uri)) {
1103 /* different namespace */
1104 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001105 }
1106
1107 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
1108
1109 /* create attributes */
1110 if (xmlctx->status == LYXML_ATTRIBUTE) {
1111 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1112 }
1113
Michal Vaskoe0665742021-02-11 11:08:44 +01001114 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1115 if (!value && !xmlctx->ws_only) {
1116 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +01001117 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001118 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001119 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001120 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001121
1122 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001123 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001124 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001125 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001126
1127 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001128 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001129 attr = NULL;
1130
Michal Vaskoe0665742021-02-11 11:08:44 +01001131 /* parser next element */
1132 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001133
Michal Vaskoe0665742021-02-11 11:08:44 +01001134cleanup:
1135 lyd_free_attr_siblings(xmlctx->ctx, attr);
1136 if (rc) {
1137 lyd_free_tree(*envp);
1138 *envp = NULL;
1139 }
1140 return rc;
1141}
1142
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001143LY_ERR
1144lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1145 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
1146 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
1147{
Michal Vaskod027f382023-02-10 09:13:25 +01001148 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001149 struct lyd_xml_ctx *lydctx;
1150 ly_bool parsed_data_nodes = 0;
1151 enum LYXML_PARSER_STATUS status;
1152
1153 assert(ctx && in && lydctx_p);
1154 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1155 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1156
1157 /* init context */
1158 lydctx = calloc(1, sizeof *lydctx);
1159 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1160 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1161 lydctx->parse_opts = parse_opts;
1162 lydctx->val_opts = val_opts;
1163 lydctx->int_opts = int_opts;
1164 lydctx->free = lyd_xml_ctx_free;
1165 lydctx->ext = ext;
1166
1167 /* find the operation node if it exists already */
1168 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1169
1170 /* parse XML data */
1171 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
Michal Vaskod027f382023-02-10 09:13:25 +01001172 r = lydxml_subtree_r(lydctx, parent, first_p, parsed);
1173 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1174
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001175 parsed_data_nodes = 1;
1176
1177 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1178 break;
1179 }
1180 }
1181
1182 /* check final state */
1183 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1184 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001185 r = LY_EVALID;
1186 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001187 }
1188 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1189 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001190 r = LY_EVALID;
1191 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001192 }
1193
1194 if (!parsed_data_nodes) {
1195 /* no data nodes were parsed */
1196 lydctx->op_node = NULL;
1197 }
1198
1199 if (parse_opts & LYD_PARSE_SUBTREE) {
1200 /* check for a sibling element */
1201 assert(subtree_sibling);
1202 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1203 *subtree_sibling = 1;
1204 } else {
1205 *subtree_sibling = 0;
1206 }
1207 }
1208
1209cleanup:
1210 /* there should be no unres stored if validation should be skipped */
1211 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1212 !lydctx->node_when.count));
1213
Michal Vaskod027f382023-02-10 09:13:25 +01001214 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001215 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1216 } else {
1217 *lydctx_p = (struct lyd_ctx *)lydctx;
1218
1219 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1220 lyxml_ctx_free(lydctx->xmlctx);
1221 lydctx->xmlctx = NULL;
1222 }
1223 return rc;
1224}
1225
Michal Vaskoe0665742021-02-11 11:08:44 +01001226/**
1227 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
1228 *
1229 * @param[in] xmlctx XML parser context.
1230 * @param[out] evnp Parsed envelope(s) (opaque node).
1231 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1232 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1233 * @return LY_SUCCESS on success.
1234 * @return LY_ERR value on error.
1235 */
1236static LY_ERR
1237lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1238{
1239 LY_ERR rc = LY_SUCCESS, r;
1240 struct lyd_node *child;
1241
1242 assert(envp && !*envp);
1243
Michal Vasko50375272022-04-26 12:07:46 +02001244 if (xmlctx->status != LYXML_ELEMENT) {
1245 /* nothing to parse */
1246 assert(xmlctx->status == LYXML_END);
1247 goto cleanup;
1248 }
1249
Michal Vaskoe0665742021-02-11 11:08:44 +01001250 /* parse "rpc" */
1251 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001252 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1253
1254 /* parse "action", if any */
1255 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
1256 if (r == LY_SUCCESS) {
1257 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001258 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001259
1260 /* NETCONF action */
1261 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1262 *close_elem = 2;
1263 } else if (r == LY_ENOT) {
1264 /* NETCONF RPC */
1265 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1266 *close_elem = 1;
1267 } else {
1268 rc = r;
1269 goto cleanup;
1270 }
1271
1272cleanup:
1273 if (rc) {
1274 lyd_free_tree(*envp);
1275 *envp = NULL;
1276 }
1277 return rc;
1278}
1279
1280/**
Michal Vasko51de7b72022-04-29 09:50:22 +02001281 * @brief Validate eventTime date-and-time value.
1282 *
1283 * @param[in] node Opaque eventTime node.
1284 * @return LY_SUCCESS on success.
1285 * @return LY_ERR value on error.
1286 */
1287static LY_ERR
1288lydxml_env_netconf_eventtime_validate(const struct lyd_node *node)
1289{
1290 LY_ERR rc = LY_SUCCESS;
1291 struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001292 struct lysc_ctx cctx;
Michal Vasko51de7b72022-04-29 09:50:22 +02001293 const struct lys_module *mod;
1294 LY_ARRAY_COUNT_TYPE u;
1295 struct ly_err_item *err = NULL;
Michal Vaskoab36a7e2022-04-29 10:25:56 +02001296 struct lysp_type *type_p = NULL;
Michal Vasko51de7b72022-04-29 09:50:22 +02001297 struct lysc_pattern **patterns = NULL;
1298 const char *value;
1299
Michal Vaskoc636ea42022-09-16 10:20:31 +02001300 LYSC_CTX_INIT_CTX(cctx, ctx);
1301
Michal Vasko51de7b72022-04-29 09:50:22 +02001302 /* get date-and-time parsed type */
1303 mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
Michal Vaskoab36a7e2022-04-29 10:25:56 +02001304 assert(mod);
Michal Vasko51de7b72022-04-29 09:50:22 +02001305 LY_ARRAY_FOR(mod->parsed->typedefs, u) {
1306 if (!strcmp(mod->parsed->typedefs[u].name, "date-and-time")) {
1307 type_p = &mod->parsed->typedefs[u].type;
1308 break;
1309 }
1310 }
1311 assert(type_p);
1312
1313 /* compile patterns */
1314 assert(type_p->patterns);
1315 LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
1316
1317 /* validate */
1318 value = lyd_get_value(node);
1319 rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
1320
1321cleanup:
Michal Vaskoc636ea42022-09-16 10:20:31 +02001322 FREE_ARRAY(&cctx.free_ctx, patterns, lysc_pattern_free);
Michal Vasko51de7b72022-04-29 09:50:22 +02001323 if (rc && err) {
1324 LOGVAL_ERRITEM(ctx, err);
1325 ly_err_free(err);
1326 LOGVAL(ctx, LYVE_DATA, "Invalid \"eventTime\" in the notification.");
1327 }
1328 return rc;
1329}
1330
1331/**
Michal Vaskoe0665742021-02-11 11:08:44 +01001332 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
1333 *
1334 * @param[in] xmlctx XML parser context.
1335 * @param[out] evnp Parsed envelope(s) (opaque node).
1336 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1337 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1338 * @return LY_SUCCESS on success.
1339 * @return LY_ERR value on error.
1340 */
1341static LY_ERR
1342lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1343{
1344 LY_ERR rc = LY_SUCCESS, r;
1345 struct lyd_node *child;
1346
1347 assert(envp && !*envp);
1348
Michal Vasko50375272022-04-26 12:07:46 +02001349 if (xmlctx->status != LYXML_ELEMENT) {
1350 /* nothing to parse */
1351 assert(xmlctx->status == LYXML_END);
1352 goto cleanup;
1353 }
1354
Michal Vaskoe0665742021-02-11 11:08:44 +01001355 /* parse "notification" */
1356 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001357 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1358
1359 /* parse "eventTime" */
1360 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
1361 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +01001362 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
1363 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001364 r = LY_EVALID;
1365 }
1366 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1367
1368 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001369 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001370
1371 /* validate value */
Michal Vasko51de7b72022-04-29 09:50:22 +02001372 r = lydxml_env_netconf_eventtime_validate(child);
1373 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001374
1375 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +01001376 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1377 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001378 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001379 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001380 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001381 goto cleanup;
1382 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001383 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1384
1385 /* NETCONF notification */
1386 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
1387 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001388
1389cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001390 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +01001391 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001392 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001393 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001394 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001395}
Michal Vasko79135ae2020-12-16 10:08:35 +01001396
Michal Vaskoe0665742021-02-11 11:08:44 +01001397/**
1398 * @brief Parse an XML element as an opaque node subtree.
1399 *
1400 * @param[in] xmlctx XML parser context.
1401 * @param[in] parent Parent to append nodes to.
1402 * @return LY_ERR value.
1403 */
1404static LY_ERR
1405lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001406{
Michal Vaskoe0665742021-02-11 11:08:44 +01001407 LY_ERR rc = LY_SUCCESS;
1408 const struct lyxml_ns *ns;
1409 struct lyd_attr *attr = NULL;
1410 struct lyd_node *child = NULL;
1411 const char *name, *prefix;
1412 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001413
Michal Vaskoe0665742021-02-11 11:08:44 +01001414 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001415
Michal Vaskoe0665742021-02-11 11:08:44 +01001416 name = xmlctx->name;
1417 name_len = xmlctx->name_len;
1418 prefix = xmlctx->prefix;
1419 prefix_len = xmlctx->prefix_len;
1420 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1421 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001422 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001423 return LY_EVALID;
1424 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001425
Michal Vaskoe0665742021-02-11 11:08:44 +01001426 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001427
Michal Vaskoe0665742021-02-11 11:08:44 +01001428 /* create attributes */
1429 if (xmlctx->status == LYXML_ATTRIBUTE) {
1430 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1431 }
1432
1433 /* create node */
1434 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1435 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001436 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child);
Michal Vaskoe0665742021-02-11 11:08:44 +01001437 LY_CHECK_GOTO(rc, cleanup);
1438
1439 /* assign atributes */
1440 ((struct lyd_node_opaq *)child)->attr = attr;
1441 attr = NULL;
1442
1443 /* parser next element */
1444 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1445
1446 /* parse all the descendants */
1447 while (xmlctx->status == LYXML_ELEMENT) {
1448 rc = lydxml_opaq_r(xmlctx, child);
1449 LY_CHECK_GOTO(rc, cleanup);
1450 }
1451
1452 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001453 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001454
1455cleanup:
1456 lyd_free_attr_siblings(xmlctx->ctx, attr);
1457 if (rc) {
1458 lyd_free_tree(child);
1459 }
1460 return rc;
1461}
1462
1463/**
1464 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1465 *
1466 * @param[in] xmlctx XML parser context.
1467 * @param[in] parent Parent to append nodes to.
1468 * @return LY_ERR value.
1469 */
1470static LY_ERR
1471lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1472{
1473 LY_ERR r;
1474 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001475 ly_bool no_dup;
1476
1477 /* there must be some child */
1478 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1479 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1480 return LY_EVALID;
1481 }
1482
1483 while (xmlctx->status == LYXML_ELEMENT) {
1484 child = NULL;
1485
1486 /*
1487 * session-id
1488 */
1489 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1490 if (r == LY_SUCCESS) {
1491 no_dup = 1;
1492 goto check_child;
1493 } else if (r != LY_ENOT) {
1494 goto error;
1495 }
1496
1497 /*
1498 * bad-attribute
1499 */
1500 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1501 if (r == LY_SUCCESS) {
1502 no_dup = 1;
1503 goto check_child;
1504 } else if (r != LY_ENOT) {
1505 goto error;
1506 }
1507
1508 /*
1509 * bad-element
1510 */
1511 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1512 if (r == LY_SUCCESS) {
1513 no_dup = 1;
1514 goto check_child;
1515 } else if (r != LY_ENOT) {
1516 goto error;
1517 }
1518
1519 /*
1520 * bad-namespace
1521 */
1522 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1523 if (r == LY_SUCCESS) {
1524 no_dup = 1;
1525 goto check_child;
1526 } else if (r != LY_ENOT) {
1527 goto error;
1528 }
1529
1530 if (r == LY_ENOT) {
1531 assert(xmlctx->status == LYXML_ELEMENT);
1532
Michal Vasko845ba392022-06-16 07:52:13 +02001533 /* custom elements, parse all the siblings */
1534 while (xmlctx->status == LYXML_ELEMENT) {
1535 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1536 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1537 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001538 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001539 }
1540
1541check_child:
1542 /* check for duplicates */
1543 if (no_dup) {
1544 LY_LIST_FOR(lyd_child(parent), iter) {
1545 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1546 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1547 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1548 ((struct lyd_node_opaq *)child)->name.name);
1549 r = LY_EVALID;
1550 goto error;
1551 }
1552 }
1553 }
1554
1555 /* finish child parsing */
1556 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1557 assert(xmlctx->status == LYXML_ELEMENT);
1558 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001559 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001560 r = LY_EVALID;
1561 goto error;
1562 }
1563 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1564
1565 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001566 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001567 }
1568
1569 return LY_SUCCESS;
1570
1571error:
1572 lyd_free_tree(child);
1573 return r;
1574}
1575
1576/**
1577 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1578 *
1579 * @param[in] xmlctx XML parser context.
1580 * @param[in] parent Parent to append nodes to.
1581 * @return LY_ERR value.
1582 */
1583static LY_ERR
1584lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1585{
1586 LY_ERR r;
1587 struct lyd_node *child, *iter;
1588 const char *val;
1589 ly_bool no_dup;
1590
1591 /* there must be some child */
1592 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1593 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1594 return LY_EVALID;
1595 }
1596
1597 while (xmlctx->status == LYXML_ELEMENT) {
1598 child = NULL;
1599
1600 /*
1601 * error-type
1602 */
1603 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1604 if (r == LY_SUCCESS) {
1605 val = ((struct lyd_node_opaq *)child)->value;
1606 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1607 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1608 ((struct lyd_node_opaq *)child)->name.name);
1609 r = LY_EVALID;
1610 goto error;
1611 }
1612
1613 no_dup = 1;
1614 goto check_child;
1615 } else if (r != LY_ENOT) {
1616 goto error;
1617 }
1618
1619 /*
1620 * error-tag
1621 */
1622 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1623 if (r == LY_SUCCESS) {
1624 val = ((struct lyd_node_opaq *)child)->value;
1625 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1626 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1627 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1628 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1629 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1630 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1631 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1632 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1633 ((struct lyd_node_opaq *)child)->name.name);
1634 r = LY_EVALID;
1635 goto error;
1636 }
1637
1638 no_dup = 1;
1639 goto check_child;
1640 } else if (r != LY_ENOT) {
1641 goto error;
1642 }
1643
1644 /*
1645 * error-severity
1646 */
1647 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1648 if (r == LY_SUCCESS) {
1649 val = ((struct lyd_node_opaq *)child)->value;
1650 if (strcmp(val, "error") && strcmp(val, "warning")) {
1651 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1652 ((struct lyd_node_opaq *)child)->name.name);
1653 r = LY_EVALID;
1654 goto error;
1655 }
1656
1657 no_dup = 1;
1658 goto check_child;
1659 } else if (r != LY_ENOT) {
1660 goto error;
1661 }
1662
1663 /*
1664 * error-app-tag
1665 */
1666 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1667 if (r == LY_SUCCESS) {
1668 no_dup = 1;
1669 goto check_child;
1670 } else if (r != LY_ENOT) {
1671 goto error;
1672 }
1673
1674 /*
1675 * error-path
1676 */
1677 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1678 if (r == LY_SUCCESS) {
1679 no_dup = 1;
1680 goto check_child;
1681 } else if (r != LY_ENOT) {
1682 goto error;
1683 }
1684
1685 /*
1686 * error-message
1687 */
1688 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1689 if (r == LY_SUCCESS) {
1690 no_dup = 1;
1691 goto check_child;
1692 } else if (r != LY_ENOT) {
1693 goto error;
1694 }
1695
1696 /* error-info */
1697 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1698 if (r == LY_SUCCESS) {
1699 /* parse all the descendants */
1700 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1701
1702 no_dup = 0;
1703 goto check_child;
1704 } else if (r != LY_ENOT) {
1705 goto error;
1706 }
1707
1708 if (r == LY_ENOT) {
1709 assert(xmlctx->status == LYXML_ELEMENT);
1710 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001711 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001712 r = LY_EVALID;
1713 goto error;
1714 }
1715
1716check_child:
1717 /* check for duplicates */
1718 if (no_dup) {
1719 LY_LIST_FOR(lyd_child(parent), iter) {
1720 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1721 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1722 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1723 ((struct lyd_node_opaq *)child)->name.name);
1724 r = LY_EVALID;
1725 goto error;
1726 }
1727 }
1728 }
1729
1730 /* finish child parsing */
1731 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1732 assert(xmlctx->status == LYXML_ELEMENT);
1733 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001734 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001735 r = LY_EVALID;
1736 goto error;
1737 }
1738 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1739
1740 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001741 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001742 }
1743
1744 return LY_SUCCESS;
1745
1746error:
1747 lyd_free_tree(child);
1748 return r;
1749}
1750
1751/**
1752 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1753 *
1754 * @param[in] xmlctx XML parser context.
1755 * @param[out] evnp Parsed envelope(s) (opaque node).
1756 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1757 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1758 * @return LY_SUCCESS on success.
1759 * @return LY_ERR value on error.
1760 */
1761static LY_ERR
1762lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1763{
1764 LY_ERR rc = LY_SUCCESS, r;
1765 struct lyd_node *child = NULL;
1766 const char *parsed_elem = NULL;
1767
1768 assert(envp && !*envp);
1769
Michal Vasko50375272022-04-26 12:07:46 +02001770 if (xmlctx->status != LYXML_ELEMENT) {
1771 /* nothing to parse */
1772 assert(xmlctx->status == LYXML_END);
1773 goto cleanup;
1774 }
1775
Michal Vaskoe0665742021-02-11 11:08:44 +01001776 /* parse "rpc-reply" */
1777 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001778 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1779
1780 /* there must be some child */
1781 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1782 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1783 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001784 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001785 }
1786
Michal Vaskoe0665742021-02-11 11:08:44 +01001787 /* try to parse "ok" */
1788 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1789 if (r == LY_SUCCESS) {
1790 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001791 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001792
1793 /* finish child parsing */
1794 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1795 assert(xmlctx->status == LYXML_ELEMENT);
1796 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001797 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001798 rc = LY_EVALID;
1799 goto cleanup;
1800 }
1801 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1802
1803 /* success */
1804 parsed_elem = "ok";
1805 goto finish;
1806 } else if (r != LY_ENOT) {
1807 rc = r;
1808 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001809 }
1810
Michal Vaskoe0665742021-02-11 11:08:44 +01001811 /* try to parse all "rpc-error" elements */
1812 while (xmlctx->status == LYXML_ELEMENT) {
1813 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1814 if (r == LY_ENOT) {
1815 break;
1816 } else if (r) {
1817 rc = r;
1818 goto cleanup;
1819 }
1820
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 /* parse all children of "rpc-error" */
1825 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1826
1827 /* finish child parsing */
1828 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1829 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1830
1831 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001832 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001833
1834finish:
1835 if (parsed_elem) {
1836 /* NETCONF rpc-reply with no data */
1837 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1838 assert(xmlctx->status == LYXML_ELEMENT);
1839 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001840 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001841 rc = LY_EVALID;
1842 goto cleanup;
1843 }
1844 }
1845
1846 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001847 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001848 *close_elem = 1;
1849
1850cleanup:
1851 if (rc) {
1852 lyd_free_tree(*envp);
1853 *envp = NULL;
1854 }
1855 return rc;
1856}
1857
Michal Vasko2552ea32020-12-08 15:32:34 +01001858LY_ERR
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001859lyd_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 +01001860 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 +01001861 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001862{
Michal Vaskoe0665742021-02-11 11:08:44 +01001863 LY_ERR rc = LY_SUCCESS;
1864 struct lyd_xml_ctx *lydctx;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001865 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001866 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001867
Michal Vaskoe0665742021-02-11 11:08:44 +01001868 assert(ctx && in && lydctx_p);
1869 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1870 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vasko2552ea32020-12-08 15:32:34 +01001871
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001872 assert((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF) ||
1873 (data_type == LYD_TYPE_REPLY_NETCONF));
1874 assert(!(parse_opts & LYD_PARSE_SUBTREE));
1875
Michal Vaskoe0665742021-02-11 11:08:44 +01001876 /* init context */
1877 lydctx = calloc(1, sizeof *lydctx);
1878 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1879 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1880 lydctx->parse_opts = parse_opts;
1881 lydctx->val_opts = val_opts;
1882 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001883 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001884
Michal Vaskoe0665742021-02-11 11:08:44 +01001885 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001886 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001887 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001888 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1889 if (rc == LY_ENOT) {
1890 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1891 }
1892 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001893 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001894 case LYD_TYPE_NOTIF_NETCONF:
1895 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001896 rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem);
1897 if (rc == LY_ENOT) {
1898 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1899 }
1900 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001901 break;
1902 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001903 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001904 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1905 if (rc == LY_ENOT) {
1906 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1907 }
1908 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001909 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001910 default:
1911 LOGINT(ctx);
1912 rc = LY_EINT;
1913 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001914 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001915
Michal Vaskoe0665742021-02-11 11:08:44 +01001916 lydctx->int_opts = int_opts;
1917
1918 /* find the operation node if it exists already */
1919 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1920
1921 /* parse XML data */
1922 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1923 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1924 parsed_data_nodes = 1;
1925
1926 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1927 break;
1928 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001929 }
1930
Michal Vaskoe0665742021-02-11 11:08:44 +01001931 /* close all opened elements */
1932 for (i = 0; i < close_elem; ++i) {
1933 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1934 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001935 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1936 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001937 rc = LY_EVALID;
1938 goto cleanup;
1939 }
1940
1941 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001942 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001943
1944 /* check final state */
1945 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1946 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1947 rc = LY_EVALID;
1948 goto cleanup;
1949 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01001950 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 +01001951 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1952 rc = LY_EVALID;
1953 goto cleanup;
1954 }
1955
1956 if (!parsed_data_nodes) {
1957 /* no data nodes were parsed */
1958 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001959 }
1960
1961cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001962 /* there should be no unres stored if validation should be skipped */
1963 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01001964 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01001965
1966 if (rc) {
1967 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1968 } else {
1969 *lydctx_p = (struct lyd_ctx *)lydctx;
1970
1971 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1972 lyxml_ctx_free(lydctx->xmlctx);
1973 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001974 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001975 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001976}