/src/libyang/src/parser_xml.c
Line | Count | Source |
1 | | /** |
2 | | * @file parser_xml.c |
3 | | * @author Radek Krejci <rkrejci@cesnet.cz> |
4 | | * @author Michal Vasko <mvasko@cesnet.cz> |
5 | | * @brief XML data parser for libyang |
6 | | * |
7 | | * Copyright (c) 2019 - 2025 CESNET, z.s.p.o. |
8 | | * |
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 | | |
16 | | #define _GNU_SOURCE |
17 | | |
18 | | #include <assert.h> |
19 | | #include <stdint.h> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | |
23 | | #include "compat.h" |
24 | | #include "context.h" |
25 | | #include "dict.h" |
26 | | #include "in_internal.h" |
27 | | #include "log.h" |
28 | | #include "ly_common.h" |
29 | | #include "parser_data.h" |
30 | | #include "parser_internal.h" |
31 | | #include "plugins_exts.h" |
32 | | #include "plugins_internal.h" |
33 | | #include "schema_compile_node.h" |
34 | | #include "set.h" |
35 | | #include "tree.h" |
36 | | #include "tree_data.h" |
37 | | #include "tree_data_internal.h" |
38 | | #include "tree_schema.h" |
39 | | #include "tree_schema_internal.h" |
40 | | #include "validation.h" |
41 | | #include "xml.h" |
42 | | |
43 | | static 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 | | |
46 | | void |
47 | | lyd_xml_ctx_free(struct lyd_ctx *lydctx) |
48 | 9.05k | { |
49 | 9.05k | struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx; |
50 | | |
51 | 9.05k | lyd_ctx_free(lydctx); |
52 | 9.05k | lyxml_ctx_free(ctx->xmlctx); |
53 | 9.05k | free(ctx); |
54 | 9.05k | } |
55 | | |
56 | | /** |
57 | | * @brief Log namespace error. |
58 | | * |
59 | | * @param[in] xmlctx XML context |
60 | | * @param[in] prefix XML prefix. |
61 | | * @param[in] prefix_len XML prefix length. |
62 | | * @param[in] attr_name Current XML attribute name. |
63 | | * @param[in] attr_len Current XML attribute name length. |
64 | | */ |
65 | | static void |
66 | | lydxml_log_namespace_err(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, |
67 | | const char *attr_name, size_t attr_len) |
68 | 351 | { |
69 | 351 | if (prefix_len && attr_len) { |
70 | 11 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\" at attribute \"%.*s\".", |
71 | 11 | (int)prefix_len, prefix, (int)attr_len, attr_name); |
72 | 340 | } else if (prefix_len) { |
73 | 15 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", |
74 | 15 | (int)prefix_len, prefix); |
75 | 325 | } else { |
76 | 325 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing XML namespace."); |
77 | 325 | } |
78 | 351 | } |
79 | | |
80 | | /** |
81 | | * @brief Parse and create XML metadata. |
82 | | * |
83 | | * @param[in] lydctx XML data parser context. |
84 | | * @param[in] sparent Schema node of the parent. |
85 | | * @param[out] meta List of created metadata instances. |
86 | | * @return LY_ERR value. |
87 | | */ |
88 | | static LY_ERR |
89 | | lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta) |
90 | 331 | { |
91 | 331 | LY_ERR ret = LY_SUCCESS; |
92 | 331 | const struct lyxml_ns *ns; |
93 | 331 | struct lys_module *mod; |
94 | 331 | const char *name; |
95 | 331 | size_t name_len; |
96 | 331 | LY_ARRAY_COUNT_TYPE u; |
97 | 331 | ly_bool filter_attrs = 0; |
98 | 331 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
99 | | |
100 | 331 | *meta = NULL; |
101 | | |
102 | 331 | LOG_LOCSET(sparent, NULL); |
103 | | |
104 | | /* check for NETCONF filter unqualified attributes */ |
105 | 331 | if (!strcmp(sparent->module->name, "notifications")) { |
106 | | /* ancient module that does not even use the extension */ |
107 | 0 | filter_attrs = 1; |
108 | 331 | } else { |
109 | 331 | LY_ARRAY_FOR(sparent->exts, u) { |
110 | 0 | if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") && |
111 | 0 | !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) { |
112 | 0 | filter_attrs = 1; |
113 | 0 | break; |
114 | 0 | } |
115 | 0 | } |
116 | 331 | } |
117 | | |
118 | 1.39k | while (xmlctx->status == LYXML_ATTRIBUTE) { |
119 | 1.12k | if (!xmlctx->prefix_len) { |
120 | | /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */ |
121 | 737 | if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) || |
122 | 0 | !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) { |
123 | 0 | mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf"); |
124 | 0 | if (!mod) { |
125 | 0 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
126 | 0 | "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes."); |
127 | 0 | ret = LY_ENOTFOUND; |
128 | 0 | goto cleanup; |
129 | 0 | } |
130 | 0 | goto create_meta; |
131 | 0 | } |
132 | | |
133 | 737 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
134 | 0 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".", |
135 | 0 | (int)xmlctx->name_len, xmlctx->name); |
136 | | /* If LYD_VALIDATE_MULTI_ERROR is set, then continue parsing, because otherwise the parser context |
137 | | * will remain in a bad state, which will cause termination on some assert or undefined behavior. |
138 | | */ |
139 | 0 | LY_DPARSER_ERR_GOTO(LY_EVALID, ret = LY_EVALID, lydctx, cleanup); |
140 | 0 | } |
141 | | |
142 | | /* skip attr */ |
143 | 737 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
144 | 737 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
145 | 737 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
146 | 715 | continue; |
147 | 737 | } |
148 | | |
149 | | /* get namespace of the attribute to find its annotation definition */ |
150 | 385 | ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len); |
151 | 385 | if (!ns) { |
152 | 11 | lydxml_log_namespace_err(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len); |
153 | 11 | ret = LY_ENOTFOUND; |
154 | 11 | goto cleanup; |
155 | 11 | } |
156 | | |
157 | | /* get the module with metadata definition */ |
158 | 374 | mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri); |
159 | 374 | if (!mod) { |
160 | 358 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
161 | 0 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
162 | 0 | "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".", |
163 | 0 | ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", |
164 | 0 | (int)xmlctx->name_len, xmlctx->name); |
165 | 0 | ret = LY_ENOTFOUND; |
166 | 0 | goto cleanup; |
167 | 0 | } |
168 | | |
169 | | /* skip attr */ |
170 | 358 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
171 | 358 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
172 | 358 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
173 | 352 | continue; |
174 | 358 | } |
175 | | |
176 | 16 | create_meta: |
177 | | /* remember meta name and get its content */ |
178 | 16 | name = xmlctx->name; |
179 | 16 | name_len = xmlctx->name_len; |
180 | 16 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
181 | 16 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
182 | | |
183 | | /* create metadata */ |
184 | 16 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value, |
185 | 16 | xmlctx->value_len * 8, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent); |
186 | 16 | LY_CHECK_GOTO(ret, cleanup); |
187 | | |
188 | | /* next attribute */ |
189 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
190 | 0 | } |
191 | | |
192 | 331 | cleanup: |
193 | 331 | LOG_LOCBACK(1, 0); |
194 | 331 | if (ret) { |
195 | 55 | lyd_free_meta_siblings(*meta); |
196 | 55 | *meta = NULL; |
197 | 55 | } |
198 | 331 | return ret; |
199 | 331 | } |
200 | | |
201 | | static LY_ERR |
202 | | lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr) |
203 | 0 | { |
204 | 0 | LY_ERR ret = LY_SUCCESS; |
205 | 0 | const struct lyxml_ns *ns; |
206 | 0 | void *val_prefix_data; |
207 | 0 | LY_VALUE_FORMAT format; |
208 | 0 | struct lyd_attr *attr2; |
209 | 0 | const char *name, *prefix; |
210 | 0 | size_t name_len, prefix_len; |
211 | |
|
212 | 0 | assert(attr); |
213 | 0 | *attr = NULL; |
214 | |
|
215 | 0 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
216 | 0 | if (*attr) { |
217 | 0 | attr2 = *attr; |
218 | 0 | } else { |
219 | 0 | attr2 = NULL; |
220 | 0 | } |
221 | | |
222 | | /* remember attr prefix, name, and get its content */ |
223 | 0 | prefix = xmlctx->prefix; |
224 | 0 | prefix_len = xmlctx->prefix_len; |
225 | 0 | name = xmlctx->name; |
226 | 0 | name_len = xmlctx->name_len; |
227 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
228 | 0 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
229 | | |
230 | | /* handle special "xml" attribute prefix */ |
231 | 0 | if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) { |
232 | 0 | name = prefix; |
233 | 0 | name_len += 1 + prefix_len; |
234 | 0 | prefix = NULL; |
235 | 0 | prefix_len = 0; |
236 | 0 | } |
237 | | |
238 | | /* find namespace of the attribute, if any */ |
239 | 0 | ns = NULL; |
240 | 0 | if (prefix_len) { |
241 | 0 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
242 | 0 | if (!ns) { |
243 | 0 | lydxml_log_namespace_err(xmlctx, prefix, prefix_len, name, name_len); |
244 | 0 | ret = LY_EVALID; |
245 | 0 | goto cleanup; |
246 | 0 | } |
247 | 0 | } |
248 | | |
249 | | /* get value prefixes */ |
250 | 0 | val_prefix_data = NULL; |
251 | 0 | LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, |
252 | 0 | &xmlctx->ns, &format, &val_prefix_data), cleanup); |
253 | | |
254 | | /* attr2 is always changed to the created attribute */ |
255 | 0 | ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL, |
256 | 0 | ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, |
257 | 0 | LYD_HINT_DATA); |
258 | 0 | LY_CHECK_GOTO(ret, cleanup); |
259 | |
|
260 | 0 | if (!*attr) { |
261 | 0 | *attr = attr2; |
262 | 0 | } |
263 | | |
264 | | /* next attribute */ |
265 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
266 | 0 | } |
267 | | |
268 | 0 | cleanup: |
269 | 0 | if (ret) { |
270 | 0 | lyd_free_attr_siblings(xmlctx->ctx, *attr); |
271 | 0 | *attr = NULL; |
272 | 0 | } |
273 | 0 | return ret; |
274 | 0 | } |
275 | | |
276 | | static LY_ERR |
277 | | lydxml_check_list(struct lyd_xml_ctx *lydctx, const struct lysc_node *list) |
278 | 0 | { |
279 | 0 | LY_ERR ret = LY_SUCCESS, r; |
280 | 0 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
281 | 0 | enum LYXML_PARSER_STATUS next; |
282 | 0 | struct ly_set key_set = {0}; |
283 | 0 | const struct lysc_node *snode; |
284 | 0 | uint32_t i, parents_count; |
285 | |
|
286 | 0 | assert(list && (list->nodetype == LYS_LIST)); |
287 | | |
288 | | /* get all keys into a set (keys do not have if-features or anything) */ |
289 | 0 | snode = NULL; |
290 | 0 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
291 | 0 | ret = ly_set_add(&key_set, (void *)snode, 1, NULL); |
292 | 0 | LY_CHECK_GOTO(ret, cleanup); |
293 | 0 | } |
294 | | |
295 | | /* remember parent count */ |
296 | 0 | parents_count = xmlctx->elements.count; |
297 | |
|
298 | 0 | while (xmlctx->status == LYXML_ELEMENT) { |
299 | | /* find key definition */ |
300 | 0 | for (i = 0; i < key_set.count; ++i) { |
301 | 0 | snode = (const struct lysc_node *)key_set.objs[i]; |
302 | 0 | if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) { |
303 | 0 | break; |
304 | 0 | } |
305 | 0 | } |
306 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
307 | | |
308 | | /* skip attributes */ |
309 | 0 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
310 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
311 | 0 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
312 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
313 | 0 | } |
314 | | |
315 | 0 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
316 | 0 | if (i < key_set.count) { |
317 | | /* validate the value */ |
318 | 0 | r = ly_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len * 8, LY_VALUE_XML, &xmlctx->ns, |
319 | 0 | LYD_HINT_DATA, lydctx->ext); |
320 | 0 | if (!r) { |
321 | | /* key with a valid value, remove from the set */ |
322 | 0 | ly_set_rm_index(&key_set, i, NULL); |
323 | 0 | } |
324 | 0 | } |
325 | | |
326 | | /* parser next */ |
327 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
328 | | |
329 | | /* skip any children, resursively */ |
330 | 0 | while (xmlctx->status == LYXML_ELEMENT) { |
331 | 0 | while (parents_count < xmlctx->elements.count) { |
332 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
333 | 0 | } |
334 | 0 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
335 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
336 | 0 | } |
337 | | |
338 | | /* parser next, but do not parse closing element of the list because it would remove its namespaces */ |
339 | 0 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
340 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup); |
341 | 0 | if (next != LYXML_ELEM_CLOSE) { |
342 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | 0 | if (key_set.count) { |
347 | | /* some keys are missing/did not validate */ |
348 | 0 | ret = LY_ENOT; |
349 | 0 | } |
350 | |
|
351 | 0 | cleanup: |
352 | 0 | ly_set_erase(&key_set, NULL); |
353 | 0 | return ret; |
354 | 0 | } |
355 | | |
356 | | /** |
357 | | * @brief Skip an element with all its descendants. |
358 | | * |
359 | | * @param[in] xmlctx XML parser context. |
360 | | * @return LY_ERR value. |
361 | | */ |
362 | | static LY_ERR |
363 | | lydxml_data_skip(struct lyxml_ctx *xmlctx) |
364 | 28.0k | { |
365 | 28.0k | uint32_t parents_count; |
366 | | |
367 | | /* remember current number of parents */ |
368 | 28.0k | parents_count = xmlctx->elements.count; |
369 | 28.0k | assert(parents_count); |
370 | | |
371 | | /* skip after the content */ |
372 | 37.2k | while (xmlctx->status != LYXML_ELEM_CONTENT) { |
373 | 9.60k | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
374 | 9.20k | } |
375 | 27.6k | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
376 | | |
377 | | /* skip all children elements, recursively, if any */ |
378 | 138k | while (parents_count <= xmlctx->elements.count) { |
379 | 112k | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
380 | 111k | } |
381 | | |
382 | | /* close element */ |
383 | 27.1k | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
384 | 26.8k | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
385 | | |
386 | 26.7k | return LY_SUCCESS; |
387 | 26.8k | } |
388 | | |
389 | | /** |
390 | | * @brief Check that the current element can be parsed as a data node. |
391 | | * |
392 | | * @param[in] lydctx XML data parser context. |
393 | | * @param[in,out] snode Found schema node, set to NULL if data node cannot be created. |
394 | | * @return LY_ERR value. |
395 | | */ |
396 | | static LY_ERR |
397 | | lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode) |
398 | 585k | { |
399 | 585k | LY_ERR ret = LY_SUCCESS, r; |
400 | 585k | struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx; |
401 | 585k | uint32_t *prev_lo, temp_lo = 0; |
402 | | |
403 | 585k | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
404 | | /* only checks specific to opaque nodes */ |
405 | 585k | return LY_SUCCESS; |
406 | 585k | } |
407 | | |
408 | 0 | if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) { |
409 | | /* nothing to check */ |
410 | 0 | return LY_SUCCESS; |
411 | 0 | } |
412 | | |
413 | 0 | assert(xmlctx->elements.count); |
414 | | |
415 | | /* backup parser */ |
416 | 0 | LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx)); |
417 | | |
418 | | /* skip attributes */ |
419 | 0 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
420 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
421 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
422 | 0 | } |
423 | | |
424 | 0 | if ((*snode)->nodetype & LYD_NODE_TERM) { |
425 | | /* value may not be valid in which case we parse it as an opaque node */ |
426 | 0 | prev_lo = ly_temp_log_options(&temp_lo); |
427 | 0 | r = ly_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len * 8, LY_VALUE_XML, &xmlctx->ns, |
428 | 0 | LYD_HINT_DATA, lydctx->ext); |
429 | 0 | ly_temp_log_options(prev_lo); |
430 | 0 | if (r) { |
431 | 0 | LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, (int)xmlctx->value_len, |
432 | 0 | xmlctx->value); |
433 | 0 | *snode = NULL; |
434 | 0 | } |
435 | 0 | } else if ((*snode)->nodetype == LYS_LIST) { |
436 | | /* skip content */ |
437 | 0 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
438 | |
|
439 | 0 | if (lydxml_check_list(lydctx, *snode)) { |
440 | | /* invalid list, parse as opaque if it missing/has invalid some keys */ |
441 | 0 | LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name); |
442 | 0 | *snode = NULL; |
443 | 0 | } |
444 | 0 | } else { |
445 | | /* if there is a non-WS value, it cannot be parsed as an inner node */ |
446 | 0 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
447 | 0 | if (!xmlctx->ws_only) { |
448 | 0 | *snode = NULL; |
449 | 0 | } |
450 | 0 | } |
451 | | |
452 | 0 | restore: |
453 | | /* restore parser */ |
454 | 0 | lyxml_ctx_restore(xmlctx, &pxmlctx); |
455 | 0 | return ret; |
456 | 0 | } |
457 | | |
458 | | /** |
459 | | * @brief Get sensible data hints for an opaque node. |
460 | | * |
461 | | * @param[in] name Node name. |
462 | | * @param[in] name_len Length of @p name. |
463 | | * @param[in] value Node value. |
464 | | * @param[in] value_len Length of @p value. |
465 | | * @param[in] first Node first sibling. |
466 | | * @param[in] ns Node module namespace, NULL for no namespace. |
467 | | * @param[out] hints Data hints to use. |
468 | | * @param[out] anchor Anchor to insert after in case of a list. |
469 | | */ |
470 | | static void |
471 | | lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, const struct lyd_node *first, |
472 | | const char *ns, uint32_t *hints, struct lyd_node **anchor) |
473 | 0 | { |
474 | 0 | struct lyd_node_opaq *opaq; |
475 | 0 | char *ptr; |
476 | | /* this needs to be at least 64bit, and it "should not" be an explicit int64_t |
477 | | * because the code calls strtoll later on, which "might" return a bigger type */ |
478 | 0 | long long num; |
479 | |
|
480 | 0 | *hints = 0; |
481 | 0 | *anchor = NULL; |
482 | |
|
483 | 0 | if (!value_len) { |
484 | | /* no value but it may also be zero-length string */ |
485 | 0 | *hints |= LYD_VALHINT_EMPTY | LYD_VALHINT_STRING; |
486 | 0 | } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) { |
487 | | /* boolean value */ |
488 | 0 | *hints |= LYD_VALHINT_BOOLEAN; |
489 | 0 | } else { |
490 | 0 | num = strtoll(value, &ptr, 10); |
491 | 0 | if ((unsigned)(ptr - value) == value_len) { |
492 | | /* number value */ |
493 | 0 | *hints |= LYD_VALHINT_DECNUM; |
494 | 0 | if ((num < INT32_MIN) || (num > UINT32_MAX)) { |
495 | | /* large number */ |
496 | 0 | *hints |= LYD_VALHINT_NUM64; |
497 | 0 | } |
498 | 0 | } else { |
499 | | /* string value */ |
500 | 0 | *hints |= LYD_VALHINT_STRING; |
501 | 0 | } |
502 | 0 | } |
503 | |
|
504 | 0 | if (!first) { |
505 | 0 | return; |
506 | 0 | } |
507 | | |
508 | | /* search backwards to find the last instance */ |
509 | 0 | do { |
510 | 0 | first = first->prev; |
511 | 0 | if (first->schema) { |
512 | 0 | continue; |
513 | 0 | } |
514 | | |
515 | 0 | opaq = (struct lyd_node_opaq *)first; |
516 | 0 | assert(opaq->format == LY_VALUE_XML); |
517 | 0 | if (!ly_strncmp(opaq->name.name, name, name_len) && |
518 | 0 | ((ns && opaq->name.module_ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) { |
519 | 0 | if (opaq->value && opaq->value[0]) { |
520 | | /* leaf-list nodes */ |
521 | 0 | opaq->hints |= LYD_NODEHINT_LEAFLIST; |
522 | 0 | *hints |= LYD_NODEHINT_LEAFLIST; |
523 | 0 | } else { |
524 | | /* list nodes */ |
525 | 0 | opaq->hints |= LYD_NODEHINT_LIST; |
526 | 0 | *hints |= LYD_NODEHINT_LIST; |
527 | 0 | } |
528 | 0 | *anchor = (struct lyd_node *)first; |
529 | 0 | break; |
530 | 0 | } |
531 | 0 | } while (first->prev->next); |
532 | 0 | } |
533 | | |
534 | | /** |
535 | | * @brief Get schema node for the current element. |
536 | | * |
537 | | * @param[in] lydctx XML data parser context. |
538 | | * @param[in] parent Parsed parent data node, if any. |
539 | | * @param[in] prefix Element prefix, if any. |
540 | | * @param[in] prefix_len Length of @p prefix. |
541 | | * @param[in] name Element name. |
542 | | * @param[in] name_len Length of @p name. |
543 | | * @param[out] snode Found schema node, NULL if no suitable was found. |
544 | | * @param[out] ext Extension instance that provided @p snode, if any. |
545 | | * @return LY_SUCCESS on success; |
546 | | * @return LY_ERR on error. |
547 | | */ |
548 | | static LY_ERR |
549 | | lydxml_subtree_get_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len, |
550 | | const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext) |
551 | 613k | { |
552 | 613k | LY_ERR r; |
553 | 613k | struct lyxml_ctx *xmlctx; |
554 | 613k | const struct ly_ctx *ctx; |
555 | 613k | const struct lyxml_ns *ns; |
556 | 613k | struct lys_module *mod; |
557 | 613k | uint32_t getnext_opts; |
558 | | |
559 | 613k | xmlctx = lydctx->xmlctx; |
560 | 613k | ctx = xmlctx->ctx; |
561 | 613k | getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
562 | | |
563 | 613k | *snode = NULL; |
564 | 613k | *ext = NULL; |
565 | | |
566 | | /* get current namespace */ |
567 | 613k | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
568 | 613k | if (!ns) { |
569 | 340 | if (lydctx->int_opts & LYD_INTOPT_ANY) { |
570 | 0 | goto unknown_module; |
571 | 0 | } |
572 | 340 | lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0); |
573 | 340 | return LY_EVALID; |
574 | 340 | } |
575 | | |
576 | | /* get the element module, use parent context if possible because of extensions */ |
577 | 613k | mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri); |
578 | 613k | if (!mod) { |
579 | | /* check for extension data */ |
580 | 3.47k | r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len, |
581 | 3.47k | snode, ext); |
582 | 3.47k | if (r != LY_ENOT) { |
583 | | /* success or error */ |
584 | 0 | return r; |
585 | 0 | } |
586 | | |
587 | 3.47k | unknown_module: |
588 | 3.47k | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
589 | 0 | if (ns) { |
590 | 0 | LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri); |
591 | 0 | } else if (prefix_len) { |
592 | 0 | LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%.*s\" in the context.", (int)prefix_len, prefix); |
593 | 0 | } else { |
594 | 0 | LOGVAL(ctx, LYVE_REFERENCE, "No default namespace in the context."); |
595 | 0 | } |
596 | 0 | return LY_EVALID; |
597 | 0 | } |
598 | 3.47k | return LY_SUCCESS; |
599 | 3.47k | } |
600 | | |
601 | | /* get the schema node */ |
602 | 610k | if (!parent && lydctx->ext) { |
603 | 0 | *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts); |
604 | 610k | } else { |
605 | | /* try to find parent schema node even if it is an opaque node (not connected to the parent) */ |
606 | 610k | *snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts); |
607 | 610k | } |
608 | 610k | if (!*snode) { |
609 | | /* check for extension data */ |
610 | 24.5k | r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, |
611 | 24.5k | name_len, snode, ext); |
612 | 24.5k | if (r != LY_ENOT) { |
613 | | /* success or error */ |
614 | 0 | return r; |
615 | 0 | } |
616 | | |
617 | | /* unknown data node */ |
618 | 24.5k | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
619 | 0 | if (parent) { |
620 | 0 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.", |
621 | 0 | (int)name_len, name, LYD_NAME(parent)); |
622 | 0 | } else if (lydctx->ext) { |
623 | 0 | if (lydctx->ext->argument) { |
624 | 0 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.", |
625 | 0 | (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name); |
626 | 0 | } else { |
627 | 0 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.", |
628 | 0 | (int)name_len, name, lydctx->ext->def->name); |
629 | 0 | } |
630 | 0 | } else { |
631 | 0 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", |
632 | 0 | (int)name_len, name, mod->name); |
633 | 0 | } |
634 | 0 | return LY_EVALID; |
635 | 0 | } |
636 | 24.5k | return LY_SUCCESS; |
637 | 585k | } else { |
638 | | /* check that schema node is valid and can be used */ |
639 | 585k | LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode)); |
640 | 585k | LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode)); |
641 | 585k | } |
642 | | |
643 | 585k | return LY_SUCCESS; |
644 | 610k | } |
645 | | |
646 | | /** |
647 | | * @brief Parse an XML opque node. |
648 | | * |
649 | | * @param[in] lydctx XML YANG data parser context. |
650 | | * @param[in] sibling Existing sibling node, if any. |
651 | | * @param[in] prefix Parsed node prefix. |
652 | | * @param[in] prefix_len Length of @p prefix. |
653 | | * @param[in] name Parsed node name. |
654 | | * @param[in] name_len Length of @p name. |
655 | | * @param[out] insert_anchor Optional anchor node for inserting this node. |
656 | | * @param[out] node Created node. |
657 | | * @return LY_ERR value. |
658 | | */ |
659 | | static LY_ERR |
660 | | lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len, |
661 | | const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node) |
662 | 0 | { |
663 | 0 | LY_ERR rc = LY_SUCCESS; |
664 | 0 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
665 | 0 | struct lyd_node_opaq *opaq; |
666 | 0 | const char *ns_uri, *value = NULL; |
667 | 0 | size_t value_len; |
668 | 0 | ly_bool ws_only, dynamic = 0; |
669 | 0 | const struct lyxml_ns *ns; |
670 | 0 | uint32_t hints; |
671 | 0 | void *val_prefix_data = NULL; |
672 | 0 | LY_VALUE_FORMAT format; |
673 | |
|
674 | 0 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
675 | | |
676 | 0 | *node = NULL; |
677 | | |
678 | | /* remember the value */ |
679 | 0 | value = xmlctx->value; |
680 | 0 | value_len = xmlctx->value_len; |
681 | 0 | ws_only = xmlctx->ws_only; |
682 | 0 | dynamic = xmlctx->dynamic; |
683 | 0 | if (dynamic) { |
684 | 0 | xmlctx->dynamic = 0; |
685 | 0 | } |
686 | | |
687 | | /* get value prefixes, if any */ |
688 | 0 | rc = ly_store_prefix_data(xmlctx->ctx, value, value_len, LY_VALUE_XML, &xmlctx->ns, &format, &val_prefix_data); |
689 | 0 | LY_CHECK_GOTO(rc, cleanup); |
690 | | |
691 | | /* get NS again, it may have been backed up and restored */ |
692 | 0 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
693 | 0 | ns_uri = ns ? ns->uri : NULL; |
694 | | |
695 | | /* get best-effort node hints */ |
696 | 0 | lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor); |
697 | | |
698 | | /* create the node without value */ |
699 | 0 | rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, NULL, 0, |
700 | 0 | NULL, format, NULL, hints, node); |
701 | 0 | LY_CHECK_GOTO(rc, cleanup); |
702 | |
|
703 | 0 | assert(*node); |
704 | 0 | LOG_LOCSET(NULL, *node); |
705 | | |
706 | | /* parser next */ |
707 | 0 | rc = lyxml_ctx_next(xmlctx); |
708 | 0 | LY_CHECK_GOTO(rc, cleanup); |
709 | | |
710 | | /* process children */ |
711 | 0 | while (xmlctx->status == LYXML_ELEMENT) { |
712 | 0 | rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
713 | 0 | LY_CHECK_GOTO(rc, cleanup); |
714 | 0 | } |
715 | | |
716 | | /* update the value */ |
717 | 0 | opaq = (struct lyd_node_opaq *)*node; |
718 | 0 | if (opaq->child) { |
719 | 0 | if (!ws_only) { |
720 | 0 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(opaq)); |
721 | 0 | rc = LY_EVALID; |
722 | 0 | goto cleanup; |
723 | 0 | } |
724 | 0 | } else if (value_len) { |
725 | 0 | lydict_remove(xmlctx->ctx, opaq->value); |
726 | 0 | if (dynamic) { |
727 | 0 | LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup); |
728 | 0 | dynamic = 0; |
729 | 0 | } else { |
730 | 0 | LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup); |
731 | 0 | } |
732 | 0 | } |
733 | | |
734 | | /* always store val_prefix_data because the format requires them */ |
735 | 0 | assert(!opaq->val_prefix_data); |
736 | 0 | opaq->val_prefix_data = val_prefix_data; |
737 | 0 | val_prefix_data = NULL; |
738 | |
|
739 | 0 | cleanup: |
740 | 0 | if (*node) { |
741 | 0 | LOG_LOCBACK(0, 1); |
742 | 0 | } |
743 | 0 | ly_free_prefix_data(format, val_prefix_data); |
744 | 0 | if (dynamic) { |
745 | 0 | free((char *)value); |
746 | 0 | } |
747 | 0 | if (rc) { |
748 | 0 | lyd_free_tree(*node); |
749 | 0 | *node = NULL; |
750 | 0 | } |
751 | 0 | return rc; |
752 | 0 | } |
753 | | |
754 | | /** |
755 | | * @brief Parse an XML leaf/leaf-list node. |
756 | | * |
757 | | * @param[in] lydctx XML YANG data parser context. |
758 | | * @param[in] parent Parent node, if any. |
759 | | * @param[in] snode Schema node of the new node. |
760 | | * @param[out] node Created node. |
761 | | * @return LY_ERR value. |
762 | | */ |
763 | | static LY_ERR |
764 | | lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode, |
765 | | struct lyd_node **node) |
766 | 564k | { |
767 | 564k | LY_ERR r, rc = LY_SUCCESS; |
768 | 564k | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
769 | 564k | struct lyd_node *anchor; |
770 | | |
771 | 564k | *node = NULL; |
772 | | |
773 | | /* create node */ |
774 | 564k | r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len * 8, &xmlctx->dynamic, |
775 | 564k | LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node); |
776 | 564k | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
777 | | |
778 | 562k | if (*node) { |
779 | 562k | LOG_LOCSET(NULL, *node); |
780 | 562k | } |
781 | | |
782 | 562k | if (*node && parent && (snode->flags & LYS_KEY)) { |
783 | | /* check the key order, the anchor must never be a key */ |
784 | 90.3k | anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node); |
785 | 90.3k | if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) { |
786 | 672 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
787 | 0 | LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name); |
788 | 0 | r = LY_EVALID; |
789 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
790 | 672 | } else { |
791 | 672 | LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name); |
792 | 672 | } |
793 | 672 | } |
794 | 90.3k | } |
795 | | |
796 | | /* parser next */ |
797 | 562k | r = lyxml_ctx_next(xmlctx); |
798 | 562k | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
799 | | |
800 | | /* no children expected */ |
801 | 560k | if (xmlctx->status == LYXML_ELEMENT) { |
802 | 99 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.", |
803 | 99 | (int)xmlctx->name_len, xmlctx->name, snode->name); |
804 | 99 | r = LY_EVALID; |
805 | 99 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
806 | 0 | } |
807 | | |
808 | 564k | cleanup: |
809 | 564k | if (*node) { |
810 | 562k | LOG_LOCBACK(0, 1); |
811 | 562k | } |
812 | 564k | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
813 | 4.31k | lyd_free_tree(*node); |
814 | 4.31k | *node = NULL; |
815 | 4.31k | } |
816 | 564k | return rc; |
817 | 560k | } |
818 | | |
819 | | /** |
820 | | * @brief Parse an XML inner node. |
821 | | * |
822 | | * @param[in] lydctx XML YANG data parser context. |
823 | | * @param[in] snode Schema node of the new node. |
824 | | * @param[in] ext Extension instance of @p snode, if any. |
825 | | * @param[out] node Created node. |
826 | | * @return LY_ERR value. |
827 | | */ |
828 | | static LY_ERR |
829 | | lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
830 | | struct lyd_node **node) |
831 | 20.5k | { |
832 | 20.5k | LY_ERR r, rc = LY_SUCCESS; |
833 | 20.5k | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
834 | 20.5k | uint32_t prev_parse_opts = lydctx->parse_opts; |
835 | | |
836 | 20.5k | *node = NULL; |
837 | | |
838 | 20.5k | if (!xmlctx->ws_only) { |
839 | | /* value in inner node */ |
840 | 4 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.", |
841 | 4 | (int)xmlctx->value_len, xmlctx->value, snode->name); |
842 | 4 | r = LY_EVALID; |
843 | 4 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
844 | 0 | } |
845 | | |
846 | | /* create node */ |
847 | 20.5k | rc = lyd_create_inner(snode, node); |
848 | 20.5k | LY_CHECK_GOTO(rc, cleanup); |
849 | | |
850 | 20.5k | assert(*node); |
851 | 20.5k | LOG_LOCSET(NULL, *node); |
852 | | |
853 | | /* parser next */ |
854 | 20.5k | rc = lyxml_ctx_next(xmlctx); |
855 | 20.5k | LY_CHECK_GOTO(rc, cleanup); |
856 | | |
857 | 20.4k | if (ext) { |
858 | | /* only parse these extension data and validate afterwards */ |
859 | 0 | lydctx->parse_opts |= LYD_PARSE_ONLY; |
860 | 0 | } |
861 | | |
862 | | /* process children */ |
863 | 597k | while (xmlctx->status == LYXML_ELEMENT) { |
864 | 578k | r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
865 | 578k | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
866 | 577k | } |
867 | | |
868 | | /* restore options */ |
869 | 19.4k | lydctx->parse_opts = prev_parse_opts; |
870 | | |
871 | 19.4k | if (snode->nodetype == LYS_LIST) { |
872 | | /* check all keys exist */ |
873 | 16.0k | r = lyd_parser_check_keys(*node); |
874 | 16.0k | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
875 | 16.0k | } |
876 | | |
877 | 19.4k | if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) { |
878 | | /* new node validation */ |
879 | 19.4k | r = lyd_parser_validate_new_implicit((struct lyd_ctx *)lydctx, *node); |
880 | 19.4k | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
881 | 19.3k | } |
882 | | |
883 | 19.3k | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
884 | | /* rememeber the RPC/action/notification */ |
885 | 0 | lydctx->op_node = *node; |
886 | 0 | } |
887 | | |
888 | 20.5k | cleanup: |
889 | 20.5k | if (*node) { |
890 | 20.5k | LOG_LOCBACK(0, 1); |
891 | 20.5k | } |
892 | 20.5k | lydctx->parse_opts = prev_parse_opts; |
893 | 20.5k | if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
894 | | /* list without keys is unusable or an error */ |
895 | 1.11k | lyd_free_tree(*node); |
896 | 1.11k | *node = NULL; |
897 | 1.11k | } |
898 | 20.5k | return rc; |
899 | 19.3k | } |
900 | | |
901 | | /** |
902 | | * @brief Parse an XML anyxml/anydata node. |
903 | | * |
904 | | * @param[in] lydctx XML YANG data parser context. |
905 | | * @param[in] snode Schema node of the new node. |
906 | | * @param[in] ext Extension instance of @p snode, if any. |
907 | | * @param[out] node Created node. |
908 | | * @return LY_ERR value. |
909 | | */ |
910 | | static LY_ERR |
911 | | lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
912 | | struct lyd_node **node) |
913 | 0 | { |
914 | 0 | LY_ERR r, rc = LY_SUCCESS; |
915 | 0 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
916 | 0 | uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts; |
917 | 0 | struct lyd_node *child = NULL; |
918 | 0 | char *val = NULL; |
919 | 0 | ly_bool log_node = 0; |
920 | |
|
921 | 0 | *node = NULL; |
922 | |
|
923 | 0 | if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) { |
924 | | /* value in anydata node, we expect a tree */ |
925 | 0 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.", |
926 | 0 | xmlctx->value_len < 20 ? (int)xmlctx->value_len : 20, xmlctx->value, snode->name); |
927 | 0 | r = LY_EVALID; |
928 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
929 | 0 | } |
930 | | |
931 | 0 | if (!xmlctx->ws_only) { |
932 | | /* use an arbitrary text value for anyxml */ |
933 | 0 | val = strndup(xmlctx->value, xmlctx->value_len); |
934 | 0 | LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup); |
935 | | |
936 | | /* parser next */ |
937 | 0 | r = lyxml_ctx_next(xmlctx); |
938 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
939 | | |
940 | | /* create node */ |
941 | 0 | r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node); |
942 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
943 | 0 | val = NULL; |
944 | 0 | } else { |
945 | | /* create node */ |
946 | 0 | r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node); |
947 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
948 | |
|
949 | 0 | assert(*node); |
950 | 0 | LOG_LOCSET(NULL, *node); |
951 | 0 | log_node = 1; |
952 | | |
953 | | /* parser next */ |
954 | 0 | r = lyxml_ctx_next(xmlctx); |
955 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
956 | | |
957 | | /* update options so that generic data can be parsed */ |
958 | 0 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
959 | 0 | lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0); |
960 | 0 | lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS; |
961 | | |
962 | | /* parse any data tree */ |
963 | 0 | while (xmlctx->status == LYXML_ELEMENT) { |
964 | 0 | r = lydxml_subtree_r(lydctx, NULL, &child, NULL); |
965 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
966 | 0 | } |
967 | | |
968 | | /* assign the data tree */ |
969 | 0 | ((struct lyd_node_any *)*node)->value.tree = child; |
970 | 0 | child = NULL; |
971 | 0 | } |
972 | | |
973 | 0 | cleanup: |
974 | 0 | if (log_node) { |
975 | 0 | LOG_LOCBACK(0, 1); |
976 | 0 | } |
977 | 0 | lydctx->parse_opts = prev_parse_opts; |
978 | 0 | lydctx->int_opts = prev_int_opts; |
979 | 0 | free(val); |
980 | 0 | lyd_free_tree(child); |
981 | 0 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
982 | 0 | lyd_free_tree(*node); |
983 | 0 | *node = NULL; |
984 | 0 | } |
985 | 0 | return rc; |
986 | 0 | } |
987 | | |
988 | | /** |
989 | | * @brief Parse an XML subtree, recursively. |
990 | | * |
991 | | * @param[in] lydctx XML YANG data parser context. |
992 | | * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
993 | | * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. |
994 | | * @param[in,out] parsed Optional set to add all the parsed siblings into. |
995 | | * @return LY_ERR value. |
996 | | */ |
997 | | static LY_ERR |
998 | | lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
999 | 614k | { |
1000 | 614k | LY_ERR r, rc = LY_SUCCESS; |
1001 | 614k | const char *prefix, *name; |
1002 | 614k | size_t prefix_len, name_len; |
1003 | 614k | struct lyxml_ctx *xmlctx; |
1004 | 614k | const struct ly_ctx *ctx; |
1005 | 614k | struct lyd_meta *meta = NULL; |
1006 | 614k | struct lyd_attr *attr = NULL; |
1007 | 614k | const struct lysc_node *snode = NULL; |
1008 | 614k | struct lysc_ext_instance *ext = NULL; |
1009 | 614k | uint32_t orig_parse_opts; |
1010 | 614k | struct lyd_node *node = NULL, *insert_anchor = NULL; |
1011 | 614k | ly_bool parse_subtree; |
1012 | | |
1013 | 614k | assert(parent || first_p); |
1014 | | |
1015 | 614k | xmlctx = lydctx->xmlctx; |
1016 | 614k | ctx = xmlctx->ctx; |
1017 | | |
1018 | 614k | parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0; |
1019 | | /* all descendants should be parsed */ |
1020 | 614k | lydctx->parse_opts &= ~LYD_PARSE_SUBTREE; |
1021 | 614k | orig_parse_opts = lydctx->parse_opts; |
1022 | | |
1023 | 614k | assert(xmlctx->status == LYXML_ELEMENT); |
1024 | | |
1025 | | /* remember element prefix and name */ |
1026 | 614k | prefix = xmlctx->prefix; |
1027 | 614k | prefix_len = xmlctx->prefix_len; |
1028 | 614k | name = xmlctx->name; |
1029 | 614k | name_len = xmlctx->name_len; |
1030 | | |
1031 | | /* parser next */ |
1032 | 614k | rc = lyxml_ctx_next(xmlctx); |
1033 | 614k | LY_CHECK_GOTO(rc, cleanup); |
1034 | | |
1035 | 613k | if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && name_len && !prefix_len && |
1036 | 0 | !ly_strncmp("eventTime", name, name_len)) { |
1037 | | /* parse eventTime, create node */ |
1038 | 0 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
1039 | 0 | rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, |
1040 | 0 | "urn:ietf:params:xml:ns:netconf:notification:1.0", 47, xmlctx->value, |
1041 | 0 | xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA, &node); |
1042 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1043 | | |
1044 | | /* validate the value */ |
1045 | 0 | r = lyd_parser_notif_eventtime_validate(node); |
1046 | 0 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
1047 | | |
1048 | | /* parser next */ |
1049 | 0 | r = lyxml_ctx_next(xmlctx); |
1050 | 0 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
1051 | 0 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
1052 | 0 | LOGVAL(ctx, LYVE_DATA, "Unexpected notification \"eventTime\" node children."); |
1053 | 0 | rc = LY_EVALID; |
1054 | 0 | lyd_free_tree(node); |
1055 | 0 | goto cleanup; |
1056 | 0 | } |
1057 | | |
1058 | 0 | goto node_parsed; |
1059 | 0 | } |
1060 | | |
1061 | | /* get the schema node */ |
1062 | 613k | r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext); |
1063 | 613k | if (r) { |
1064 | 340 | rc = r; |
1065 | 340 | if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) { |
1066 | | /* skip the invalid data */ |
1067 | 0 | if ((r = lydxml_data_skip(xmlctx))) { |
1068 | 0 | rc = r; |
1069 | 0 | } |
1070 | 0 | } |
1071 | 340 | goto cleanup; |
1072 | 613k | } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
1073 | 28.0k | LOGVRB("Skipping parsing of unknown node \"%.*s\".", (int)name_len, name); |
1074 | | |
1075 | | /* skip element with children */ |
1076 | 28.0k | rc = lydxml_data_skip(xmlctx); |
1077 | 28.0k | goto cleanup; |
1078 | 28.0k | } |
1079 | | |
1080 | | /* create metadata/attributes */ |
1081 | 585k | if (xmlctx->status == LYXML_ATTRIBUTE) { |
1082 | 331 | if (snode) { |
1083 | 331 | r = lydxml_metadata(lydctx, snode, &meta); |
1084 | 331 | } else { |
1085 | 0 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
1086 | 0 | r = lydxml_attrs(xmlctx, &attr); |
1087 | 0 | } |
1088 | 331 | } |
1089 | 585k | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1090 | | |
1091 | 585k | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
1092 | 585k | if (!snode) { |
1093 | | /* opaque */ |
1094 | 0 | r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len, |
1095 | 0 | &insert_anchor, &node); |
1096 | 585k | } else if (snode->nodetype & LYD_NODE_TERM) { |
1097 | | /* term */ |
1098 | 564k | r = lydxml_subtree_term(lydctx, parent, snode, &node); |
1099 | 564k | } else if (snode->nodetype & LYD_NODE_INNER) { |
1100 | | /* inner */ |
1101 | 20.5k | r = lydxml_subtree_inner(lydctx, snode, ext, &node); |
1102 | 20.5k | } else { |
1103 | | /* any */ |
1104 | 0 | assert(snode->nodetype & LYD_NODE_ANY); |
1105 | 0 | r = lydxml_subtree_any(lydctx, snode, ext, &node); |
1106 | 0 | } |
1107 | 585k | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1108 | | |
1109 | 579k | node_parsed: |
1110 | 579k | if (node && snode) { |
1111 | | /* add/correct flags */ |
1112 | 579k | r = lyd_parser_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext); |
1113 | 579k | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
1114 | 579k | } |
1115 | | |
1116 | | /* parser next */ |
1117 | 579k | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
1118 | 579k | if (!parse_subtree) { |
1119 | 579k | r = lyxml_ctx_next(xmlctx); |
1120 | 579k | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
1121 | 579k | } |
1122 | | |
1123 | 579k | LY_CHECK_GOTO(!node, cleanup); |
1124 | | |
1125 | | /* add metadata/attributes */ |
1126 | 579k | if (snode) { |
1127 | 579k | lyd_insert_meta(node, meta, 0); |
1128 | 579k | meta = NULL; |
1129 | 579k | } else { |
1130 | 0 | lyd_insert_attr(node, attr); |
1131 | 0 | attr = NULL; |
1132 | 0 | } |
1133 | | |
1134 | | /* insert, keep first pointer correct */ |
1135 | 579k | if (insert_anchor) { |
1136 | 0 | lyd_insert_after(insert_anchor, node); |
1137 | 579k | } else if (ext) { |
1138 | 0 | r = lyplg_ext_insert(parent, node); |
1139 | 0 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
1140 | 579k | } else { |
1141 | 579k | lyd_insert_node(parent, first_p, node, |
1142 | 579k | lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT); |
1143 | 579k | } |
1144 | 579k | while (!parent && (*first_p)->prev->next) { |
1145 | 0 | *first_p = (*first_p)->prev; |
1146 | 0 | } |
1147 | | |
1148 | | /* rememeber a successfully parsed node */ |
1149 | 579k | if (parsed) { |
1150 | 23.1k | ly_set_add(parsed, node, 1, NULL); |
1151 | 23.1k | } |
1152 | | |
1153 | 614k | cleanup: |
1154 | 614k | lydctx->parse_opts = orig_parse_opts; |
1155 | 614k | lyd_free_meta_siblings(meta); |
1156 | 614k | lyd_free_attr_siblings(ctx, attr); |
1157 | 614k | return rc; |
1158 | 579k | } |
1159 | | |
1160 | | /** |
1161 | | * @brief Parse a specific XML element into an opaque node. |
1162 | | * |
1163 | | * @param[in] xmlctx XML parser context. |
1164 | | * @param[in] name Name of the element. |
1165 | | * @param[in] uri URI of the element. |
1166 | | * @param[in] value Whether a value is expected in the element. |
1167 | | * @param[out] evnp Parsed envelope (opaque node). |
1168 | | * @return LY_SUCCESS on success. |
1169 | | * @return LY_ENOT if the specified element did not match. |
1170 | | * @return LY_ERR value on error. |
1171 | | */ |
1172 | | static LY_ERR |
1173 | | lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, ly_bool value, struct lyd_node **envp) |
1174 | 0 | { |
1175 | 0 | LY_ERR rc = LY_SUCCESS; |
1176 | 0 | const struct lyxml_ns *ns; |
1177 | 0 | struct lyd_attr *attr = NULL; |
1178 | 0 | const char *prefix; |
1179 | 0 | size_t prefix_len; |
1180 | |
|
1181 | 0 | if (xmlctx->status != LYXML_ELEMENT) { |
1182 | | /* nothing to parse */ |
1183 | 0 | return LY_ENOT; |
1184 | 0 | } |
1185 | | |
1186 | 0 | if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) { |
1187 | | /* not the expected element */ |
1188 | 0 | return LY_ENOT; |
1189 | 0 | } |
1190 | | |
1191 | 0 | prefix = xmlctx->prefix; |
1192 | 0 | prefix_len = xmlctx->prefix_len; |
1193 | 0 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
1194 | 0 | if (!ns) { |
1195 | 0 | lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0); |
1196 | 0 | return LY_EVALID; |
1197 | 0 | } else if (strcmp(ns->uri, uri)) { |
1198 | | /* different namespace */ |
1199 | 0 | return LY_ENOT; |
1200 | 0 | } |
1201 | | |
1202 | 0 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
1203 | | |
1204 | | /* create attributes */ |
1205 | 0 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
1206 | 0 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
1207 | 0 | } |
1208 | | |
1209 | 0 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
1210 | 0 | if (!value && !xmlctx->ws_only) { |
1211 | 0 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.", |
1212 | 0 | (int)xmlctx->value_len, xmlctx->value, name); |
1213 | 0 | rc = LY_EVALID; |
1214 | 0 | goto cleanup; |
1215 | 0 | } |
1216 | | |
1217 | | /* create node */ |
1218 | 0 | rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value, |
1219 | 0 | xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp); |
1220 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1221 | | |
1222 | | /* assign atributes */ |
1223 | 0 | ((struct lyd_node_opaq *)(*envp))->attr = attr; |
1224 | 0 | attr = NULL; |
1225 | | |
1226 | | /* parser next element */ |
1227 | 0 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
1228 | |
|
1229 | 0 | cleanup: |
1230 | 0 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
1231 | 0 | if (rc) { |
1232 | 0 | lyd_free_tree(*envp); |
1233 | 0 | *envp = NULL; |
1234 | 0 | } |
1235 | 0 | return rc; |
1236 | 0 | } |
1237 | | |
1238 | | LY_ERR |
1239 | | lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
1240 | | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts, |
1241 | | struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p) |
1242 | 9.05k | { |
1243 | 9.05k | LY_ERR r, rc = LY_SUCCESS; |
1244 | 9.05k | struct lyd_xml_ctx *lydctx; |
1245 | 9.05k | ly_bool parsed_data_nodes = 0, close_elem = 0; |
1246 | 9.05k | struct lyd_node *act = NULL; |
1247 | 9.05k | enum LYXML_PARSER_STATUS status; |
1248 | | |
1249 | 9.05k | assert(ctx && in && lydctx_p); |
1250 | 9.05k | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
1251 | 9.05k | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
1252 | | |
1253 | | /* init context */ |
1254 | 9.05k | lydctx = calloc(1, sizeof *lydctx); |
1255 | 9.05k | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
1256 | 9.05k | LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup); |
1257 | 7.79k | lydctx->parse_opts = parse_opts; |
1258 | 7.79k | lydctx->val_opts = val_opts; |
1259 | 7.79k | lydctx->int_opts = int_opts; |
1260 | 7.79k | lydctx->free = lyd_xml_ctx_free; |
1261 | 7.79k | lydctx->ext = ext; |
1262 | | |
1263 | | /* find the operation node if it exists already */ |
1264 | 7.79k | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
1265 | | |
1266 | 7.79k | if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) { |
1267 | | /* can be either, try to parse "action" */ |
1268 | 0 | if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) { |
1269 | 0 | close_elem = 1; |
1270 | 0 | int_opts &= ~LYD_INTOPT_RPC; |
1271 | 0 | } |
1272 | 0 | } |
1273 | | |
1274 | | /* parse XML data */ |
1275 | 36.9k | while (lydctx->xmlctx->status == LYXML_ELEMENT) { |
1276 | 36.3k | r = lydxml_subtree_r(lydctx, parent, first_p, parsed); |
1277 | 36.3k | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1278 | | |
1279 | 29.1k | parsed_data_nodes = 1; |
1280 | | |
1281 | 29.1k | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
1282 | 0 | break; |
1283 | 0 | } |
1284 | 29.1k | } |
1285 | | |
1286 | | /* close an opened element */ |
1287 | 548 | if (close_elem) { |
1288 | 0 | if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) { |
1289 | 0 | assert(lydctx->xmlctx->status == LYXML_ELEMENT); |
1290 | 0 | LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".", |
1291 | 0 | (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name); |
1292 | 0 | rc = LY_EVALID; |
1293 | 0 | goto cleanup; |
1294 | 0 | } |
1295 | | |
1296 | 0 | LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup); |
1297 | 0 | } |
1298 | | |
1299 | | /* check final state */ |
1300 | 548 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) { |
1301 | 0 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
1302 | 0 | r = LY_EVALID; |
1303 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1304 | 0 | } |
1305 | 548 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
1306 | 0 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
1307 | 0 | r = LY_EVALID; |
1308 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1309 | 0 | } |
1310 | | |
1311 | 548 | if (!parsed_data_nodes) { |
1312 | | /* no data nodes were parsed */ |
1313 | 48 | lydctx->op_node = NULL; |
1314 | 48 | } |
1315 | | |
1316 | 548 | if (parse_opts & LYD_PARSE_SUBTREE) { |
1317 | | /* check for a sibling element */ |
1318 | 0 | assert(subtree_sibling); |
1319 | 0 | if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) { |
1320 | 0 | *subtree_sibling = 1; |
1321 | 0 | } else { |
1322 | 0 | *subtree_sibling = 0; |
1323 | 0 | } |
1324 | 0 | } |
1325 | | |
1326 | 9.05k | cleanup: |
1327 | | /* there should be no unres stored if validation should be skipped */ |
1328 | 9.05k | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
1329 | 9.05k | !lydctx->node_when.count)); |
1330 | | |
1331 | 9.05k | lyd_free_tree(act); |
1332 | 9.05k | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
1333 | 8.50k | lyd_xml_ctx_free((struct lyd_ctx *)lydctx); |
1334 | 8.50k | } else { |
1335 | 548 | *lydctx_p = (struct lyd_ctx *)lydctx; |
1336 | | |
1337 | | /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
1338 | 548 | lyxml_ctx_free(lydctx->xmlctx); |
1339 | 548 | lydctx->xmlctx = NULL; |
1340 | 548 | } |
1341 | 9.05k | return rc; |
1342 | 9.05k | } |
1343 | | |
1344 | | /** |
1345 | | * @brief Parse all expected non-data XML elements of a NETCONF rpc message. |
1346 | | * |
1347 | | * @param[in] xmlctx XML parser context. |
1348 | | * @param[out] evnp Parsed envelope(s) (opaque node). |
1349 | | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
1350 | | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
1351 | | * @return LY_SUCCESS on success. |
1352 | | * @return LY_ERR value on error. |
1353 | | */ |
1354 | | static LY_ERR |
1355 | | lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
1356 | 0 | { |
1357 | 0 | LY_ERR rc = LY_SUCCESS, r; |
1358 | 0 | struct lyd_node *child; |
1359 | |
|
1360 | 0 | assert(envp && !*envp); |
1361 | | |
1362 | | /* parse "rpc" */ |
1363 | 0 | r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp); |
1364 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1365 | | |
1366 | | /* parse "action", if any */ |
1367 | 0 | r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child); |
1368 | 0 | if (r == LY_SUCCESS) { |
1369 | | /* insert */ |
1370 | 0 | lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_DEFAULT); |
1371 | | |
1372 | | /* NETCONF action */ |
1373 | 0 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION; |
1374 | 0 | *close_elem = 2; |
1375 | 0 | } else if (r == LY_ENOT) { |
1376 | | /* NETCONF RPC */ |
1377 | 0 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC; |
1378 | 0 | *close_elem = 1; |
1379 | 0 | } else { |
1380 | 0 | rc = r; |
1381 | 0 | goto cleanup; |
1382 | 0 | } |
1383 | | |
1384 | 0 | cleanup: |
1385 | 0 | if (rc) { |
1386 | 0 | lyd_free_tree(*envp); |
1387 | 0 | *envp = NULL; |
1388 | 0 | } |
1389 | 0 | return rc; |
1390 | 0 | } |
1391 | | |
1392 | | /** |
1393 | | * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message. |
1394 | | * |
1395 | | * @param[in] lydctx XML YANG data parser context. |
1396 | | * @param[out] evnp Parsed envelope(s) (opaque node). |
1397 | | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
1398 | | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
1399 | | * @return LY_SUCCESS on success. |
1400 | | * @return LY_ERR value on error. |
1401 | | */ |
1402 | | static LY_ERR |
1403 | | lydxml_env_netconf_reply(struct lyd_xml_ctx *lydctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
1404 | 0 | { |
1405 | 0 | LY_ERR rc = LY_SUCCESS, r; |
1406 | 0 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
1407 | 0 | struct lyd_node *child = NULL; |
1408 | 0 | const char *parsed_elem = NULL; |
1409 | |
|
1410 | 0 | assert(envp && !*envp); |
1411 | | |
1412 | | /* not all nodes are represented as internal schema nodes and there may even be custom additional nodes */ |
1413 | 0 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
1414 | 0 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
1415 | | |
1416 | | /* parse "rpc-reply" */ |
1417 | 0 | r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp); |
1418 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1419 | | |
1420 | | /* there must be some child */ |
1421 | 0 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
1422 | 0 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\"."); |
1423 | 0 | rc = LY_EVALID; |
1424 | 0 | goto cleanup; |
1425 | 0 | } |
1426 | | |
1427 | | /* try to parse "ok" */ |
1428 | 0 | r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
1429 | 0 | if (r == LY_SUCCESS) { |
1430 | | /* insert */ |
1431 | 0 | lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST); |
1432 | | |
1433 | | /* finish child parsing */ |
1434 | 0 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
1435 | 0 | assert(xmlctx->status == LYXML_ELEMENT); |
1436 | 0 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".", |
1437 | 0 | (int)xmlctx->name_len, xmlctx->name); |
1438 | 0 | rc = LY_EVALID; |
1439 | 0 | goto cleanup; |
1440 | 0 | } |
1441 | 0 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
1442 | | |
1443 | | /* success */ |
1444 | 0 | parsed_elem = "ok"; |
1445 | 0 | goto finish; |
1446 | 0 | } else if (r != LY_ENOT) { |
1447 | 0 | rc = r; |
1448 | 0 | goto cleanup; |
1449 | 0 | } |
1450 | | |
1451 | | /* try to parse all "rpc-error" elements */ |
1452 | 0 | while ((xmlctx->status == LYXML_ELEMENT) && !ly_strncmp("rpc-error", xmlctx->name, xmlctx->name_len)) { |
1453 | 0 | LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, *envp, lyd_node_child_p(*envp), NULL), cleanup); |
1454 | |
|
1455 | 0 | parsed_elem = "rpc-error"; |
1456 | 0 | } |
1457 | | |
1458 | 0 | finish: |
1459 | 0 | if (parsed_elem) { |
1460 | | /* NETCONF rpc-reply with no data */ |
1461 | 0 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
1462 | 0 | assert(xmlctx->status == LYXML_ELEMENT); |
1463 | 0 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".", |
1464 | 0 | (int)xmlctx->name_len, xmlctx->name, parsed_elem); |
1465 | 0 | rc = LY_EVALID; |
1466 | 0 | goto cleanup; |
1467 | 0 | } |
1468 | 0 | } |
1469 | | |
1470 | | /* NETCONF rpc-reply */ |
1471 | 0 | *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY; |
1472 | 0 | *close_elem = 1; |
1473 | |
|
1474 | 0 | cleanup: |
1475 | 0 | if (rc) { |
1476 | 0 | lyd_free_tree(*envp); |
1477 | 0 | *envp = NULL; |
1478 | 0 | } |
1479 | 0 | return rc; |
1480 | 0 | } |
1481 | | |
1482 | | LY_ERR |
1483 | | lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
1484 | | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
1485 | | struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
1486 | 0 | { |
1487 | 0 | LY_ERR rc = LY_SUCCESS; |
1488 | 0 | struct lyd_xml_ctx *lydctx; |
1489 | 0 | struct lyd_node *node; |
1490 | 0 | uint32_t i, int_opts = 0, close_elem = 0; |
1491 | 0 | ly_bool parsed_data_nodes = 0; |
1492 | |
|
1493 | 0 | assert(ctx && in && lydctx_p); |
1494 | 0 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
1495 | 0 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
1496 | 0 | assert(!(parse_opts & LYD_PARSE_SUBTREE)); |
1497 | | |
1498 | | /* init context */ |
1499 | 0 | lydctx = calloc(1, sizeof *lydctx); |
1500 | 0 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
1501 | 0 | LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup); |
1502 | 0 | lydctx->parse_opts = parse_opts; |
1503 | 0 | lydctx->val_opts = val_opts; |
1504 | 0 | lydctx->free = lyd_xml_ctx_free; |
1505 | 0 | lydctx->ext = ext; |
1506 | |
|
1507 | 0 | switch (data_type) { |
1508 | 0 | case LYD_TYPE_RPC_NETCONF: |
1509 | 0 | assert(!parent); |
1510 | 0 | rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem); |
1511 | 0 | if (rc == LY_ENOT) { |
1512 | 0 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace."); |
1513 | 0 | } |
1514 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1515 | 0 | break; |
1516 | 0 | case LYD_TYPE_NOTIF_NETCONF: |
1517 | 0 | assert(!parent); |
1518 | | |
1519 | | /* parse "notification" */ |
1520 | 0 | rc = lydxml_envelope(lydctx->xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp); |
1521 | 0 | if (rc == LY_ENOT) { |
1522 | 0 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace."); |
1523 | 0 | } |
1524 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1525 | | |
1526 | | /* NETCONF notification */ |
1527 | 0 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME; |
1528 | 0 | close_elem = 1; |
1529 | 0 | break; |
1530 | 0 | case LYD_TYPE_REPLY_NETCONF: |
1531 | 0 | assert(parent); |
1532 | 0 | rc = lydxml_env_netconf_reply(lydctx, envp, &int_opts, &close_elem); |
1533 | 0 | if (rc == LY_ENOT) { |
1534 | 0 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace."); |
1535 | 0 | } |
1536 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1537 | 0 | break; |
1538 | 0 | case LYD_TYPE_RPC_RESTCONF: |
1539 | 0 | assert(parent); |
1540 | | |
1541 | | /* parse "input" */ |
1542 | 0 | rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_node_module(parent)->ns, 0, envp); |
1543 | 0 | if (rc == LY_ENOT) { |
1544 | 0 | LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace."); |
1545 | 0 | } |
1546 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1547 | |
|
1548 | 0 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION; |
1549 | 0 | close_elem = 1; |
1550 | 0 | break; |
1551 | 0 | case LYD_TYPE_REPLY_RESTCONF: |
1552 | 0 | assert(parent); |
1553 | | |
1554 | | /* parse "output" */ |
1555 | 0 | rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_node_module(parent)->ns, 0, envp); |
1556 | 0 | if (rc == LY_ENOT) { |
1557 | 0 | LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace."); |
1558 | 0 | } |
1559 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1560 | |
|
1561 | 0 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY; |
1562 | 0 | close_elem = 1; |
1563 | 0 | break; |
1564 | 0 | default: |
1565 | 0 | LOGINT(ctx); |
1566 | 0 | rc = LY_EINT; |
1567 | 0 | goto cleanup; |
1568 | 0 | } |
1569 | | |
1570 | 0 | lydctx->int_opts = int_opts; |
1571 | | |
1572 | | /* find the operation node if it exists already */ |
1573 | 0 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
1574 | | |
1575 | | /* parse XML data */ |
1576 | 0 | while (lydctx->xmlctx->status == LYXML_ELEMENT) { |
1577 | 0 | LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup); |
1578 | 0 | parsed_data_nodes = 1; |
1579 | |
|
1580 | 0 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
1581 | 0 | break; |
1582 | 0 | } |
1583 | 0 | } |
1584 | | |
1585 | | /* close all opened elements */ |
1586 | 0 | for (i = 0; i < close_elem; ++i) { |
1587 | 0 | if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) { |
1588 | 0 | assert(lydctx->xmlctx->status == LYXML_ELEMENT); |
1589 | 0 | LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".", |
1590 | 0 | (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name); |
1591 | 0 | rc = LY_EVALID; |
1592 | 0 | goto cleanup; |
1593 | 0 | } |
1594 | | |
1595 | 0 | LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup); |
1596 | 0 | } |
1597 | | |
1598 | | /* check final state */ |
1599 | 0 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) { |
1600 | 0 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
1601 | 0 | rc = LY_EVALID; |
1602 | 0 | goto cleanup; |
1603 | 0 | } |
1604 | 0 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
1605 | 0 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
1606 | 0 | rc = LY_EVALID; |
1607 | 0 | goto cleanup; |
1608 | 0 | } |
1609 | 0 | if (int_opts & LYD_INTOPT_EVENTTIME) { |
1610 | | /* parse as a child of the envelope */ |
1611 | 0 | node = (*first_p)->prev; |
1612 | 0 | if (node->schema) { |
1613 | 0 | LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node."); |
1614 | 0 | rc = LY_EVALID; |
1615 | 0 | goto cleanup; |
1616 | 0 | } else { |
1617 | | /* can be the only opaque node and an operation had to be parsed */ |
1618 | 0 | assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next); |
1619 | 0 | lyd_unlink(node); |
1620 | 0 | assert(*envp); |
1621 | 0 | lyd_insert_child(*envp, node); |
1622 | 0 | } |
1623 | 0 | } |
1624 | | |
1625 | 0 | if (!parsed_data_nodes) { |
1626 | | /* no data nodes were parsed */ |
1627 | 0 | lydctx->op_node = NULL; |
1628 | 0 | } |
1629 | |
|
1630 | 0 | cleanup: |
1631 | | /* there should be no unres stored if validation should be skipped */ |
1632 | 0 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
1633 | 0 | !lydctx->node_when.count)); |
1634 | | |
1635 | 0 | if (rc) { |
1636 | 0 | lyd_xml_ctx_free((struct lyd_ctx *)lydctx); |
1637 | 0 | } else { |
1638 | 0 | *lydctx_p = (struct lyd_ctx *)lydctx; |
1639 | | |
1640 | | /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
1641 | 0 | lyxml_ctx_free(lydctx->xmlctx); |
1642 | | lydctx->xmlctx = NULL; |
1643 | 0 | } |
1644 | 0 | return rc; |
1645 | 0 | } |