blob: 944d3d530792955e629dd9beb5da4530c7a2adc5 [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;
Jan Kundrát863b18b2024-07-06 20:55:23 +0200444 /* this needs to be at least 64bit, and it "should not" be an explicit int64_t
445 * because the code calls strtoll later on, which "might" return a bigger type */
446 long long num;
Michal Vaskocea58712022-04-01 14:37:08 +0200447
448 *hints = 0;
449 *anchor = NULL;
450
451 if (!value_len) {
Michal Vasko6235e152023-08-07 13:39:13 +0200452 /* no value but it may also be zero-length string */
453 *hints |= LYD_VALHINT_EMPTY | LYD_VALHINT_STRING;
Michal Vaskocea58712022-04-01 14:37:08 +0200454 } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) {
455 /* boolean value */
456 *hints |= LYD_VALHINT_BOOLEAN;
457 } else {
Jan Kundrát863b18b2024-07-06 20:55:23 +0200458 num = strtoll(value, &ptr, 10);
Michal Vaskocea58712022-04-01 14:37:08 +0200459 if ((unsigned)(ptr - value) == value_len) {
460 /* number value */
461 *hints |= LYD_VALHINT_DECNUM;
Jan Kundrát863b18b2024-07-06 20:55:23 +0200462 if ((num < INT32_MIN) || (num > UINT32_MAX)) {
Michal Vaskocea58712022-04-01 14:37:08 +0200463 /* large number */
464 *hints |= LYD_VALHINT_NUM64;
465 }
466 } else {
467 /* string value */
468 *hints |= LYD_VALHINT_STRING;
469 }
470 }
471
472 if (!first) {
473 return;
474 }
475
476 /* search backwards to find the last instance */
477 do {
478 first = first->prev;
479 if (first->schema) {
480 continue;
481 }
482
483 opaq = (struct lyd_node_opaq *)first;
484 assert(opaq->format == LY_VALUE_XML);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100485 if (!ly_strncmp(opaq->name.name, name, name_len) &&
zhangtaogb348a8b2024-02-21 15:43:30 +0800486 ((ns && opaq->name.module_ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) {
Michal Vaskocea58712022-04-01 14:37:08 +0200487 if (opaq->value && opaq->value[0]) {
488 /* leaf-list nodes */
489 opaq->hints |= LYD_NODEHINT_LEAFLIST;
490 *hints |= LYD_NODEHINT_LEAFLIST;
491 } else {
492 /* list nodes */
493 opaq->hints |= LYD_NODEHINT_LIST;
494 *hints |= LYD_NODEHINT_LIST;
495 }
Michal Vaskod027f382023-02-10 09:13:25 +0100496 *anchor = (struct lyd_node *)first;
Michal Vaskocea58712022-04-01 14:37:08 +0200497 break;
498 }
499 } while (first->prev->next);
500}
501
502/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200503 * @brief Get schema node for the current element.
Michal Vaskoddd76592022-01-17 13:34:48 +0100504 *
505 * @param[in] lydctx XML data parser context.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200506 * @param[in] parent Parsed parent data node, if any.
507 * @param[in] prefix Element prefix, if any.
508 * @param[in] prefix_len Length of @p prefix.
509 * @param[in] name Element name.
510 * @param[in] name_len Length of @p name.
511 * @param[out] snode Found schema node, NULL if no suitable was found.
512 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vaskoddd76592022-01-17 13:34:48 +0100513 * @return LY_SUCCESS on success;
Michal Vaskoddd76592022-01-17 13:34:48 +0100514 * @return LY_ERR on error.
515 */
516static LY_ERR
Michal Vaskod027f382023-02-10 09:13:25 +0100517lydxml_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 +0200518 const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vaskoddd76592022-01-17 13:34:48 +0100519{
520 LY_ERR r;
Michal Vaskob36053d2020-03-26 15:49:30 +0100521 struct lyxml_ctx *xmlctx;
522 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200523 const struct lyxml_ns *ns;
Radek Krejcie7b95092019-05-15 11:03:07 +0200524 struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200525 uint32_t getnext_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +0100526
Michal Vaskob36053d2020-03-26 15:49:30 +0100527 xmlctx = lydctx->xmlctx;
528 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100529 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100530
Michal Vasko8cc3f662022-03-29 11:25:51 +0200531 *snode = NULL;
532 *ext = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100533
Michal Vasko8cc3f662022-03-29 11:25:51 +0200534 /* get current namespace */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200535 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200536 if (!ns) {
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100537 if (lydctx->int_opts & LYD_INTOPT_ANY) {
538 goto unknown_module;
539 }
540
541 if (prefix_len) {
542 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
543 } else {
544 LOGVAL(ctx, LYVE_REFERENCE, "Missing XML namespace.");
545 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200546 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200547 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200548
549 /* get the element module, use parent context if possible because of extensions */
550 mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200551 if (!mod) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100552 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200553 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len,
554 snode, ext);
555 if (r != LY_ENOT) {
556 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100557 return r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100558 }
559
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100560unknown_module:
Michal Vaskoe0665742021-02-11 11:08:44 +0100561 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200562 if (ns) {
563 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
564 } else if (prefix_len) {
565 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%.*s\" in the context.", (int)prefix_len, prefix);
566 } else {
567 LOGVAL(ctx, LYVE_REFERENCE, "No default namespace in the context.");
568 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200569 return LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200570 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200571 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200572 }
573
Michal Vaskoa5da3292020-08-12 13:10:50 +0200574 /* get the schema node */
Michal Vaskoa878a892023-08-18 12:22:07 +0200575 if (!parent && lydctx->ext) {
576 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
577 } else {
578 /* try to find parent schema node even if it is an opaque node (not connected to the parent) */
579 *snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts);
580 }
581 if (!*snode) {
582 /* check for extension data */
583 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
584 name_len, snode, ext);
585 if (r != LY_ENOT) {
586 /* success or error */
587 return r;
Radek Krejcif16e2542021-02-17 15:39:23 +0100588 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100589
Michal Vaskoa878a892023-08-18 12:22:07 +0200590 /* unknown data node */
591 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
592 if (parent) {
593 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
594 (int)name_len, name, LYD_NAME(parent));
595 } else if (lydctx->ext) {
596 if (lydctx->ext->argument) {
597 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
598 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100599 } else {
Michal Vaskoa878a892023-08-18 12:22:07 +0200600 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
601 (int)name_len, name, lydctx->ext->def->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100602 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200603 } else {
604 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
605 (int)name_len, name, mod->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200606 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200607 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200608 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200609 return LY_SUCCESS;
610 } else {
611 /* check that schema node is valid and can be used */
612 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
613 LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
Michal Vaskoa5da3292020-08-12 13:10:50 +0200614 }
615
Michal Vasko8cc3f662022-03-29 11:25:51 +0200616 return LY_SUCCESS;
617}
618
619/**
Michal Vaskod027f382023-02-10 09:13:25 +0100620 * @brief Parse an XML opque node.
621 *
622 * @param[in] lydctx XML YANG data parser context.
623 * @param[in] sibling Existing sibling node, if any.
624 * @param[in] prefix Parsed node prefix.
625 * @param[in] prefix_len Length of @p prefix.
626 * @param[in] name Parsed node name.
627 * @param[in] name_len Length of @p name.
628 * @param[out] insert_anchor Optional anchor node for inserting this node.
629 * @param[out] node Created node.
630 * @return LY_ERR value.
631 */
632static LY_ERR
633lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len,
634 const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node)
635{
636 LY_ERR rc = LY_SUCCESS;
637 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200638 struct lyd_node_opaq *opaq;
639 const char *ns_uri, *value = NULL;
640 size_t value_len;
641 ly_bool ws_only, dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100642 const struct lyxml_ns *ns;
643 uint32_t hints;
644 void *val_prefix_data = NULL;
645 LY_VALUE_FORMAT format;
646
647 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
648
649 *node = NULL;
650
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200651 /* remember the value */
652 value = xmlctx->value;
653 value_len = xmlctx->value_len;
654 ws_only = xmlctx->ws_only;
655 dynamic = xmlctx->dynamic;
656 if (dynamic) {
Michal Vaskod027f382023-02-10 09:13:25 +0100657 xmlctx->dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100658 }
659
Michal Vasko535d21c2023-08-09 10:41:44 +0200660 /* get value prefixes, if any */
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200661 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 +0200662 LY_CHECK_GOTO(rc, cleanup);
663
Michal Vaskod027f382023-02-10 09:13:25 +0100664 /* get NS again, it may have been backed up and restored */
665 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
666 ns_uri = ns ? ns->uri : NULL;
667
668 /* get best-effort node hints */
669 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor);
670
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200671 /* create the node without value */
672 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, NULL, 0,
673 NULL, format, NULL, hints, node);
Michal Vaskod027f382023-02-10 09:13:25 +0100674 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100675
Michal Vaskoa878a892023-08-18 12:22:07 +0200676 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100677 LOG_LOCSET(NULL, *node);
Michal Vaskoa878a892023-08-18 12:22:07 +0200678
Michal Vaskod027f382023-02-10 09:13:25 +0100679 /* parser next */
680 rc = lyxml_ctx_next(xmlctx);
681 LY_CHECK_GOTO(rc, cleanup);
682
683 /* process children */
684 while (xmlctx->status == LYXML_ELEMENT) {
685 rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
686 LY_CHECK_GOTO(rc, cleanup);
687 }
688
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200689 /* update the value */
690 opaq = (struct lyd_node_opaq *)*node;
691 if (opaq->child) {
692 if (!ws_only) {
693 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(opaq));
694 rc = LY_EVALID;
695 goto cleanup;
696 }
697 } else if (value_len) {
698 lydict_remove(xmlctx->ctx, opaq->value);
699 if (dynamic) {
700 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
701 dynamic = 0;
702 } else {
703 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
704 }
705 }
706
707 /* always store val_prefix_data because the format requires them */
708 assert(!opaq->val_prefix_data);
709 opaq->val_prefix_data = val_prefix_data;
710 val_prefix_data = NULL;
711
Michal Vaskod027f382023-02-10 09:13:25 +0100712cleanup:
Michal Vaskoa878a892023-08-18 12:22:07 +0200713 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100714 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +0200715 }
Michal Vaskod027f382023-02-10 09:13:25 +0100716 ly_free_prefix_data(format, val_prefix_data);
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200717 if (dynamic) {
718 free((char *)value);
719 }
Michal Vaskod027f382023-02-10 09:13:25 +0100720 if (rc) {
721 lyd_free_tree(*node);
722 *node = NULL;
723 }
724 return rc;
725}
726
727/**
728 * @brief Parse an XML leaf/leaf-list node.
729 *
730 * @param[in] lydctx XML YANG data parser context.
731 * @param[in] parent Parent node, if any.
732 * @param[in] snode Schema node of the new node.
733 * @param[out] node Created node.
734 * @return LY_ERR value.
735 */
736static LY_ERR
737lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode,
738 struct lyd_node **node)
739{
740 LY_ERR r, rc = LY_SUCCESS;
741 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
742 struct lyd_node *anchor;
743
744 *node = NULL;
745
746 /* create node */
747 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
748 LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node);
749 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
750
751 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100752 LOG_LOCSET(NULL, *node);
Michal Vaskod027f382023-02-10 09:13:25 +0100753 }
754
Michal Vasko58a1a702023-03-01 13:45:38 +0100755 if (*node && parent && (snode->flags & LYS_KEY)) {
Michal Vaskod027f382023-02-10 09:13:25 +0100756 /* check the key order, the anchor must never be a key */
757 anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node);
758 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
759 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
760 LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name);
761 r = LY_EVALID;
762 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
763 } else {
764 LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name);
765 }
766 }
767 }
768
769 /* parser next */
770 r = lyxml_ctx_next(xmlctx);
771 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
772
773 /* no children expected */
774 if (xmlctx->status == LYXML_ELEMENT) {
775 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
776 (int)xmlctx->name_len, xmlctx->name, snode->name);
777 r = LY_EVALID;
778 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
779 }
780
781cleanup:
782 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100783 LOG_LOCBACK(0, 1);
Michal Vaskod027f382023-02-10 09:13:25 +0100784 }
785 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
786 lyd_free_tree(*node);
787 *node = NULL;
788 }
789 return rc;
790}
791
792/**
793 * @brief Parse an XML inner node.
794 *
795 * @param[in] lydctx XML YANG data parser context.
796 * @param[in] snode Schema node of the new node.
797 * @param[in] ext Extension instance of @p snode, if any.
798 * @param[out] node Created node.
799 * @return LY_ERR value.
800 */
801static LY_ERR
802lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
803 struct lyd_node **node)
804{
805 LY_ERR r, rc = LY_SUCCESS;
806 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
807 uint32_t prev_parse_opts = lydctx->parse_opts;
808
809 *node = NULL;
810
811 if (!xmlctx->ws_only) {
812 /* value in inner node */
813 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
814 (int)xmlctx->value_len, xmlctx->value, snode->name);
815 r = LY_EVALID;
816 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
817 }
818
819 /* create node */
820 rc = lyd_create_inner(snode, node);
821 LY_CHECK_GOTO(rc, cleanup);
822
Michal Vasko58a1a702023-03-01 13:45:38 +0100823 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100824 LOG_LOCSET(NULL, *node);
Michal Vaskod027f382023-02-10 09:13:25 +0100825
826 /* parser next */
827 rc = lyxml_ctx_next(xmlctx);
828 LY_CHECK_GOTO(rc, cleanup);
829
830 if (ext) {
831 /* only parse these extension data and validate afterwards */
832 lydctx->parse_opts |= LYD_PARSE_ONLY;
833 }
834
835 /* process children */
836 while (xmlctx->status == LYXML_ELEMENT) {
837 r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
838 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
839 }
840
841 /* restore options */
842 lydctx->parse_opts = prev_parse_opts;
843
844 if (snode->nodetype == LYS_LIST) {
845 /* check all keys exist */
846 r = lyd_parse_check_keys(*node);
Michal Vasko202d8162023-03-01 14:42:19 +0100847 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100848 }
849
Michal Vasko40015732023-11-20 13:48:15 +0100850 if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) {
851 /* new node validation, autodelete CANNOT occur (it can if multi-error), all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +0100852 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
853 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
854
855 /* add any missing default children */
856 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
857 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
858 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
859 }
860
861 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
862 /* rememeber the RPC/action/notification */
863 lydctx->op_node = *node;
864 }
865
866cleanup:
867 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100868 LOG_LOCBACK(0, 1);
Michal Vaskod027f382023-02-10 09:13:25 +0100869 }
870 lydctx->parse_opts = prev_parse_opts;
Michal Vasko202d8162023-03-01 14:42:19 +0100871 if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
872 /* list without keys is unusable or an error */
Michal Vaskod027f382023-02-10 09:13:25 +0100873 lyd_free_tree(*node);
874 *node = NULL;
875 }
876 return rc;
877}
878
879/**
880 * @brief Parse an XML anyxml/anydata node.
881 *
882 * @param[in] lydctx XML YANG data parser context.
883 * @param[in] snode Schema node of the new node.
884 * @param[in] ext Extension instance of @p snode, if any.
885 * @param[out] node Created node.
886 * @return LY_ERR value.
887 */
888static LY_ERR
889lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
890 struct lyd_node **node)
891{
892 LY_ERR r, rc = LY_SUCCESS;
893 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
894 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
895 struct lyd_node *child = NULL;
896 char *val = NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +0200897 ly_bool log_node = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100898
899 *node = NULL;
900
901 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
902 /* value in anydata node, we expect a tree */
903 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200904 xmlctx->value_len < 20 ? (int)xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskod027f382023-02-10 09:13:25 +0100905 r = LY_EVALID;
906 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
907 }
908
909 if (!xmlctx->ws_only) {
910 /* use an arbitrary text value for anyxml */
911 val = strndup(xmlctx->value, xmlctx->value_len);
912 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
913
914 /* parser next */
915 r = lyxml_ctx_next(xmlctx);
916 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
917
918 /* create node */
919 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
920 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
921 val = NULL;
922 } else {
Michal Vasko85be65e2023-06-13 09:44:17 +0200923 /* create node */
924 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
925 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
926
927 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100928 LOG_LOCSET(NULL, *node);
Michal Vasko85be65e2023-06-13 09:44:17 +0200929 log_node = 1;
930
Michal Vaskod027f382023-02-10 09:13:25 +0100931 /* parser next */
932 r = lyxml_ctx_next(xmlctx);
933 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
934
935 /* update options so that generic data can be parsed */
936 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
937 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
938 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
939
940 /* parse any data tree */
941 while (xmlctx->status == LYXML_ELEMENT) {
942 r = lydxml_subtree_r(lydctx, NULL, &child, NULL);
943 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
944 }
945
Michal Vasko85be65e2023-06-13 09:44:17 +0200946 /* assign the data tree */
947 ((struct lyd_node_any *)*node)->value.tree = child;
Michal Vaskod027f382023-02-10 09:13:25 +0100948 child = NULL;
949 }
950
951cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +0200952 if (log_node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100953 LOG_LOCBACK(0, 1);
Michal Vasko85be65e2023-06-13 09:44:17 +0200954 }
Michal Vaskod027f382023-02-10 09:13:25 +0100955 lydctx->parse_opts = prev_parse_opts;
956 lydctx->int_opts = prev_int_opts;
957 free(val);
958 lyd_free_tree(child);
959 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
960 lyd_free_tree(*node);
961 *node = NULL;
962 }
963 return rc;
964}
965
966/**
967 * @brief Parse an XML subtree, recursively.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200968 *
969 * @param[in] lydctx XML YANG data parser context.
970 * @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 +0100971 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200972 * @param[in,out] parsed Optional set to add all the parsed siblings into.
973 * @return LY_ERR value.
974 */
975static LY_ERR
976lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
977{
Michal Vaskod027f382023-02-10 09:13:25 +0100978 LY_ERR r, rc = LY_SUCCESS;
979 const char *prefix, *name;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200980 size_t prefix_len, name_len;
981 struct lyxml_ctx *xmlctx;
982 const struct ly_ctx *ctx;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200983 struct lyd_meta *meta = NULL;
984 struct lyd_attr *attr = NULL;
Michal Vasko4a1e3e82023-09-05 08:45:01 +0200985 const struct lysc_node *snode = NULL;
986 struct lysc_ext_instance *ext = NULL;
Michal Vaskod027f382023-02-10 09:13:25 +0100987 uint32_t orig_parse_opts;
988 struct lyd_node *node = NULL, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200989 ly_bool parse_subtree;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200990
991 assert(parent || first_p);
992
993 xmlctx = lydctx->xmlctx;
994 ctx = xmlctx->ctx;
995
996 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
997 /* all descendants should be parsed */
998 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
999 orig_parse_opts = lydctx->parse_opts;
1000
1001 assert(xmlctx->status == LYXML_ELEMENT);
1002
1003 /* remember element prefix and name */
1004 prefix = xmlctx->prefix;
1005 prefix_len = xmlctx->prefix_len;
1006 name = xmlctx->name;
1007 name_len = xmlctx->name_len;
1008
1009 /* parser next */
Michal Vaskod027f382023-02-10 09:13:25 +01001010 rc = lyxml_ctx_next(xmlctx);
1011 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001012
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001013 if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && name_len && !prefix_len &&
1014 !ly_strncmp("eventTime", name, name_len)) {
1015 /* parse eventTime, create node */
1016 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1017 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len,
1018 "urn:ietf:params:xml:ns:netconf:notification:1.0", 47, xmlctx->value,
1019 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA, &node);
1020 LY_CHECK_GOTO(rc, cleanup);
1021
1022 /* validate the value */
1023 r = lyd_parser_notif_eventtime_validate(node);
1024 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
1025
1026 /* parser next */
1027 r = lyxml_ctx_next(xmlctx);
1028 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
1029 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1030 LOGVAL(ctx, LYVE_DATA, "Unexpected notification \"eventTime\" node children.");
1031 rc = LY_EVALID;
1032 lyd_free_tree(node);
1033 goto cleanup;
1034 }
1035
1036 goto node_parsed;
1037 }
1038
Michal Vasko8cc3f662022-03-29 11:25:51 +02001039 /* get the schema node */
Michal Vasko202d8162023-03-01 14:42:19 +01001040 r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext);
1041 if (r) {
1042 rc = r;
1043 if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1044 /* skip the invalid data */
1045 if ((r = lydxml_data_skip(xmlctx))) {
1046 rc = r;
1047 }
1048 }
1049 goto cleanup;
1050 } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001051 LOGVRB("Skipping parsing of unknown node \"%.*s\".", (int)name_len, name);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001052
1053 /* skip element with children */
Michal Vaskod027f382023-02-10 09:13:25 +01001054 rc = lydxml_data_skip(xmlctx);
1055 goto cleanup;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001056 }
1057
Michal Vaskoa5da3292020-08-12 13:10:50 +02001058 /* create metadata/attributes */
1059 if (xmlctx->status == LYXML_ATTRIBUTE) {
1060 if (snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001061 rc = lydxml_metadata(lydctx, snode, &meta);
1062 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001063 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001064 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskod027f382023-02-10 09:13:25 +01001065 rc = lydxml_attrs(xmlctx, &attr);
1066 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001067 }
1068 }
1069
1070 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1071 if (!snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001072 /* opaque */
1073 r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len,
1074 &insert_anchor, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001075 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskod027f382023-02-10 09:13:25 +01001076 /* term */
1077 r = lydxml_subtree_term(lydctx, parent, snode, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001078 } else if (snode->nodetype & LYD_NODE_INNER) {
Michal Vaskod027f382023-02-10 09:13:25 +01001079 /* inner */
1080 r = lydxml_subtree_inner(lydctx, snode, ext, &node);
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001081 } else {
Michal Vaskod027f382023-02-10 09:13:25 +01001082 /* any */
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001083 assert(snode->nodetype & LYD_NODE_ANY);
Michal Vaskod027f382023-02-10 09:13:25 +01001084 r = lydxml_subtree_any(lydctx, snode, ext, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001085 }
Michal Vaskod027f382023-02-10 09:13:25 +01001086 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko1524f1a2023-03-01 09:36:34 +01001087
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001088node_parsed:
Michal Vasko58a1a702023-03-01 13:45:38 +01001089 if (node && snode) {
Michal Vasko135719f2022-08-25 12:18:17 +02001090 /* add/correct flags */
Michal Vaskod027f382023-02-10 09:13:25 +01001091 r = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko567d7032023-02-13 08:53:31 +01001092 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001093
Michal Vaskoeba23112022-08-26 08:35:41 +02001094 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1095 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001096 r = lyd_validate_node_ext(node, &lydctx->ext_node);
1097 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001098 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001099 }
1100
1101 /* parser next */
1102 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +01001103 if (!parse_subtree) {
Michal Vaskod027f382023-02-10 09:13:25 +01001104 r = lyxml_ctx_next(xmlctx);
Michal Vasko567d7032023-02-13 08:53:31 +01001105 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001106 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001107
Michal Vasko58a1a702023-03-01 13:45:38 +01001108 LY_CHECK_GOTO(!node, cleanup);
1109
Michal Vaskoa5da3292020-08-12 13:10:50 +02001110 /* add metadata/attributes */
1111 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +01001112 lyd_insert_meta(node, meta, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001113 meta = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001114 } else {
1115 lyd_insert_attr(node, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001116 attr = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001117 }
1118
1119 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +02001120 if (insert_anchor) {
1121 lyd_insert_after(insert_anchor, node);
1122 } else if (ext) {
Michal Vaskod027f382023-02-10 09:13:25 +01001123 r = lyplg_ext_insert(parent, node);
Michal Vasko567d7032023-02-13 08:53:31 +01001124 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001125 } else {
aPiecek1462ab12024-02-07 09:13:29 +01001126 lyd_insert_node(parent, first_p, node,
1127 lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001128 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001129 while (!parent && (*first_p)->prev->next) {
1130 *first_p = (*first_p)->prev;
1131 }
1132
Michal Vaskoe0665742021-02-11 11:08:44 +01001133 /* rememeber a successfully parsed node */
1134 if (parsed) {
1135 ly_set_add(parsed, node, 1, NULL);
1136 }
1137
Michal Vaskod027f382023-02-10 09:13:25 +01001138cleanup:
Michal Vasko8cc3f662022-03-29 11:25:51 +02001139 lydctx->parse_opts = orig_parse_opts;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001140 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001141 lyd_free_attr_siblings(ctx, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001142 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +02001143}
1144
Michal Vaskoe0665742021-02-11 11:08:44 +01001145/**
1146 * @brief Parse a specific XML element into an opaque node.
1147 *
1148 * @param[in] xmlctx XML parser context.
1149 * @param[in] name Name of the element.
1150 * @param[in] uri URI of the element.
1151 * @param[in] value Whether a value is expected in the element.
1152 * @param[out] evnp Parsed envelope (opaque node).
1153 * @return LY_SUCCESS on success.
1154 * @return LY_ENOT if the specified element did not match.
1155 * @return LY_ERR value on error.
1156 */
Michal Vasko1bf09392020-03-27 12:38:10 +01001157static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001158lydxml_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 +01001159{
Michal Vaskoe0665742021-02-11 11:08:44 +01001160 LY_ERR rc = LY_SUCCESS;
1161 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +02001162 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +01001163 const char *prefix;
1164 size_t prefix_len;
1165
Michal Vaskof048ba52023-06-13 10:40:43 +02001166 if (xmlctx->status != LYXML_ELEMENT) {
1167 /* nothing to parse */
1168 return LY_ENOT;
1169 }
1170
Michal Vasko1bf09392020-03-27 12:38:10 +01001171 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
1172 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +01001173 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001174 }
1175
1176 prefix = xmlctx->prefix;
1177 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001178 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001179 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001180 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001181 return LY_EVALID;
1182 } else if (strcmp(ns->uri, uri)) {
1183 /* different namespace */
1184 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001185 }
1186
1187 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
1188
1189 /* create attributes */
1190 if (xmlctx->status == LYXML_ATTRIBUTE) {
1191 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1192 }
1193
Michal Vaskoe0665742021-02-11 11:08:44 +01001194 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1195 if (!value && !xmlctx->ws_only) {
1196 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +01001197 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001198 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001199 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001200 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001201
1202 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001203 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001204 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001205 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001206
1207 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001208 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001209 attr = NULL;
1210
Michal Vaskoe0665742021-02-11 11:08:44 +01001211 /* parser next element */
1212 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001213
Michal Vaskoe0665742021-02-11 11:08:44 +01001214cleanup:
1215 lyd_free_attr_siblings(xmlctx->ctx, attr);
1216 if (rc) {
1217 lyd_free_tree(*envp);
1218 *envp = NULL;
1219 }
1220 return rc;
1221}
1222
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001223LY_ERR
1224lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1225 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
1226 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
1227{
Michal Vaskod027f382023-02-10 09:13:25 +01001228 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001229 struct lyd_xml_ctx *lydctx;
Michal Vasko64592692023-06-12 13:50:11 +02001230 ly_bool parsed_data_nodes = 0, close_elem = 0;
1231 struct lyd_node *act = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001232 enum LYXML_PARSER_STATUS status;
1233
1234 assert(ctx && in && lydctx_p);
1235 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1236 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1237
1238 /* init context */
1239 lydctx = calloc(1, sizeof *lydctx);
1240 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1241 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1242 lydctx->parse_opts = parse_opts;
1243 lydctx->val_opts = val_opts;
1244 lydctx->int_opts = int_opts;
1245 lydctx->free = lyd_xml_ctx_free;
1246 lydctx->ext = ext;
1247
1248 /* find the operation node if it exists already */
1249 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1250
Michal Vasko64592692023-06-12 13:50:11 +02001251 if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) {
1252 /* can be either, try to parse "action" */
1253 if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) {
1254 close_elem = 1;
1255 int_opts &= ~LYD_INTOPT_RPC;
1256 }
1257 }
1258
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001259 /* parse XML data */
1260 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
Michal Vaskod027f382023-02-10 09:13:25 +01001261 r = lydxml_subtree_r(lydctx, parent, first_p, parsed);
1262 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1263
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001264 parsed_data_nodes = 1;
1265
1266 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1267 break;
1268 }
1269 }
1270
Michal Vasko64592692023-06-12 13:50:11 +02001271 /* close an opened element */
1272 if (close_elem) {
1273 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1274 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
1275 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1276 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
1277 rc = LY_EVALID;
1278 goto cleanup;
1279 }
1280
1281 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
1282 }
1283
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001284 /* check final state */
1285 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1286 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001287 r = LY_EVALID;
1288 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001289 }
1290 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1291 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001292 r = LY_EVALID;
1293 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001294 }
1295
1296 if (!parsed_data_nodes) {
1297 /* no data nodes were parsed */
1298 lydctx->op_node = NULL;
1299 }
1300
1301 if (parse_opts & LYD_PARSE_SUBTREE) {
1302 /* check for a sibling element */
1303 assert(subtree_sibling);
1304 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1305 *subtree_sibling = 1;
1306 } else {
1307 *subtree_sibling = 0;
1308 }
1309 }
1310
1311cleanup:
1312 /* there should be no unres stored if validation should be skipped */
1313 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1314 !lydctx->node_when.count));
1315
Michal Vasko64592692023-06-12 13:50:11 +02001316 lyd_free_tree(act);
Michal Vaskod027f382023-02-10 09:13:25 +01001317 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001318 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1319 } else {
1320 *lydctx_p = (struct lyd_ctx *)lydctx;
1321
1322 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1323 lyxml_ctx_free(lydctx->xmlctx);
1324 lydctx->xmlctx = NULL;
1325 }
1326 return rc;
1327}
1328
Michal Vaskoe0665742021-02-11 11:08:44 +01001329/**
1330 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
1331 *
1332 * @param[in] xmlctx XML parser context.
1333 * @param[out] evnp Parsed envelope(s) (opaque node).
1334 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1335 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1336 * @return LY_SUCCESS on success.
1337 * @return LY_ERR value on error.
1338 */
1339static LY_ERR
1340lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1341{
1342 LY_ERR rc = LY_SUCCESS, r;
1343 struct lyd_node *child;
1344
1345 assert(envp && !*envp);
1346
1347 /* parse "rpc" */
1348 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001349 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1350
1351 /* parse "action", if any */
1352 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
1353 if (r == LY_SUCCESS) {
1354 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001355 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_DEFAULT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001356
1357 /* NETCONF action */
1358 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1359 *close_elem = 2;
1360 } else if (r == LY_ENOT) {
1361 /* NETCONF RPC */
1362 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1363 *close_elem = 1;
1364 } else {
1365 rc = r;
1366 goto cleanup;
1367 }
1368
1369cleanup:
1370 if (rc) {
1371 lyd_free_tree(*envp);
1372 *envp = NULL;
1373 }
1374 return rc;
1375}
1376
1377/**
Michal Vaskoe0665742021-02-11 11:08:44 +01001378 * @brief Parse an XML element as an opaque node subtree.
1379 *
1380 * @param[in] xmlctx XML parser context.
1381 * @param[in] parent Parent to append nodes to.
1382 * @return LY_ERR value.
1383 */
1384static LY_ERR
1385lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001386{
Michal Vaskoe0665742021-02-11 11:08:44 +01001387 LY_ERR rc = LY_SUCCESS;
1388 const struct lyxml_ns *ns;
1389 struct lyd_attr *attr = NULL;
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001390 struct lyd_node *node = NULL;
1391 struct lyd_node_opaq *opaq;
1392 const char *name, *prefix, *value = NULL;
1393 size_t name_len, prefix_len, value_len;
1394 ly_bool ws_only, dynamic = 0;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001395
Michal Vaskoe0665742021-02-11 11:08:44 +01001396 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001397
Michal Vaskoe0665742021-02-11 11:08:44 +01001398 name = xmlctx->name;
1399 name_len = xmlctx->name_len;
1400 prefix = xmlctx->prefix;
1401 prefix_len = xmlctx->prefix_len;
1402 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1403 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001404 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001405 return LY_EVALID;
1406 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001407
Michal Vaskoe0665742021-02-11 11:08:44 +01001408 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001409
Michal Vaskoe0665742021-02-11 11:08:44 +01001410 /* create attributes */
1411 if (xmlctx->status == LYXML_ATTRIBUTE) {
1412 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1413 }
1414
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001415 /* remember the value */
Michal Vaskoe0665742021-02-11 11:08:44 +01001416 assert(xmlctx->status == LYXML_ELEM_CONTENT);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001417 value = xmlctx->value;
1418 value_len = xmlctx->value_len;
1419 ws_only = xmlctx->ws_only;
1420 dynamic = xmlctx->dynamic;
1421 if (dynamic) {
1422 xmlctx->dynamic = 0;
1423 }
1424
1425 /* create the node without value */
1426 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), NULL, 0, NULL,
1427 LY_VALUE_XML, NULL, 0, &node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001428 LY_CHECK_GOTO(rc, cleanup);
1429
1430 /* assign atributes */
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001431 ((struct lyd_node_opaq *)node)->attr = attr;
Michal Vaskoe0665742021-02-11 11:08:44 +01001432 attr = NULL;
1433
1434 /* parser next element */
1435 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1436
1437 /* parse all the descendants */
1438 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001439 rc = lydxml_opaq_r(xmlctx, node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001440 LY_CHECK_GOTO(rc, cleanup);
1441 }
1442
1443 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001444 lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_LAST);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001445
1446 /* update the value */
1447 opaq = (struct lyd_node_opaq *)node;
1448 if (opaq->child) {
1449 if (!ws_only) {
1450 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(node));
1451 rc = LY_EVALID;
1452 goto cleanup;
1453 }
1454 } else if (value_len) {
1455 lydict_remove(xmlctx->ctx, opaq->value);
1456 if (dynamic) {
1457 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
1458 dynamic = 0;
1459 } else {
1460 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
1461 }
1462 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001463
1464cleanup:
1465 lyd_free_attr_siblings(xmlctx->ctx, attr);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001466 if (dynamic) {
1467 free((char *)value);
1468 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001469 if (rc) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001470 lyd_free_tree(node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001471 }
1472 return rc;
1473}
1474
1475/**
1476 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1477 *
1478 * @param[in] xmlctx XML parser context.
1479 * @param[in] parent Parent to append nodes to.
1480 * @return LY_ERR value.
1481 */
1482static LY_ERR
1483lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1484{
1485 LY_ERR r;
1486 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001487 ly_bool no_dup;
1488
1489 /* there must be some child */
1490 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1491 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1492 return LY_EVALID;
1493 }
1494
1495 while (xmlctx->status == LYXML_ELEMENT) {
1496 child = NULL;
1497
1498 /*
1499 * session-id
1500 */
1501 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1502 if (r == LY_SUCCESS) {
1503 no_dup = 1;
1504 goto check_child;
1505 } else if (r != LY_ENOT) {
1506 goto error;
1507 }
1508
1509 /*
1510 * bad-attribute
1511 */
1512 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1513 if (r == LY_SUCCESS) {
1514 no_dup = 1;
1515 goto check_child;
1516 } else if (r != LY_ENOT) {
1517 goto error;
1518 }
1519
1520 /*
1521 * bad-element
1522 */
1523 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1524 if (r == LY_SUCCESS) {
1525 no_dup = 1;
1526 goto check_child;
1527 } else if (r != LY_ENOT) {
1528 goto error;
1529 }
1530
1531 /*
1532 * bad-namespace
1533 */
1534 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1535 if (r == LY_SUCCESS) {
1536 no_dup = 1;
1537 goto check_child;
1538 } else if (r != LY_ENOT) {
1539 goto error;
1540 }
1541
1542 if (r == LY_ENOT) {
1543 assert(xmlctx->status == LYXML_ELEMENT);
1544
Michal Vasko845ba392022-06-16 07:52:13 +02001545 /* custom elements, parse all the siblings */
1546 while (xmlctx->status == LYXML_ELEMENT) {
1547 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1548 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1549 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001550 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001551 }
1552
1553check_child:
1554 /* check for duplicates */
1555 if (no_dup) {
1556 LY_LIST_FOR(lyd_child(parent), iter) {
1557 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1558 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1559 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1560 ((struct lyd_node_opaq *)child)->name.name);
1561 r = LY_EVALID;
1562 goto error;
1563 }
1564 }
1565 }
1566
1567 /* finish child parsing */
1568 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1569 assert(xmlctx->status == LYXML_ELEMENT);
1570 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001571 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001572 r = LY_EVALID;
1573 goto error;
1574 }
1575 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1576
1577 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001578 lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001579 }
1580
1581 return LY_SUCCESS;
1582
1583error:
1584 lyd_free_tree(child);
1585 return r;
1586}
1587
1588/**
1589 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1590 *
1591 * @param[in] xmlctx XML parser context.
1592 * @param[in] parent Parent to append nodes to.
1593 * @return LY_ERR value.
1594 */
1595static LY_ERR
1596lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1597{
1598 LY_ERR r;
1599 struct lyd_node *child, *iter;
1600 const char *val;
1601 ly_bool no_dup;
1602
1603 /* there must be some child */
1604 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1605 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1606 return LY_EVALID;
1607 }
1608
1609 while (xmlctx->status == LYXML_ELEMENT) {
1610 child = NULL;
1611
1612 /*
1613 * error-type
1614 */
1615 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1616 if (r == LY_SUCCESS) {
1617 val = ((struct lyd_node_opaq *)child)->value;
1618 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1619 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1620 ((struct lyd_node_opaq *)child)->name.name);
1621 r = LY_EVALID;
1622 goto error;
1623 }
1624
1625 no_dup = 1;
1626 goto check_child;
1627 } else if (r != LY_ENOT) {
1628 goto error;
1629 }
1630
1631 /*
1632 * error-tag
1633 */
1634 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1635 if (r == LY_SUCCESS) {
1636 val = ((struct lyd_node_opaq *)child)->value;
1637 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1638 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1639 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1640 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1641 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1642 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1643 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1644 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1645 ((struct lyd_node_opaq *)child)->name.name);
1646 r = LY_EVALID;
1647 goto error;
1648 }
1649
1650 no_dup = 1;
1651 goto check_child;
1652 } else if (r != LY_ENOT) {
1653 goto error;
1654 }
1655
1656 /*
1657 * error-severity
1658 */
1659 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1660 if (r == LY_SUCCESS) {
1661 val = ((struct lyd_node_opaq *)child)->value;
1662 if (strcmp(val, "error") && strcmp(val, "warning")) {
1663 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1664 ((struct lyd_node_opaq *)child)->name.name);
1665 r = LY_EVALID;
1666 goto error;
1667 }
1668
1669 no_dup = 1;
1670 goto check_child;
1671 } else if (r != LY_ENOT) {
1672 goto error;
1673 }
1674
1675 /*
1676 * error-app-tag
1677 */
1678 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1679 if (r == LY_SUCCESS) {
1680 no_dup = 1;
1681 goto check_child;
1682 } else if (r != LY_ENOT) {
1683 goto error;
1684 }
1685
1686 /*
1687 * error-path
1688 */
1689 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1690 if (r == LY_SUCCESS) {
1691 no_dup = 1;
1692 goto check_child;
1693 } else if (r != LY_ENOT) {
1694 goto error;
1695 }
1696
1697 /*
1698 * error-message
1699 */
1700 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1701 if (r == LY_SUCCESS) {
1702 no_dup = 1;
1703 goto check_child;
1704 } else if (r != LY_ENOT) {
1705 goto error;
1706 }
1707
1708 /* error-info */
1709 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1710 if (r == LY_SUCCESS) {
1711 /* parse all the descendants */
1712 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1713
1714 no_dup = 0;
1715 goto check_child;
1716 } else if (r != LY_ENOT) {
1717 goto error;
1718 }
1719
1720 if (r == LY_ENOT) {
1721 assert(xmlctx->status == LYXML_ELEMENT);
1722 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001723 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001724 r = LY_EVALID;
1725 goto error;
1726 }
1727
1728check_child:
1729 /* check for duplicates */
1730 if (no_dup) {
1731 LY_LIST_FOR(lyd_child(parent), iter) {
1732 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1733 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1734 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1735 ((struct lyd_node_opaq *)child)->name.name);
1736 r = LY_EVALID;
1737 goto error;
1738 }
1739 }
1740 }
1741
1742 /* finish child parsing */
1743 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1744 assert(xmlctx->status == LYXML_ELEMENT);
1745 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001746 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001747 r = LY_EVALID;
1748 goto error;
1749 }
1750 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1751
1752 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001753 lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001754 }
1755
1756 return LY_SUCCESS;
1757
1758error:
1759 lyd_free_tree(child);
1760 return r;
1761}
1762
1763/**
1764 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1765 *
1766 * @param[in] xmlctx XML parser context.
1767 * @param[out] evnp Parsed envelope(s) (opaque node).
1768 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1769 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1770 * @return LY_SUCCESS on success.
1771 * @return LY_ERR value on error.
1772 */
1773static LY_ERR
1774lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1775{
1776 LY_ERR rc = LY_SUCCESS, r;
1777 struct lyd_node *child = NULL;
1778 const char *parsed_elem = NULL;
1779
1780 assert(envp && !*envp);
1781
1782 /* parse "rpc-reply" */
1783 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001784 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1785
1786 /* there must be some child */
1787 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1788 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1789 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001790 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001791 }
1792
Michal Vaskoe0665742021-02-11 11:08:44 +01001793 /* try to parse "ok" */
1794 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1795 if (r == LY_SUCCESS) {
1796 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001797 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001798
1799 /* finish child parsing */
1800 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1801 assert(xmlctx->status == LYXML_ELEMENT);
1802 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001803 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001804 rc = LY_EVALID;
1805 goto cleanup;
1806 }
1807 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1808
1809 /* success */
1810 parsed_elem = "ok";
1811 goto finish;
1812 } else if (r != LY_ENOT) {
1813 rc = r;
1814 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001815 }
1816
Michal Vaskoe0665742021-02-11 11:08:44 +01001817 /* try to parse all "rpc-error" elements */
1818 while (xmlctx->status == LYXML_ELEMENT) {
1819 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1820 if (r == LY_ENOT) {
1821 break;
1822 } else if (r) {
1823 rc = r;
1824 goto cleanup;
1825 }
1826
1827 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001828 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001829
1830 /* parse all children of "rpc-error" */
1831 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1832
1833 /* finish child parsing */
1834 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1835 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1836
1837 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001838 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001839
1840finish:
1841 if (parsed_elem) {
1842 /* NETCONF rpc-reply with no data */
1843 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1844 assert(xmlctx->status == LYXML_ELEMENT);
1845 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001846 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001847 rc = LY_EVALID;
1848 goto cleanup;
1849 }
1850 }
1851
1852 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001853 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001854 *close_elem = 1;
1855
1856cleanup:
1857 if (rc) {
1858 lyd_free_tree(*envp);
1859 *envp = NULL;
1860 }
1861 return rc;
1862}
1863
Michal Vasko2552ea32020-12-08 15:32:34 +01001864LY_ERR
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001865lyd_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 +01001866 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 +01001867 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001868{
Michal Vaskoe0665742021-02-11 11:08:44 +01001869 LY_ERR rc = LY_SUCCESS;
1870 struct lyd_xml_ctx *lydctx;
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001871 struct lyd_node *node;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001872 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001873 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001874
Michal Vaskoe0665742021-02-11 11:08:44 +01001875 assert(ctx && in && lydctx_p);
1876 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1877 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001878 assert(!(parse_opts & LYD_PARSE_SUBTREE));
1879
Michal Vaskoe0665742021-02-11 11:08:44 +01001880 /* init context */
1881 lydctx = calloc(1, sizeof *lydctx);
1882 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1883 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1884 lydctx->parse_opts = parse_opts;
1885 lydctx->val_opts = val_opts;
1886 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001887 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001888
Michal Vaskoe0665742021-02-11 11:08:44 +01001889 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001890 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001891 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001892 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1893 if (rc == LY_ENOT) {
1894 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1895 }
1896 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001897 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001898 case LYD_TYPE_NOTIF_NETCONF:
1899 assert(!parent);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001900
1901 /* parse "notification" */
1902 rc = lydxml_envelope(lydctx->xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001903 if (rc == LY_ENOT) {
1904 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1905 }
1906 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001907
1908 /* NETCONF notification */
1909 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME;
1910 close_elem = 1;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001911 break;
1912 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001913 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001914 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1915 if (rc == LY_ENOT) {
1916 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1917 }
1918 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001919 break;
Michal Vasko820efe82023-05-12 15:47:43 +02001920 case LYD_TYPE_RPC_RESTCONF:
1921 assert(parent);
1922
1923 /* parse "input" */
Michal Vasko420cc252023-08-24 08:14:24 +02001924 rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_node_module(parent)->ns, 0, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001925 if (rc == LY_ENOT) {
1926 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace.");
1927 }
1928 LY_CHECK_GOTO(rc, cleanup);
1929
1930 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
1931 close_elem = 1;
1932 break;
1933 case LYD_TYPE_REPLY_RESTCONF:
1934 assert(parent);
1935
1936 /* parse "output" */
Michal Vasko420cc252023-08-24 08:14:24 +02001937 rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_node_module(parent)->ns, 0, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001938 if (rc == LY_ENOT) {
1939 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace.");
1940 }
1941 LY_CHECK_GOTO(rc, cleanup);
1942
1943 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
1944 close_elem = 1;
1945 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001946 default:
1947 LOGINT(ctx);
1948 rc = LY_EINT;
1949 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001950 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001951
Michal Vaskoe0665742021-02-11 11:08:44 +01001952 lydctx->int_opts = int_opts;
1953
1954 /* find the operation node if it exists already */
1955 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1956
1957 /* parse XML data */
1958 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1959 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1960 parsed_data_nodes = 1;
1961
1962 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1963 break;
1964 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001965 }
1966
Michal Vaskoe0665742021-02-11 11:08:44 +01001967 /* close all opened elements */
1968 for (i = 0; i < close_elem; ++i) {
1969 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1970 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001971 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1972 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001973 rc = LY_EVALID;
1974 goto cleanup;
1975 }
1976
1977 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001978 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001979
1980 /* check final state */
1981 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1982 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1983 rc = LY_EVALID;
1984 goto cleanup;
1985 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01001986 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 +01001987 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1988 rc = LY_EVALID;
1989 goto cleanup;
1990 }
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001991 if (int_opts & LYD_INTOPT_EVENTTIME) {
1992 /* parse as a child of the envelope */
1993 node = (*first_p)->prev;
1994 if (node->schema) {
1995 LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node.");
1996 rc = LY_EVALID;
1997 goto cleanup;
1998 } else {
1999 /* can be the only opaque node and an operation had to be parsed */
2000 assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
Michal Vasko2e784f82024-01-11 09:51:22 +01002001 lyd_unlink(node);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002002 assert(*envp);
2003 lyd_insert_child(*envp, node);
2004 }
2005 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002006
2007 if (!parsed_data_nodes) {
2008 /* no data nodes were parsed */
2009 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01002010 }
2011
2012cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01002013 /* there should be no unres stored if validation should be skipped */
2014 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01002015 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01002016
2017 if (rc) {
2018 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
2019 } else {
2020 *lydctx_p = (struct lyd_ctx *)lydctx;
2021
2022 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
2023 lyxml_ctx_free(lydctx->xmlctx);
2024 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002025 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002026 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002027}